3 # Copyright 2015 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 16;
25 use Koha::Authority::Types;
27 use Koha::IssuingRules;
28 use Koha::Patron::Category;
29 use Koha::Patron::Categories;
33 use t::lib::TestBuilder;
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
39 my $builder = t::lib::TestBuilder->new;
41 is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
43 my @columns = Koha::Patrons->columns;
44 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
45 is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
47 subtest 'find' => sub {
49 my $patron = $builder->build({source => 'Borrower'});
50 my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
51 is( $patron_object->borrowernumber, $patron->{borrowernumber}, '->find should return the correct object' );
53 eval { my @patrons = Koha::Patrons->find( $patron->{borrowernumber} ); };
54 like( $@, qr|^Cannot use "->find" in list context|, "->find should not be called in list context to avoid side-effects" );
56 # Test sending undef to find; should not generate a warning
57 warning_is { $patron = Koha::Patrons->find( undef ); }
58 "", "Sending undef does not trigger a DBIx warning";
59 warning_is { $patron = Koha::Patrons->find( undef, undef ); }
60 "", "Sending two undefs does not trigger a DBIx warning too";
63 subtest 'update' => sub {
66 $builder->build( { source => 'City', value => { city_country => 'UK' } } );
67 $builder->build( { source => 'City', value => { city_country => 'UK' } } );
68 $builder->build( { source => 'City', value => { city_country => 'UK' } } );
69 $builder->build( { source => 'City', value => { city_country => 'France' } } );
70 $builder->build( { source => 'City', value => { city_country => 'France' } } );
71 $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
72 Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
73 is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
74 is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
77 subtest 'pager' => sub {
79 my $pager = Koha::Patrons->search( {}, { page => 1, rows => 2 } )->pager;
80 is( ref($pager), 'DBIx::Class::ResultSet::Pager', 'Koha::Objects->pager returns a valid DBIx::Class object' );
83 subtest 'reset' => sub {
86 my $patrons = Koha::Patrons->search;
87 my $first_borrowernumber = $patrons->next->borrowernumber;
88 my $second_borrowernumber = $patrons->next->borrowernumber;
89 is( ref( $patrons->reset ), 'Koha::Patrons', 'Koha::Objects->reset should allow chaining' );
90 is( ref( $patrons->reset->next ), 'Koha::Patron', 'Koha::Objects->reset should allow chaining' );
91 is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
94 subtest 'delete' => sub {
97 my $patron_1 = $builder->build({source => 'Borrower'});
98 my $patron_2 = $builder->build({source => 'Borrower'});
99 is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, '');
100 is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->count, 0, '');
103 subtest 'not_covered_yet' => sub {
105 warning_is { Koha::Patrons->search->not_covered_yet } { carped => 'The method not_covered_yet is not covered by tests' }, "If a method is not covered by tests, the AUTOLOAD method won't execute the method";
107 subtest 'new' => sub {
109 my $a_cat_code = 'A_CAT_CODE';
110 my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store;
111 is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
112 Koha::Patron::Categories->find($a_cat_code)->delete;
113 $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store;
114 is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value even if the argument exists but is not defined' );
115 Koha::Patron::Categories->find($a_cat_code)->delete;
118 subtest 'find' => sub {
121 # check find on a single PK
122 my $patron = $builder->build({ source => 'Borrower' });
123 is( Koha::Patrons->find($patron->{borrowernumber})->surname,
124 $patron->{surname}, "Checking an arbitrary patron column after find"
126 # check find with unique column
127 my $obj = Koha::Patrons->find($patron->{cardnumber}, { key => 'cardnumber' });
128 is( $obj->borrowernumber, $patron->{borrowernumber},
129 'Find with unique column and key specified' );
130 # check find with an additional where clause in the attrs hash
131 # we do not expect to find something now
132 is( Koha::Patrons->find(
133 $patron->{borrowernumber},
134 { where => { surname => { '!=', $patron->{surname} }}},
135 ), undef, 'Additional where clause in find call' );
137 # check find with a composite FK
138 my $rule = $builder->build({ source => 'Issuingrule' });
139 my @pk = ( $rule->{branchcode}, $rule->{categorycode}, $rule->{itemtype} );
140 is( ref(Koha::IssuingRules->find(@pk)), "Koha::IssuingRule",
141 'Find returned a Koha object for composite primary key' );
143 is( Koha::Patrons->find(), undef, 'Find returns undef if no params passed' );
146 subtest 'search_related' => sub {
148 my $builder = t::lib::TestBuilder->new;
149 my $patron_1 = $builder->build( { source => 'Borrower' } );
150 my $patron_2 = $builder->build( { source => 'Borrower' } );
151 my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
152 is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
153 is( $libraries->count, 2, 'Koha::Objects->search_related should work as expected' );
154 is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
155 is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
157 my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
158 is( ref( $libraries[0] ), 'Koha::Library', 'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
159 is( scalar(@libraries), 2, 'Koha::Objects->search_related should work as expected' );
160 is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
161 is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
164 subtest 'single' => sub {
166 my $builder = t::lib::TestBuilder->new;
167 my $patron_1 = $builder->build( { source => 'Borrower' } );
168 my $patron_2 = $builder->build( { source => 'Borrower' } );
169 my $patron = Koha::Patrons->search({}, { rows => 1 })->single;
170 is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
171 warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/,
172 "Warning is presented if single is used for a result with multiple rows.";
175 subtest 'last' => sub {
177 my $builder = t::lib::TestBuilder->new;
178 my $patron_1 = $builder->build( { source => 'Borrower' } );
179 my $patron_2 = $builder->build( { source => 'Borrower' } );
180 my $last_patron = Koha::Patrons->search->last;
181 is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' );
182 $last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last;
183 is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' );
184 $last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last;
185 is( $last_patron, undef, '->last should return undef if search does not return any results' );
188 subtest 'get_column' => sub {
190 my @cities = Koha::Cities->search;
191 my @city_names = map { $_->city_name } @cities;
192 is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' );
195 subtest 'Exceptions' => sub {
198 my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
199 my $patron = Koha::Patrons->find( $patron_borrowernumber );
202 $patron->blah('blah');
204 ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
205 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
209 $patron->set({ blah => 'blah' });
211 ok( $_->isa('Koha::Exceptions::Object::PropertyNotFound'),
212 'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' );
216 $schema->storage->txn_rollback;
218 subtest '->is_paged tests' => sub {
222 $schema->storage->txn_begin;
225 $builder->build_object({ class => 'Koha::Patrons' });
228 my $patrons = Koha::Patrons->search();
229 ok( !$patrons->is_paged, 'Search is not paged' );
230 $patrons = Koha::Patrons->search( undef, { 'page' => 1, 'rows' => 3 } );
231 ok( $patrons->is_paged, 'Search is paged' );
233 $schema->storage->txn_rollback;