3 # Copyright 2019 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
=> 24;
27 use Koha
::Authority
::Types
;
30 use Koha
::Patron
::Category
;
31 use Koha
::Patron
::Categories
;
34 use Koha
::DateUtils
qw( dt_from_string );
36 use t
::lib
::TestBuilder
;
41 my $schema = Koha
::Database
->new->schema;
42 $schema->storage->txn_begin;
43 my $builder = t
::lib
::TestBuilder
->new;
45 is
( ref(Koha
::Authority
::Types
->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
47 my @columns = Koha
::Patrons
->columns;
48 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
49 is
( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
51 subtest
'find' => sub {
53 my $patron = $builder->build({source
=> 'Borrower'});
54 my $patron_object = Koha
::Patrons
->find( $patron->{borrowernumber
} );
55 is
( $patron_object->borrowernumber, $patron->{borrowernumber
}, '->find should return the correct object' );
57 my @patrons = Koha
::Patrons
->find( $patron->{borrowernumber
} );
58 is
(scalar @patrons, 1, '->find in list context returns a value');
59 is
($patrons[0]->borrowernumber, $patron->{borrowernumber
}, '->find in list context returns the same value as in scalar context');
62 foo
=> Koha
::Patrons
->find('foo'),
65 is
($patrons->{foo
}, undef, '->find in list context returns undef when no record is found');
67 # Test sending undef to find; should not generate a warning
68 warning_is
{ $patron = Koha
::Patrons
->find( undef ); }
69 "", "Sending undef does not trigger a DBIx warning";
70 warning_is
{ $patron = Koha
::Patrons
->find( undef, undef ); }
71 "", "Sending two undefs does not trigger a DBIx warning too";
74 subtest
'update' => sub {
77 $builder->build( { source
=> 'City', value
=> { city_country
=> 'UK' } } );
78 $builder->build( { source
=> 'City', value
=> { city_country
=> 'UK' } } );
79 $builder->build( { source
=> 'City', value
=> { city_country
=> 'UK' } } );
80 $builder->build( { source
=> 'City', value
=> { city_country
=> 'France' } } );
81 $builder->build( { source
=> 'City', value
=> { city_country
=> 'France' } } );
82 $builder->build( { source
=> 'City', value
=> { city_country
=> 'Germany' } } );
83 Koha
::Cities
->search( { city_country
=> 'UK' } )->update( { city_country
=> 'EU' } );
84 is
( Koha
::Cities
->search( { city_country
=> 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
85 is
( Koha
::Cities
->search( { city_country
=> 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
88 subtest
'reset' => sub {
91 my $patrons = Koha
::Patrons
->search;
92 my $first_borrowernumber = $patrons->next->borrowernumber;
93 my $second_borrowernumber = $patrons->next->borrowernumber;
94 is
( ref( $patrons->reset ), 'Koha::Patrons', 'Koha::Objects->reset should allow chaining' );
95 is
( ref( $patrons->reset->next ), 'Koha::Patron', 'Koha::Objects->reset should allow chaining' );
96 is
( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
99 subtest
'delete' => sub {
102 my $patron_1 = $builder->build({source
=> 'Borrower'});
103 my $patron_2 = $builder->build({source
=> 'Borrower'});
104 is
( Koha
::Patrons
->search({ -or => { borrowernumber
=> [ $patron_1->{borrowernumber
}, $patron_2->{borrowernumber
}]}})->delete, 2, '');
105 is
( Koha
::Patrons
->search({ -or => { borrowernumber
=> [ $patron_1->{borrowernumber
}, $patron_2->{borrowernumber
}]}})->count, 0, '');
108 subtest
'new' => sub {
110 my $a_cat_code = 'A_CAT_CODE';
111 my $patron_category = Koha
::Patron
::Category
->new( { categorycode
=> $a_cat_code } )->store;
112 is
( Koha
::Patron
::Categories
->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
113 Koha
::Patron
::Categories
->find($a_cat_code)->delete;
114 $patron_category = Koha
::Patron
::Category
->new( { categorycode
=> $a_cat_code, category_type
=> undef } )->store;
115 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' );
116 Koha
::Patron
::Categories
->find($a_cat_code)->delete;
119 subtest
'find' => sub {
122 # check find on a single PK
123 my $patron = $builder->build({ source
=> 'Borrower' });
124 is
( Koha
::Patrons
->find($patron->{borrowernumber
})->surname,
125 $patron->{surname
}, "Checking an arbitrary patron column after find"
127 # check find with unique column
128 my $obj = Koha
::Patrons
->find($patron->{cardnumber
}, { key
=> 'cardnumber' });
129 is
( $obj->borrowernumber, $patron->{borrowernumber
},
130 'Find with unique column and key specified' );
131 # check find with an additional where clause in the attrs hash
132 # we do not expect to find something now
133 is
( Koha
::Patrons
->find(
134 $patron->{borrowernumber
},
135 { where
=> { surname
=> { '!=', $patron->{surname
} }}},
136 ), undef, 'Additional where clause in find call' );
138 is
( Koha
::Patrons
->find(), undef, 'Find returns undef if no params passed' );
141 subtest
'search_related' => sub {
143 my $builder = t
::lib
::TestBuilder
->new;
144 my $patron_1 = $builder->build( { source
=> 'Borrower' } );
145 my $patron_2 = $builder->build( { source
=> 'Borrower' } );
146 my $libraries = Koha
::Patrons
->search( { -or => { borrowernumber
=> [ $patron_1->{borrowernumber
}, $patron_2->{borrowernumber
} ] } } )->search_related('branchcode');
147 is
( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
148 is
( $libraries->count, 2, 'Koha::Objects->search_related should work as expected' );
149 is
( $libraries->next->branchcode, $patron_1->{branchcode
}, 'Koha::Objects->search_related should work as expected' );
150 is
( $libraries->next->branchcode, $patron_2->{branchcode
}, 'Koha::Objects->search_related should work as expected' );
152 my @libraries = Koha
::Patrons
->search( { -or => { borrowernumber
=> [ $patron_1->{borrowernumber
}, $patron_2->{borrowernumber
} ] } } )->search_related('branchcode');
153 is
( ref( $libraries[0] ), 'Koha::Library', 'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
154 is
( scalar(@libraries), 2, 'Koha::Objects->search_related should work as expected' );
155 is
( $libraries[0]->branchcode, $patron_1->{branchcode
}, 'Koha::Objects->search_related should work as expected' );
156 is
( $libraries[1]->branchcode, $patron_2->{branchcode
}, 'Koha::Objects->search_related should work as expected' );
159 subtest
'single' => sub {
161 my $builder = t
::lib
::TestBuilder
->new;
162 my $patron_1 = $builder->build( { source
=> 'Borrower' } );
163 my $patron_2 = $builder->build( { source
=> 'Borrower' } );
164 my $patron = Koha
::Patrons
->search({}, { rows
=> 1 })->single;
165 is
(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
166 warning_like
{ Koha
::Patrons
->search->single } qr/SQL that returns multiple rows/,
167 "Warning is presented if single is used for a result with multiple rows.";
170 subtest
'last' => sub {
172 my $builder = t
::lib
::TestBuilder
->new;
173 my $patron_1 = $builder->build( { source
=> 'Borrower' } );
174 my $patron_2 = $builder->build( { source
=> 'Borrower' } );
175 my $last_patron = Koha
::Patrons
->search->last;
176 is
( $last_patron->borrowernumber, $patron_2->{borrowernumber
}, '->last should return the last inserted patron' );
177 $last_patron = Koha
::Patrons
->search({ borrowernumber
=> $patron_1->{borrowernumber
} })->last;
178 is
( $last_patron->borrowernumber, $patron_1->{borrowernumber
}, '->last should work even if there is only 1 result' );
179 $last_patron = Koha
::Patrons
->search({ surname
=> 'should_not_exist' })->last;
180 is
( $last_patron, undef, '->last should return undef if search does not return any results' );
183 subtest
'get_column' => sub {
185 my @cities = Koha
::Cities
->search;
186 my @city_names = map { $_->city_name } @cities;
187 is_deeply
( [ Koha
::Cities
->search->get_column('city_name') ], \
@city_names, 'Koha::Objects->get_column should be allowed' );
190 subtest
'Exceptions' => sub {
193 my $patron_borrowernumber = $builder->build({ source
=> 'Borrower' })->{ borrowernumber
};
194 my $patron = Koha
::Patrons
->find( $patron_borrowernumber );
198 $patron->blah('blah');
200 ok
( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
201 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
202 is
( $_->message, 'The method Koha::Patron->blah is not covered by tests!', 'The message raised should contain the package and the method' );
206 $patron->set({ blah
=> 'blah' });
208 ok
( $_->isa('Koha::Exceptions::Object::PropertyNotFound'),
209 'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' );
214 Koha
::Patrons
->search->not_covered_yet;
216 ok
( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
217 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
218 is
( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
222 Koha
::Patrons
->not_covered_yet;
224 ok
( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
225 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
226 is
( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
230 $schema->storage->txn_rollback;
232 subtest
'->is_paged and ->pager tests' => sub {
236 $schema->storage->txn_begin;
238 # Delete existing patrons
239 Koha
::Checkouts
->delete;
240 Koha
::Patrons
->delete;
243 $builder->build_object({ class => 'Koha::Patrons' });
246 # Non-paginated search
247 my $patrons = Koha
::Patrons
->search();
248 is
( $patrons->count, 10, 'Search returns all patrons' );
249 ok
( !$patrons->is_paged, 'Search is not paged' );
252 $patrons = Koha
::Patrons
->search( undef, { 'page' => 1, 'rows' => 3 } );
253 is
( $patrons->count, 3, 'Search returns only one page, 3 patrons' );
254 ok
( $patrons->is_paged, 'Search is paged' );
255 my $pager = $patrons->pager;
256 is
( ref($patrons->pager), 'DBIx::Class::ResultSet::Pager',
257 'Koha::Objects->pager returns a valid DBIx::Class object' );
259 $schema->storage->txn_rollback;
262 subtest
'->search() tests' => sub {
266 $schema->storage->txn_begin;
268 my $count = Koha
::Patrons
->search->count;
272 $builder->build_object({ class => 'Koha::Patrons' });
275 my $patrons = Koha
::Patrons
->search();
276 is
( ref($patrons), 'Koha::Patrons', 'search in scalar context returns the Koha::Object-based type' );
277 my @patrons = Koha
::Patrons
->search();
278 is
( scalar @patrons, $count + 10, 'search in list context returns a list of objects' );
281 is
( ref($patrons[$i]), 'Koha::Patron', 'Objects in the list have the singular type' );
285 $schema->storage->txn_rollback;
288 subtest
"to_api() tests" => sub {
292 $schema->storage->txn_begin;
294 my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
295 my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
297 my $cities = Koha
::Cities
->search(
299 cityid
=> [ $city_1->cityid, $city_2->cityid ]
301 { -orderby
=> { -desc
=> 'cityid' } }
304 is
( $cities->count, 2, 'Count is correct' );
305 my $cities_api = $cities->to_api;
306 is
( ref( $cities_api ), 'ARRAY', 'to_api returns an array' );
307 is_deeply
( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' );
308 is_deeply
( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' );
310 my $biblio_1 = $builder->build_sample_biblio();
311 my $item_1 = $builder->build_sample_item({ biblionumber
=> $biblio_1->biblionumber });
312 my $hold_1 = $builder->build_object(
314 class => 'Koha::Holds',
315 value
=> { itemnumber
=> $item_1->itemnumber }
319 my $biblio_2 = $builder->build_sample_biblio();
320 my $item_2 = $builder->build_sample_item({ biblionumber
=> $biblio_2->biblionumber });
321 my $hold_2 = $builder->build_object(
323 class => 'Koha::Holds',
324 value
=> { itemnumber
=> $item_2->itemnumber }
328 my $embed = { 'items' => {} };
331 my @items = ( $item_1, $item_2 );
332 my @holds = ( $hold_1, $hold_2 );
334 my $biblios_api = Koha
::Biblios
->search(
336 biblionumber
=> [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
338 )->to_api( { embed
=> $embed } );
340 foreach my $biblio_api ( @
{ $biblios_api } ) {
341 ok
(exists $biblio_api->{items
}, 'Items where embedded in biblio results');
342 is
($biblio_api->{items
}->[0]->{item_id
}, $items[$i]->itemnumber, 'Item matches');
343 ok
(!exists $biblio_api->{items
}->[0]->{holds
}, 'No holds info should be embedded yet');
351 children
=> { 'holds' => {} }
357 $biblios_api = Koha
::Biblios
->search(
359 biblionumber
=> [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
361 )->to_api( { embed
=> $embed } );
363 foreach my $biblio_api ( @
{ $biblios_api } ) {
365 ok
(exists $biblio_api->{items
}, 'Items where embedded in biblio results');
366 is
($biblio_api->{items
}->[0]->{item_id
}, $items[$i]->itemnumber, 'Item still matches');
367 ok
(exists $biblio_api->{items
}->[0]->{holds
}, 'Holds info should be embedded');
368 is
($biblio_api->{items
}->[0]->{holds
}->[0]->{hold_id
}, $holds[$i]->reserve_id, 'Hold matches');
373 $schema->storage->txn_rollback;
376 subtest
"TO_JSON() tests" => sub {
380 $schema->storage->txn_begin;
382 my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
383 my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
385 my $cities = Koha
::Cities
->search(
387 cityid
=> [ $city_1->cityid, $city_2->cityid ]
389 { -orderby
=> { -desc
=> 'cityid' } }
392 is
( $cities->count, 2, 'Count is correct' );
393 my $cities_json = $cities->TO_JSON;
394 is
( ref($cities_json), 'ARRAY', 'to_api returns an array' );
395 is_deeply
( $cities_json->[0], $city_1->TO_JSON, 'TO_JSON returns the individual objects with ->TO_JSON' );
396 is_deeply
( $cities_json->[1], $city_2->TO_JSON,'TO_JSON returns the individual objects with ->TO_JSON' );
398 $schema->storage->txn_rollback;
401 # Koha::Object[s] must behave the same as DBIx::Class
402 subtest
'Return same values as DBIx::Class' => sub {
405 subtest
'Delete' => sub {
408 $schema->storage->txn_begin;
410 subtest
'Simple Koha::Objects - Koha::Cities' => sub {
413 subtest
'Koha::Object->delete' => sub {
417 my ( $r_us, $e_us, $r_them, $e_them );
419 # CASE 1 - Delete an existing object
420 my $c = Koha
::City
->new( { city_name
=> 'city4test' } )->store;
421 try
{ $r_us = $c->delete; } catch
{ $e_us = $_ };
422 $c = $schema->resultset('City')->new( { city_name
=> 'city4test_2' } )->update_or_insert;
423 try
{ $r_them = $c->delete; } catch
{ $e_them = $_ };
424 ok
( ref($r_us) && ref($r_them),
425 'Successful delete should return the object ' );
426 ok
( !defined $e_us && !defined $e_them,
427 'Successful delete should not raise an exception' );
428 is
( ref($r_us), 'Koha::City', 'Successful delete should return our Koha::Obect based object' );
430 # CASE 2 - Delete an object that is not in storage
431 try
{ $r_us = $r_us->delete; } catch
{ $e_us = $_ };
432 try
{ $r_them = $r_them->delete; } catch
{ $e_them = $_ };
434 defined $e_us && defined $e_them,
435 'Delete an object that is not in storage should raise an exception'
437 is
( ref($e_us), 'DBIx::Class::Exception' )
438 ; # FIXME This needs adjustement, we want to throw a Koha::Exception
442 subtest
'Koha::Objects->delete' => sub {
446 my ( $r_us, $e_us, $r_them, $e_them );
448 # CASE 1 - Delete existing objects
449 my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
450 my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
451 my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
452 my $cities = Koha
::Cities
->search(
464 try
{ $r_us = $cities->delete; } catch
{ $e_us = $_ };
466 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
467 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
468 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
469 $cities = $schema->resultset('City')->search(
481 try
{ $r_them = $cities->delete; } catch
{ $e_them = $_ };
483 ok
( $r_us == 3 && $r_them == 3 );
484 ok
(!defined($e_us) && !defined($e_them));
486 # CASE 2 - One of the object is not in storage
487 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
488 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
489 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
490 $cities = Koha
::Cities
->search(
502 $city_2->delete; # We delete one of the object
503 try
{ $r_us = $cities->delete; } catch
{ $e_us = $_ };
505 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
506 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
507 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
508 $cities = $schema->resultset('City')->search(
520 $city_2->delete; # We delete one of the object
521 try
{ $r_them = $cities->delete; } catch
{ $e_them = $_ };
523 ok
( $r_us == 2 && $r_them == 2 );
524 ok
(!defined($e_us) && !defined($e_them));
528 subtest
'Overwritten Koha::Objects->delete - Koha::Patrons' => sub {
532 subtest
'Koha::Object->delete' => sub {
536 my ( $r_us, $e_us, $r_them, $e_them );
538 # CASE 1 - Delete an existing patron
539 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
540 my $patron_data = $patron->unblessed;
543 $patron = Koha
::Patron
->new( $patron_data )->store;
544 try
{$r_us = $patron->delete;} catch
{ $e_us = $_ };
545 $patron = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
546 try
{$r_them = $patron->delete;} catch
{ $e_them = $_ };
547 ok
( ref($r_us) && ref($r_them),
548 'Successful delete should return the patron object' );
549 ok
( !defined $e_us && !defined $e_them,
550 'Successful delete should not raise an exception' );
551 is
( ref($r_us), 'Koha::Patron',
552 'Successful delete should return our Koha::Obect based object' );
554 # CASE 2 - Delete a patron that is not in storage
555 try
{ $r_us = $r_us->delete; } catch
{ $e_us = $_ };
556 try
{ $r_them = $r_them->delete; } catch
{ $e_them = $_ };
558 defined $e_us && defined $e_them,
559 'Delete a patron that is not in storage should raise an exception'
561 is
( ref($e_us), 'DBIx::Class::Exception' )
562 ; # FIXME This needs adjustement, we want to throw a Koha::Exception
564 # CASE 3 - Delete a patron that cannot be deleted (as a checkout)
565 $patron = Koha
::Patron
->new($patron_data)->store;
566 $builder->build_object(
568 class => 'Koha::Checkouts',
569 value
=> { borrowernumber
=> $patron->borrowernumber }
572 try
{ $r_us = $r_us->delete; } catch
{ $e_us = $_ };
573 $patron = $schema->resultset('Borrower')->find( $patron->borrowernumber );
574 try
{ $r_them = $r_them->delete; } catch
{ $e_them = $_ };
576 defined $e_us && defined $e_them,
577 'Delete a patron that cannot be deleted should raise an exception'
579 is
( ref($e_us), 'DBIx::Class::Exception' )
580 ; # FIXME This needs adjustement, we want to throw a Koha::Exception
583 subtest
'Koha::Objects->delete' => sub {
587 my ( $r_us, $e_us, $r_them, $e_them );
589 # CASE 1 - Delete existing objects
590 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
591 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
592 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
593 my $patrons = Koha
::Patrons
->search(
597 $patron_1->borrowernumber,
598 $patron_2->borrowernumber,
599 $patron_3->borrowernumber
605 try
{ $r_us = $patrons->delete; } catch
{ $e_us = $_ };
607 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
608 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
609 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
610 $patrons = $schema->resultset('Borrower')->search(
614 $patron_1->borrowernumber,
615 $patron_2->borrowernumber,
616 $patron_3->borrowernumber
622 try
{ $r_them = $patrons->delete; } catch
{ $e_them = $_ };
624 ok
( $r_us == 3 && $r_them == 3, '->delete should return the number of deleted patrons' );
625 ok
(!defined($e_us) && !defined($e_them), '->delete should not raise exception if everything went well');
627 # CASE 2 - One of the patrons is not in storage
628 undef $_ for $r_us, $e_us, $r_them, $e_them;
629 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
630 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
631 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
632 $patrons = Koha
::Patrons
->search(
636 $patron_1->borrowernumber,
637 $patron_2->borrowernumber,
638 $patron_3->borrowernumber
644 $patron_2->delete; # We delete one of the patron
645 try
{ $r_us = $patrons->delete; } catch
{ $e_us = $_ };
647 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
648 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
649 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
650 $patrons = $schema->resultset('Borrower')->search(
654 $patron_1->borrowernumber,
655 $patron_2->borrowernumber,
656 $patron_3->borrowernumber
662 $patron_2->delete; # We delete one of the patron
663 try
{ $r_them = $patrons->delete; } catch
{ $e_them = $_ };
665 ok
( $r_us == 2 && $r_them == 2, 'Delete patrons with one that was not in storage should delete the patrons' );
666 ok
(!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
668 # CASE 3 - Delete a set of patrons with one that that cannot be deleted (as a checkout)
669 undef $_ for $r_us, $e_us, $r_them, $e_them;
670 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
671 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
672 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
673 $patrons = Koha
::Patrons
->search(
677 $patron_1->borrowernumber,
678 $patron_2->borrowernumber,
679 $patron_3->borrowernumber
685 # Adding a checkout to patron_2
686 $builder->build_object(
688 class => 'Koha::Checkouts',
689 value
=> { borrowernumber
=> $patron_2->borrowernumber }
693 try
{ $r_us = $patrons->delete; } catch
{ $e_us = $_ };
694 my $not_deleted_us = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
696 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
697 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
698 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
699 $patrons = $schema->resultset('Borrower')->search(
703 $patron_1->borrowernumber,
704 $patron_2->borrowernumber,
705 $patron_3->borrowernumber
711 # Adding a checkout to patron_2
712 $builder->build_object(
714 class => 'Koha::Checkouts',
715 value
=> { borrowernumber
=> $patron_2->borrowernumber }
719 try
{ $r_them = $patrons->delete; } catch
{ $e_them = $_ };
721 my $not_deleted_them = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
723 defined $e_us && defined $e_them,
724 'Delete patrons with one that cannot be deleted should raise an exception'
726 is
( ref($e_us), 'DBIx::Class::Exception' )
727 ; # FIXME This needs adjustement, we want to throw a Koha::Exception
729 ok
($not_deleted_us == 3 && $not_deleted_them == 3, 'If one patron cannot be deleted, none should have been deleted');
733 $schema->storage->txn_rollback;
737 subtest
'Update (set/store)' => sub {
740 $schema->storage->txn_begin;
742 subtest
'Simple Koha::Objects - Koha::Cities' => sub {
745 subtest
'Koha::Object->update' => sub {
749 my ( $r_us, $e_us, $r_them, $e_them );
751 # CASE 1 - Update an existing object
752 my $c_us = Koha
::City
->new( { city_name
=> 'city4test' } )->store;
753 try
{ $r_us = $c_us->update({ city_country
=> 'country4test' }); } catch
{ $e_us = $_ };
754 my $c_them = $schema->resultset('City')->new( { city_name
=> 'city4test_2' } )->update_or_insert;
755 try
{ $r_them = $c_them->update({ city_country
=> 'country4test_2' }); } catch
{ $e_them = $_ };
756 ok
( ref($r_us) && ref($r_them),
757 'Successful update should return the object ' );
758 ok
( !defined $e_us && !defined $e_them,
759 'Successful update should not raise an exception' );
760 is
( ref($r_us), 'Koha::City', 'Successful update should return our Koha::Obect based object' );
762 # CASE 2 - Update an object that is not in storage
765 try
{ $r_us = $c_us->update({ city_country
=> 'another_country' }); } catch
{ $e_us = $_ };
766 try
{ $r_them = $c_them->update({ city_country
=> 'another_country' }); } catch
{ $e_them = $_ };
768 defined $e_us && defined $e_them,
769 'Update an object that is not in storage should raise an exception'
771 is
( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' );
774 subtest
'Koha::Objects->update' => sub {
778 my ( $r_us, $e_us, $r_them, $e_them );
780 # CASE 1 - update existing objects
781 my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
782 my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
783 my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
784 my $cities = Koha
::Cities
->search(
796 try
{ $r_us = $cities->update({ city_country
=> 'country4test' }); } catch
{ $e_us = $_ };
798 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
799 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
800 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
801 $cities = $schema->resultset('City')->search(
813 try
{ $r_them = $cities->update({ city_country
=> 'country4test' }); } catch
{ $e_them = $_ };
815 ok
( $r_us == 3 && $r_them == 3, '->update should return the number of updated cities' );
816 ok
(!defined($e_us) && !defined($e_them));
818 # CASE 2 - One of the object is not in storage
819 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
820 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
821 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
822 $cities = Koha
::Cities
->search(
834 $city_2->delete; # We delete one of the object
835 try
{ $r_us = $cities->update({ city_country
=> 'country4test' }); } catch
{ $e_us = $_ };
837 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
838 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
839 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
840 $cities = $schema->resultset('City')->search(
852 $city_2->delete; # We delete one of the object
853 try
{ $r_them = $cities->update({ city_country
=> 'country4test' }); } catch
{ $e_them = $_ };
855 ok
( $r_us == 2 && $r_them == 2, '->update should return the number of updated cities' );
856 ok
(!defined($e_us) && !defined($e_them));
859 { Koha
::Cities
->update({ city_country
=> 'Castalia' }); }
860 'Koha::Exceptions::Object::NotInstantiated',
861 'Exception thrown if not instantiated class';
863 is
( "$@", 'Tried to access the \'update\' method, but Koha::Cities is not instantiated', 'Exception stringified correctly' );
868 subtest
'Overwritten Koha::Objects->store|update - Koha::Patrons' => sub {
872 subtest
'Koha::Object->update' => sub {
876 my ( $r_us, $e_us, $r_them, $e_them );
878 # CASE 1 - Update an existing patron
879 my $patron_us = $builder->build_object({ class => 'Koha::Patrons' });
880 try
{$r_us = $patron_us->update({city
=> 'a_city'});} catch
{ $e_us = $_ };
882 my $patron_data = $builder->build_object({ class => 'Koha::Patrons' })->delete->unblessed;
883 my $patron_them = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
884 try
{$r_them = $patron_them->update({city
=> 'a_city'});} catch
{ $e_them = $_ };
885 ok
( ref($r_us) && ref($r_them),
886 'Successful update should return the patron object' );
887 ok
( !defined $e_us && !defined $e_them,
888 'Successful update should not raise an exception' );
889 is
( ref($r_us), 'Koha::Patron',
890 'Successful update should return our Koha::Obect based object' );
892 # CASE 2 - Update a patron that is not in storage
894 $patron_them->delete;
895 try
{ $r_us = $patron_us->update({ city
=> 'another_city' }); } catch
{ $e_us = $_ };
896 try
{ $r_them = $patron_them->update({ city
=> 'another_city' }); } catch
{ $e_them = $_ };
898 defined $e_us && defined $e_them,
899 'Update a patron that is not in storage should raise an exception'
901 is
( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' );
905 subtest
'Koha::Objects->Update ' => sub {
909 my ( $r_us, $e_us, $r_them, $e_them );
911 # CASE 1 - Update existing objects
912 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
913 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
914 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
915 my $patrons_us = Koha
::Patrons
->search(
919 $patron_1->borrowernumber,
920 $patron_2->borrowernumber,
921 $patron_3->borrowernumber
927 try
{ $r_us = $patrons_us->update({ city
=> 'a_city' }); } catch
{ $e_us = $_ };
929 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
930 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
931 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
932 my $patrons_them = $schema->resultset('Borrower')->search(
936 $patron_1->borrowernumber,
937 $patron_2->borrowernumber,
938 $patron_3->borrowernumber
944 try
{ $r_them = $patrons_them->update({ city
=> 'a_city' }); } catch
{ $e_them = $_ };
946 ok
( $r_us == 3 && $r_them == 3, '->update should return the number of update patrons' );
947 ok
(!defined($e_us) && !defined($e_them), '->update should not raise exception if everything went well');
949 # CASE 2 - One of the patrons is not in storage
950 undef $_ for $r_us, $e_us, $r_them, $e_them;
951 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
952 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
953 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
954 $patrons_us = Koha
::Patrons
->search(
958 $patron_1->borrowernumber,
959 $patron_2->borrowernumber,
960 $patron_3->borrowernumber
966 $patron_2->delete; # We delete one of the patron
967 try
{ $r_us = $patrons_us->update({ city
=> 'another_city' }); } catch
{ $e_us = $_ };
969 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
970 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
971 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
972 $patrons_them = $schema->resultset('Borrower')->search(
976 $patron_1->borrowernumber,
977 $patron_2->borrowernumber,
978 $patron_3->borrowernumber
984 $patron_2->delete; # We delete one of the patron
985 try
{ $r_them = $patrons_them->update({ city
=> 'another_city' }); } catch
{ $e_them = $_ };
987 ok
( $r_us == 2 && $r_them == 2, 'Update patrons with one that was not in storage should update the patrons' );
988 ok
(!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
991 # Testing no_triggers
992 t
::lib
::Mocks
::mock_preference
('uppercasesurnames', 1);
993 $patrons_us = Koha
::Patrons
->search(
997 $patron_1->borrowernumber,
998 $patron_2->borrowernumber,
999 $patron_3->borrowernumber
1004 $patrons_us->update({ surname
=> 'foo' }); # Koha::Patron->store is supposed to uppercase the surnames
1005 is
( $patrons_us->search({ surname
=> 'FOO' })->count, 2, 'Koha::Patron->store is hit' );
1007 $patrons_us->update({ surname
=> 'foo' }, { no_triggers
=> 1 }); # The surnames won't be uppercase as we won't hit Koha::Patron->store
1008 is
( $patrons_us->search({ surname
=> 'foo' })->count, 2, 'Koha::Patron->store is not hit');
1014 $schema->storage->txn_rollback;
1020 subtest
"attributes_from_api() tests" => sub {
1024 $schema->storage->txn_begin;
1026 my $cities_rs = Koha
::Cities
->new;
1027 my $city = Koha
::City
->new;
1029 my $api_attributes = {
1035 $cities_rs->attributes_from_api($api_attributes),
1036 $city->attributes_from_api($api_attributes)
1039 $schema->storage->txn_rollback;
1043 subtest
"filter_by_last_update" => sub {
1045 $schema->storage->txn_begin;
1047 my $now = dt_from_string
->truncate( to
=> 'day' );
1048 my @borrowernumbers;
1049 # Building 6 patrons that have been created today, yesterday, ... 1 per day
1050 for my $i ( 0 .. 5 ) {
1051 push @borrowernumbers,
1052 $builder->build_object(
1054 class => 'Koha::Patrons',
1055 value
=> { updated_on
=> $now->clone->subtract( days
=> $i ) }
1060 my $patrons = Koha
::Patrons
->search(
1061 { borrowernumber
=> { -in => \
@borrowernumbers } } );
1064 $patrons->filter_by_last_update( { timestamp_column_name
=> 'updated_on' } )
1069 $_->isa('Koha::Exceptions::MissingParameter'),
1070 'Should raise an exception if no parameter given'
1074 my $count = $patrons->filter_by_last_update(
1075 { timestamp_column_name
=> 'updated_on', days
=> 2 } )->count;
1076 is
( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' );
1078 $count = $patrons->filter_by_last_update(
1079 { timestamp_column_name
=> 'updated_on', days
=> 1 } )->count;
1080 is
( $count, 4, '4 patrons have been updated before yesterday (exclusive)' );
1082 $count = $patrons->filter_by_last_update(
1083 { timestamp_column_name
=> 'updated_on', days
=> 0 } )->count;
1084 is
( $count, 5, '5 patrons have been updated before today (exclusive)' );
1086 $count = $patrons->filter_by_last_update(
1087 { timestamp_column_name
=> 'updated_on', from
=> $now } )->count;
1088 is
( $count, 1, '1 patron has been updated "from today" (inclusive)' );
1090 $count = $patrons->filter_by_last_update(
1091 { timestamp_column_name
=> 'updated_on', to
=> $now } )->count;
1092 is
( $count, 6, '6 patrons have been updated "to today" (inclusive)' );
1094 $count = $patrons->filter_by_last_update(
1096 timestamp_column_name
=> 'updated_on',
1097 from
=> $now->clone->subtract( days
=> 4 ),
1098 to
=> $now->clone->subtract( days
=> 2 )
1101 is
( $count, 3, '3 patrons have been updated between D-4 and D-2' );
1103 t
::lib
::Mocks
::mock_preference
( 'dateformat', 'metric' );
1105 $count = $patrons->filter_by_last_update(
1106 { timestamp_column_name
=> 'updated_on', from
=> '1970-12-31' } )
1112 'No exception raised, from and to parameters can take an iso formatted date'
1117 $count = $patrons->filter_by_last_update(
1118 { timestamp_column_name
=> 'updated_on', from
=> '31/12/1970' } )
1124 'No exception raised, from and to parameters can take an metric formatted date (depending on dateformat syspref)'
1129 $schema->storage->txn_rollback;
1132 subtest
"from_api_mapping() tests" => sub {
1136 $schema->storage->txn_begin;
1138 my $cities_rs = Koha
::Cities
->new;
1139 my $city = Koha
::City
->new;
1142 $cities_rs->from_api_mapping,
1143 $city->from_api_mapping
1146 $schema->storage->txn_rollback;
1149 subtest
'prefetch_whitelist() tests' => sub {
1153 $schema->storage->txn_begin;
1155 my $biblios = Koha
::Biblios
->new;
1157 my $prefetch_whitelist = $biblios->prefetch_whitelist;
1160 exists $prefetch_whitelist->{orders
},
1161 'Relationship matching method name is listed'
1164 $prefetch_whitelist->{orders
},
1165 'Koha::Acquisition::Order',
1166 'Guessed the non-standard object class correctly'
1170 $prefetch_whitelist->{items
},
1172 'Guessed the standard object class correctly'
1175 $schema->storage->txn_rollback;
1178 subtest
'empty() tests' => sub {
1182 $schema->storage->txn_begin;
1184 # Add a patron, we need more than 0
1185 $builder->build_object({ class => 'Koha::Patrons' });
1186 ok
( Koha
::Patrons
->count > 0, 'There is more than one Koha::Patron on the resultset' );
1188 my $empty = Koha
::Patrons
->new->empty;
1189 is
( ref($empty), 'Koha::Patrons', '->empty returns a Koha::Patrons iterator' );
1190 is
( $empty->count, 0, 'The empty resultset is, well, empty :-D' );
1193 { Koha
::Patrons
->empty; }
1194 'Koha::Exceptions::Object::NotInstantiated',
1195 'Exception thrown if not instantiated class';
1197 is
( "$@", 'Tried to access the \'empty\' method, but Koha::Patrons is not instantiated', 'Exception stringified correctly' );
1199 $schema->storage->txn_rollback;
1202 subtest
'delete() tests' => sub {
1206 $schema->storage->txn_begin;
1208 # Make sure no cities
1209 warnings_are
{ Koha
::Cities
->delete }[],
1210 "No warnings, no Koha::City->delete called as it doesn't exist";
1213 my $mocked_city = Test
::MockModule
->new('Koha::City');
1217 shift->_result->delete;
1218 warn "delete called!";
1223 $builder->build_object( { class => 'Koha::Cities' } );
1224 $builder->build_object( { class => 'Koha::Cities' } );
1226 my $cities = Koha
::Cities
->search;
1228 warnings_are
{ $cities->delete }
1229 [ "delete called!", "delete called!" ],
1230 "No warnings, no Koha::City->delete called as it doesn't exist";
1232 $schema->storage->txn_rollback;