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