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