Bug 18936: Convert issuingrules fields to circulation_rules
[koha.git] / t / db_dependent / Koha / Objects.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 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 => 20;
23 use Test::Exception;
24 use Test::Warn;
25
26 use Koha::Authority::Types;
27 use Koha::Cities;
28 use Koha::Patron::Category;
29 use Koha::Patron::Categories;
30 use Koha::Patrons;
31 use Koha::Database;
32
33 use t::lib::TestBuilder;
34
35 use Try::Tiny;
36
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
39 my $builder = t::lib::TestBuilder->new;
40
41 is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
42
43 my @columns = Koha::Patrons->columns;
44 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
45 is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
46
47 subtest 'find' => sub {
48     plan tests => 6;
49     my $patron = $builder->build({source => 'Borrower'});
50     my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
51     is( $patron_object->borrowernumber, $patron->{borrowernumber}, '->find should return the correct object' );
52
53     my @patrons = Koha::Patrons->find( $patron->{borrowernumber} );
54     is(scalar @patrons, 1, '->find in list context returns a value');
55     is($patrons[0]->borrowernumber, $patron->{borrowernumber}, '->find in list context returns the same value as in scalar context');
56
57     my $patrons = {
58         foo => Koha::Patrons->find('foo'),
59         bar => 'baz',
60     };
61     is ($patrons->{foo}, undef, '->find in list context returns undef when no record is found');
62
63     # Test sending undef to find; should not generate a warning
64     warning_is { $patron = Koha::Patrons->find( undef ); }
65         "", "Sending undef does not trigger a DBIx warning";
66     warning_is { $patron = Koha::Patrons->find( undef, undef ); }
67         "", "Sending two undefs does not trigger a DBIx warning too";
68 };
69
70 subtest 'update' => sub {
71     plan tests => 2;
72
73     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
74     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
75     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
76     $builder->build( { source => 'City', value => { city_country => 'France' } } );
77     $builder->build( { source => 'City', value => { city_country => 'France' } } );
78     $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
79     Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
80     is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
81     is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
82 };
83
84 subtest 'reset' => sub {
85     plan tests => 3;
86
87     my $patrons = Koha::Patrons->search;
88     my $first_borrowernumber = $patrons->next->borrowernumber;
89     my $second_borrowernumber = $patrons->next->borrowernumber;
90     is( ref( $patrons->reset ), 'Koha::Patrons', 'Koha::Objects->reset should allow chaining' );
91     is( ref( $patrons->reset->next ), 'Koha::Patron', 'Koha::Objects->reset should allow chaining' );
92     is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
93 };
94
95 subtest 'delete' => sub {
96     plan tests => 2;
97
98     my $patron_1 = $builder->build({source => 'Borrower'});
99     my $patron_2 = $builder->build({source => 'Borrower'});
100     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, '');
101     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->count, 0, '');
102 };
103
104 subtest 'new' => sub {
105     plan tests => 2;
106     my $a_cat_code = 'A_CAT_CODE';
107     my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store;
108     is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
109     Koha::Patron::Categories->find($a_cat_code)->delete;
110     $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store;
111     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' );
112     Koha::Patron::Categories->find($a_cat_code)->delete;
113 };
114
115 subtest 'find' => sub {
116     plan tests => 4;
117
118     # check find on a single PK
119     my $patron = $builder->build({ source => 'Borrower' });
120     is( Koha::Patrons->find($patron->{borrowernumber})->surname,
121         $patron->{surname}, "Checking an arbitrary patron column after find"
122     );
123     # check find with unique column
124     my $obj = Koha::Patrons->find($patron->{cardnumber}, { key => 'cardnumber' });
125     is( $obj->borrowernumber, $patron->{borrowernumber},
126         'Find with unique column and key specified' );
127     # check find with an additional where clause in the attrs hash
128     # we do not expect to find something now
129     is( Koha::Patrons->find(
130         $patron->{borrowernumber},
131         { where => { surname => { '!=', $patron->{surname} }}},
132     ), undef, 'Additional where clause in find call' );
133
134     is( Koha::Patrons->find(), undef, 'Find returns undef if no params passed' );
135 };
136
137 subtest 'search_related' => sub {
138     plan tests => 8;
139     my $builder   = t::lib::TestBuilder->new;
140     my $patron_1  = $builder->build( { source => 'Borrower' } );
141     my $patron_2  = $builder->build( { source => 'Borrower' } );
142     my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
143     is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
144     is( $libraries->count,            2,                       'Koha::Objects->search_related should work as expected' );
145     is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
146     is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
147
148     my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
149     is( ref( $libraries[0] ),      'Koha::Library',         'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
150     is( scalar(@libraries),        2,                       'Koha::Objects->search_related should work as expected' );
151     is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
152     is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
153 };
154
155 subtest 'single' => sub {
156     plan tests => 2;
157     my $builder   = t::lib::TestBuilder->new;
158     my $patron_1  = $builder->build( { source => 'Borrower' } );
159     my $patron_2  = $builder->build( { source => 'Borrower' } );
160     my $patron = Koha::Patrons->search({}, { rows => 1 })->single;
161     is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
162     warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/,
163     "Warning is presented if single is used for a result with multiple rows.";
164 };
165
166 subtest 'last' => sub {
167     plan tests => 3;
168     my $builder = t::lib::TestBuilder->new;
169     my $patron_1  = $builder->build( { source => 'Borrower' } );
170     my $patron_2  = $builder->build( { source => 'Borrower' } );
171     my $last_patron = Koha::Patrons->search->last;
172     is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' );
173     $last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last;
174     is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' );
175     $last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last;
176     is( $last_patron, undef, '->last should return undef if search does not return any results' );
177 };
178
179 subtest 'get_column' => sub {
180     plan tests => 1;
181     my @cities = Koha::Cities->search;
182     my @city_names = map { $_->city_name } @cities;
183     is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' );
184 };
185
186 subtest 'Exceptions' => sub {
187     plan tests => 7;
188
189     my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
190     my $patron = Koha::Patrons->find( $patron_borrowernumber );
191
192     # Koha::Object
193     try {
194         $patron->blah('blah');
195     } catch {
196         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
197             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
198         is( $_->message, 'The method Koha::Patron->blah is not covered by tests!', 'The message raised should contain the package and the method' );
199     };
200
201     try {
202         $patron->set({ blah => 'blah' });
203     } catch {
204         ok( $_->isa('Koha::Exceptions::Object::PropertyNotFound'),
205             'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' );
206     };
207
208     # Koha::Objects
209     try {
210         Koha::Patrons->search->not_covered_yet;
211     } catch {
212         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
213             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
214         is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
215     };
216
217     try {
218         Koha::Patrons->not_covered_yet;
219     } catch {
220         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
221             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
222         is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
223     };
224 };
225
226 $schema->storage->txn_rollback;
227
228 subtest '->is_paged and ->pager tests' => sub {
229
230     plan tests => 5;
231
232     $schema->storage->txn_begin;
233
234     # Delete existing patrons
235     Koha::Checkouts->delete;
236     Koha::Patrons->delete;
237     # Create 10 patrons
238     foreach (1..10) {
239         $builder->build_object({ class => 'Koha::Patrons' });
240     }
241
242     # Non-paginated search
243     my $patrons = Koha::Patrons->search();
244     is( $patrons->count, 10, 'Search returns all patrons' );
245     ok( !$patrons->is_paged, 'Search is not paged' );
246
247     # Paginated search
248     $patrons = Koha::Patrons->search( undef, { 'page' => 1, 'rows' => 3 } );
249     is( $patrons->count, 3, 'Search returns only one page, 3 patrons' );
250     ok( $patrons->is_paged, 'Search is paged' );
251     my $pager = $patrons->pager;
252     is( ref($patrons->pager), 'DBIx::Class::ResultSet::Pager',
253        'Koha::Objects->pager returns a valid DBIx::Class object' );
254
255     $schema->storage->txn_rollback;
256 };
257
258 subtest '->search() tests' => sub {
259
260     plan tests => 12;
261
262     $schema->storage->txn_begin;
263
264     my $count = Koha::Patrons->search->count;
265
266     # Create 10 patrons
267     foreach (1..10) {
268         $builder->build_object({ class => 'Koha::Patrons' });
269     }
270
271     my $patrons = Koha::Patrons->search();
272     is( ref($patrons), 'Koha::Patrons', 'search in scalar context returns the Koha::Object-based type' );
273     my @patrons = Koha::Patrons->search();
274     is( scalar @patrons, $count + 10, 'search in list context returns a list of objects' );
275     my $i = 0;
276     foreach (1..10) {
277         is( ref($patrons[$i]), 'Koha::Patron', 'Objects in the list have the singular type' );
278         $i++;
279     }
280
281     $schema->storage->txn_rollback;
282 };
283
284 subtest "to_api() tests" => sub {
285
286     plan tests => 18;
287
288     $schema->storage->txn_begin;
289
290     my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
291     my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
292
293     my $cities = Koha::Cities->search(
294         {
295             cityid => [ $city_1->cityid, $city_2->cityid ]
296         },
297         { -orderby => { -desc => 'cityid' } }
298     );
299
300     is( $cities->count, 2, 'Count is correct' );
301     my $cities_api = $cities->to_api;
302     is( ref( $cities_api ), 'ARRAY', 'to_api returns an array' );
303     is_deeply( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' );
304     is_deeply( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' );
305
306     my $biblio_1 = $builder->build_sample_biblio();
307     my $item_1   = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
308     my $hold_1   = $builder->build_object(
309         {
310             class => 'Koha::Holds',
311             value => { itemnumber => $item_1->itemnumber }
312         }
313     );
314
315     my $biblio_2 = $builder->build_sample_biblio();
316     my $item_2   = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
317     my $hold_2   = $builder->build_object(
318         {
319             class => 'Koha::Holds',
320             value => { itemnumber => $item_2->itemnumber }
321         }
322     );
323
324     my $embed = { 'items' => {} };
325
326     my $i = 0;
327     my @items = ( $item_1, $item_2 );
328     my @holds = ( $hold_1, $hold_2 );
329
330     my $biblios_api = Koha::Biblios->search(
331         {
332             biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
333         }
334     )->to_api( { embed => $embed } );
335
336     foreach my $biblio_api ( @{ $biblios_api } ) {
337         ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
338         is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item matches');
339         ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
340
341         $i++;
342     }
343
344     # One more level
345     $embed = {
346         'items' => {
347             children => { 'holds' => {} }
348         }
349     };
350
351     $i = 0;
352
353     $biblios_api = Koha::Biblios->search(
354         {
355             biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
356         }
357     )->to_api( { embed => $embed } );
358
359     foreach my $biblio_api ( @{ $biblios_api } ) {
360
361         ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
362         is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item still matches');
363         ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
364         is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $holds[$i]->reserve_id, 'Hold matches');
365
366         $i++;
367     }
368
369     $schema->storage->txn_rollback;
370 };
371
372 subtest "TO_JSON() tests" => sub {
373
374     plan tests => 4;
375
376     $schema->storage->txn_begin;
377
378     my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
379     my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
380
381     my $cities = Koha::Cities->search(
382         {
383             cityid => [ $city_1->cityid, $city_2->cityid ]
384         },
385         { -orderby => { -desc => 'cityid' } }
386     );
387
388     is( $cities->count, 2, 'Count is correct' );
389     my $cities_json = $cities->TO_JSON;
390     is( ref($cities_json), 'ARRAY', 'to_api returns an array' );
391     is_deeply( $cities_json->[0], $city_1->TO_JSON, 'TO_JSON returns the individual objects with ->TO_JSON' );
392     is_deeply( $cities_json->[1], $city_2->TO_JSON,'TO_JSON returns the individual objects with ->TO_JSON' );
393
394     $schema->storage->txn_rollback;
395 };
396
397 # Koha::Object[s] must behave the same as DBIx::Class
398 subtest 'Return same values as DBIx::Class' => sub {
399     plan tests => 1;
400
401     subtest 'Delete' => sub {
402         plan tests => 2;
403
404         $schema->storage->txn_begin;
405
406         subtest 'Simple Koha::Objects - Koha::Cities' => sub {
407             plan tests => 2;
408
409             subtest 'Koha::Object->delete' => sub {
410
411                 plan tests => 5;
412
413                 my ( $r_us, $e_us, $r_them, $e_them );
414
415                 # CASE 1 - Delete an existing object
416                 my $c = Koha::City->new( { city_name => 'city4test' } )->store;
417                 try { $r_us = $c->delete; } catch { $e_us = $_ };
418                 $c = $schema->resultset('City')->new( { city_name => 'city4test_2' } )->update_or_insert;
419                 try { $r_them = $c->delete; } catch { $e_them = $_ };
420                 ok( ref($r_us) && ref($r_them),
421                     'Successful delete should return the object ' );
422                 ok( !defined $e_us && !defined $e_them,
423                     'Successful delete should not raise an exception' );
424                 is( ref($r_us), 'Koha::City', 'Successful delete should return our Koha::Obect based object' );
425
426                 # CASE 2 - Delete an object that is not in storage
427                 try { $r_us   = $r_us->delete;   } catch { $e_us   = $_ };
428                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
429                 ok(
430                     defined $e_us && defined $e_them,
431                     'Delete an object that is not in storage should raise an exception'
432                 );
433                 is( ref($e_us), 'DBIx::Class::Exception' )
434                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
435
436             };
437
438             subtest 'Koha::Objects->delete' => sub {
439
440                 plan tests => 4;
441
442                 my ( $r_us, $e_us, $r_them, $e_them );
443
444                 # CASE 1 - Delete existing objects
445                 my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
446                 my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
447                 my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
448                 my $cities = Koha::Cities->search(
449                     {
450                         cityid => {
451                             -in => [
452                                 $city_1->cityid,
453                                 $city_2->cityid,
454                                 $city_3->cityid,
455                             ]
456                         }
457                     }
458                 );
459
460                 try { $r_us = $cities->delete; } catch { $e_us = $_ };
461
462                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
463                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
464                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
465                 $cities = $schema->resultset('City')->search(
466                     {
467                         cityid => {
468                             -in => [
469                                 $city_1->cityid,
470                                 $city_2->cityid,
471                                 $city_3->cityid,
472                             ]
473                         }
474                     }
475                 );
476
477                 try { $r_them = $cities->delete; } catch { $e_them = $_ };
478
479                 ok( $r_us == 3 && $r_them == 3 );
480                 ok (!defined($e_us) && !defined($e_them));
481
482                 # CASE 2 - One of the object is not in storage
483                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
484                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
485                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
486                 $cities = Koha::Cities->search(
487                     {
488                         cityid => {
489                             -in => [
490                                 $city_1->cityid,
491                                 $city_2->cityid,
492                                 $city_3->cityid,
493                             ]
494                         }
495                     }
496                 );
497
498                 $city_2->delete; # We delete one of the object
499                 try { $r_us = $cities->delete; } catch { $e_us = $_ };
500
501                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
502                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
503                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
504                 $cities = $schema->resultset('City')->search(
505                     {
506                         cityid => {
507                             -in => [
508                                 $city_1->cityid,
509                                 $city_2->cityid,
510                                 $city_3->cityid,
511                             ]
512                         }
513                     }
514                 );
515
516                 $city_2->delete; # We delete one of the object
517                 try { $r_them = $cities->delete; } catch { $e_them = $_ };
518
519                 ok( $r_us == 2 && $r_them == 2 );
520                 ok (!defined($e_us) && !defined($e_them));
521             };
522         };
523
524         subtest 'Overwritten Koha::Objects->delete - Koha::Patrons' => sub {
525
526             plan tests => 2;
527
528             subtest 'Koha::Object->delete' => sub {
529
530                 plan tests => 7;
531
532                 my ( $r_us, $e_us, $r_them, $e_them );
533
534                 # CASE 1 - Delete an existing patron
535                 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
536                 my $patron_data = $patron->unblessed;
537                 $patron->delete;
538
539                 $patron = Koha::Patron->new( $patron_data )->store;
540                 try {$r_us = $patron->delete;} catch { $e_us = $_ };
541                 $patron = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
542                 try {$r_them = $patron->delete;} catch { $e_them = $_ };
543                 ok( ref($r_us) && ref($r_them),
544                     'Successful delete should return the patron object' );
545                 ok( !defined $e_us && !defined $e_them,
546                     'Successful delete should not raise an exception' );
547                 is( ref($r_us), 'Koha::Patron',
548                     'Successful delete should return our Koha::Obect based object' );
549
550                 # CASE 2 - Delete a patron that is not in storage
551                 try { $r_us   = $r_us->delete;   } catch { $e_us   = $_ };
552                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
553                 ok(
554                     defined $e_us && defined $e_them,
555                     'Delete a patron that is not in storage should raise an exception'
556                 );
557                 is( ref($e_us), 'DBIx::Class::Exception' )
558                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
559
560                 # CASE 3 - Delete a patron that cannot be deleted (as a checkout)
561                 $patron = Koha::Patron->new($patron_data)->store;
562                 $builder->build_object(
563                     {
564                         class => 'Koha::Checkouts',
565                         value => { borrowernumber => $patron->borrowernumber }
566                     }
567                 );
568                 try { $r_us = $r_us->delete; } catch { $e_us = $_ };
569                 $patron = $schema->resultset('Borrower')->find( $patron->borrowernumber );
570                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
571                 ok(
572                     defined $e_us && defined $e_them,
573                     'Delete a patron that cannot be deleted should raise an exception'
574                 );
575                 is( ref($e_us), 'DBIx::Class::Exception' )
576                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
577             };
578
579             subtest 'Koha::Objects->delete' => sub {
580
581                 plan tests => 9;
582
583                 my ( $r_us, $e_us, $r_them, $e_them );
584
585                 # CASE 1 - Delete existing objects
586                 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
587                 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
588                 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
589                 my $patrons = Koha::Patrons->search(
590                     {
591                         borrowernumber => {
592                             -in => [
593                                 $patron_1->borrowernumber,
594                                 $patron_2->borrowernumber,
595                                 $patron_3->borrowernumber
596                             ]
597                         }
598                     }
599                 );
600
601                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
602
603                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
604                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
605                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
606                 $patrons = $schema->resultset('Borrower')->search(
607                     {
608                         borrowernumber => {
609                             -in => [
610                                 $patron_1->borrowernumber,
611                                 $patron_2->borrowernumber,
612                                 $patron_3->borrowernumber
613                             ]
614                         }
615                     }
616                 );
617
618                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
619
620                 ok( $r_us == 3 && $r_them == 3, '->delete should return the number of deleted patrons' );
621                 ok (!defined($e_us) && !defined($e_them), '->delete should not raise exception if everything went well');
622
623                 # CASE 2 - One of the patrons is not in storage
624                 undef $_ for $r_us, $e_us, $r_them, $e_them;
625                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
626                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
627                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
628                 $patrons = Koha::Patrons->search(
629                     {
630                         borrowernumber => {
631                             -in => [
632                                 $patron_1->borrowernumber,
633                                 $patron_2->borrowernumber,
634                                 $patron_3->borrowernumber
635                             ]
636                         }
637                     }
638                 );
639
640                 $patron_2->delete; # We delete one of the patron
641                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
642
643                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
644                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
645                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
646                 $patrons = $schema->resultset('Borrower')->search(
647                     {
648                         borrowernumber => {
649                             -in => [
650                                 $patron_1->borrowernumber,
651                                 $patron_2->borrowernumber,
652                                 $patron_3->borrowernumber
653                             ]
654                         }
655                     }
656                 );
657
658                 $patron_2->delete; # We delete one of the patron
659                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
660
661                 ok( $r_us == 2 && $r_them == 2, 'Delete patrons with one that was not in storage should delete the patrons' );
662                 ok (!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
663
664                 # CASE 3 - Delete a set of patrons with one that that cannot be deleted (as a checkout)
665                 undef $_ for $r_us, $e_us, $r_them, $e_them;
666                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
667                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
668                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
669                 $patrons = Koha::Patrons->search(
670                     {
671                         borrowernumber => {
672                             -in => [
673                                 $patron_1->borrowernumber,
674                                 $patron_2->borrowernumber,
675                                 $patron_3->borrowernumber
676                             ]
677                         }
678                     }
679                 );
680
681                 # Adding a checkout to patron_2
682                 $builder->build_object(
683                     {
684                         class => 'Koha::Checkouts',
685                         value => { borrowernumber => $patron_2->borrowernumber }
686                     }
687                 );
688
689                 warning_like {
690                     try { $r_us = $patrons->delete; } catch { $e_us = $_ };
691                 }
692                 qr{DBD::mysql::st execute failed: Cannot delete or update a parent row: a foreign key constraint fails},
693                   "Foreign key constraint DBI error should be logged";
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                 warning_like {
720                     try { $r_them = $patrons->delete; } catch { $e_them = $_ };
721                 }
722                 qr{DBD::mysql::st execute failed: Cannot delete or update a parent row: a foreign key constraint fails},
723                   "Foreign key constraint DBI error should be logged";
724
725                 my $not_deleted_them = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
726                 ok(
727                     defined $e_us && defined $e_them,
728                     'Delete patrons with one that cannot be deleted should raise an exception'
729                 );
730                 is( ref($e_us), 'DBIx::Class::Exception' )
731                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
732
733                 ok($not_deleted_us == 3 && $not_deleted_them == 3, 'If one patron cannot be deleted, none should have been deleted');
734             };
735         };
736
737         $schema->storage->txn_rollback;
738
739     };
740 };
741
742 subtest "attributes_from_api() tests" => sub {
743
744     plan tests => 1;
745
746     $schema->storage->txn_begin;
747
748     my $cities_rs = Koha::Cities->new;
749     my $city      = Koha::City->new;
750
751     my $api_attributes = {
752         name        => 'Cordoba',
753         postal_code => 5000
754     };
755
756     is_deeply(
757         $cities_rs->attributes_from_api($api_attributes),
758         $city->attributes_from_api($api_attributes)
759     );
760
761     $schema->storage->txn_rollback;
762 };
763
764 subtest "from_api_mapping() tests" => sub {
765
766     plan tests => 1;
767
768     $schema->storage->txn_begin;
769
770     my $cities_rs = Koha::Cities->new;
771     my $city      = Koha::City->new;
772
773     is_deeply(
774         $cities_rs->from_api_mapping,
775         $city->from_api_mapping
776     );
777
778     $schema->storage->txn_rollback;
779 };