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