Bug 28948: Add a generic way to handle API privileged access attributes deny-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 @privileged_attrs = @{ Koha::Library->api_privileged_attrs };
404
405         # Create sample libraries
406         my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
407         my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
408         my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
409         my $libraries = Koha::Libraries->search(
410             {
411                 branchcode => {
412                     '-in' => [
413                         $library_1->branchcode, $library_2->branchcode,
414                         $library_3->branchcode
415                     ]
416                 }
417             }
418         );
419
420         plan tests => scalar @privileged_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 $privileged_attr ( @privileged_attrs ) {
429                 ok( exists $privileged_representation->{$privileged_attr},
430                     "Attribute $privileged_attr' is present" );
431                 ok( !exists $unprivileged_representation->{$privileged_attr},
432                     "Attribute '$privileged_attr' is not present" );
433             }
434         }
435     };
436
437
438     $schema->storage->txn_rollback;
439 };
440
441 subtest "TO_JSON() tests" => sub {
442
443     plan tests => 4;
444
445     $schema->storage->txn_begin;
446
447     my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
448     my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
449
450     my $cities = Koha::Cities->search(
451         {
452             cityid => [ $city_1->cityid, $city_2->cityid ]
453         },
454         { -orderby => { -desc => 'cityid' } }
455     );
456
457     is( $cities->count, 2, 'Count is correct' );
458     my $cities_json = $cities->TO_JSON;
459     is( ref($cities_json), 'ARRAY', 'to_api returns an array' );
460     is_deeply( $cities_json->[0], $city_1->TO_JSON, 'TO_JSON returns the individual objects with ->TO_JSON' );
461     is_deeply( $cities_json->[1], $city_2->TO_JSON,'TO_JSON returns the individual objects with ->TO_JSON' );
462
463     $schema->storage->txn_rollback;
464 };
465
466 # Koha::Object[s] must behave the same as DBIx::Class
467 subtest 'Return same values as DBIx::Class' => sub {
468     plan tests => 2;
469
470     subtest 'Delete' => sub {
471         plan tests => 2;
472
473         $schema->storage->txn_begin;
474
475         subtest 'Simple Koha::Objects - Koha::Cities' => sub {
476             plan tests => 2;
477
478             subtest 'Koha::Object->delete' => sub {
479
480                 plan tests => 5;
481
482                 my ( $r_us, $e_us, $r_them, $e_them );
483
484                 # CASE 1 - Delete an existing object
485                 my $c = Koha::City->new( { city_name => 'city4test' } )->store;
486                 try { $r_us = $c->delete; } catch { $e_us = $_ };
487                 $c = $schema->resultset('City')->new( { city_name => 'city4test_2' } )->update_or_insert;
488                 try { $r_them = $c->delete; } catch { $e_them = $_ };
489                 ok( ref($r_us) && ref($r_them),
490                     'Successful delete should return the object ' );
491                 ok( !defined $e_us && !defined $e_them,
492                     'Successful delete should not raise an exception' );
493                 is( ref($r_us), 'Koha::City', 'Successful delete should return our Koha::Obect based object' );
494
495                 # CASE 2 - Delete an object that is not in storage
496                 try { $r_us   = $r_us->delete;   } catch { $e_us   = $_ };
497                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
498                 ok(
499                     defined $e_us && defined $e_them,
500                     'Delete an object that is not in storage should raise an exception'
501                 );
502                 is( ref($e_us), 'DBIx::Class::Exception' )
503                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
504
505             };
506
507             subtest 'Koha::Objects->delete' => sub {
508
509                 plan tests => 4;
510
511                 my ( $r_us, $e_us, $r_them, $e_them );
512
513                 # CASE 1 - Delete existing objects
514                 my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
515                 my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
516                 my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
517                 my $cities = Koha::Cities->search(
518                     {
519                         cityid => {
520                             -in => [
521                                 $city_1->cityid,
522                                 $city_2->cityid,
523                                 $city_3->cityid,
524                             ]
525                         }
526                     }
527                 );
528
529                 try { $r_us = $cities->delete; } catch { $e_us = $_ };
530
531                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
532                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
533                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
534                 $cities = $schema->resultset('City')->search(
535                     {
536                         cityid => {
537                             -in => [
538                                 $city_1->cityid,
539                                 $city_2->cityid,
540                                 $city_3->cityid,
541                             ]
542                         }
543                     }
544                 );
545
546                 try { $r_them = $cities->delete; } catch { $e_them = $_ };
547
548                 ok( $r_us == 3 && $r_them == 3 );
549                 ok (!defined($e_us) && !defined($e_them));
550
551                 # CASE 2 - One of the object is not in storage
552                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
553                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
554                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
555                 $cities = Koha::Cities->search(
556                     {
557                         cityid => {
558                             -in => [
559                                 $city_1->cityid,
560                                 $city_2->cityid,
561                                 $city_3->cityid,
562                             ]
563                         }
564                     }
565                 );
566
567                 $city_2->delete; # We delete one of the object
568                 try { $r_us = $cities->delete; } catch { $e_us = $_ };
569
570                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
571                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
572                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
573                 $cities = $schema->resultset('City')->search(
574                     {
575                         cityid => {
576                             -in => [
577                                 $city_1->cityid,
578                                 $city_2->cityid,
579                                 $city_3->cityid,
580                             ]
581                         }
582                     }
583                 );
584
585                 $city_2->delete; # We delete one of the object
586                 try { $r_them = $cities->delete; } catch { $e_them = $_ };
587
588                 ok( $r_us == 2 && $r_them == 2 );
589                 ok (!defined($e_us) && !defined($e_them));
590             };
591         };
592
593         subtest 'Overwritten Koha::Objects->delete - Koha::Patrons' => sub {
594
595             plan tests => 2;
596
597             subtest 'Koha::Object->delete' => sub {
598
599                 plan tests => 7;
600
601                 my ( $r_us, $e_us, $r_them, $e_them );
602
603                 # CASE 1 - Delete an existing patron
604                 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
605                 my $patron_data = $patron->unblessed;
606                 $patron->delete;
607
608                 $patron = Koha::Patron->new( $patron_data )->store;
609                 try {$r_us = $patron->delete;} catch { $e_us = $_ };
610                 $patron = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
611                 try {$r_them = $patron->delete;} catch { $e_them = $_ };
612                 ok( ref($r_us) && ref($r_them),
613                     'Successful delete should return the patron object' );
614                 ok( !defined $e_us && !defined $e_them,
615                     'Successful delete should not raise an exception' );
616                 is( ref($r_us), 'Koha::Patron',
617                     'Successful delete should return our Koha::Obect based object' );
618
619                 # CASE 2 - Delete a patron that is not in storage
620                 try { $r_us   = $r_us->delete;   } catch { $e_us   = $_ };
621                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
622                 ok(
623                     defined $e_us && defined $e_them,
624                     'Delete a patron that is not in storage should raise an exception'
625                 );
626                 is( ref($e_us), 'DBIx::Class::Exception' )
627                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
628
629                 # CASE 3 - Delete a patron that cannot be deleted (as a checkout)
630                 $patron = Koha::Patron->new($patron_data)->store;
631                 $builder->build_object(
632                     {
633                         class => 'Koha::Checkouts',
634                         value => { borrowernumber => $patron->borrowernumber }
635                     }
636                 );
637                 try { $r_us = $r_us->delete; } catch { $e_us = $_ };
638                 $patron = $schema->resultset('Borrower')->find( $patron->borrowernumber );
639                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
640                 ok(
641                     defined $e_us && defined $e_them,
642                     'Delete a patron that cannot be deleted should raise an exception'
643                 );
644                 is( ref($e_us), 'DBIx::Class::Exception' )
645                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
646             };
647
648             subtest 'Koha::Objects->delete' => sub {
649
650                 plan tests => 7;
651
652                 my ( $r_us, $e_us, $r_them, $e_them );
653
654                 # CASE 1 - Delete existing objects
655                 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
656                 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
657                 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
658                 my $patrons = Koha::Patrons->search(
659                     {
660                         borrowernumber => {
661                             -in => [
662                                 $patron_1->borrowernumber,
663                                 $patron_2->borrowernumber,
664                                 $patron_3->borrowernumber
665                             ]
666                         }
667                     }
668                 );
669
670                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
671
672                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
673                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
674                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
675                 $patrons = $schema->resultset('Borrower')->search(
676                     {
677                         borrowernumber => {
678                             -in => [
679                                 $patron_1->borrowernumber,
680                                 $patron_2->borrowernumber,
681                                 $patron_3->borrowernumber
682                             ]
683                         }
684                     }
685                 );
686
687                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
688
689                 ok( $r_us == 3 && $r_them == 3, '->delete should return the number of deleted patrons' );
690                 ok (!defined($e_us) && !defined($e_them), '->delete should not raise exception if everything went well');
691
692                 # CASE 2 - One of the patrons is not in storage
693                 undef $_ for $r_us, $e_us, $r_them, $e_them;
694                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
695                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
696                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
697                 $patrons = Koha::Patrons->search(
698                     {
699                         borrowernumber => {
700                             -in => [
701                                 $patron_1->borrowernumber,
702                                 $patron_2->borrowernumber,
703                                 $patron_3->borrowernumber
704                             ]
705                         }
706                     }
707                 );
708
709                 $patron_2->delete; # We delete one of the patron
710                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
711
712                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
713                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
714                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
715                 $patrons = $schema->resultset('Borrower')->search(
716                     {
717                         borrowernumber => {
718                             -in => [
719                                 $patron_1->borrowernumber,
720                                 $patron_2->borrowernumber,
721                                 $patron_3->borrowernumber
722                             ]
723                         }
724                     }
725                 );
726
727                 $patron_2->delete; # We delete one of the patron
728                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
729
730                 ok( $r_us == 2 && $r_them == 2, 'Delete patrons with one that was not in storage should delete the patrons' );
731                 ok (!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
732
733                 # CASE 3 - Delete a set of patrons with one that that cannot be deleted (as a checkout)
734                 undef $_ for $r_us, $e_us, $r_them, $e_them;
735                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
736                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
737                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
738                 $patrons = Koha::Patrons->search(
739                     {
740                         borrowernumber => {
741                             -in => [
742                                 $patron_1->borrowernumber,
743                                 $patron_2->borrowernumber,
744                                 $patron_3->borrowernumber
745                             ]
746                         }
747                     }
748                 );
749
750                 # Adding a checkout to patron_2
751                 $builder->build_object(
752                     {
753                         class => 'Koha::Checkouts',
754                         value => { borrowernumber => $patron_2->borrowernumber }
755                     }
756                 );
757
758                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
759                 my $not_deleted_us = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
760
761                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
762                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
763                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
764                 $patrons = $schema->resultset('Borrower')->search(
765                     {
766                         borrowernumber => {
767                             -in => [
768                                 $patron_1->borrowernumber,
769                                 $patron_2->borrowernumber,
770                                 $patron_3->borrowernumber
771                             ]
772                         }
773                     }
774                 );
775
776                 # Adding a checkout to patron_2
777                 $builder->build_object(
778                     {
779                         class => 'Koha::Checkouts',
780                         value => { borrowernumber => $patron_2->borrowernumber }
781                     }
782                 );
783
784                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
785
786                 my $not_deleted_them = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
787                 ok(
788                     defined $e_us && defined $e_them,
789                     'Delete patrons with one that cannot be deleted should raise an exception'
790                 );
791                 is( ref($e_us), 'DBIx::Class::Exception' )
792                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
793
794                 ok($not_deleted_us == 3 && $not_deleted_them == 3, 'If one patron cannot be deleted, none should have been deleted');
795             };
796         };
797
798         $schema->storage->txn_rollback;
799
800     };
801
802     subtest 'Update (set/store)' => sub {
803         plan tests => 2;
804
805         $schema->storage->txn_begin;
806
807         subtest 'Simple Koha::Objects - Koha::Cities' => sub {
808             plan tests => 2;
809
810             subtest 'Koha::Object->update' => sub {
811
812                 plan tests => 5;
813
814                 my ( $r_us, $e_us, $r_them, $e_them );
815
816                 # CASE 1 - Update an existing object
817                 my $c_us = Koha::City->new( { city_name => 'city4test' } )->store;
818                 try { $r_us = $c_us->update({ city_country => 'country4test' }); } catch { $e_us = $_ };
819                 my $c_them = $schema->resultset('City')->new( { city_name => 'city4test_2' } )->update_or_insert;
820                 try { $r_them = $c_them->update({ city_country => 'country4test_2' }); } catch { $e_them = $_ };
821                 ok( ref($r_us) && ref($r_them),
822                     'Successful update should return the object ' );
823                 ok( !defined $e_us && !defined $e_them,
824                     'Successful update should not raise an exception' );
825                 is( ref($r_us), 'Koha::City', 'Successful update should return our Koha::Obect based object' );
826
827                 # CASE 2 - Update an object that is not in storage
828                 $c_us->delete;
829                 $c_them->delete;
830                 try { $r_us   = $c_us->update({ city_country => 'another_country' });   } catch { $e_us   = $_ };
831                 try { $r_them = $c_them->update({ city_country => 'another_country' }); } catch { $e_them = $_ };
832                 ok(
833                     defined $e_us && defined $e_them,
834                     'Update an object that is not in storage should raise an exception'
835                 );
836                 is( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' );
837             };
838
839             subtest 'Koha::Objects->update' => sub {
840
841                 plan tests => 6;
842
843                 my ( $r_us, $e_us, $r_them, $e_them );
844
845                 # CASE 1 - update existing objects
846                 my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
847                 my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
848                 my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
849                 my $cities = Koha::Cities->search(
850                     {
851                         cityid => {
852                             -in => [
853                                 $city_1->cityid,
854                                 $city_2->cityid,
855                                 $city_3->cityid,
856                             ]
857                         }
858                     }
859                 );
860
861                 try { $r_us = $cities->update({ city_country => 'country4test' }); } catch { $e_us = $_ };
862
863                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
864                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
865                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
866                 $cities = $schema->resultset('City')->search(
867                     {
868                         cityid => {
869                             -in => [
870                                 $city_1->cityid,
871                                 $city_2->cityid,
872                                 $city_3->cityid,
873                             ]
874                         }
875                     }
876                 );
877
878                 try { $r_them = $cities->update({ city_country => 'country4test' }); } catch { $e_them = $_ };
879
880                 ok( $r_us == 3 && $r_them == 3, '->update should return the number of updated cities' );
881                 ok(!defined($e_us) && !defined($e_them));
882
883                 # CASE 2 - One of the object is not in storage
884                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
885                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
886                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
887                 $cities = Koha::Cities->search(
888                     {
889                         cityid => {
890                             -in => [
891                                 $city_1->cityid,
892                                 $city_2->cityid,
893                                 $city_3->cityid,
894                             ]
895                         }
896                     }
897                 );
898
899                 $city_2->delete; # We delete one of the object
900                 try { $r_us = $cities->update({ city_country => 'country4test' }); } catch { $e_us = $_ };
901
902                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
903                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
904                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
905                 $cities = $schema->resultset('City')->search(
906                     {
907                         cityid => {
908                             -in => [
909                                 $city_1->cityid,
910                                 $city_2->cityid,
911                                 $city_3->cityid,
912                             ]
913                         }
914                     }
915                 );
916
917                 $city_2->delete; # We delete one of the object
918                 try { $r_them = $cities->update({ city_country => 'country4test' }); } catch { $e_them = $_ };
919
920                 ok( $r_us == 2 && $r_them == 2, '->update should return the number of updated cities' );
921                 ok(!defined($e_us) && !defined($e_them));
922
923                 throws_ok
924                     { Koha::Cities->update({ city_country => 'Castalia' }); }
925                     'Koha::Exceptions::Object::NotInstantiated',
926                     'Exception thrown if not instantiated class';
927
928                 is( "$@", 'Tried to access the \'update\' method, but Koha::Cities is not instantiated', 'Exception stringified correctly' );
929
930             };
931         };
932
933         subtest 'Overwritten Koha::Objects->store|update - Koha::Patrons' => sub {
934
935             plan tests => 2;
936
937             subtest 'Koha::Object->update' => sub {
938
939                 plan tests => 5;
940
941                 my ( $r_us, $e_us, $r_them, $e_them );
942
943                 # CASE 1 - Update an existing patron
944                 my $patron_us = $builder->build_object({ class => 'Koha::Patrons' });
945                 try {$r_us = $patron_us->update({city => 'a_city'});} catch { $e_us = $_ };
946
947                 my $patron_data = $builder->build_object({ class => 'Koha::Patrons' })->delete->unblessed;
948                 my $patron_them = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
949                 try {$r_them = $patron_them->update({city => 'a_city'});} catch { $e_them = $_ };
950                 ok( ref($r_us) && ref($r_them),
951                     'Successful update should return the patron object' );
952                 ok( !defined $e_us && !defined $e_them,
953                     'Successful update should not raise an exception' );
954                 is( ref($r_us), 'Koha::Patron',
955                     'Successful update should return our Koha::Obect based object' );
956
957                 # CASE 2 - Update a patron that is not in storage
958                 $patron_us->delete;
959                 $patron_them->delete;
960                 try { $r_us   = $patron_us->update({ city => 'another_city' });   } catch { $e_us   = $_ };
961                 try { $r_them = $patron_them->update({ city => 'another_city' }); } catch { $e_them = $_ };
962                 ok(
963                     defined $e_us && defined $e_them,
964                     'Update a patron that is not in storage should raise an exception'
965                 );
966                 is( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' );
967
968             };
969
970             subtest 'Koha::Objects->Update ' => sub {
971
972                 plan tests => 6;
973
974                 my ( $r_us, $e_us, $r_them, $e_them );
975
976                 # CASE 1 - Update existing objects
977                 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
978                 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
979                 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
980                 my $patrons_us = Koha::Patrons->search(
981                     {
982                         borrowernumber => {
983                             -in => [
984                                 $patron_1->borrowernumber,
985                                 $patron_2->borrowernumber,
986                                 $patron_3->borrowernumber
987                             ]
988                         }
989                     }
990                 );
991
992                 try { $r_us = $patrons_us->update({ city => 'a_city' }); } catch { $e_us = $_ };
993
994                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
995                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
996                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
997                 my $patrons_them = $schema->resultset('Borrower')->search(
998                     {
999                         borrowernumber => {
1000                             -in => [
1001                                 $patron_1->borrowernumber,
1002                                 $patron_2->borrowernumber,
1003                                 $patron_3->borrowernumber
1004                             ]
1005                         }
1006                     }
1007                 );
1008
1009                 try { $r_them = $patrons_them->update({ city => 'a_city' }); } catch { $e_them = $_ };
1010
1011                 ok( $r_us == 3 && $r_them == 3, '->update should return the number of update patrons' );
1012                 ok (!defined($e_us) && !defined($e_them), '->update should not raise exception if everything went well');
1013
1014                 # CASE 2 - One of the patrons is not in storage
1015                 undef $_ for $r_us, $e_us, $r_them, $e_them;
1016                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
1017                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
1018                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
1019                 $patrons_us = Koha::Patrons->search(
1020                     {
1021                         borrowernumber => {
1022                             -in => [
1023                                 $patron_1->borrowernumber,
1024                                 $patron_2->borrowernumber,
1025                                 $patron_3->borrowernumber
1026                             ]
1027                         }
1028                     }
1029                 );
1030
1031                 $patron_2->delete; # We delete one of the patron
1032                 try { $r_us = $patrons_us->update({ city => 'another_city' }); } catch { $e_us = $_ };
1033
1034                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
1035                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
1036                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
1037                 $patrons_them = $schema->resultset('Borrower')->search(
1038                     {
1039                         borrowernumber => {
1040                             -in => [
1041                                 $patron_1->borrowernumber,
1042                                 $patron_2->borrowernumber,
1043                                 $patron_3->borrowernumber
1044                             ]
1045                         }
1046                     }
1047                 );
1048
1049                 $patron_2->delete; # We delete one of the patron
1050                 try { $r_them = $patrons_them->update({ city => 'another_city' }); } catch { $e_them = $_ };
1051
1052                 ok( $r_us == 2 && $r_them == 2, 'Update patrons with one that was not in storage should update the patrons' );
1053                 ok (!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
1054
1055
1056                 # Testing no_triggers
1057                 t::lib::Mocks::mock_preference('uppercasesurnames', 1);
1058                 $patrons_us = Koha::Patrons->search(
1059                     {
1060                         borrowernumber => {
1061                             -in => [
1062                                 $patron_1->borrowernumber,
1063                                 $patron_2->borrowernumber,
1064                                 $patron_3->borrowernumber
1065                             ]
1066                         }
1067                     }
1068                 );
1069                 $patrons_us->update({ surname => 'foo' }); # Koha::Patron->store is supposed to uppercase the surnames
1070                 is( $patrons_us->search({ surname => 'FOO' })->count, 2, 'Koha::Patron->store is hit' );
1071
1072                 $patrons_us->update({ surname => 'foo' }, { no_triggers => 1 }); # The surnames won't be uppercase as we won't hit Koha::Patron->store
1073                 is( $patrons_us->search({ surname => 'foo' })->count, 2, 'Koha::Patron->store is not hit');
1074
1075             };
1076
1077         };
1078
1079         $schema->storage->txn_rollback;
1080
1081     };
1082
1083 };
1084
1085 subtest "attributes_from_api() tests" => sub {
1086
1087     plan tests => 1;
1088
1089     $schema->storage->txn_begin;
1090
1091     my $cities_rs = Koha::Cities->new;
1092     my $city      = Koha::City->new;
1093
1094     my $api_attributes = {
1095         name        => 'Cordoba',
1096         postal_code => 5000
1097     };
1098
1099     is_deeply(
1100         $cities_rs->attributes_from_api($api_attributes),
1101         $city->attributes_from_api($api_attributes)
1102     );
1103
1104     $schema->storage->txn_rollback;
1105
1106 };
1107
1108 subtest "filter_by_last_update" => sub {
1109
1110     $schema->storage->txn_begin;
1111
1112     my $now = dt_from_string->truncate( to => 'day' );
1113     my @borrowernumbers;
1114     # Building 6 patrons that have been created today, yesterday, ... 1 per day
1115     for my $i ( 0 .. 5 ) {
1116         push @borrowernumbers,
1117           $builder->build_object(
1118             {
1119                 class => 'Koha::Patrons',
1120                 value => { updated_on => $now->clone->subtract( days => $i ) }
1121             }
1122           )->borrowernumber;
1123     }
1124
1125     my $patrons = Koha::Patrons->search(
1126         { borrowernumber => { -in => \@borrowernumbers } } );
1127
1128     try {
1129         $patrons->filter_by_last_update( { timestamp_column_name => 'updated_on' } )
1130           ->count;
1131     }
1132     catch {
1133         ok(
1134             $_->isa('Koha::Exceptions::MissingParameter'),
1135             'Should raise an exception if no parameter given'
1136         );
1137     };
1138
1139     my $filtered_patrons = $patrons->filter_by_last_update(
1140         { timestamp_column_name => 'updated_on', days => 2 } );
1141     is( ref($filtered_patrons), 'Koha::Patrons', 'filter_by_last_update must return a Koha::Objects-based object' );
1142
1143     my $count = $patrons->filter_by_last_update(
1144         { timestamp_column_name => 'updated_on', days => 2 } )->count;
1145     is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' );
1146
1147     $count = $patrons->filter_by_last_update(
1148         { timestamp_column_name => 'updated_on', days => 2, days_inclusive => 1 } )->count;
1149     is( $count, 4, '4 patrons have been updated before the last 2 days (inclusive)' );
1150
1151     $count = $patrons->filter_by_last_update(
1152         { timestamp_column_name => 'updated_on', days => 1 } )->count;
1153     is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' );
1154
1155     $count = $patrons->filter_by_last_update(
1156         { timestamp_column_name => 'updated_on', days => 1, days_inclusive => 1 } )->count;
1157     is( $count, 5, '5 patrons have been updated before yesterday (inclusive)' );
1158
1159     $count = $patrons->filter_by_last_update(
1160         { timestamp_column_name => 'updated_on', days => 0 } )->count;
1161     is( $count, 5, '5 patrons have been updated before today (exclusive)' );
1162
1163     $count = $patrons->filter_by_last_update(
1164         { timestamp_column_name => 'updated_on', days => 0, days_inclusive => 1 } )->count;
1165     is( $count, 6, '6 patrons have been updated before today (inclusive)' );
1166
1167     $count = $patrons->filter_by_last_update(
1168         { timestamp_column_name => 'updated_on', from => $now } )->count;
1169     is( $count, 1, '1 patron has been updated "from today" (inclusive)' );
1170
1171     $count = $patrons->filter_by_last_update(
1172         { timestamp_column_name => 'updated_on', to => $now } )->count;
1173     is( $count, 6, '6 patrons have been updated "to today" (inclusive)' );
1174
1175     $count = $patrons->filter_by_last_update(
1176         {
1177             timestamp_column_name => 'updated_on',
1178             from                  => $now->clone->subtract( days => 4 ),
1179             to                    => $now->clone->subtract( days => 2 )
1180         }
1181     )->count;
1182     is( $count, 3, '3 patrons have been updated between D-4 and D-2' );
1183
1184     t::lib::Mocks::mock_preference( 'dateformat', 'metric' );
1185     try {
1186         $count = $patrons->filter_by_last_update(
1187             { timestamp_column_name => 'updated_on', from => '1970-12-31' } )
1188           ->count;
1189     }
1190     catch {
1191         ok(
1192             $_->isa(
1193                 'No exception raised, from and to parameters can take an iso formatted date'
1194             )
1195         );
1196     };
1197     try {
1198         $count = $patrons->filter_by_last_update(
1199             { timestamp_column_name => 'updated_on', from => '31/12/1970' } )
1200           ->count;
1201     }
1202     catch {
1203         ok(
1204             $_->isa(
1205                 'No exception raised, from and to parameters can take an metric formatted date (depending on dateformat syspref)'
1206             )
1207         );
1208     };
1209
1210     $schema->storage->txn_rollback;
1211 };
1212
1213 subtest "from_api_mapping() tests" => sub {
1214
1215     plan tests => 1;
1216
1217     $schema->storage->txn_begin;
1218
1219     my $cities_rs = Koha::Cities->new;
1220     my $city      = Koha::City->new;
1221
1222     is_deeply(
1223         $cities_rs->from_api_mapping,
1224         $city->from_api_mapping
1225     );
1226
1227     $schema->storage->txn_rollback;
1228 };
1229
1230 subtest 'prefetch_whitelist() tests' => sub {
1231
1232     plan tests => 3;
1233
1234     $schema->storage->txn_begin;
1235
1236     my $biblios = Koha::Biblios->new;
1237
1238     my $prefetch_whitelist = $biblios->prefetch_whitelist;
1239
1240     ok(
1241         exists $prefetch_whitelist->{orders},
1242         'Relationship matching method name is listed'
1243     );
1244     is(
1245         $prefetch_whitelist->{orders},
1246         'Koha::Acquisition::Order',
1247         'Guessed the non-standard object class correctly'
1248     );
1249
1250     is(
1251         $prefetch_whitelist->{items},
1252         'Koha::Item',
1253         'Guessed the standard object class correctly'
1254     );
1255
1256     $schema->storage->txn_rollback;
1257 };
1258
1259 subtest 'empty() tests' => sub {
1260
1261     plan tests => 6;
1262
1263     $schema->storage->txn_begin;
1264
1265     # Add a patron, we need at least 1
1266     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1267     ok( Koha::Patrons->count > 0, 'There is at least one Koha::Patron on the resultset' );
1268
1269     my $empty = Koha::Patrons->new->empty;
1270     is( ref($empty), 'Koha::Patrons', '->empty returns a Koha::Patrons iterator' );
1271     is( $empty->count, 0, 'The empty resultset is, well, empty :-D' );
1272
1273     my $new_rs = $empty->search({ borrowernumber => $patron->borrowernumber });
1274
1275     is( $new_rs->count, 0, 'Further chaining an empty resultset, returns an empty resultset' );
1276
1277     throws_ok
1278         { Koha::Patrons->empty; }
1279         'Koha::Exceptions::Object::NotInstantiated',
1280         'Exception thrown if not instantiated class';
1281
1282     is( "$@", 'Tried to access the \'empty\' method, but Koha::Patrons is not instantiated', 'Exception stringified correctly' );
1283
1284     $schema->storage->txn_rollback;
1285 };
1286
1287 subtest 'delete() tests' => sub {
1288
1289     plan tests => 2;
1290
1291     $schema->storage->txn_begin;
1292
1293     # Make sure no cities
1294     warnings_are { Koha::Cities->delete }[],
1295       "No warnings, no Koha::City->delete called as it doesn't exist";
1296
1297     # Mock Koha::City
1298     my $mocked_city = Test::MockModule->new('Koha::City');
1299     $mocked_city->mock(
1300         'delete',
1301         sub {
1302             shift->_result->delete;
1303             warn "delete called!";
1304         }
1305     );
1306
1307     # Add two cities
1308     $builder->build_object( { class => 'Koha::Cities' } );
1309     $builder->build_object( { class => 'Koha::Cities' } );
1310
1311     my $cities = Koha::Cities->search;
1312     $cities->next;
1313     warnings_are { $cities->delete }
1314         [ "delete called!", "delete called!" ],
1315         "No warnings, no Koha::City->delete called as it doesn't exist";
1316
1317     $schema->storage->txn_rollback;
1318 };