Bug 28948: (QA follow-up) Convert to allow-list
[koha.git] / t / db_dependent / Koha / Objects.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 24;
23 use Test::Exception;
24 use Test::MockModule;
25 use Test::Warn;
26
27 use C4::Context;
28
29 use Koha::Authority::Types;
30 use Koha::Cities;
31 use Koha::Biblios;
32 use Koha::Patron::Category;
33 use Koha::Patron::Categories;
34 use Koha::Patrons;
35 use Koha::Database;
36 use Koha::DateUtils qw( dt_from_string );
37
38 use t::lib::TestBuilder;
39 use t::lib::Mocks;
40
41 use Try::Tiny;
42
43 my $schema = Koha::Database->new->schema;
44 $schema->storage->txn_begin;
45 my $builder = t::lib::TestBuilder->new;
46
47 is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
48
49 my @columns = Koha::Patrons->columns;
50 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
51 is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
52
53 subtest 'find' => sub {
54     plan tests => 6;
55     my $patron = $builder->build({source => 'Borrower'});
56     my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
57     is( $patron_object->borrowernumber, $patron->{borrowernumber}, '->find should return the correct object' );
58
59     my @patrons = Koha::Patrons->find( $patron->{borrowernumber} );
60     is(scalar @patrons, 1, '->find in list context returns a value');
61     is($patrons[0]->borrowernumber, $patron->{borrowernumber}, '->find in list context returns the same value as in scalar context');
62
63     my $patrons = {
64         foo => Koha::Patrons->find('foo'),
65         bar => 'baz',
66     };
67     is ($patrons->{foo}, undef, '->find in list context returns undef when no record is found');
68
69     # Test sending undef to find; should not generate a warning
70     warning_is { $patron = Koha::Patrons->find( undef ); }
71         "", "Sending undef does not trigger a DBIx warning";
72     warning_is { $patron = Koha::Patrons->find( undef, undef ); }
73         "", "Sending two undefs does not trigger a DBIx warning too";
74 };
75
76 subtest 'update' => sub {
77     plan tests => 2;
78
79     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
80     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
81     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
82     $builder->build( { source => 'City', value => { city_country => 'France' } } );
83     $builder->build( { source => 'City', value => { city_country => 'France' } } );
84     $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
85     Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
86     is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
87     is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
88 };
89
90 subtest 'reset' => sub {
91     plan tests => 3;
92
93     my $patrons = Koha::Patrons->search;
94     my $first_borrowernumber = $patrons->next->borrowernumber;
95     my $second_borrowernumber = $patrons->next->borrowernumber;
96     is( ref( $patrons->reset ), 'Koha::Patrons', 'Koha::Objects->reset should allow chaining' );
97     is( ref( $patrons->reset->next ), 'Koha::Patron', 'Koha::Objects->reset should allow chaining' );
98     is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
99 };
100
101 subtest 'delete' => sub {
102     plan tests => 2;
103
104     my $patron_1 = $builder->build({source => 'Borrower'});
105     my $patron_2 = $builder->build({source => 'Borrower'});
106     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, '');
107     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->count, 0, '');
108 };
109
110 subtest 'new' => sub {
111     plan tests => 2;
112     my $a_cat_code = 'A_CAT_CODE';
113     my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store;
114     is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
115     Koha::Patron::Categories->find($a_cat_code)->delete;
116     $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store;
117     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' );
118     Koha::Patron::Categories->find($a_cat_code)->delete;
119 };
120
121 subtest 'find' => sub {
122     plan tests => 4;
123
124     # check find on a single PK
125     my $patron = $builder->build({ source => 'Borrower' });
126     is( Koha::Patrons->find($patron->{borrowernumber})->surname,
127         $patron->{surname}, "Checking an arbitrary patron column after find"
128     );
129     # check find with unique column
130     my $obj = Koha::Patrons->find($patron->{cardnumber}, { key => 'cardnumber' });
131     is( $obj->borrowernumber, $patron->{borrowernumber},
132         'Find with unique column and key specified' );
133     # check find with an additional where clause in the attrs hash
134     # we do not expect to find something now
135     is( Koha::Patrons->find(
136         $patron->{borrowernumber},
137         { where => { surname => { '!=', $patron->{surname} }}},
138     ), undef, 'Additional where clause in find call' );
139
140     is( Koha::Patrons->find(), undef, 'Find returns undef if no params passed' );
141 };
142
143 subtest 'search_related' => sub {
144     plan tests => 6;
145     my $builder   = t::lib::TestBuilder->new;
146     my $patron_1  = $builder->build( { source => 'Borrower' } );
147     my $patron_2  = $builder->build( { source => 'Borrower' } );
148     my $libraries = Koha::Patrons->search(
149         {
150             -or => {
151                 borrowernumber =>
152                   [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ]
153             }
154         }
155     )->search_related('branchcode');
156     is( ref($libraries), 'Koha::Libraries',
157         'Koha::Objects->search_related should return an instanciated Koha::Objects-based object'
158     );
159     is( $libraries->count, 2,
160         'Koha::Objects->search_related should work as expected' );
161     ok( eq_array(
162         [ $libraries->get_column('branchcode') ],
163         [ $patron_1->{branchcode}, $patron_2->{branchcode} ] ),
164         'Koha::Objects->search_related should work as expected'
165     );
166
167     my @libraries = Koha::Patrons->search(
168         {
169             -or => {
170                 borrowernumber =>
171                   [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ]
172             }
173         }
174     )->search_related('branchcode');
175     is(
176         ref( $libraries[0] ), 'Koha::Library',
177         'Koha::Objects->search_related should return a list of Koha::Object-based objects'
178     );
179     is( scalar(@libraries), 2,
180         'Koha::Objects->search_related should work as expected' );
181     ok( eq_array(
182         [ map { $_->branchcode } @libraries ],
183         [ $patron_1->{branchcode}, $patron_2->{branchcode} ] ),
184         'Koha::Objects->search_related should work as expected'
185     );
186 };
187
188 subtest 'single' => sub {
189     plan tests => 2;
190     my $builder   = t::lib::TestBuilder->new;
191     my $patron_1  = $builder->build( { source => 'Borrower' } );
192     my $patron_2  = $builder->build( { source => 'Borrower' } );
193     my $patron = Koha::Patrons->search({}, { rows => 1 })->single;
194     is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
195     warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/,
196     "Warning is presented if single is used for a result with multiple rows.";
197 };
198
199 subtest 'last' => sub {
200     plan tests => 3;
201     my $builder = t::lib::TestBuilder->new;
202     my $patron_1  = $builder->build( { source => 'Borrower' } );
203     my $patron_2  = $builder->build( { source => 'Borrower' } );
204     my $last_patron = Koha::Patrons->search->last;
205     is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' );
206     $last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last;
207     is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' );
208     $last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last;
209     is( $last_patron, undef, '->last should return undef if search does not return any results' );
210 };
211
212 subtest 'get_column' => sub {
213     plan tests => 1;
214     my @cities = Koha::Cities->search;
215     my @city_names = map { $_->city_name } @cities;
216     is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' );
217 };
218
219 subtest 'Exceptions' => sub {
220     plan tests => 7;
221
222     my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
223     my $patron = Koha::Patrons->find( $patron_borrowernumber );
224
225     # Koha::Object
226     try {
227         $patron->blah('blah');
228     } catch {
229         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
230             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
231         is( $_->message, 'The method Koha::Patron->blah is not covered by tests!', 'The message raised should contain the package and the method' );
232     };
233
234     try {
235         $patron->set({ blah => 'blah' });
236     } catch {
237         ok( $_->isa('Koha::Exceptions::Object::PropertyNotFound'),
238             'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' );
239     };
240
241     # Koha::Objects
242     try {
243         Koha::Patrons->search->not_covered_yet;
244     } catch {
245         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
246             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
247         is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
248     };
249
250     try {
251         Koha::Patrons->not_covered_yet;
252     } catch {
253         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
254             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
255         is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
256     };
257 };
258
259 $schema->storage->txn_rollback;
260
261 subtest '->is_paged and ->pager tests' => sub {
262
263     plan tests => 5;
264
265     $schema->storage->txn_begin;
266
267     # Count existing patrons
268     my $nb_patrons = Koha::Patrons->search()->count;
269     # Create 10 more patrons
270     foreach (1..10) {
271         $builder->build_object({ class => 'Koha::Patrons' });
272     }
273
274     # Non-paginated search
275     my $patrons = Koha::Patrons->search();
276     is( $patrons->count, $nb_patrons + 10, 'Search returns all patrons' );
277     ok( !$patrons->is_paged, 'Search is not paged' );
278
279     # Paginated search
280     $patrons = Koha::Patrons->search( undef, { 'page' => 1, 'rows' => 3 } );
281     is( $patrons->count, 3, 'Search returns only one page, 3 patrons' );
282     ok( $patrons->is_paged, 'Search is paged' );
283     my $pager = $patrons->pager;
284     is( ref($patrons->pager), 'DBIx::Class::ResultSet::Pager',
285        'Koha::Objects->pager returns a valid DBIx::Class object' );
286
287     $schema->storage->txn_rollback;
288 };
289
290 subtest '->search() tests' => sub {
291
292     plan tests => 12;
293
294     $schema->storage->txn_begin;
295
296     my $count = Koha::Patrons->search->count;
297
298     # Create 10 patrons
299     foreach (1..10) {
300         $builder->build_object({ class => 'Koha::Patrons' });
301     }
302
303     my $patrons = Koha::Patrons->search();
304     is( ref($patrons), 'Koha::Patrons', 'search in scalar context returns the Koha::Object-based type' );
305     my @patrons = Koha::Patrons->search();
306     is( scalar @patrons, $count + 10, 'search in list context returns a list of objects' );
307     my $i = 0;
308     foreach (1..10) {
309         is( ref($patrons[$i]), 'Koha::Patron', 'Objects in the list have the singular type' );
310         $i++;
311     }
312
313     $schema->storage->txn_rollback;
314 };
315
316 subtest "to_api() tests" => sub {
317
318     plan tests => 19;
319
320     $schema->storage->txn_begin;
321
322     my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
323     my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
324
325     my $cities = Koha::Cities->search(
326         {
327             cityid => [ $city_1->cityid, $city_2->cityid ]
328         },
329         { -orderby => { -desc => 'cityid' } }
330     );
331
332     is( $cities->count, 2, 'Count is correct' );
333     my $cities_api = $cities->to_api;
334     is( ref( $cities_api ), 'ARRAY', 'to_api returns an array' );
335     is_deeply( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' );
336     is_deeply( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' );
337
338     my $biblio_1 = $builder->build_sample_biblio();
339     my $item_1   = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
340     my $hold_1   = $builder->build_object(
341         {
342             class => 'Koha::Holds',
343             value => { itemnumber => $item_1->itemnumber }
344         }
345     );
346
347     my $biblio_2 = $builder->build_sample_biblio();
348     my $item_2   = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
349     my $hold_2   = $builder->build_object(
350         {
351             class => 'Koha::Holds',
352             value => { itemnumber => $item_2->itemnumber }
353         }
354     );
355
356     my $embed = { 'items' => {} };
357
358     my $i = 0;
359     my @items = ( $item_1, $item_2 );
360     my @holds = ( $hold_1, $hold_2 );
361
362     my $biblios_api = Koha::Biblios->search(
363         {
364             biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
365         }
366     )->to_api( { embed => $embed } );
367
368     foreach my $biblio_api ( @{ $biblios_api } ) {
369         ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
370         is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item matches');
371         ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
372
373         $i++;
374     }
375
376     # One more level
377     $embed = {
378         'items' => {
379             children => { 'holds' => {} }
380         }
381     };
382
383     $i = 0;
384
385     $biblios_api = Koha::Biblios->search(
386         {
387             biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
388         }
389     )->to_api( { embed => $embed } );
390
391     foreach my $biblio_api ( @{ $biblios_api } ) {
392
393         ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
394         is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item still matches');
395         ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
396         is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $holds[$i]->reserve_id, 'Hold matches');
397
398         $i++;
399     }
400
401     subtest 'unprivileged request tests' => sub {
402
403         my @all_attrs = Koha::Libraries->columns();
404         my $public_attrs = { map { $_ => 1 } @{ Koha::Library->public_read_list() } };
405         my $mapping = Koha::Library->to_api_mapping;
406
407         # Create sample libraries
408         my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
409         my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
410         my $libraries = Koha::Libraries->search(
411             {
412                 branchcode => {
413                     '-in' => [
414                         $library_1->branchcode, $library_2->branchcode
415                     ]
416                 }
417             }
418         );
419
420         plan tests => scalar @all_attrs * 2 * $libraries->count;
421
422         my $libraries_unprivileged_representation = $libraries->to_api({ public => 1 });
423         my $libraries_privileged_representation   = $libraries->to_api();
424
425         for (my $i = 0; $i < $libraries->count; $i++) {
426             my $privileged_representation   = $libraries_privileged_representation->[$i];
427             my $unprivileged_representation = $libraries_unprivileged_representation->[$i];
428             foreach my $attr (@all_attrs) {
429                 my $mapped = exists $mapping->{$attr} ? $mapping->{$attr} : $attr;
430                 if ( defined($mapped) ) {
431                     ok(
432                         exists $privileged_representation->{$mapped},
433                         "Attribute '$attr' is present when privileged"
434                     );
435                     if ( exists $public_attrs->{$attr} ) {
436                         ok(
437                             exists $unprivileged_representation->{$mapped},
438                             "Attribute '$attr' is present when public"
439                         );
440                     }
441                     else {
442                         ok(
443                             !exists $unprivileged_representation->{$mapped},
444                             "Attribute '$attr' is not present when public"
445                         );
446                     }
447                 }
448                 else {
449                     ok(
450                         !exists $privileged_representation->{$attr},
451                         "Unmapped attribute '$attr' is not present when privileged"
452                     );
453                     ok(
454                         !exists $unprivileged_representation->{$attr},
455                         "Unmapped attribute '$attr' is not present when public"
456                     );
457                 }
458             }
459         }
460     };
461
462
463     $schema->storage->txn_rollback;
464 };
465
466 subtest "TO_JSON() tests" => sub {
467
468     plan tests => 4;
469
470     $schema->storage->txn_begin;
471
472     my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
473     my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
474
475     my $cities = Koha::Cities->search(
476         {
477             cityid => [ $city_1->cityid, $city_2->cityid ]
478         },
479         { -orderby => { -desc => 'cityid' } }
480     );
481
482     is( $cities->count, 2, 'Count is correct' );
483     my $cities_json = $cities->TO_JSON;
484     is( ref($cities_json), 'ARRAY', 'to_api returns an array' );
485     is_deeply( $cities_json->[0], $city_1->TO_JSON, 'TO_JSON returns the individual objects with ->TO_JSON' );
486     is_deeply( $cities_json->[1], $city_2->TO_JSON,'TO_JSON returns the individual objects with ->TO_JSON' );
487
488     $schema->storage->txn_rollback;
489 };
490
491 # Koha::Object[s] must behave the same as DBIx::Class
492 subtest 'Return same values as DBIx::Class' => sub {
493     plan tests => 2;
494
495     subtest 'Delete' => sub {
496         plan tests => 2;
497
498         $schema->storage->txn_begin;
499
500         subtest 'Simple Koha::Objects - Koha::Cities' => sub {
501             plan tests => 2;
502
503             subtest 'Koha::Object->delete' => sub {
504
505                 plan tests => 5;
506
507                 my ( $r_us, $e_us, $r_them, $e_them );
508
509                 # CASE 1 - Delete an existing object
510                 my $c = Koha::City->new( { city_name => 'city4test' } )->store;
511                 try { $r_us = $c->delete; } catch { $e_us = $_ };
512                 $c = $schema->resultset('City')->new( { city_name => 'city4test_2' } )->update_or_insert;
513                 try { $r_them = $c->delete; } catch { $e_them = $_ };
514                 ok( ref($r_us) && ref($r_them),
515                     'Successful delete should return the object ' );
516                 ok( !defined $e_us && !defined $e_them,
517                     'Successful delete should not raise an exception' );
518                 is( ref($r_us), 'Koha::City', 'Successful delete should return our Koha::Obect based object' );
519
520                 # CASE 2 - Delete an object that is not in storage
521                 try { $r_us   = $r_us->delete;   } catch { $e_us   = $_ };
522                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
523                 ok(
524                     defined $e_us && defined $e_them,
525                     'Delete an object that is not in storage should raise an exception'
526                 );
527                 is( ref($e_us), 'DBIx::Class::Exception' )
528                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
529
530             };
531
532             subtest 'Koha::Objects->delete' => sub {
533
534                 plan tests => 4;
535
536                 my ( $r_us, $e_us, $r_them, $e_them );
537
538                 # CASE 1 - Delete existing objects
539                 my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
540                 my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
541                 my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
542                 my $cities = Koha::Cities->search(
543                     {
544                         cityid => {
545                             -in => [
546                                 $city_1->cityid,
547                                 $city_2->cityid,
548                                 $city_3->cityid,
549                             ]
550                         }
551                     }
552                 );
553
554                 try { $r_us = $cities->delete; } catch { $e_us = $_ };
555
556                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
557                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
558                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
559                 $cities = $schema->resultset('City')->search(
560                     {
561                         cityid => {
562                             -in => [
563                                 $city_1->cityid,
564                                 $city_2->cityid,
565                                 $city_3->cityid,
566                             ]
567                         }
568                     }
569                 );
570
571                 try { $r_them = $cities->delete; } catch { $e_them = $_ };
572
573                 ok( $r_us == 3 && $r_them == 3 );
574                 ok (!defined($e_us) && !defined($e_them));
575
576                 # CASE 2 - One of the object is not in storage
577                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
578                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
579                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
580                 $cities = Koha::Cities->search(
581                     {
582                         cityid => {
583                             -in => [
584                                 $city_1->cityid,
585                                 $city_2->cityid,
586                                 $city_3->cityid,
587                             ]
588                         }
589                     }
590                 );
591
592                 $city_2->delete; # We delete one of the object
593                 try { $r_us = $cities->delete; } catch { $e_us = $_ };
594
595                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
596                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
597                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
598                 $cities = $schema->resultset('City')->search(
599                     {
600                         cityid => {
601                             -in => [
602                                 $city_1->cityid,
603                                 $city_2->cityid,
604                                 $city_3->cityid,
605                             ]
606                         }
607                     }
608                 );
609
610                 $city_2->delete; # We delete one of the object
611                 try { $r_them = $cities->delete; } catch { $e_them = $_ };
612
613                 ok( $r_us == 2 && $r_them == 2 );
614                 ok (!defined($e_us) && !defined($e_them));
615             };
616         };
617
618         subtest 'Overwritten Koha::Objects->delete - Koha::Patrons' => sub {
619
620             plan tests => 2;
621
622             subtest 'Koha::Object->delete' => sub {
623
624                 plan tests => 7;
625
626                 my ( $r_us, $e_us, $r_them, $e_them );
627
628                 # CASE 1 - Delete an existing patron
629                 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
630                 my $patron_data = $patron->unblessed;
631                 $patron->delete;
632
633                 $patron = Koha::Patron->new( $patron_data )->store;
634                 try {$r_us = $patron->delete;} catch { $e_us = $_ };
635                 $patron = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
636                 try {$r_them = $patron->delete;} catch { $e_them = $_ };
637                 ok( ref($r_us) && ref($r_them),
638                     'Successful delete should return the patron object' );
639                 ok( !defined $e_us && !defined $e_them,
640                     'Successful delete should not raise an exception' );
641                 is( ref($r_us), 'Koha::Patron',
642                     'Successful delete should return our Koha::Obect based object' );
643
644                 # CASE 2 - Delete a patron that is not in storage
645                 try { $r_us   = $r_us->delete;   } catch { $e_us   = $_ };
646                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
647                 ok(
648                     defined $e_us && defined $e_them,
649                     'Delete a patron that is not in storage should raise an exception'
650                 );
651                 is( ref($e_us), 'DBIx::Class::Exception' )
652                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
653
654                 # CASE 3 - Delete a patron that cannot be deleted (as a checkout)
655                 $patron = Koha::Patron->new($patron_data)->store;
656                 $builder->build_object(
657                     {
658                         class => 'Koha::Checkouts',
659                         value => { borrowernumber => $patron->borrowernumber }
660                     }
661                 );
662                 try { $r_us = $r_us->delete; } catch { $e_us = $_ };
663                 $patron = $schema->resultset('Borrower')->find( $patron->borrowernumber );
664                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
665                 ok(
666                     defined $e_us && defined $e_them,
667                     'Delete a patron that cannot be deleted should raise an exception'
668                 );
669                 is( ref($e_us), 'DBIx::Class::Exception' )
670                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
671             };
672
673             subtest 'Koha::Objects->delete' => sub {
674
675                 plan tests => 7;
676
677                 my ( $r_us, $e_us, $r_them, $e_them );
678
679                 # CASE 1 - Delete existing objects
680                 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
681                 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
682                 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
683                 my $patrons = Koha::Patrons->search(
684                     {
685                         borrowernumber => {
686                             -in => [
687                                 $patron_1->borrowernumber,
688                                 $patron_2->borrowernumber,
689                                 $patron_3->borrowernumber
690                             ]
691                         }
692                     }
693                 );
694
695                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
696
697                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
698                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
699                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
700                 $patrons = $schema->resultset('Borrower')->search(
701                     {
702                         borrowernumber => {
703                             -in => [
704                                 $patron_1->borrowernumber,
705                                 $patron_2->borrowernumber,
706                                 $patron_3->borrowernumber
707                             ]
708                         }
709                     }
710                 );
711
712                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
713
714                 ok( $r_us == 3 && $r_them == 3, '->delete should return the number of deleted patrons' );
715                 ok (!defined($e_us) && !defined($e_them), '->delete should not raise exception if everything went well');
716
717                 # CASE 2 - One of the patrons is not in storage
718                 undef $_ for $r_us, $e_us, $r_them, $e_them;
719                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
720                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
721                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
722                 $patrons = Koha::Patrons->search(
723                     {
724                         borrowernumber => {
725                             -in => [
726                                 $patron_1->borrowernumber,
727                                 $patron_2->borrowernumber,
728                                 $patron_3->borrowernumber
729                             ]
730                         }
731                     }
732                 );
733
734                 $patron_2->delete; # We delete one of the patron
735                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
736
737                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
738                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
739                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
740                 $patrons = $schema->resultset('Borrower')->search(
741                     {
742                         borrowernumber => {
743                             -in => [
744                                 $patron_1->borrowernumber,
745                                 $patron_2->borrowernumber,
746                                 $patron_3->borrowernumber
747                             ]
748                         }
749                     }
750                 );
751
752                 $patron_2->delete; # We delete one of the patron
753                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
754
755                 ok( $r_us == 2 && $r_them == 2, 'Delete patrons with one that was not in storage should delete the patrons' );
756                 ok (!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
757
758                 # CASE 3 - Delete a set of patrons with one that that cannot be deleted (as a checkout)
759                 undef $_ for $r_us, $e_us, $r_them, $e_them;
760                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
761                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
762                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
763                 $patrons = Koha::Patrons->search(
764                     {
765                         borrowernumber => {
766                             -in => [
767                                 $patron_1->borrowernumber,
768                                 $patron_2->borrowernumber,
769                                 $patron_3->borrowernumber
770                             ]
771                         }
772                     }
773                 );
774
775                 # Adding a checkout to patron_2
776                 $builder->build_object(
777                     {
778                         class => 'Koha::Checkouts',
779                         value => { borrowernumber => $patron_2->borrowernumber }
780                     }
781                 );
782
783                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
784                 my $not_deleted_us = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
785
786                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
787                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
788                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
789                 $patrons = $schema->resultset('Borrower')->search(
790                     {
791                         borrowernumber => {
792                             -in => [
793                                 $patron_1->borrowernumber,
794                                 $patron_2->borrowernumber,
795                                 $patron_3->borrowernumber
796                             ]
797                         }
798                     }
799                 );
800
801                 # Adding a checkout to patron_2
802                 $builder->build_object(
803                     {
804                         class => 'Koha::Checkouts',
805                         value => { borrowernumber => $patron_2->borrowernumber }
806                     }
807                 );
808
809                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
810
811                 my $not_deleted_them = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
812                 ok(
813                     defined $e_us && defined $e_them,
814                     'Delete patrons with one that cannot be deleted should raise an exception'
815                 );
816                 is( ref($e_us), 'DBIx::Class::Exception' )
817                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
818
819                 ok($not_deleted_us == 3 && $not_deleted_them == 3, 'If one patron cannot be deleted, none should have been deleted');
820             };
821         };
822
823         $schema->storage->txn_rollback;
824
825     };
826
827     subtest 'Update (set/store)' => sub {
828         plan tests => 2;
829
830         $schema->storage->txn_begin;
831
832         subtest 'Simple Koha::Objects - Koha::Cities' => sub {
833             plan tests => 2;
834
835             subtest 'Koha::Object->update' => sub {
836
837                 plan tests => 5;
838
839                 my ( $r_us, $e_us, $r_them, $e_them );
840
841                 # CASE 1 - Update an existing object
842                 my $c_us = Koha::City->new( { city_name => 'city4test' } )->store;
843                 try { $r_us = $c_us->update({ city_country => 'country4test' }); } catch { $e_us = $_ };
844                 my $c_them = $schema->resultset('City')->new( { city_name => 'city4test_2' } )->update_or_insert;
845                 try { $r_them = $c_them->update({ city_country => 'country4test_2' }); } catch { $e_them = $_ };
846                 ok( ref($r_us) && ref($r_them),
847                     'Successful update should return the object ' );
848                 ok( !defined $e_us && !defined $e_them,
849                     'Successful update should not raise an exception' );
850                 is( ref($r_us), 'Koha::City', 'Successful update should return our Koha::Obect based object' );
851
852                 # CASE 2 - Update an object that is not in storage
853                 $c_us->delete;
854                 $c_them->delete;
855                 try { $r_us   = $c_us->update({ city_country => 'another_country' });   } catch { $e_us   = $_ };
856                 try { $r_them = $c_them->update({ city_country => 'another_country' }); } catch { $e_them = $_ };
857                 ok(
858                     defined $e_us && defined $e_them,
859                     'Update an object that is not in storage should raise an exception'
860                 );
861                 is( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' );
862             };
863
864             subtest 'Koha::Objects->update' => sub {
865
866                 plan tests => 6;
867
868                 my ( $r_us, $e_us, $r_them, $e_them );
869
870                 # CASE 1 - update existing objects
871                 my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
872                 my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
873                 my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
874                 my $cities = Koha::Cities->search(
875                     {
876                         cityid => {
877                             -in => [
878                                 $city_1->cityid,
879                                 $city_2->cityid,
880                                 $city_3->cityid,
881                             ]
882                         }
883                     }
884                 );
885
886                 try { $r_us = $cities->update({ city_country => 'country4test' }); } catch { $e_us = $_ };
887
888                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
889                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
890                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
891                 $cities = $schema->resultset('City')->search(
892                     {
893                         cityid => {
894                             -in => [
895                                 $city_1->cityid,
896                                 $city_2->cityid,
897                                 $city_3->cityid,
898                             ]
899                         }
900                     }
901                 );
902
903                 try { $r_them = $cities->update({ city_country => 'country4test' }); } catch { $e_them = $_ };
904
905                 ok( $r_us == 3 && $r_them == 3, '->update should return the number of updated cities' );
906                 ok(!defined($e_us) && !defined($e_them));
907
908                 # CASE 2 - One of the object is not in storage
909                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
910                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
911                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
912                 $cities = Koha::Cities->search(
913                     {
914                         cityid => {
915                             -in => [
916                                 $city_1->cityid,
917                                 $city_2->cityid,
918                                 $city_3->cityid,
919                             ]
920                         }
921                     }
922                 );
923
924                 $city_2->delete; # We delete one of the object
925                 try { $r_us = $cities->update({ city_country => 'country4test' }); } catch { $e_us = $_ };
926
927                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
928                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
929                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
930                 $cities = $schema->resultset('City')->search(
931                     {
932                         cityid => {
933                             -in => [
934                                 $city_1->cityid,
935                                 $city_2->cityid,
936                                 $city_3->cityid,
937                             ]
938                         }
939                     }
940                 );
941
942                 $city_2->delete; # We delete one of the object
943                 try { $r_them = $cities->update({ city_country => 'country4test' }); } catch { $e_them = $_ };
944
945                 ok( $r_us == 2 && $r_them == 2, '->update should return the number of updated cities' );
946                 ok(!defined($e_us) && !defined($e_them));
947
948                 throws_ok
949                     { Koha::Cities->update({ city_country => 'Castalia' }); }
950                     'Koha::Exceptions::Object::NotInstantiated',
951                     'Exception thrown if not instantiated class';
952
953                 is( "$@", 'Tried to access the \'update\' method, but Koha::Cities is not instantiated', 'Exception stringified correctly' );
954
955             };
956         };
957
958         subtest 'Overwritten Koha::Objects->store|update - Koha::Patrons' => sub {
959
960             plan tests => 2;
961
962             subtest 'Koha::Object->update' => sub {
963
964                 plan tests => 5;
965
966                 my ( $r_us, $e_us, $r_them, $e_them );
967
968                 # CASE 1 - Update an existing patron
969                 my $patron_us = $builder->build_object({ class => 'Koha::Patrons' });
970                 try {$r_us = $patron_us->update({city => 'a_city'});} catch { $e_us = $_ };
971
972                 my $patron_data = $builder->build_object({ class => 'Koha::Patrons' })->delete->unblessed;
973                 my $patron_them = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
974                 try {$r_them = $patron_them->update({city => 'a_city'});} catch { $e_them = $_ };
975                 ok( ref($r_us) && ref($r_them),
976                     'Successful update should return the patron object' );
977                 ok( !defined $e_us && !defined $e_them,
978                     'Successful update should not raise an exception' );
979                 is( ref($r_us), 'Koha::Patron',
980                     'Successful update should return our Koha::Obect based object' );
981
982                 # CASE 2 - Update a patron that is not in storage
983                 $patron_us->delete;
984                 $patron_them->delete;
985                 try { $r_us   = $patron_us->update({ city => 'another_city' });   } catch { $e_us   = $_ };
986                 try { $r_them = $patron_them->update({ city => 'another_city' }); } catch { $e_them = $_ };
987                 ok(
988                     defined $e_us && defined $e_them,
989                     'Update a patron that is not in storage should raise an exception'
990                 );
991                 is( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' );
992
993             };
994
995             subtest 'Koha::Objects->Update ' => sub {
996
997                 plan tests => 6;
998
999                 my ( $r_us, $e_us, $r_them, $e_them );
1000
1001                 # CASE 1 - Update existing objects
1002                 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
1003                 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
1004                 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
1005                 my $patrons_us = Koha::Patrons->search(
1006                     {
1007                         borrowernumber => {
1008                             -in => [
1009                                 $patron_1->borrowernumber,
1010                                 $patron_2->borrowernumber,
1011                                 $patron_3->borrowernumber
1012                             ]
1013                         }
1014                     }
1015                 );
1016
1017                 try { $r_us = $patrons_us->update({ city => 'a_city' }); } catch { $e_us = $_ };
1018
1019                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
1020                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
1021                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
1022                 my $patrons_them = $schema->resultset('Borrower')->search(
1023                     {
1024                         borrowernumber => {
1025                             -in => [
1026                                 $patron_1->borrowernumber,
1027                                 $patron_2->borrowernumber,
1028                                 $patron_3->borrowernumber
1029                             ]
1030                         }
1031                     }
1032                 );
1033
1034                 try { $r_them = $patrons_them->update({ city => 'a_city' }); } catch { $e_them = $_ };
1035
1036                 ok( $r_us == 3 && $r_them == 3, '->update should return the number of update patrons' );
1037                 ok (!defined($e_us) && !defined($e_them), '->update should not raise exception if everything went well');
1038
1039                 # CASE 2 - One of the patrons is not in storage
1040                 undef $_ for $r_us, $e_us, $r_them, $e_them;
1041                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
1042                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
1043                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
1044                 $patrons_us = Koha::Patrons->search(
1045                     {
1046                         borrowernumber => {
1047                             -in => [
1048                                 $patron_1->borrowernumber,
1049                                 $patron_2->borrowernumber,
1050                                 $patron_3->borrowernumber
1051                             ]
1052                         }
1053                     }
1054                 );
1055
1056                 $patron_2->delete; # We delete one of the patron
1057                 try { $r_us = $patrons_us->update({ city => 'another_city' }); } catch { $e_us = $_ };
1058
1059                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
1060                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
1061                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
1062                 $patrons_them = $schema->resultset('Borrower')->search(
1063                     {
1064                         borrowernumber => {
1065                             -in => [
1066                                 $patron_1->borrowernumber,
1067                                 $patron_2->borrowernumber,
1068                                 $patron_3->borrowernumber
1069                             ]
1070                         }
1071                     }
1072                 );
1073
1074                 $patron_2->delete; # We delete one of the patron
1075                 try { $r_them = $patrons_them->update({ city => 'another_city' }); } catch { $e_them = $_ };
1076
1077                 ok( $r_us == 2 && $r_them == 2, 'Update patrons with one that was not in storage should update the patrons' );
1078                 ok (!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
1079
1080
1081                 # Testing no_triggers
1082                 t::lib::Mocks::mock_preference('uppercasesurnames', 1);
1083                 $patrons_us = Koha::Patrons->search(
1084                     {
1085                         borrowernumber => {
1086                             -in => [
1087                                 $patron_1->borrowernumber,
1088                                 $patron_2->borrowernumber,
1089                                 $patron_3->borrowernumber
1090                             ]
1091                         }
1092                     }
1093                 );
1094                 $patrons_us->update({ surname => 'foo' }); # Koha::Patron->store is supposed to uppercase the surnames
1095                 is( $patrons_us->search({ surname => 'FOO' })->count, 2, 'Koha::Patron->store is hit' );
1096
1097                 $patrons_us->update({ surname => 'foo' }, { no_triggers => 1 }); # The surnames won't be uppercase as we won't hit Koha::Patron->store
1098                 is( $patrons_us->search({ surname => 'foo' })->count, 2, 'Koha::Patron->store is not hit');
1099
1100             };
1101
1102         };
1103
1104         $schema->storage->txn_rollback;
1105
1106     };
1107
1108 };
1109
1110 subtest "attributes_from_api() tests" => sub {
1111
1112     plan tests => 1;
1113
1114     $schema->storage->txn_begin;
1115
1116     my $cities_rs = Koha::Cities->new;
1117     my $city      = Koha::City->new;
1118
1119     my $api_attributes = {
1120         name        => 'Cordoba',
1121         postal_code => 5000
1122     };
1123
1124     is_deeply(
1125         $cities_rs->attributes_from_api($api_attributes),
1126         $city->attributes_from_api($api_attributes)
1127     );
1128
1129     $schema->storage->txn_rollback;
1130
1131 };
1132
1133 subtest "filter_by_last_update" => sub {
1134
1135     $schema->storage->txn_begin;
1136
1137     my $now = dt_from_string->truncate( to => 'day' );
1138     my @borrowernumbers;
1139     # Building 6 patrons that have been created today, yesterday, ... 1 per day
1140     for my $i ( 0 .. 5 ) {
1141         push @borrowernumbers,
1142           $builder->build_object(
1143             {
1144                 class => 'Koha::Patrons',
1145                 value => { updated_on => $now->clone->subtract( days => $i ) }
1146             }
1147           )->borrowernumber;
1148     }
1149
1150     my $patrons = Koha::Patrons->search(
1151         { borrowernumber => { -in => \@borrowernumbers } } );
1152
1153     try {
1154         $patrons->filter_by_last_update( { timestamp_column_name => 'updated_on' } )
1155           ->count;
1156     }
1157     catch {
1158         ok(
1159             $_->isa('Koha::Exceptions::MissingParameter'),
1160             'Should raise an exception if no parameter given'
1161         );
1162     };
1163
1164     my $filtered_patrons = $patrons->filter_by_last_update(
1165         { timestamp_column_name => 'updated_on', days => 2 } );
1166     is( ref($filtered_patrons), 'Koha::Patrons', 'filter_by_last_update must return a Koha::Objects-based object' );
1167
1168     my $count = $patrons->filter_by_last_update(
1169         { timestamp_column_name => 'updated_on', days => 2 } )->count;
1170     is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' );
1171
1172     $count = $patrons->filter_by_last_update(
1173         { timestamp_column_name => 'updated_on', days => 2, days_inclusive => 1 } )->count;
1174     is( $count, 4, '4 patrons have been updated before the last 2 days (inclusive)' );
1175
1176     $count = $patrons->filter_by_last_update(
1177         { timestamp_column_name => 'updated_on', days => 1 } )->count;
1178     is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' );
1179
1180     $count = $patrons->filter_by_last_update(
1181         { timestamp_column_name => 'updated_on', days => 1, days_inclusive => 1 } )->count;
1182     is( $count, 5, '5 patrons have been updated before yesterday (inclusive)' );
1183
1184     $count = $patrons->filter_by_last_update(
1185         { timestamp_column_name => 'updated_on', days => 0 } )->count;
1186     is( $count, 5, '5 patrons have been updated before today (exclusive)' );
1187
1188     $count = $patrons->filter_by_last_update(
1189         { timestamp_column_name => 'updated_on', days => 0, days_inclusive => 1 } )->count;
1190     is( $count, 6, '6 patrons have been updated before today (inclusive)' );
1191
1192     $count = $patrons->filter_by_last_update(
1193         { timestamp_column_name => 'updated_on', from => $now } )->count;
1194     is( $count, 1, '1 patron has been updated "from today" (inclusive)' );
1195
1196     $count = $patrons->filter_by_last_update(
1197         { timestamp_column_name => 'updated_on', to => $now } )->count;
1198     is( $count, 6, '6 patrons have been updated "to today" (inclusive)' );
1199
1200     $count = $patrons->filter_by_last_update(
1201         {
1202             timestamp_column_name => 'updated_on',
1203             from                  => $now->clone->subtract( days => 4 ),
1204             to                    => $now->clone->subtract( days => 2 )
1205         }
1206     )->count;
1207     is( $count, 3, '3 patrons have been updated between D-4 and D-2' );
1208
1209     t::lib::Mocks::mock_preference( 'dateformat', 'metric' );
1210     try {
1211         $count = $patrons->filter_by_last_update(
1212             { timestamp_column_name => 'updated_on', from => '1970-12-31' } )
1213           ->count;
1214     }
1215     catch {
1216         ok(
1217             $_->isa(
1218                 'No exception raised, from and to parameters can take an iso formatted date'
1219             )
1220         );
1221     };
1222     try {
1223         $count = $patrons->filter_by_last_update(
1224             { timestamp_column_name => 'updated_on', from => '31/12/1970' } )
1225           ->count;
1226     }
1227     catch {
1228         ok(
1229             $_->isa(
1230                 'No exception raised, from and to parameters can take an metric formatted date (depending on dateformat syspref)'
1231             )
1232         );
1233     };
1234
1235     $schema->storage->txn_rollback;
1236 };
1237
1238 subtest "from_api_mapping() tests" => sub {
1239
1240     plan tests => 1;
1241
1242     $schema->storage->txn_begin;
1243
1244     my $cities_rs = Koha::Cities->new;
1245     my $city      = Koha::City->new;
1246
1247     is_deeply(
1248         $cities_rs->from_api_mapping,
1249         $city->from_api_mapping
1250     );
1251
1252     $schema->storage->txn_rollback;
1253 };
1254
1255 subtest 'prefetch_whitelist() tests' => sub {
1256
1257     plan tests => 3;
1258
1259     $schema->storage->txn_begin;
1260
1261     my $biblios = Koha::Biblios->new;
1262
1263     my $prefetch_whitelist = $biblios->prefetch_whitelist;
1264
1265     ok(
1266         exists $prefetch_whitelist->{orders},
1267         'Relationship matching method name is listed'
1268     );
1269     is(
1270         $prefetch_whitelist->{orders},
1271         'Koha::Acquisition::Order',
1272         'Guessed the non-standard object class correctly'
1273     );
1274
1275     is(
1276         $prefetch_whitelist->{items},
1277         'Koha::Item',
1278         'Guessed the standard object class correctly'
1279     );
1280
1281     $schema->storage->txn_rollback;
1282 };
1283
1284 subtest 'empty() tests' => sub {
1285
1286     plan tests => 6;
1287
1288     $schema->storage->txn_begin;
1289
1290     # Add a patron, we need at least 1
1291     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1292     ok( Koha::Patrons->count > 0, 'There is at least one Koha::Patron on the resultset' );
1293
1294     my $empty = Koha::Patrons->new->empty;
1295     is( ref($empty), 'Koha::Patrons', '->empty returns a Koha::Patrons iterator' );
1296     is( $empty->count, 0, 'The empty resultset is, well, empty :-D' );
1297
1298     my $new_rs = $empty->search({ borrowernumber => $patron->borrowernumber });
1299
1300     is( $new_rs->count, 0, 'Further chaining an empty resultset, returns an empty resultset' );
1301
1302     throws_ok
1303         { Koha::Patrons->empty; }
1304         'Koha::Exceptions::Object::NotInstantiated',
1305         'Exception thrown if not instantiated class';
1306
1307     is( "$@", 'Tried to access the \'empty\' method, but Koha::Patrons is not instantiated', 'Exception stringified correctly' );
1308
1309     $schema->storage->txn_rollback;
1310 };
1311
1312 subtest 'delete() tests' => sub {
1313
1314     plan tests => 2;
1315
1316     $schema->storage->txn_begin;
1317
1318     # Make sure no cities
1319     warnings_are { Koha::Cities->delete }[],
1320       "No warnings, no Koha::City->delete called as it doesn't exist";
1321
1322     # Mock Koha::City
1323     my $mocked_city = Test::MockModule->new('Koha::City');
1324     $mocked_city->mock(
1325         'delete',
1326         sub {
1327             shift->_result->delete;
1328             warn "delete called!";
1329         }
1330     );
1331
1332     # Add two cities
1333     $builder->build_object( { class => 'Koha::Cities' } );
1334     $builder->build_object( { class => 'Koha::Cities' } );
1335
1336     my $cities = Koha::Cities->search;
1337     $cities->next;
1338     warnings_are { $cities->delete }
1339         [ "delete called!", "delete called!" ],
1340         "No warnings, no Koha::City->delete called as it doesn't exist";
1341
1342     $schema->storage->txn_rollback;
1343 };