3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 18;
26 use C4::Circulation; # AddIssue
27 use C4::Biblio; # AddBiblio
30 use Koha::DateUtils qw( dt_from_string );
35 use Scalar::Util qw( isvstring );
38 use t::lib::TestBuilder;
42 use_ok('Koha::Object');
43 use_ok('Koha::Patron');
46 my $schema = Koha::Database->new->schema;
47 my $builder = t::lib::TestBuilder->new();
49 subtest 'is_changed / make_column_dirty' => sub {
52 $schema->storage->txn_begin;
54 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
55 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
57 my $object = Koha::Patron->new();
58 $object->categorycode( $categorycode );
59 $object->branchcode( $branchcode );
60 $object->surname("Test Surname");
62 is( $object->is_changed(), 0, "Object is unchanged" );
63 $object->surname("Test Surname");
64 is( $object->is_changed(), 0, "Object is still unchanged" );
65 $object->surname("Test Surname 2");
66 is( $object->is_changed(), 1, "Object is changed" );
69 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
71 $object->set({ firstname => 'Test Firstname' });
72 is( $object->is_changed(), 1, "Object is changed after Set" );
74 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
76 # Test make_column_dirty
77 is( $object->make_column_dirty('firstname'), '', 'make_column_dirty returns empty string on success' );
78 is( $object->make_column_dirty('firstname'), 1, 'make_column_dirty returns 1 if already dirty' );
79 is( $object->is_changed, 1, "Object is changed after make dirty" );
81 is( $object->is_changed, 0, "Store clears dirty mark" );
82 $object->make_column_dirty('firstname');
83 $object->discard_changes;
84 is( $object->is_changed, 0, "Discard clears dirty mark too" );
86 $schema->storage->txn_rollback;
89 subtest 'in_storage' => sub {
92 $schema->storage->txn_begin;
94 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
95 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
97 my $object = Koha::Patron->new();
98 is( $object->in_storage, 0, "Object is not in storage" );
99 $object->categorycode( $categorycode );
100 $object->branchcode( $branchcode );
101 $object->surname("Test Surname");
103 is( $object->in_storage, 1, "Object is now stored" );
104 $object->surname("another surname");
105 is( $object->in_storage, 1 );
107 my $borrowernumber = $object->borrowernumber;
108 my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
109 is( $patron->surname(), "Test Surname", "Object found in database" );
112 $patron = $schema->resultset('Borrower')->find( $borrowernumber );
113 ok( ! $patron, "Object no longer found in database" );
114 is( $object->in_storage, 0, "Object is not in storage" );
116 $schema->storage->txn_rollback;
119 subtest 'id' => sub {
122 $schema->storage->txn_begin;
124 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
125 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
127 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
128 is( $patron->id, $patron->borrowernumber );
130 $schema->storage->txn_rollback;
133 subtest 'get_column' => sub {
136 $schema->storage->txn_begin;
138 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
139 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
141 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
142 is( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
144 $schema->storage->txn_rollback;
147 subtest 'discard_changes' => sub {
150 $schema->storage->txn_begin;
152 my $patron = $builder->build( { source => 'Borrower' } );
153 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
154 $patron->dateexpiry(dt_from_string);
155 $patron->discard_changes;
157 dt_from_string( $patron->dateexpiry ),
158 dt_from_string->truncate( to => 'day' ),
159 'discard_changes should refresh the object'
162 $schema->storage->txn_rollback;
165 subtest 'TO_JSON tests' => sub {
169 $schema->storage->txn_begin;
171 my $dt = dt_from_string();
172 my $borrowernumber = $builder->build(
173 { source => 'Borrower',
174 value => { lost => 1,
175 sms_provider_id => undef,
178 lastseen => $dt, } })->{borrowernumber};
180 my $patron = Koha::Patrons->find($borrowernumber);
181 my $lost = $patron->TO_JSON()->{lost};
182 my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
183 my $updated_on = $patron->TO_JSON->{updated_on};
184 my $lastseen = $patron->TO_JSON->{lastseen};
186 ok( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
187 is( $lost, 1, 'Boolean attribute value is correct (true)' );
189 ok( $gonenoaddress->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
190 is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
192 is( $patron->TO_JSON->{sms_provider_id}, undef, 'Undef values should not be casted to 0' );
194 ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
196 my $rfc3999_regex = qr/
208 (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))
210 like( $updated_on, $rfc3999_regex, "Date-time $updated_on formatted correctly");
211 like( $lastseen, $rfc3999_regex, "Date-time $updated_on formatted correctly");
213 $schema->storage->txn_rollback;
216 subtest "to_api() tests" => sub {
220 $schema->storage->txn_begin;
222 my $city = $builder->build_object({ class => 'Koha::Cities' });
225 # cityid => 'city_id',
226 # city_country => 'country',
227 # city_name => 'name',
228 # city_state => 'state',
229 # city_zipcode => 'postal_code'
231 my $api_city = $city->to_api;
233 is( $api_city->{city_id}, $city->cityid, 'Attribute translated correctly' );
234 is( $api_city->{country}, $city->city_country, 'Attribute translated correctly' );
235 is( $api_city->{name}, $city->city_name, 'Attribute translated correctly' );
236 is( $api_city->{state}, $city->city_state, 'Attribute translated correctly' );
237 is( $api_city->{postal_code}, $city->city_zipcode, 'Attribute translated correctly' );
239 # Lets emulate an undef
240 my $city_class = Test::MockModule->new('Koha::City');
241 $city_class->mock( 'to_api_mapping',
245 city_country => 'country',
247 city_state => 'state',
248 city_zipcode => undef
253 $api_city = $city->to_api;
255 is( $api_city->{city_id}, $city->cityid, 'Attribute translated correctly' );
256 is( $api_city->{country}, $city->city_country, 'Attribute translated correctly' );
257 is( $api_city->{name}, $city->city_name, 'Attribute translated correctly' );
258 is( $api_city->{state}, $city->city_state, 'Attribute translated correctly' );
259 ok( !exists $api_city->{postal_code}, 'Attribute removed' );
261 # Pick a class that won't have a mapping for the API
262 my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
263 is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no overloaded to_api_mapping method, return TO_JSON' );
265 my $biblio = $builder->build_sample_biblio();
266 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
267 my $hold = $builder->build_object({ class => 'Koha::Holds', value => { itemnumber => $item->itemnumber } });
269 my $embeds = { 'items' => {} };
271 my $biblio_api = $biblio->to_api({ embed => $embeds });
273 ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
274 is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item matches');
275 ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
287 $biblio_api = $biblio->to_api({ embed => $embeds });
289 ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
290 is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item still matches');
291 ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
292 is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches');
293 is_deeply($biblio_api->{biblioitem}, $biblio->biblioitem->to_api, 'More than one root');
295 my $hold_api = $hold->to_api(
297 embed => { 'item' => {} }
301 is( ref($hold_api->{item}), 'HASH', 'Single nested object works correctly' );
302 is( $hold_api->{item}->{item_id}, $item->itemnumber, 'Object embedded correctly' );
304 # biblio with no items
305 my $new_biblio = $builder->build_sample_biblio;
306 my $new_biblio_api = $new_biblio->to_api({ embed => $embeds });
308 is_deeply( $new_biblio_api->{items}, [], 'Empty list if no items' );
310 my $biblio_class = Test::MockModule->new('Koha::Biblio');
311 $biblio_class->mock( 'undef_result', sub { return; } );
313 $new_biblio_api = $new_biblio->to_api({ embed => ( { 'undef_result' => {} } ) });
314 ok( exists $new_biblio_api->{undef_result}, 'If a method returns undef, then the attribute is defined' );
315 is( $new_biblio_api->{undef_result}, undef, 'If a method returns undef, then the attribute is undef' );
317 $biblio_class->mock( 'items',
318 sub { return [ bless { itemnumber => 1 }, 'Somethings' ]; } );
321 $new_biblio_api = $new_biblio->to_api(
322 { embed => { 'items' => { children => { asd => {} } } } } );
324 'Koha::Exceptions::Exception',
325 "An exception is thrown if a blessed object to embed doesn't implement to_api";
329 "Asked to embed items but its return value doesn't implement to_api",
330 "Exception message correct"
334 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
335 $builder->build_object(
337 class => 'Koha::Holds',
339 biblionumber => $biblio->biblionumber,
340 borrowernumber => $patron->borrowernumber
344 $builder->build_object(
346 class => 'Koha::Holds',
348 biblionumber => $biblio->biblionumber,
349 borrowernumber => $patron->borrowernumber
354 my $patron_api = $patron->to_api(
356 embed => { holds_count => { is_count => 1 } }
359 is( $patron_api->{holds_count}, $patron->holds->count, 'Count embeds are supported and work as expected' );
363 $patron->to_api({ embed => { holds_count => {} } });
365 'Koha::Exceptions::Object::MethodNotCoveredByTests',
366 'Unknown method exception thrown if is_count not specified';
368 $schema->storage->txn_rollback;
371 subtest "to_api_mapping() tests" => sub {
375 $schema->storage->txn_begin;
377 my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
378 is_deeply( $illrequest->to_api_mapping, {}, 'If no to_api_mapping present, return empty hashref' );
380 $schema->storage->txn_rollback;
383 subtest "from_api_mapping() tests" => sub {
387 $schema->storage->txn_begin;
389 my $city = $builder->build_object({ class => 'Koha::Cities' });
391 # Lets emulate an undef
392 my $city_class = Test::MockModule->new('Koha::City');
393 $city_class->mock( 'to_api_mapping',
397 city_country => 'country',
398 city_zipcode => undef
404 $city->from_api_mapping,
407 country => 'city_country'
409 'Mapping returns correctly, undef ommited'
412 $city_class->unmock( 'to_api_mapping');
413 $city_class->mock( 'to_api_mapping',
417 city_country => 'country',
418 city_zipcode => 'postal_code'
424 $city->from_api_mapping,
427 country => 'city_country'
429 'Reverse mapping is cached'
433 $city = $builder->build_object({ class => 'Koha::Cities' });
435 $city->from_api_mapping,
438 country => 'city_country',
439 postal_code => 'city_zipcode'
441 'Fresh mapping loaded'
444 $schema->storage->txn_rollback;
447 subtest 'set_from_api() tests' => sub {
451 $schema->storage->txn_begin;
453 my $city = $builder->build_object({ class => 'Koha::Cities' });
454 my $city_unblessed = $city->unblessed;
457 country => 'Argentina',
458 postal_code => '5000'
460 $city->set_from_api($attrs);
462 is( $city->city_state, $city_unblessed->{city_state}, 'Untouched attributes are preserved' );
463 is( $city->city_name, $attrs->{name}, 'city_name updated correctly' );
464 is( $city->city_country, $attrs->{country}, 'city_country updated correctly' );
465 is( $city->city_zipcode, $attrs->{postal_code}, 'city_zipcode updated correctly' );
467 $schema->storage->txn_rollback;
470 subtest 'new_from_api() tests' => sub {
474 $schema->storage->txn_begin;
478 country => 'Argentina',
479 postal_code => '5000'
481 my $city = Koha::City->new_from_api($attrs);
483 is( ref($city), 'Koha::City', 'Object type is correct' );
484 is( $city->city_name, $attrs->{name}, 'city_name updated correctly' );
485 is( $city->city_country, $attrs->{country}, 'city_country updated correctly' );
486 is( $city->city_zipcode, $attrs->{postal_code}, 'city_zipcode updated correctly' );
488 $schema->storage->txn_rollback;
491 subtest 'attributes_from_api() tests' => sub {
495 my $patron = Koha::Patron->new();
497 my $attrs = $patron->attributes_from_api(
499 updated_on => '2019-12-27T14:53:00',
503 ok( exists $attrs->{updated_on},
504 'No translation takes place if no mapping' );
506 ref( $attrs->{updated_on} ),
508 'Given a string, a timestamp field is converted into a DateTime object'
511 $attrs = $patron->attributes_from_api(
513 last_seen => '2019-12-27T14:53:00'
517 ok( exists $attrs->{lastseen},
518 'Translation takes place because of the defined mapping' );
520 ref( $attrs->{lastseen} ),
522 'Given a string, a datetime field is converted into a DateTime object'
525 $attrs = $patron->attributes_from_api(
527 date_of_birth => '2019-12-27'
531 ok( exists $attrs->{dateofbirth},
532 'Translation takes place because of the defined mapping' );
534 ref( $attrs->{dateofbirth} ),
536 'Given a string, a date field is converted into a DateTime object'
541 $attrs = $patron->attributes_from_api(
543 date_of_birth => '20141205',
547 'Koha::Exceptions::BadParameter',
548 'Bad date throws an exception';
553 'Exception parameter is the API field name, not the DB one'
557 $attrs = $patron->attributes_from_api(
559 incorrect_address => Mojo::JSON->true,
560 patron_card_lost => Mojo::JSON->false,
564 ok( exists $attrs->{gonenoaddress}, 'Attribute gets translated' );
565 is( $attrs->{gonenoaddress}, 1, 'Boolean correctly translated to integer (true => 1)' );
566 ok( exists $attrs->{lost}, 'Attribute gets translated' );
567 is( $attrs->{lost}, 0, 'Boolean correctly translated to integer (false => 0)' );
570 subtest "Test update method" => sub {
573 $schema->storage->txn_begin;
575 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
576 my $library = Koha::Libraries->find( $branchcode );
577 $library->update({ branchname => 'New_Name', branchcity => 'AMS' });
578 is( $library->branchname, 'New_Name', 'Changed name with update' );
579 is( $library->branchcity, 'AMS', 'Changed city too' );
580 is( $library->is_changed, 0, 'Change should be stored already' );
583 branchcity => 'NYC', not_a_column => 53, branchname => 'Name3',
585 fail( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
587 ok( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
588 $library->discard_changes; #requery after failing update
590 # Check if the columns are not updated
591 is( $library->branchcity, 'AMS', 'First column not updated' );
592 is( $library->branchname, 'New_Name', 'Third column not updated' );
594 $schema->storage->txn_rollback;
597 subtest 'store() tests' => sub {
601 # Using Koha::ApiKey to test Koha::Object>-store
602 # Simple object with foreign keys and unique key
604 $schema->storage->txn_begin;
606 # Create a patron to make sure its ID doesn't exist on the DB
607 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
608 my $patron_id = $patron->id;
611 my $api_key = Koha::ApiKey->new({ patron_id => $patron_id, secret => 'a secret', description => 'a description' });
613 my $print_error = $schema->storage->dbh->{PrintError};
614 $schema->storage->dbh->{PrintError} = 0;
617 'Koha::Exceptions::Object::FKConstraint',
618 'Exception is thrown correctly';
621 "Broken FK constraint",
622 'Exception message is correct'
627 'Exception field is correct'
630 $patron = $builder->build_object({ class => 'Koha::Patrons' });
631 $api_key = $builder->build_object({ class => 'Koha::ApiKeys' });
633 my $new_api_key = Koha::ApiKey->new({
634 patron_id => $patron_id,
635 secret => $api_key->secret,
636 description => 'a description',
640 { $new_api_key->store }
641 'Koha::Exceptions::Object::DuplicateID',
642 'Exception is thrown correctly';
647 'Exception message is correct'
653 'Exception field is correct'
656 $schema->storage->dbh->{PrintError} = $print_error;
659 $api_key->set({ secret => 'Manuel' });
660 my $ret = $api_key->store;
661 is( ref($ret), 'Koha::ApiKey', 'store() returns the object on success' );
663 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
664 my $patron_category = $builder->build_object(
666 class => 'Koha::Patron::Categories',
667 value => { category_type => 'P', enrolmentfee => 0 }
674 categorycode => $patron_category->categorycode,
675 branchcode => $library->branchcode,
676 dateofbirth => "", # date will be set to NULL
677 sms_provider_id => "", # Integer will be set to NULL
678 privacy => "", # privacy cannot be NULL but has a default value
682 is( $@, '', 'No error should be raised by ->store if empty strings are passed' );
683 is( $patron->privacy, 1, 'Default value for privacy should be set to 1' );
684 is( $patron->dateofbirth, undef, 'dateofbirth must have been set to undef');
685 is( $patron->sms_provider_id, undef, 'sms_provider_id must have been set to undef');
687 my $itemtype = eval {
690 itemtype => 'IT4test',
697 is( $@, '', 'No error should be raised by ->store if empty strings are passed' );
698 is( $itemtype->rentalcharge, undef, 'decimal DEFAULT NULL should default to null');
699 is( $itemtype->notforloan, undef, 'int DEFAULT NULL should default to null');
700 is( $itemtype->hideinopac, 0, 'int NOT NULL DEFAULT 0 should default to 0');
702 subtest 'Bad value tests' => sub {
706 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
708 my $print_error = $schema->storage->dbh->{PrintError};
709 $schema->storage->dbh->{PrintError} = 0;
712 $patron->lastseen('wrong_value')->store;
714 ok( $_->isa('Koha::Exceptions::Object::BadValue'), 'Exception thrown correctly' );
715 like( $_->property, qr/(borrowers\.)?lastseen/, 'Column should be the expected one' ); # The table name is not always displayed, it depends on the DBMS version
716 is( $_->value, 'wrong_value', 'Value should be the expected one' );
719 $schema->storage->dbh->{PrintError} = $print_error;
722 $schema->storage->txn_rollback;
725 subtest 'unblessed_all_relateds' => sub {
728 $schema->storage->txn_begin;
730 # FIXME It's very painful to create an issue in tests!
731 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
732 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
734 my $patron_category = $builder->build(
736 source => 'Category',
738 category_type => 'P',
740 BlockExpiredPatronOpacActions => -1, # Pick the pref value
745 firstname => 'firstname',
746 surname => 'surname',
747 categorycode => $patron_category->{categorycode},
748 branchcode => $library->branchcode,
750 my $patron = Koha::Patron->new($patron_data)->store;
751 my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
752 my $biblio = Koha::Biblios->find( $biblionumber );
753 my $item = $builder->build_object(
755 class => 'Koha::Items',
757 homebranch => $library->branchcode,
758 holdingbranch => $library->branchcode,
759 biblionumber => $biblio->biblionumber,
766 my $issue = AddIssue( $patron->unblessed, $item->barcode, DateTime->now->subtract( days => 1 ) );
767 my $overdues = Koha::Patrons->find( $patron->id )->get_overdues; # Koha::Patron->get_overdue prefetches
768 my $overdue = $overdues->next->unblessed_all_relateds;
769 is( $overdue->{issue_id}, $issue->issue_id, 'unblessed_all_relateds has field from the original table (issues)' );
770 is( $overdue->{title}, $biblio->title, 'unblessed_all_relateds has field from other tables (biblio)' );
771 is( $overdue->{homebranch}, $item->homebranch, 'unblessed_all_relateds has field from other tables (items)' );
773 $schema->storage->txn_rollback;
776 subtest 'get_from_storage' => sub {
779 $schema->storage->txn_begin;
781 my $biblio = $builder->build_sample_biblio;
783 my $old_title = $biblio->title;
784 my $new_title = 'new_title';
785 Koha::Biblios->find( $biblio->biblionumber )->title($new_title)->store;
787 is( $biblio->title, $old_title, 'current $biblio should not be modified' );
788 is( $biblio->get_from_storage->title,
789 $new_title, 'get_from_storage should return an updated object' );
791 Koha::Biblios->find( $biblio->biblionumber )->delete;
792 is( ref($biblio), 'Koha::Biblio', 'current $biblio should not be deleted' );
793 is( $biblio->get_from_storage, undef,
794 'get_from_storage should return undef if the object has been deleted' );
796 $schema->storage->txn_rollback;