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 => 21;
26 use C4::Circulation qw( AddIssue );
27 use C4::Biblio qw( AddBiblio );
31 use Koha::Acquisition::Orders;
32 use Koha::DateUtils qw( dt_from_string );
35 use Koha::Library::Groups;
38 use Scalar::Util qw( isvstring );
41 use t::lib::TestBuilder;
45 use_ok('Koha::Object');
46 use_ok('Koha::Patron');
49 my $schema = Koha::Database->new->schema;
50 my $builder = t::lib::TestBuilder->new();
52 subtest 'is_changed / make_column_dirty' => sub {
55 $schema->storage->txn_begin;
57 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
58 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
60 my $object = Koha::Patron->new();
61 $object->categorycode( $categorycode );
62 $object->branchcode( $branchcode );
63 $object->surname("Test Surname");
64 $object->store->discard_changes;
65 is( $object->is_changed(), 0, "Object is unchanged" );
66 $object->surname("Test Surname");
67 is( $object->is_changed(), 0, "Object is still unchanged" );
68 $object->surname("Test Surname 2");
69 is( $object->is_changed(), 1, "Object is changed" );
72 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
74 $object->set({ firstname => 'Test Firstname' });
75 is( $object->is_changed(), 1, "Object is changed after Set" );
77 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
79 # Test make_column_dirty
80 is( $object->make_column_dirty('firstname'), '', 'make_column_dirty returns empty string on success' );
81 is( $object->make_column_dirty('firstname'), 1, 'make_column_dirty returns 1 if already dirty' );
82 is( $object->is_changed, 1, "Object is changed after make dirty" );
84 is( $object->is_changed, 0, "Store clears dirty mark" );
85 $object->make_column_dirty('firstname');
86 $object->discard_changes;
87 is( $object->is_changed, 0, "Discard clears dirty mark too" );
89 $schema->storage->txn_rollback;
92 subtest 'in_storage' => sub {
95 $schema->storage->txn_begin;
97 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
98 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
100 my $object = Koha::Patron->new();
101 is( $object->in_storage, 0, "Object is not in storage" );
102 $object->categorycode( $categorycode );
103 $object->branchcode( $branchcode );
104 $object->surname("Test Surname");
106 is( $object->in_storage, 1, "Object is now stored" );
107 $object->surname("another surname");
108 is( $object->in_storage, 1 );
110 my $borrowernumber = $object->borrowernumber;
111 my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
112 is( $patron->surname(), "Test Surname", "Object found in database" );
115 $patron = $schema->resultset('Borrower')->find( $borrowernumber );
116 ok( ! $patron, "Object no longer found in database" );
117 is( $object->in_storage, 0, "Object is not in storage" );
119 $schema->storage->txn_rollback;
122 subtest 'id' => sub {
125 $schema->storage->txn_begin;
127 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
128 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
130 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
131 is( $patron->id, $patron->borrowernumber );
133 $schema->storage->txn_rollback;
136 subtest 'get_column' => sub {
139 $schema->storage->txn_begin;
141 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
142 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
144 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
145 is( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
147 $schema->storage->txn_rollback;
150 subtest 'discard_changes' => sub {
153 $schema->storage->txn_begin;
155 my $patron = $builder->build( { source => 'Borrower' } );
156 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
157 $patron->dateexpiry(dt_from_string);
158 $patron->discard_changes;
160 dt_from_string( $patron->dateexpiry ),
161 dt_from_string->truncate( to => 'day' ),
162 'discard_changes should refresh the object'
165 $schema->storage->txn_rollback;
168 subtest 'TO_JSON tests' => sub {
172 $schema->storage->txn_begin;
174 my $dt = dt_from_string();
175 my $borrowernumber = $builder->build(
176 { source => 'Borrower',
177 value => { lost => 1,
178 sms_provider_id => undef,
181 lastseen => $dt, } })->{borrowernumber};
183 my $patron = Koha::Patrons->find($borrowernumber);
184 my $lost = $patron->TO_JSON()->{lost};
185 my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
186 my $updated_on = $patron->TO_JSON->{updated_on};
187 my $lastseen = $patron->TO_JSON->{lastseen};
189 ok( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
190 is( $lost, 1, 'Boolean attribute value is correct (true)' );
192 ok( $gonenoaddress->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
193 is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
195 is( $patron->TO_JSON->{sms_provider_id}, undef, 'Undef values should not be casted to 0' );
197 ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
199 my $rfc3999_regex = qr/
211 (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))
213 like( $updated_on, $rfc3999_regex, "Date-time $updated_on formatted correctly");
214 like( $lastseen, $rfc3999_regex, "Date-time $updated_on formatted correctly");
216 # Test JSON doesn't receive strings
217 my $order = $builder->build_object({ class => 'Koha::Acquisition::Orders' });
218 $order = Koha::Acquisition::Orders->find( $order->ordernumber );
219 is_deeply( $order->TO_JSON, decode_json( encode_json( $order->TO_JSON ) ), 'Orders are similar' );
221 $schema->storage->txn_rollback;
224 subtest "to_api() tests" => sub {
228 $schema->storage->txn_begin;
230 my $city = $builder->build_object({ class => 'Koha::Cities' });
233 # cityid => 'city_id',
234 # city_country => 'country',
235 # city_name => 'name',
236 # city_state => 'state',
237 # city_zipcode => 'postal_code'
239 my $api_city = $city->to_api;
241 is( $api_city->{city_id}, $city->cityid, 'Attribute translated correctly' );
242 is( $api_city->{country}, $city->city_country, 'Attribute translated correctly' );
243 is( $api_city->{name}, $city->city_name, 'Attribute translated correctly' );
244 is( $api_city->{state}, $city->city_state, 'Attribute translated correctly' );
245 is( $api_city->{postal_code}, $city->city_zipcode, 'Attribute translated correctly' );
247 # Lets emulate an undef
248 my $city_class = Test::MockModule->new('Koha::City');
249 $city_class->mock( 'to_api_mapping',
253 city_country => 'country',
255 city_state => 'state',
256 city_zipcode => undef
261 $api_city = $city->to_api;
263 is( $api_city->{city_id}, $city->cityid, 'Attribute translated correctly' );
264 is( $api_city->{country}, $city->city_country, 'Attribute translated correctly' );
265 is( $api_city->{name}, $city->city_name, 'Attribute translated correctly' );
266 is( $api_city->{state}, $city->city_state, 'Attribute translated correctly' );
267 ok( !exists $api_city->{postal_code}, 'Attribute removed' );
269 # Pick a class that won't have a mapping for the API
270 my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
271 is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no overloaded to_api_mapping method, return TO_JSON' );
273 my $biblio = $builder->build_sample_biblio();
274 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
275 my $hold = $builder->build_object({ class => 'Koha::Holds', value => { itemnumber => $item->itemnumber } });
277 my $embeds = { 'items' => {} };
279 my $biblio_api = $biblio->to_api({ embed => $embeds });
281 ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
282 is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item matches');
283 ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
295 $biblio_api = $biblio->to_api({ embed => $embeds });
297 ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
298 is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item still matches');
299 ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
300 is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches');
301 is_deeply($biblio_api->{biblioitem}, $biblio->biblioitem->to_api, 'More than one root');
303 my $hold_api = $hold->to_api(
305 embed => { 'item' => {} }
309 is( ref($hold_api->{item}), 'HASH', 'Single nested object works correctly' );
310 is( $hold_api->{item}->{item_id}, $item->itemnumber, 'Object embedded correctly' );
312 # biblio with no items
313 my $new_biblio = $builder->build_sample_biblio;
314 my $new_biblio_api = $new_biblio->to_api({ embed => $embeds });
316 is_deeply( $new_biblio_api->{items}, [], 'Empty list if no items' );
318 my $biblio_class = Test::MockModule->new('Koha::Biblio');
319 $biblio_class->mock( 'undef_result', sub { return; } );
321 $new_biblio_api = $new_biblio->to_api({ embed => ( { 'undef_result' => {} } ) });
322 ok( exists $new_biblio_api->{undef_result}, 'If a method returns undef, then the attribute is defined' );
323 is( $new_biblio_api->{undef_result}, undef, 'If a method returns undef, then the attribute is undef' );
325 $biblio_class->mock( 'items',
326 sub { return [ bless { itemnumber => 1 }, 'Somethings' ]; } );
329 $new_biblio_api = $new_biblio->to_api(
330 { embed => { 'items' => { children => { asd => {} } } } } );
333 "An exception is thrown if a blessed object to embed doesn't implement to_api";
337 "Asked to embed items but its return value doesn't implement to_api",
338 "Exception message correct"
342 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
343 $builder->build_object(
345 class => 'Koha::Holds',
347 biblionumber => $biblio->biblionumber,
348 borrowernumber => $patron->borrowernumber
352 $builder->build_object(
354 class => 'Koha::Holds',
356 biblionumber => $biblio->biblionumber,
357 borrowernumber => $patron->borrowernumber
362 my $patron_api = $patron->to_api(
364 embed => { holds_count => { is_count => 1 } }
367 is( $patron_api->{holds_count}, $patron->holds->count, 'Count embeds are supported and work as expected' );
371 $patron->to_api({ embed => { holds_count => {} } });
373 'Koha::Exceptions::Object::MethodNotCoveredByTests',
374 'Unknown method exception thrown if is_count not specified';
376 subtest 'unprivileged request tests' => sub {
378 my @all_attrs = Koha::Libraries->columns();
379 my $public_attrs = { map { $_ => 1 } @{ Koha::Library->public_read_list() } };
380 my $mapping = Koha::Library->to_api_mapping;
382 plan tests => scalar @all_attrs * 2;
384 # Create a sample library
385 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
387 my $unprivileged_representation = $library->to_api({ public => 1 });
388 my $privileged_representation = $library->to_api;
390 foreach my $attr (@all_attrs) {
391 my $mapped = exists $mapping->{$attr} ? $mapping->{$attr} : $attr;
392 if ( defined($mapped) ) {
394 exists $privileged_representation->{$mapped},
395 "Attribute '$attr' is present when privileged"
397 if ( exists $public_attrs->{$attr} ) {
399 exists $unprivileged_representation->{$mapped},
400 "Attribute '$attr' is present when public"
405 !exists $unprivileged_representation->{$mapped},
406 "Attribute '$attr' is not present when public"
412 !exists $privileged_representation->{$attr},
413 "Unmapped attribute '$attr' is not present when privileged"
416 !exists $unprivileged_representation->{$attr},
417 "Unmapped attribute '$attr' is not present when public"
423 $schema->storage->txn_rollback;
426 subtest "to_api_mapping() tests" => sub {
430 $schema->storage->txn_begin;
432 my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
433 is_deeply( $illrequest->to_api_mapping, {}, 'If no to_api_mapping present, return empty hashref' );
435 $schema->storage->txn_rollback;
438 subtest "from_api_mapping() tests" => sub {
442 $schema->storage->txn_begin;
444 my $city = $builder->build_object({ class => 'Koha::Cities' });
446 # Lets emulate an undef
447 my $city_class = Test::MockModule->new('Koha::City');
448 $city_class->mock( 'to_api_mapping',
452 city_country => 'country',
453 city_zipcode => undef
459 $city->from_api_mapping,
462 country => 'city_country'
464 'Mapping returns correctly, undef ommited'
467 $city_class->unmock( 'to_api_mapping');
468 $city_class->mock( 'to_api_mapping',
472 city_country => 'country',
473 city_zipcode => 'postal_code'
479 $city->from_api_mapping,
482 country => 'city_country'
484 'Reverse mapping is cached'
488 $city = $builder->build_object({ class => 'Koha::Cities' });
490 $city->from_api_mapping,
493 country => 'city_country',
494 postal_code => 'city_zipcode'
496 'Fresh mapping loaded'
499 $city_class->unmock( 'to_api_mapping');
500 $city_class->mock( 'to_api_mapping', undef );
503 $city = $builder->build_object({ class => 'Koha::Cities' });
505 $city->from_api_mapping,
507 'No to_api_mapping then empty hashref'
510 $city_class->unmock( 'to_api_mapping');
511 $city_class->mock( 'to_api_mapping', sub { return; } );
514 $city = $builder->build_object({ class => 'Koha::Cities' });
516 $city->from_api_mapping,
518 'Empty to_api_mapping then empty hashref'
521 $schema->storage->txn_rollback;
524 subtest 'set_from_api() tests' => sub {
528 $schema->storage->txn_begin;
530 my $city = $builder->build_object({ class => 'Koha::Cities' });
531 my $city_unblessed = $city->unblessed;
534 country => 'Argentina',
535 postal_code => '5000'
537 $city->set_from_api($attrs);
539 is( $city->city_state, $city_unblessed->{city_state}, 'Untouched attributes are preserved' );
540 is( $city->city_name, $attrs->{name}, 'city_name updated correctly' );
541 is( $city->city_country, $attrs->{country}, 'city_country updated correctly' );
542 is( $city->city_zipcode, $attrs->{postal_code}, 'city_zipcode updated correctly' );
544 $schema->storage->txn_rollback;
547 subtest 'new_from_api() tests' => sub {
551 $schema->storage->txn_begin;
555 country => 'Argentina',
556 postal_code => '5000'
558 my $city = Koha::City->new_from_api($attrs);
560 is( ref($city), 'Koha::City', 'Object type is correct' );
561 is( $city->city_name, $attrs->{name}, 'city_name updated correctly' );
562 is( $city->city_country, $attrs->{country}, 'city_country updated correctly' );
563 is( $city->city_zipcode, $attrs->{postal_code}, 'city_zipcode updated correctly' );
565 $schema->storage->txn_rollback;
568 subtest 'attributes_from_api() tests' => sub {
572 subtest 'date and date-time handling tests' => sub {
576 my $patron = Koha::Patron->new();
578 my $attrs = $patron->attributes_from_api(
580 updated_on => '2019-12-27T14:53:00Z',
581 last_seen => '2019-12-27T14:53:00Z',
582 date_of_birth => '2019-12-27',
586 ok( exists $attrs->{updated_on},
587 'No translation takes place if no mapping' );
589 $attrs->{updated_on},
590 '2019-12-27 14:53:00',
591 'Given an rfc3339 formatted datetime string, a timestamp field is converted into an SQL formatted datetime string'
594 ok( exists $attrs->{lastseen},
595 'Translation takes place because of the defined mapping' );
598 '2019-12-27 14:53:00',
599 'Given an rfc3339 formatted datetime string, a datetime field is converted into an SQL formatted datetime string'
602 ok( exists $attrs->{dateofbirth},
603 'Translation takes place because of the defined mapping' );
605 $attrs->{dateofbirth},
607 'Given an rfc3339 formatted date string, a date field is converted into an SQL formatted date string'
610 $attrs = $patron->attributes_from_api(
613 date_of_birth => undef,
617 ok( exists $attrs->{lastseen},
618 'undef parameter is not skipped (Bug 29157)' );
622 'Given undef, a datetime field is set to undef (Bug 29157)'
625 ok( exists $attrs->{dateofbirth},
626 'undef parameter is not skipped (Bug 29157)' );
628 $attrs->{dateofbirth},
630 'Given undef, a date field is set to undef (Bug 29157)'
635 $attrs = $patron->attributes_from_api(
637 date_of_birth => '20141205',
641 'Koha::Exceptions::BadParameter',
642 'Bad date throws an exception';
647 'Exception parameter is the API field name, not the DB one'
651 subtest 'booleans handling tests' => sub {
655 my $patron = Koha::Patron->new;
657 my $attrs = $patron->attributes_from_api(
659 incorrect_address => Mojo::JSON->true,
660 patron_card_lost => Mojo::JSON->false,
664 ok( exists $attrs->{gonenoaddress}, 'Attribute gets translated' );
665 is( $attrs->{gonenoaddress}, 1, 'Boolean correctly translated to integer (true => 1)' );
666 ok( exists $attrs->{lost}, 'Attribute gets translated' );
667 is( $attrs->{lost}, 0, 'Boolean correctly translated to integer (false => 0)' );
671 subtest "Test update method" => sub {
674 $schema->storage->txn_begin;
676 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
677 my $library = Koha::Libraries->find( $branchcode );
678 $library->update({ branchname => 'New_Name', branchcity => 'AMS' });
679 is( $library->branchname, 'New_Name', 'Changed name with update' );
680 is( $library->branchcity, 'AMS', 'Changed city too' );
681 is( $library->is_changed, 0, 'Change should be stored already' );
684 branchcity => 'NYC', not_a_column => 53, branchname => 'Name3',
686 fail( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
688 ok( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
689 $library->discard_changes; #requery after failing update
691 # Check if the columns are not updated
692 is( $library->branchcity, 'AMS', 'First column not updated' );
693 is( $library->branchname, 'New_Name', 'Third column not updated' );
695 $schema->storage->txn_rollback;
698 subtest 'store() tests' => sub {
702 # Using Koha::Library::Groups to test Koha::Object>-store
703 # Simple object with foreign keys and unique key
705 $schema->storage->txn_begin;
707 # Create a library to make sure its ID doesn't exist on the DB
708 my $library = $builder->build_object({ class => 'Koha::Libraries' });
709 my $branchcode = $library->branchcode;
712 my $library_group = Koha::Library::Group->new(
714 branchcode => $library->branchcode,
719 my $dbh = $schema->storage->dbh;
722 open STDERR, '>', '/dev/null';
724 { $library_group->store }
725 'Koha::Exceptions::Object::FKConstraint',
726 'Exception is thrown correctly';
729 "Broken FK constraint",
730 'Exception message is correct'
735 'Exception field is correct'
738 $library_group = $builder->build_object({ class => 'Koha::Library::Groups' });
740 my $new_library_group = Koha::Library::Group->new(
742 branchcode => $library_group->branchcode,
743 title => $library_group->title,
748 { $new_library_group->store }
749 'Koha::Exceptions::Object::DuplicateID',
750 'Exception is thrown correctly';
755 'Exception message is correct'
760 qr/(library_groups\.)?title/,
761 'Exception field is correct (note that MySQL 8 is displaying the tablename)'
767 $library_group->set({ title => 'Manuel' });
768 my $ret = $library_group->store;
769 is( ref($ret), 'Koha::Library::Group', 'store() returns the object on success' );
771 $library = $builder->build_object( { class => 'Koha::Libraries' } );
772 my $patron_category = $builder->build_object(
774 class => 'Koha::Patron::Categories',
775 value => { category_type => 'P', enrolmentfee => 0 }
782 categorycode => $patron_category->categorycode,
783 branchcode => $library->branchcode,
784 dateofbirth => "", # date will be set to NULL
785 sms_provider_id => "", # Integer will be set to NULL
786 privacy => "", # privacy cannot be NULL but has a default value
790 is( $@, '', 'No error should be raised by ->store if empty strings are passed' );
791 is( $patron->privacy, 1, 'Default value for privacy should be set to 1' );
792 is( $patron->dateofbirth, undef, 'dateofbirth must have been set to undef');
793 is( $patron->sms_provider_id, undef, 'sms_provider_id must have been set to undef');
795 my $itemtype = eval {
798 itemtype => 'IT4test',
805 is( $@, '', 'No error should be raised by ->store if empty strings are passed' );
806 is( $itemtype->rentalcharge, undef, 'decimal DEFAULT NULL should default to null');
807 is( $itemtype->notforloan, undef, 'int DEFAULT NULL should default to null');
808 is( $itemtype->hideinopac, 0, 'int NOT NULL DEFAULT 0 should default to 0');
810 subtest 'Bad value tests' => sub {
814 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
819 open STDERR, '>', '/dev/null';
820 $patron->lastseen('wrong_value')->store;
823 ok( $_->isa('Koha::Exceptions::Object::BadValue'), 'Exception thrown correctly' );
824 like( $_->property, qr/(borrowers\.)?lastseen/, 'Column should be the expected one' ); # The table name is not always displayed, it depends on the DBMS version
825 is( $_->value, 'wrong_value', 'Value should be the expected one' );
829 $schema->storage->txn_rollback;
832 subtest 'unblessed_all_relateds' => sub {
835 $schema->storage->txn_begin;
837 # FIXME It's very painful to create an issue in tests!
838 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
839 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
841 my $patron_category = $builder->build(
843 source => 'Category',
845 category_type => 'P',
847 BlockExpiredPatronOpacActions => -1, # Pick the pref value
852 firstname => 'firstname',
853 surname => 'surname',
854 categorycode => $patron_category->{categorycode},
855 branchcode => $library->branchcode,
857 my $patron = Koha::Patron->new($patron_data)->store;
858 my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
859 my $biblio = Koha::Biblios->find( $biblionumber );
860 my $item = $builder->build_object(
862 class => 'Koha::Items',
864 homebranch => $library->branchcode,
865 holdingbranch => $library->branchcode,
866 biblionumber => $biblio->biblionumber,
873 my $issue = AddIssue( $patron->unblessed, $item->barcode, DateTime->now->subtract( days => 1 ) );
874 my $overdues = Koha::Patrons->find( $patron->id )->get_overdues; # Koha::Patron->get_overdue prefetches
875 my $overdue = $overdues->next->unblessed_all_relateds;
876 is( $overdue->{issue_id}, $issue->issue_id, 'unblessed_all_relateds has field from the original table (issues)' );
877 is( $overdue->{title}, $biblio->title, 'unblessed_all_relateds has field from other tables (biblio)' );
878 is( $overdue->{homebranch}, $item->homebranch, 'unblessed_all_relateds has field from other tables (items)' );
880 $schema->storage->txn_rollback;
883 subtest 'get_from_storage' => sub {
886 $schema->storage->txn_begin;
888 my $biblio = $builder->build_sample_biblio;
890 my $old_title = $biblio->title;
891 my $new_title = 'new_title';
892 Koha::Biblios->find( $biblio->biblionumber )->title($new_title)->store;
894 is( $biblio->title, $old_title, 'current $biblio should not be modified' );
895 is( $biblio->get_from_storage->title,
896 $new_title, 'get_from_storage should return an updated object' );
898 Koha::Biblios->find( $biblio->biblionumber )->delete;
899 is( ref($biblio), 'Koha::Biblio', 'current $biblio should not be deleted' );
900 is( $biblio->get_from_storage, undef,
901 'get_from_storage should return undef if the object has been deleted' );
903 $schema->storage->txn_rollback;
906 subtest 'prefetch_whitelist() tests' => sub {
910 $schema->storage->txn_begin;
912 my $biblio = Koha::Biblio->new;
914 my $prefetch_whitelist = $biblio->prefetch_whitelist;
917 exists $prefetch_whitelist->{orders},
918 'Relationship matching method name is listed'
921 $prefetch_whitelist->{orders},
922 'Koha::Acquisition::Order',
923 'Guessed the non-standard object class correctly'
927 $prefetch_whitelist->{items},
929 'Guessed the standard object class correctly'
932 $schema->storage->txn_rollback;
935 subtest 'set_or_blank' => sub {
939 $schema->storage->txn_begin;
941 my $item = $builder->build_sample_item;
942 my $item_info = $item->unblessed;
943 $item = $item->set_or_blank($item_info);
944 is_deeply($item->unblessed, $item_info, 'set_or_blank assign the correct value if unchanged');
947 delete $item_info->{itemlost};
948 $item = $item->set_or_blank($item_info);
949 is($item->itemlost, 0, 'set_or_blank should have set itemlost to 0, default value defined in DB');
952 delete $item_info->{restricted};
953 $item = $item->set_or_blank($item_info);
954 is($item->restricted, undef, 'set_or_blank should have set restristed to null' );
957 delete $item_info->{dateaccessioned};
958 $item = $item->set_or_blank($item_info);
959 is($item->dateaccessioned, undef, 'set_or_blank should have set dateaccessioned to null');
962 delete $item_info->{timestamp};
963 $item = $item->set_or_blank($item_info);
964 isnt($item->timestamp, undef, 'set_or_blank should have set timestamp to a correct value');
966 $schema->storage->txn_rollback;
969 subtest 'messages() and add_message() tests' => sub {
973 $schema->storage->txn_begin;
975 my $patron = Koha::Patron->new;
977 my @messages = @{ $patron->object_messages };
978 is( scalar @messages, 0, 'No messages' );
980 $patron->add_message({ message => "message_1" });
981 $patron->add_message({ message => "message_2" });
983 @messages = @{ $patron->object_messages };
985 is( scalar @messages, 2, 'Messages are returned' );
986 is( ref($messages[0]), 'Koha::Object::Message', 'Right type returned' );
987 is( ref($messages[1]), 'Koha::Object::Message', 'Right type returned' );
988 is( $messages[0]->message, 'message_1', 'Right message recorded' );
990 my $patron_id = $builder->build_object({ class => 'Koha::Patrons' })->id;
991 # get a patron from the DB, ->new is not called, ->object_messages should initialize _messages as an empty arrayref
992 $patron = Koha::Patrons->find( $patron_id );
994 isnt( $patron->object_messages, undef, '->messages initializes the array if required' );
995 is( scalar @{ $patron->object_messages }, 0, '->messages returns an empty arrayref' );
997 $schema->storage->txn_rollback;