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 => 17;
26 use Koha::Authority::Types;
28 use Koha::IssuingRules;
29 use Koha::Patron::Category;
30 use Koha::Patron::Categories;
34 use t::lib::TestBuilder;
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
40 my $builder = t::lib::TestBuilder->new;
42 is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
44 my @columns = Koha::Patrons->columns;
45 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
46 is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
48 subtest 'find' => sub {
50 my $patron = $builder->build({source => 'Borrower'});
51 my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
52 is( $patron_object->borrowernumber, $patron->{borrowernumber}, '->find should return the correct object' );
54 my @patrons = Koha::Patrons->find( $patron->{borrowernumber} );
55 is(scalar @patrons, 1, '->find in list context returns a value');
56 is($patrons[0]->borrowernumber, $patron->{borrowernumber}, '->find in list context returns the same value as in scalar context');
59 foo => Koha::Patrons->find('foo'),
62 is ($patrons->{foo}, undef, '->find in list context returns undef when no record is found');
64 # Test sending undef to find; should not generate a warning
65 warning_is { $patron = Koha::Patrons->find( undef ); }
66 "", "Sending undef does not trigger a DBIx warning";
67 warning_is { $patron = Koha::Patrons->find( undef, undef ); }
68 "", "Sending two undefs does not trigger a DBIx warning too";
71 subtest 'update' => sub {
74 $builder->build( { source => 'City', value => { city_country => 'UK' } } );
75 $builder->build( { source => 'City', value => { city_country => 'UK' } } );
76 $builder->build( { source => 'City', value => { city_country => 'UK' } } );
77 $builder->build( { source => 'City', value => { city_country => 'France' } } );
78 $builder->build( { source => 'City', value => { city_country => 'France' } } );
79 $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
80 Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
81 is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
82 is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
85 subtest 'reset' => sub {
88 my $patrons = Koha::Patrons->search;
89 my $first_borrowernumber = $patrons->next->borrowernumber;
90 my $second_borrowernumber = $patrons->next->borrowernumber;
91 is( ref( $patrons->reset ), 'Koha::Patrons', 'Koha::Objects->reset should allow chaining' );
92 is( ref( $patrons->reset->next ), 'Koha::Patron', 'Koha::Objects->reset should allow chaining' );
93 is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
96 subtest 'delete' => sub {
99 my $patron_1 = $builder->build({source => 'Borrower'});
100 my $patron_2 = $builder->build({source => 'Borrower'});
101 is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, '');
102 is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->count, 0, '');
105 subtest 'new' => sub {
107 my $a_cat_code = 'A_CAT_CODE';
108 my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store;
109 is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
110 Koha::Patron::Categories->find($a_cat_code)->delete;
111 $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store;
112 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' );
113 Koha::Patron::Categories->find($a_cat_code)->delete;
116 subtest 'find' => sub {
119 # check find on a single PK
120 my $patron = $builder->build({ source => 'Borrower' });
121 is( Koha::Patrons->find($patron->{borrowernumber})->surname,
122 $patron->{surname}, "Checking an arbitrary patron column after find"
124 # check find with unique column
125 my $obj = Koha::Patrons->find($patron->{cardnumber}, { key => 'cardnumber' });
126 is( $obj->borrowernumber, $patron->{borrowernumber},
127 'Find with unique column and key specified' );
128 # check find with an additional where clause in the attrs hash
129 # we do not expect to find something now
130 is( Koha::Patrons->find(
131 $patron->{borrowernumber},
132 { where => { surname => { '!=', $patron->{surname} }}},
133 ), undef, 'Additional where clause in find call' );
135 # check find with a composite FK
136 my $rule = $builder->build({ source => 'Issuingrule' });
137 my @pk = ( $rule->{branchcode}, $rule->{categorycode}, $rule->{itemtype} );
138 is( ref(Koha::IssuingRules->find(@pk)), "Koha::IssuingRule",
139 'Find returned a Koha object for composite primary key' );
141 is( Koha::Patrons->find(), undef, 'Find returns undef if no params passed' );
144 subtest 'search_related' => sub {
146 my $builder = t::lib::TestBuilder->new;
147 my $patron_1 = $builder->build( { source => 'Borrower' } );
148 my $patron_2 = $builder->build( { source => 'Borrower' } );
149 my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
150 is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
151 is( $libraries->count, 2, 'Koha::Objects->search_related should work as expected' );
152 is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
153 is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
155 my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
156 is( ref( $libraries[0] ), 'Koha::Library', 'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
157 is( scalar(@libraries), 2, 'Koha::Objects->search_related should work as expected' );
158 is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
159 is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
162 subtest 'single' => sub {
164 my $builder = t::lib::TestBuilder->new;
165 my $patron_1 = $builder->build( { source => 'Borrower' } );
166 my $patron_2 = $builder->build( { source => 'Borrower' } );
167 my $patron = Koha::Patrons->search({}, { rows => 1 })->single;
168 is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
169 warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/,
170 "Warning is presented if single is used for a result with multiple rows.";
173 subtest 'last' => sub {
175 my $builder = t::lib::TestBuilder->new;
176 my $patron_1 = $builder->build( { source => 'Borrower' } );
177 my $patron_2 = $builder->build( { source => 'Borrower' } );
178 my $last_patron = Koha::Patrons->search->last;
179 is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' );
180 $last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last;
181 is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' );
182 $last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last;
183 is( $last_patron, undef, '->last should return undef if search does not return any results' );
186 subtest 'get_column' => sub {
188 my @cities = Koha::Cities->search;
189 my @city_names = map { $_->city_name } @cities;
190 is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' );
193 subtest 'Exceptions' => sub {
196 my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
197 my $patron = Koha::Patrons->find( $patron_borrowernumber );
201 $patron->blah('blah');
203 ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
204 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
205 is( $_->message, 'The method Koha::Patron->blah is not covered by tests!', 'The message raised should contain the package and the method' );
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' );
217 Koha::Patrons->search->not_covered_yet;
219 ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
220 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
221 is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
225 Koha::Patrons->not_covered_yet;
227 ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
228 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
229 is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
233 $schema->storage->txn_rollback;
235 subtest '->is_paged and ->pager tests' => sub {
239 $schema->storage->txn_begin;
241 # Delete existing patrons
242 Koha::Checkouts->delete;
243 Koha::Patrons->delete;
246 $builder->build_object({ class => 'Koha::Patrons' });
249 # Non-paginated search
250 my $patrons = Koha::Patrons->search();
251 is( $patrons->count, 10, 'Search returns all patrons' );
252 ok( !$patrons->is_paged, 'Search is not paged' );
255 $patrons = Koha::Patrons->search( undef, { 'page' => 1, 'rows' => 3 } );
256 is( $patrons->count, 3, 'Search returns only one page, 3 patrons' );
257 ok( $patrons->is_paged, 'Search is paged' );
258 my $pager = $patrons->pager;
259 is( ref($patrons->pager), 'DBIx::Class::ResultSet::Pager',
260 'Koha::Objects->pager returns a valid DBIx::Class object' );
262 $schema->storage->txn_rollback;
265 subtest '->search() tests' => sub {
269 $schema->storage->txn_begin;
271 my $count = Koha::Patrons->search->count;
275 $builder->build_object({ class => 'Koha::Patrons' });
278 my $patrons = Koha::Patrons->search();
279 is( ref($patrons), 'Koha::Patrons', 'search in scalar context returns the Koha::Object-based type' );
280 my @patrons = Koha::Patrons->search();
281 is( scalar @patrons, $count + 10, 'search in list context returns a list of objects' );
284 is( ref($patrons[$i]), 'Koha::Patron', 'Objects in the list have the singular type' );
288 $schema->storage->txn_rollback;
291 subtest "to_api() tests" => sub {
295 $schema->storage->txn_begin;
297 my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
298 my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
300 my $cities = Koha::Cities->search(
302 cityid => [ $city_1->cityid, $city_2->cityid ]
304 { -orderby => { -desc => 'cityid' } }
307 is( $cities->count, 2, 'Count is correct' );
308 my $cities_api = $cities->to_api;
309 is( ref( $cities_api ), 'ARRAY', 'to_api returns an array' );
310 is_deeply( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' );
311 is_deeply( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' );
313 my $biblio_1 = $builder->build_sample_biblio();
314 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
315 my $hold_1 = $builder->build_object(
317 class => 'Koha::Holds',
318 value => { itemnumber => $item_1->itemnumber }
322 my $biblio_2 = $builder->build_sample_biblio();
323 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
324 my $hold_2 = $builder->build_object(
326 class => 'Koha::Holds',
327 value => { itemnumber => $item_2->itemnumber }
331 my $embed = { 'items' => {} };
334 my @items = ( $item_1, $item_2 );
335 my @holds = ( $hold_1, $hold_2 );
337 my $biblios_api = Koha::Biblios->search(
339 biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
341 )->to_api( { embed => $embed } );
343 foreach my $biblio_api ( @{ $biblios_api } ) {
344 ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
345 is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item matches');
346 ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
354 children => { 'holds' => {} }
360 $biblios_api = Koha::Biblios->search(
362 biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
364 )->to_api( { embed => $embed } );
366 foreach my $biblio_api ( @{ $biblios_api } ) {
368 ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
369 is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item still matches');
370 ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
371 is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $holds[$i]->reserve_id, 'Hold matches');
376 $schema->storage->txn_rollback;
379 subtest "TO_JSON() tests" => sub {
383 $schema->storage->txn_begin;
385 my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
386 my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
388 my $cities = Koha::Cities->search(
390 cityid => [ $city_1->cityid, $city_2->cityid ]
392 { -orderby => { -desc => 'cityid' } }
395 is( $cities->count, 2, 'Count is correct' );
396 my $cities_json = $cities->TO_JSON;
397 is( ref($cities_json), 'ARRAY', 'to_api returns an array' );
398 is_deeply( $cities_json->[0], $city_1->TO_JSON, 'TO_JSON returns the individual objects with ->TO_JSON' );
399 is_deeply( $cities_json->[1], $city_2->TO_JSON,'TO_JSON returns the individual objects with ->TO_JSON' );
401 $schema->storage->txn_rollback;