Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[koha.git] / t / db_dependent / Koha / Object.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 21;
21 use Test::Exception;
22 use Test::Warn;
23 use DateTime;
24
25 use C4::Context;
26 use C4::Circulation qw( AddIssue );
27 use C4::Biblio qw( AddBiblio );
28
29 use Koha::Database;
30
31 use Koha::Acquisition::Orders;
32 use Koha::ApiKeys;
33 use Koha::AuthorisedValueCategories;
34 use Koha::AuthorisedValues;
35 use Koha::DateUtils qw( dt_from_string );
36 use Koha::Libraries;
37 use Koha::Patrons;
38 use Koha::Library::Groups;
39
40 use JSON;
41 use Scalar::Util qw( isvstring );
42 use Try::Tiny;
43
44 use t::lib::TestBuilder;
45 use t::lib::Mocks;
46
47 BEGIN {
48     use_ok('Koha::Object');
49     use_ok('Koha::Patron');
50 }
51
52 my $schema  = Koha::Database->new->schema;
53 my $builder = t::lib::TestBuilder->new();
54
55 subtest 'is_changed / make_column_dirty' => sub {
56     plan tests => 11;
57
58     $schema->storage->txn_begin;
59
60     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
61     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
62
63     my $object = Koha::Patron->new();
64     $object->categorycode( $categorycode );
65     $object->branchcode( $branchcode );
66     $object->surname("Test Surname");
67     $object->store->discard_changes;
68     is( $object->is_changed(), 0, "Object is unchanged" );
69     $object->surname("Test Surname");
70     is( $object->is_changed(), 0, "Object is still unchanged" );
71     $object->surname("Test Surname 2");
72     is( $object->is_changed(), 1, "Object is changed" );
73
74     $object->store();
75     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
76
77     $object->set({ firstname => 'Test Firstname' });
78     is( $object->is_changed(), 1, "Object is changed after Set" );
79     $object->store();
80     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
81
82     # Test make_column_dirty
83     is( $object->make_column_dirty('firstname'), '', 'make_column_dirty returns empty string on success' );
84     is( $object->make_column_dirty('firstname'), 1, 'make_column_dirty returns 1 if already dirty' );
85     is( $object->is_changed, 1, "Object is changed after make dirty" );
86     $object->store;
87     is( $object->is_changed, 0, "Store clears dirty mark" );
88     $object->make_column_dirty('firstname');
89     $object->discard_changes;
90     is( $object->is_changed, 0, "Discard clears dirty mark too" );
91
92     $schema->storage->txn_rollback;
93 };
94
95 subtest 'in_storage' => sub {
96     plan tests => 6;
97
98     $schema->storage->txn_begin;
99
100     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
101     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
102
103     my $object = Koha::Patron->new();
104     is( $object->in_storage, 0, "Object is not in storage" );
105     $object->categorycode( $categorycode );
106     $object->branchcode( $branchcode );
107     $object->surname("Test Surname");
108     $object->store();
109     is( $object->in_storage, 1, "Object is now stored" );
110     $object->surname("another surname");
111     is( $object->in_storage, 1 );
112
113     my $borrowernumber = $object->borrowernumber;
114     my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
115     is( $patron->surname(), "Test Surname", "Object found in database" );
116
117     $object->delete();
118     $patron = $schema->resultset('Borrower')->find( $borrowernumber );
119     ok( ! $patron, "Object no longer found in database" );
120     is( $object->in_storage, 0, "Object is not in storage" );
121
122     $schema->storage->txn_rollback;
123 };
124
125 subtest 'id' => sub {
126     plan tests => 1;
127
128     $schema->storage->txn_begin;
129
130     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
131     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
132
133     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
134     is( $patron->id, $patron->borrowernumber );
135
136     $schema->storage->txn_rollback;
137 };
138
139 subtest 'get_column' => sub {
140     plan tests => 1;
141
142     $schema->storage->txn_begin;
143
144     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
145     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
146
147     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
148     is( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
149
150     $schema->storage->txn_rollback;
151 };
152
153 subtest 'discard_changes' => sub {
154     plan tests => 1;
155
156     $schema->storage->txn_begin;
157
158     my $patron = $builder->build( { source => 'Borrower' } );
159     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
160     $patron->dateexpiry(dt_from_string);
161     $patron->discard_changes;
162     is(
163         dt_from_string( $patron->dateexpiry ),
164         dt_from_string->truncate( to => 'day' ),
165         'discard_changes should refresh the object'
166     );
167
168     $schema->storage->txn_rollback;
169 };
170
171 subtest 'TO_JSON tests' => sub {
172
173     plan tests => 9;
174
175     $schema->storage->txn_begin;
176
177     my $dt = dt_from_string();
178     my $borrowernumber = $builder->build(
179         { source => 'Borrower',
180           value => { lost => 1,
181                      sms_provider_id => undef,
182                      gonenoaddress => 0,
183                      updated_on => $dt,
184                      lastseen   => $dt, } })->{borrowernumber};
185
186     my $patron = Koha::Patrons->find($borrowernumber);
187     my $lost = $patron->TO_JSON()->{lost};
188     my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
189     my $updated_on = $patron->TO_JSON->{updated_on};
190     my $lastseen = $patron->TO_JSON->{lastseen};
191
192     ok( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
193     is( $lost, 1, 'Boolean attribute value is correct (true)' );
194
195     ok( $gonenoaddress->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
196     is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
197
198     is( $patron->TO_JSON->{sms_provider_id}, undef, 'Undef values should not be casted to 0' );
199
200     ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
201
202     my $rfc3999_regex = qr/
203             (?<year>\d{4})
204             -
205             (?<month>\d{2})
206             -
207             (?<day>\d{2})
208             ([Tt\s])
209             (?<hour>\d{2})
210             :
211             (?<minute>\d{2})
212             :
213             (?<second>\d{2})
214             (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))
215         /xms;
216     like( $updated_on, $rfc3999_regex, "Date-time $updated_on formatted correctly");
217     like( $lastseen, $rfc3999_regex, "Date-time $updated_on formatted correctly");
218
219     # Test JSON doesn't receive strings
220     my $order = $builder->build_object({ class => 'Koha::Acquisition::Orders' });
221     $order = Koha::Acquisition::Orders->find( $order->ordernumber );
222     is_deeply( $order->TO_JSON, decode_json( encode_json( $order->TO_JSON ) ), 'Orders are similar' );
223
224     $schema->storage->txn_rollback;
225 };
226
227 subtest "to_api() tests" => sub {
228
229     plan tests => 31;
230
231     $schema->storage->txn_begin;
232
233     my $city = $builder->build_object({ class => 'Koha::Cities' });
234
235     # THE mapping
236     # cityid       => 'city_id',
237     # city_country => 'country',
238     # city_name    => 'name',
239     # city_state   => 'state',
240     # city_zipcode => 'postal_code'
241
242     my $api_city = $city->to_api;
243
244     is( $api_city->{city_id},     $city->cityid,       'Attribute translated correctly' );
245     is( $api_city->{country},     $city->city_country, 'Attribute translated correctly' );
246     is( $api_city->{name},        $city->city_name,    'Attribute translated correctly' );
247     is( $api_city->{state},       $city->city_state,   'Attribute translated correctly' );
248     is( $api_city->{postal_code}, $city->city_zipcode, 'Attribute translated correctly' );
249
250     # Lets emulate an undef
251     my $city_class = Test::MockModule->new('Koha::City');
252     $city_class->mock( 'to_api_mapping',
253         sub {
254             return {
255                 cityid       => 'city_id',
256                 city_country => 'country',
257                 city_name    => 'name',
258                 city_state   => 'state',
259                 city_zipcode => undef
260             };
261         }
262     );
263
264     $api_city = $city->to_api;
265
266     is( $api_city->{city_id},     $city->cityid,       'Attribute translated correctly' );
267     is( $api_city->{country},     $city->city_country, 'Attribute translated correctly' );
268     is( $api_city->{name},        $city->city_name,    'Attribute translated correctly' );
269     is( $api_city->{state},       $city->city_state,   'Attribute translated correctly' );
270     ok( !exists $api_city->{postal_code}, 'Attribute removed' );
271
272     # Pick a class that won't have a mapping for the API
273     my $action_log = $builder->build_object({ class => 'Koha::ActionLogs' });
274     is_deeply( $action_log->to_api, $action_log->TO_JSON, 'If no overloaded to_api_mapping method, return TO_JSON' );
275
276     my $biblio = $builder->build_sample_biblio();
277     my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
278     my $hold = $builder->build_object({ class => 'Koha::Holds', value => { itemnumber => $item->itemnumber } });
279
280     my $embeds = { 'items' => {} };
281
282     my $biblio_api = $biblio->to_api({ embed => $embeds });
283
284     ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
285     is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item matches');
286     ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
287
288     $embeds = (
289         {
290             'items' => {
291                 'children' => {
292                     'holds' => {}
293                 }
294             },
295             'biblioitem' => {}
296         }
297     );
298     $biblio_api = $biblio->to_api({ embed => $embeds });
299
300     ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
301     is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item still matches');
302     ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
303     is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches');
304     is_deeply($biblio_api->{biblioitem}, $biblio->biblioitem->to_api, 'More than one root');
305
306     my $_strings = {
307         location => {
308             category => 'ASD',
309             str      => 'Estante alto',
310             type     => 'av'
311         }
312     };
313
314     # mock Koha::Item so it implements 'strings_map'
315     my $item_mock = Test::MockModule->new('Koha::Item');
316     $item_mock->mock(
317         'strings_map',
318         sub {
319             return $_strings;
320         }
321     );
322
323     my $hold_api = $hold->to_api(
324         {
325             embed => { 'item' => { strings => 1 } }
326         }
327     );
328
329     is( ref($hold_api->{item}), 'HASH', 'Single nested object works correctly' );
330     is( $hold_api->{item}->{item_id}, $item->itemnumber, 'Object embedded correctly' );
331     is_deeply(
332         $hold_api->{item}->{_strings},
333         $_strings,
334         '_strings correctly added to nested embed'
335     );
336
337     # biblio with no items
338     my $new_biblio = $builder->build_sample_biblio;
339     my $new_biblio_api = $new_biblio->to_api({ embed => $embeds });
340
341     is_deeply( $new_biblio_api->{items}, [], 'Empty list if no items' );
342
343     my $biblio_class = Test::MockModule->new('Koha::Biblio');
344     $biblio_class->mock( 'undef_result', sub { return; } );
345
346     $new_biblio_api = $new_biblio->to_api({ embed => ( { 'undef_result' => {} } ) });
347     ok( exists $new_biblio_api->{undef_result}, 'If a method returns undef, then the attribute is defined' );
348     is( $new_biblio_api->{undef_result}, undef, 'If a method returns undef, then the attribute is undef' );
349
350     $biblio_class->mock( 'items',
351         sub { return [ bless { itemnumber => 1 }, 'Somethings' ]; } );
352
353     throws_ok {
354         $new_biblio_api = $new_biblio->to_api(
355             { embed => { 'items' => { children => { asd => {} } } } } );
356     }
357     'Koha::Exception',
358 "An exception is thrown if a blessed object to embed doesn't implement to_api";
359
360     is(
361         $@->message,
362         "Asked to embed items but its return value doesn't implement to_api",
363         "Exception message correct"
364     );
365
366
367     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
368     $builder->build_object(
369         {
370             class => 'Koha::Holds',
371             value => {
372                 biblionumber   => $biblio->biblionumber,
373                 borrowernumber => $patron->borrowernumber
374             }
375         }
376     );
377     $builder->build_object(
378         {
379             class => 'Koha::Holds',
380             value => {
381                 biblionumber   => $biblio->biblionumber,
382                 borrowernumber => $patron->borrowernumber
383             }
384         }
385     );
386
387     my $patron_api = $patron->to_api(
388         {
389             embed => { holds_count => { is_count => 1 } }
390         }
391     );
392     is( $patron_api->{holds_count}, $patron->holds->count, 'Count embeds are supported and work as expected' );
393
394     throws_ok
395         {
396             $patron->to_api({ embed => { holds_count => {} } });
397         }
398         'Koha::Exceptions::Object::MethodNotCoveredByTests',
399         'Unknown method exception thrown if is_count not specified';
400
401     subtest 'unprivileged request tests' => sub {
402
403         my @all_attrs = Koha::Libraries->columns();
404         my $public_attrs = { map { $_ => 1 } @{ Koha::Library->public_read_list() } };
405         my $mapping = Koha::Library->to_api_mapping;
406
407         plan tests => scalar @all_attrs * 2;
408
409         # Create a sample library
410         my $library = $builder->build_object( { class => 'Koha::Libraries' } );
411
412         my $unprivileged_representation = $library->to_api({ public => 1 });
413         my $privileged_representation   = $library->to_api;
414
415         foreach my $attr (@all_attrs) {
416             my $mapped = exists $mapping->{$attr} ? $mapping->{$attr} : $attr;
417             if ( defined($mapped) ) {
418                 ok(
419                     exists $privileged_representation->{$mapped},
420                     "Attribute '$attr' is present when privileged"
421                 );
422                 if ( exists $public_attrs->{$attr} ) {
423                     ok(
424                         exists $unprivileged_representation->{$mapped},
425                         "Attribute '$attr' is present when public"
426                     );
427                 }
428                 else {
429                     ok(
430                         !exists $unprivileged_representation->{$mapped},
431                         "Attribute '$attr' is not present when public"
432                     );
433                 }
434             }
435             else {
436                 ok(
437                     !exists $privileged_representation->{$attr},
438                     "Unmapped attribute '$attr' is not present when privileged"
439                 );
440                 ok(
441                     !exists $unprivileged_representation->{$attr},
442                     "Unmapped attribute '$attr' is not present when public"
443                 );
444             }
445         }
446     };
447
448     subtest 'Authorised values expansion' => sub {
449
450         plan tests => 4;
451
452         $schema->storage->txn_begin;
453
454         # new category
455         my $category = $builder->build_object({ class => 'Koha::AuthorisedValueCategories' });
456         # add two countries
457         my $argentina = $builder->build_object(
458             {   class => 'Koha::AuthorisedValues',
459                 value => {
460                     category => $category->category_name,
461                     lib      => 'AR (Argentina)',
462                     lib_opac => 'Argentina',
463                 }
464             }
465         );
466         my $france = $builder->build_object(
467             {   class => 'Koha::AuthorisedValues',
468                 value => {
469                     category => $category->category_name,
470                     lib      => 'FR (France)',
471                     lib_opac => 'France',
472                 }
473             }
474         );
475
476         my $city_mock = Test::MockModule->new('Koha::City');
477         $city_mock->mock(
478             'strings_map',
479             sub {
480                 my ( $self, $params ) = @_;
481
482                 my $av = Koha::AuthorisedValues->find(
483                     {
484                         authorised_value => $self->city_country,
485                         category         => $category->category_name,
486                     }
487                 );
488
489                 return {
490                     city_country => {
491                         category => $av->category,
492                         str      => ( $params->{public} ) ? $av->lib_opac : $av->lib,
493                         type     => 'av',
494                     }
495                 };
496             }
497         );
498         $city_mock->mock( 'public_read_list', sub { return [ 'city_id', 'city_country', 'city_name', 'city_state' ] } );
499
500         my $cordoba = $builder->build_object(
501             {   class => 'Koha::Cities',
502                 value => { city_country => $argentina->authorised_value, city_name => 'Cordoba' }
503             }
504         );
505         my $marseille = $builder->build_object(
506             {   class => 'Koha::Cities',
507                 value => { city_country => $france->authorised_value, city_name => 'Marseille' }
508             }
509         );
510
511         my $mobj = $marseille->to_api( { strings => 1, public => 1 } );
512         my $cobj = $cordoba->to_api( { strings => 1, public => 0 } );
513
514         ok( exists $mobj->{_strings}, '_strings exists for Marseille' );
515         ok( exists $cobj->{_strings}, '_strings exists for Córdoba' );
516
517         is_deeply(
518             $mobj->{_strings}->{country},
519             {
520                 category => $category->category_name,
521                 str      => $france->lib_opac,
522                 type     => 'av',
523             },
524             'Authorised value for country expanded'
525         );
526         is_deeply(
527             $cobj->{_strings}->{country},
528             {
529                 category => $category->category_name,
530                 str      => $argentina->lib,
531                 type     => 'av'
532             },
533             'Authorised value for country expanded'
534         );
535
536         $schema->storage->txn_rollback;
537     };
538
539     $schema->storage->txn_rollback;
540 };
541
542 subtest "to_api_mapping() tests" => sub {
543
544     plan tests => 1;
545
546     $schema->storage->txn_begin;
547
548     my $action_log = $builder->build_object({ class => 'Koha::ActionLogs' });
549     is_deeply( $action_log->to_api_mapping, {}, 'If no to_api_mapping present, return empty hashref' );
550
551     $schema->storage->txn_rollback;
552 };
553
554 subtest "from_api_mapping() tests" => sub {
555
556     plan tests => 5;
557
558     $schema->storage->txn_begin;
559
560     my $city = $builder->build_object({ class => 'Koha::Cities' });
561
562     # Lets emulate an undef
563     my $city_class = Test::MockModule->new('Koha::City');
564     $city_class->mock( 'to_api_mapping',
565         sub {
566             return {
567                 cityid       => 'city_id',
568                 city_country => 'country',
569                 city_zipcode => undef
570             };
571         }
572     );
573
574     is_deeply(
575         $city->from_api_mapping,
576         {
577             city_id => 'cityid',
578             country => 'city_country'
579         },
580         'Mapping returns correctly, undef ommited'
581     );
582
583     $city_class->unmock( 'to_api_mapping');
584     $city_class->mock( 'to_api_mapping',
585         sub {
586             return {
587                 cityid       => 'city_id',
588                 city_country => 'country',
589                 city_zipcode => 'postal_code'
590             };
591         }
592     );
593
594     is_deeply(
595         $city->from_api_mapping,
596         {
597             city_id => 'cityid',
598             country => 'city_country'
599         },
600         'Reverse mapping is cached'
601     );
602
603     # Get a fresh object
604     $city = $builder->build_object({ class => 'Koha::Cities' });
605     is_deeply(
606         $city->from_api_mapping,
607         {
608             city_id     => 'cityid',
609             country     => 'city_country',
610             postal_code => 'city_zipcode'
611         },
612         'Fresh mapping loaded'
613     );
614
615     $city_class->unmock( 'to_api_mapping');
616     $city_class->mock( 'to_api_mapping', undef );
617
618     # Get a fresh object
619     $city = $builder->build_object({ class => 'Koha::Cities' });
620     is_deeply(
621         $city->from_api_mapping,
622         {},
623         'No to_api_mapping then empty hashref'
624     );
625
626     $city_class->unmock( 'to_api_mapping');
627     $city_class->mock( 'to_api_mapping', sub { return; } );
628
629     # Get a fresh object
630     $city = $builder->build_object({ class => 'Koha::Cities' });
631     is_deeply(
632         $city->from_api_mapping,
633         {},
634         'Empty to_api_mapping then empty hashref'
635     );
636
637     $schema->storage->txn_rollback;
638 };
639
640 subtest 'set_from_api() tests' => sub {
641
642     plan tests => 4;
643
644     $schema->storage->txn_begin;
645
646     my $city = $builder->build_object({ class => 'Koha::Cities' });
647     my $city_unblessed = $city->unblessed;
648     my $attrs = {
649         name        => 'Cordoba',
650         country     => 'Argentina',
651         postal_code => '5000'
652     };
653     $city->set_from_api($attrs);
654
655     is( $city->city_state, $city_unblessed->{city_state}, 'Untouched attributes are preserved' );
656     is( $city->city_name, $attrs->{name}, 'city_name updated correctly' );
657     is( $city->city_country, $attrs->{country}, 'city_country updated correctly' );
658     is( $city->city_zipcode, $attrs->{postal_code}, 'city_zipcode updated correctly' );
659
660     $schema->storage->txn_rollback;
661 };
662
663 subtest 'new_from_api() tests' => sub {
664
665     plan tests => 4;
666
667     $schema->storage->txn_begin;
668
669     my $attrs = {
670         name        => 'Cordoba',
671         country     => 'Argentina',
672         postal_code => '5000'
673     };
674     my $city = Koha::City->new_from_api($attrs);
675
676     is( ref($city), 'Koha::City', 'Object type is correct' );
677     is( $city->city_name,    $attrs->{name}, 'city_name updated correctly' );
678     is( $city->city_country, $attrs->{country}, 'city_country updated correctly' );
679     is( $city->city_zipcode, $attrs->{postal_code}, 'city_zipcode updated correctly' );
680
681     $schema->storage->txn_rollback;
682 };
683
684 subtest 'attributes_from_api() tests' => sub {
685
686     plan tests => 2;
687
688     subtest 'date and date-time handling tests' => sub {
689
690         plan tests => 12;
691
692         my $patron = Koha::Patron->new();
693
694         delete $C4::Context::context->{tz};
695         local %ENV;
696         $ENV{TZ} = 'Etc/UTC';    # following tests implicitly assume it
697
698         my $attrs = $patron->attributes_from_api(
699             {
700                 updated_on     => '2019-12-27T14:53:00Z',
701                 last_seen      => '2019-12-27T14:53:00Z',
702                 date_of_birth  => '2019-12-27',
703             }
704         );
705
706         ok( exists $attrs->{updated_on},
707             'No translation takes place if no mapping' );
708         is(
709             $attrs->{updated_on},
710             '2019-12-27 14:53:00',
711             'Given an rfc3339 formatted datetime string, a timestamp field is converted into an SQL formatted datetime string'
712         );
713
714         ok( exists $attrs->{lastseen},
715             'Translation takes place because of the defined mapping' );
716         is(
717             $attrs->{lastseen},
718             '2019-12-27 14:53:00',
719             'Given an rfc3339 formatted datetime string, a datetime field is converted into an SQL formatted datetime string'
720         );
721
722         ok( exists $attrs->{dateofbirth},
723             'Translation takes place because of the defined mapping' );
724         is(
725             $attrs->{dateofbirth},
726             '2019-12-27',
727             'Given an rfc3339 formatted date string, a date field is converted into an SQL formatted date string'
728         );
729
730         $attrs = $patron->attributes_from_api(
731             {
732                 last_seen      => undef,
733                 date_of_birth  => undef,
734             }
735         );
736
737         ok( exists $attrs->{lastseen},
738             'undef parameter is not skipped (Bug 29157)' );
739         is(
740             $attrs->{lastseen},
741             undef,
742             'Given undef, a datetime field is set to undef (Bug 29157)'
743         );
744
745         ok( exists $attrs->{dateofbirth},
746             'undef parameter is not skipped (Bug 29157)' );
747         is(
748             $attrs->{dateofbirth},
749             undef,
750             'Given undef, a date field is set to undef (Bug 29157)'
751         );
752
753         throws_ok
754             {
755                 $attrs = $patron->attributes_from_api(
756                     {
757                         date_of_birth => '20141205',
758                     }
759                 );
760             }
761             'Koha::Exceptions::BadParameter',
762             'Bad date throws an exception';
763
764         is(
765             $@->parameter,
766             'date_of_birth',
767             'Exception parameter is the API field name, not the DB one'
768         );
769
770         # Remove timezone change
771         delete $C4::Context::context->{tz};
772     };
773
774     subtest 'booleans handling tests' => sub {
775
776         plan tests => 4;
777
778         my $patron = Koha::Patron->new;
779
780         my $attrs = $patron->attributes_from_api(
781             {
782                 incorrect_address => Mojo::JSON->true,
783                 patron_card_lost  => Mojo::JSON->false,
784             }
785         );
786
787         ok( exists $attrs->{gonenoaddress}, 'Attribute gets translated' );
788         is( $attrs->{gonenoaddress}, 1, 'Boolean correctly translated to integer (true => 1)' );
789         ok( exists $attrs->{lost}, 'Attribute gets translated' );
790         is( $attrs->{lost}, 0, 'Boolean correctly translated to integer (false => 0)' );
791     };
792 };
793
794 subtest "Test update method" => sub {
795     plan tests => 6;
796
797     $schema->storage->txn_begin;
798
799     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
800     my $library = Koha::Libraries->find( $branchcode );
801     $library->update({ branchname => 'New_Name', branchcity => 'AMS' });
802     is( $library->branchname, 'New_Name', 'Changed name with update' );
803     is( $library->branchcity, 'AMS', 'Changed city too' );
804     is( $library->is_changed, 0, 'Change should be stored already' );
805     try {
806         $library->update({
807             branchcity => 'NYC', not_a_column => 53, branchname => 'Name3',
808         });
809         fail( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
810     } catch {
811         ok( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
812         $library->discard_changes; #requery after failing update
813     };
814     # Check if the columns are not updated
815     is( $library->branchcity, 'AMS', 'First column not updated' );
816     is( $library->branchname, 'New_Name', 'Third column not updated' );
817
818     $schema->storage->txn_rollback;
819 };
820
821 subtest 'store() tests' => sub {
822
823     plan tests => 16;
824
825     # Using Koha::Library::Groups to test Koha::Object>-store
826     # Simple object with foreign keys and unique key
827
828     $schema->storage->txn_begin;
829
830     # Create a library to make sure its ID doesn't exist on the DB
831     my $library = $builder->build_object({ class => 'Koha::Libraries' });
832     my $branchcode = $library->branchcode;
833     $library->delete;
834
835     my $library_group = Koha::Library::Group->new(
836         {
837             branchcode      => $library->branchcode,
838             title => 'a title',
839         }
840     );
841
842     my $dbh = $schema->storage->dbh;
843     {
844         local *STDERR;
845         open STDERR, '>', '/dev/null';
846         throws_ok
847             { $library_group->store }
848             'Koha::Exceptions::Object::FKConstraint',
849             'Exception is thrown correctly';
850         is(
851             $@->message,
852             "Broken FK constraint",
853             'Exception message is correct'
854         );
855         is(
856             $@->broken_fk,
857             'branchcode',
858             'Exception field is correct'
859         );
860
861         $library_group = $builder->build_object({ class => 'Koha::Library::Groups' });
862
863         my $new_library_group = Koha::Library::Group->new(
864             {
865                 branchcode      => $library_group->branchcode,
866                 title        => $library_group->title,
867             }
868         );
869
870         throws_ok
871             { $new_library_group->store }
872             'Koha::Exceptions::Object::DuplicateID',
873             'Exception is thrown correctly';
874
875         is(
876             $@->message,
877             'Duplicate ID',
878             'Exception message is correct'
879         );
880
881         like(
882            $@->duplicate_id,
883            qr/(library_groups\.)?title/,
884            'Exception field is correct (note that MySQL 8 is displaying the tablename)'
885         );
886         close STDERR;
887     }
888
889     # Successful test
890     $library_group->set({ title => 'Manuel' });
891     my $ret = $library_group->store;
892     is( ref($ret), 'Koha::Library::Group', 'store() returns the object on success' );
893
894     $library = $builder->build_object( { class => 'Koha::Libraries' } );
895     my $patron_category = $builder->build_object(
896         {
897             class => 'Koha::Patron::Categories',
898             value => { category_type => 'P', enrolmentfee => 0 }
899         }
900     );
901
902     my $patron = eval {
903         Koha::Patron->new(
904             {
905                 categorycode    => $patron_category->categorycode,
906                 branchcode      => $library->branchcode,
907                 dateofbirth     => "", # date will be set to NULL
908                 sms_provider_id => "", # Integer will be set to NULL
909                 privacy         => "", # privacy cannot be NULL but has a default value
910             }
911         )->store;
912     };
913     is( $@, '', 'No error should be raised by ->store if empty strings are passed' );
914     is( $patron->privacy, 1, 'Default value for privacy should be set to 1' );
915     is( $patron->dateofbirth,     undef, 'dateofbirth must have been set to undef');
916     is( $patron->sms_provider_id, undef, 'sms_provider_id must have been set to undef');
917
918     my $itemtype = eval {
919         Koha::ItemType->new(
920             {
921                 itemtype        => 'IT4test',
922                 rentalcharge    => "",
923                 notforloan      => "",
924                 hideinopac      => "",
925             }
926         )->store;
927     };
928     is( $@, '', 'No error should be raised by ->store if empty strings are passed' );
929     is( $itemtype->rentalcharge, undef, 'decimal DEFAULT NULL should default to null');
930     is( $itemtype->notforloan, undef, 'int DEFAULT NULL should default to null');
931     is( $itemtype->hideinopac, 0, 'int NOT NULL DEFAULT 0 should default to 0');
932
933     subtest 'Bad value tests' => sub {
934
935         plan tests => 3;
936
937         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
938
939
940         try {
941             local *STDERR;
942             open STDERR, '>', '/dev/null';
943             $patron->lastseen('wrong_value')->store;
944             close STDERR;
945         } catch {
946             ok( $_->isa('Koha::Exceptions::Object::BadValue'), 'Exception thrown correctly' );
947             like( $_->property, qr/(borrowers\.)?lastseen/, 'Column should be the expected one' ); # The table name is not always displayed, it depends on the DBMS version
948             is( $_->value, 'wrong_value', 'Value should be the expected one' );
949         };
950     };
951
952     $schema->storage->txn_rollback;
953 };
954
955 subtest 'unblessed_all_relateds' => sub {
956     plan tests => 3;
957
958     $schema->storage->txn_begin;
959
960     # FIXME It's very painful to create an issue in tests!
961     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
962     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
963
964     my $patron_category = $builder->build(
965         {
966             source => 'Category',
967             value  => {
968                 category_type                 => 'P',
969                 enrolmentfee                  => 0,
970                 BlockExpiredPatronOpacActions => -1, # Pick the pref value
971             }
972         }
973     );
974     my $patron_data = {
975         firstname =>  'firstname',
976         surname => 'surname',
977         categorycode => $patron_category->{categorycode},
978         branchcode => $library->branchcode,
979     };
980     my $patron = Koha::Patron->new($patron_data)->store;
981     my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
982     my $biblio = Koha::Biblios->find( $biblionumber );
983     my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
984
985     my $item = $builder->build_object(
986         {
987             class => 'Koha::Items',
988             value => {
989                 homebranch    => $library->branchcode,
990                 holdingbranch => $library->branchcode,
991                 biblionumber  => $biblio->biblionumber,
992                 itemlost      => 0,
993                 withdrawn     => 0,
994                 itype => $itemtype
995             }
996         }
997     );
998
999     my $issue = AddIssue( $patron->unblessed, $item->barcode, DateTime->now->subtract( days => 1 ) );
1000     my $overdues = Koha::Patrons->find( $patron->id )->overdues; # Koha::Patron->overdues prefetches
1001     my $overdue = $overdues->next->unblessed_all_relateds;
1002     is( $overdue->{issue_id}, $issue->issue_id, 'unblessed_all_relateds has field from the original table (issues)' );
1003     is( $overdue->{title}, $biblio->title, 'unblessed_all_relateds has field from other tables (biblio)' );
1004     is( $overdue->{homebranch}, $item->homebranch, 'unblessed_all_relateds has field from other tables (items)' );
1005
1006     $schema->storage->txn_rollback;
1007 };
1008
1009 subtest 'get_from_storage' => sub {
1010     plan tests => 4;
1011
1012     $schema->storage->txn_begin;
1013
1014     my $biblio = $builder->build_sample_biblio;
1015
1016     my $old_title = $biblio->title;
1017     my $new_title = 'new_title';
1018     Koha::Biblios->find( $biblio->biblionumber )->title($new_title)->store;
1019
1020     is( $biblio->title, $old_title, 'current $biblio should not be modified' );
1021     is( $biblio->get_from_storage->title,
1022         $new_title, 'get_from_storage should return an updated object' );
1023
1024     Koha::Biblios->find( $biblio->biblionumber )->delete;
1025     is( ref($biblio), 'Koha::Biblio', 'current $biblio should not be deleted' );
1026     is( $biblio->get_from_storage, undef,
1027         'get_from_storage should return undef if the object has been deleted' );
1028
1029     $schema->storage->txn_rollback;
1030 };
1031
1032 subtest 'prefetch_whitelist() tests' => sub {
1033
1034     plan tests => 3;
1035
1036     $schema->storage->txn_begin;
1037
1038     my $biblio = Koha::Biblio->new;
1039
1040     my $prefetch_whitelist = $biblio->prefetch_whitelist;
1041
1042     ok(
1043         exists $prefetch_whitelist->{orders},
1044         'Relationship matching method name is listed'
1045     );
1046     is(
1047         $prefetch_whitelist->{orders},
1048         'Koha::Acquisition::Order',
1049         'Guessed the non-standard object class correctly'
1050     );
1051
1052     is(
1053         $prefetch_whitelist->{items},
1054         'Koha::Item',
1055         'Guessed the standard object class correctly'
1056     );
1057
1058     $schema->storage->txn_rollback;
1059 };
1060
1061 subtest 'set_or_blank' => sub {
1062
1063     plan tests => 5;
1064
1065     $schema->storage->txn_begin;
1066
1067     my $item = $builder->build_sample_item;
1068     my $item_info = $item->unblessed;
1069     $item = $item->set_or_blank($item_info);
1070     is_deeply($item->unblessed, $item_info, 'set_or_blank assign the correct value if unchanged');
1071
1072     # int not null
1073     delete $item_info->{itemlost};
1074     $item = $item->set_or_blank($item_info);
1075     is($item->itemlost, 0, 'set_or_blank should have set itemlost to 0, default value defined in DB');
1076
1077     # int nullable
1078     delete $item_info->{restricted};
1079     $item = $item->set_or_blank($item_info);
1080     is($item->restricted, undef, 'set_or_blank should have set restristed to null' );
1081
1082     # datetime nullable
1083     delete $item_info->{dateaccessioned};
1084     $item = $item->set_or_blank($item_info);
1085     is($item->dateaccessioned, undef, 'set_or_blank should have set dateaccessioned to null');
1086
1087     # timestamp not null
1088     delete $item_info->{timestamp};
1089     $item = $item->set_or_blank($item_info);
1090     isnt($item->timestamp, undef, 'set_or_blank should have set timestamp to a correct value');
1091
1092     $schema->storage->txn_rollback;
1093 };
1094
1095 subtest 'messages() and add_message() tests' => sub {
1096
1097     plan tests => 7;
1098
1099     $schema->storage->txn_begin;
1100
1101     my $patron = Koha::Patron->new;
1102
1103     my @messages = @{ $patron->object_messages };
1104     is( scalar @messages, 0, 'No messages' );
1105
1106     $patron->add_message({ message => "message_1" });
1107     $patron->add_message({ message => "message_2" });
1108
1109     @messages = @{ $patron->object_messages };
1110
1111     is( scalar @messages, 2, 'Messages are returned' );
1112     is( ref($messages[0]), 'Koha::Object::Message', 'Right type returned' );
1113     is( ref($messages[1]), 'Koha::Object::Message', 'Right type returned' );
1114     is( $messages[0]->message, 'message_1', 'Right message recorded' );
1115
1116     my $patron_id = $builder->build_object({ class => 'Koha::Patrons' })->id;
1117     # get a patron from the DB, ->new is not called, ->object_messages should initialize _messages as an empty arrayref
1118     $patron = Koha::Patrons->find( $patron_id );
1119
1120     isnt( $patron->object_messages, undef, '->messages initializes the array if required' );
1121     is( scalar @{ $patron->object_messages }, 0, '->messages returns an empty arrayref' );
1122
1123     $schema->storage->txn_rollback;
1124 };