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 => 11;
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;
41 use_ok('Koha::Object');
42 use_ok('Koha::Patron');
45 my $schema = Koha::Database->new->schema;
46 my $builder = t::lib::TestBuilder->new();
48 subtest 'is_changed / make_column_dirty' => sub {
51 $schema->storage->txn_begin;
53 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
54 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
56 my $object = Koha::Patron->new();
57 $object->categorycode( $categorycode );
58 $object->branchcode( $branchcode );
59 $object->surname("Test Surname");
61 is( $object->is_changed(), 0, "Object is unchanged" );
62 $object->surname("Test Surname");
63 is( $object->is_changed(), 0, "Object is still unchanged" );
64 $object->surname("Test Surname 2");
65 is( $object->is_changed(), 1, "Object is changed" );
68 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
70 $object->set({ firstname => 'Test Firstname' });
71 is( $object->is_changed(), 1, "Object is changed after Set" );
73 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
75 # Test make_column_dirty
76 is( $object->make_column_dirty('firstname'), '', 'make_column_dirty returns empty string on success' );
77 is( $object->make_column_dirty('firstname'), 1, 'make_column_dirty returns 1 if already dirty' );
78 is( $object->is_changed, 1, "Object is changed after make dirty" );
80 is( $object->is_changed, 0, "Store clears dirty mark" );
81 $object->make_column_dirty('firstname');
82 $object->discard_changes;
83 is( $object->is_changed, 0, "Discard clears dirty mark too" );
85 $schema->storage->txn_rollback;
88 subtest 'in_storage' => sub {
91 $schema->storage->txn_begin;
93 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
94 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
96 my $object = Koha::Patron->new();
97 is( $object->in_storage, 0, "Object is not in storage" );
98 $object->categorycode( $categorycode );
99 $object->branchcode( $branchcode );
100 $object->surname("Test Surname");
102 is( $object->in_storage, 1, "Object is now stored" );
103 $object->surname("another surname");
104 is( $object->in_storage, 1 );
106 my $borrowernumber = $object->borrowernumber;
107 my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
108 is( $patron->surname(), "Test Surname", "Object found in database" );
111 $patron = $schema->resultset('Borrower')->find( $borrowernumber );
112 ok( ! $patron, "Object no longer found in database" );
113 is( $object->in_storage, 0, "Object is not in storage" );
115 $schema->storage->txn_rollback;
118 subtest 'id' => sub {
121 $schema->storage->txn_begin;
123 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
124 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
126 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
127 is( $patron->id, $patron->borrowernumber );
129 $schema->storage->txn_rollback;
132 subtest 'get_column' => sub {
135 $schema->storage->txn_begin;
137 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
138 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
140 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
141 is( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
143 $schema->storage->txn_rollback;
146 subtest 'discard_changes' => sub {
149 $schema->storage->txn_begin;
151 my $patron = $builder->build( { source => 'Borrower' } );
152 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
153 $patron->dateexpiry(dt_from_string);
154 $patron->discard_changes;
156 dt_from_string( $patron->dateexpiry ),
157 dt_from_string->truncate( to => 'day' ),
158 'discard_changes should refresh the object'
161 $schema->storage->txn_rollback;
164 subtest 'TO_JSON tests' => sub {
168 $schema->storage->txn_begin;
170 my $dt = dt_from_string();
171 my $borrowernumber = $builder->build(
172 { source => 'Borrower',
173 value => { lost => 1,
174 sms_provider_id => undef,
177 lastseen => $dt, } })->{borrowernumber};
179 my $patron = Koha::Patrons->find($borrowernumber);
180 my $lost = $patron->TO_JSON()->{lost};
181 my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
182 my $updated_on = $patron->TO_JSON->{updated_on};
183 my $lastseen = $patron->TO_JSON->{lastseen};
185 ok( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
186 is( $lost, 1, 'Boolean attribute value is correct (true)' );
188 ok( $gonenoaddress->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
189 is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
191 is( $patron->TO_JSON->{sms_provider_id}, undef, 'Undef values should not be casted to 0' );
193 ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
195 my $rfc3999_regex = qr/
207 (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))
209 like( $updated_on, $rfc3999_regex, "Date-time $updated_on formatted correctly");
210 like( $lastseen, $rfc3999_regex, "Date-time $updated_on formatted correctly");
212 $schema->storage->txn_rollback;
215 subtest "Test update method" => sub {
218 $schema->storage->txn_begin;
220 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
221 my $library = Koha::Libraries->find( $branchcode );
222 $library->update({ branchname => 'New_Name', branchcity => 'AMS' });
223 is( $library->branchname, 'New_Name', 'Changed name with update' );
224 is( $library->branchcity, 'AMS', 'Changed city too' );
225 is( $library->is_changed, 0, 'Change should be stored already' );
228 branchcity => 'NYC', not_a_column => 53, branchname => 'Name3',
230 fail( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
232 ok( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
233 $library->discard_changes; #requery after failing update
235 # Check if the columns are not updated
236 is( $library->branchcity, 'AMS', 'First column not updated' );
237 is( $library->branchname, 'New_Name', 'Third column not updated' );
239 $schema->storage->txn_rollback;
242 subtest 'store() tests' => sub {
246 # Using Koha::ApiKey to test Koha::Object>-store
247 # Simple object with foreign keys and unique key
249 $schema->storage->txn_begin;
251 # Create a patron to make sure its ID doesn't exist on the DB
252 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
253 my $patron_id = $patron->id;
256 my $api_key = Koha::ApiKey->new({ patron_id => $patron_id });
258 my $print_error = $schema->storage->dbh->{PrintError};
259 $schema->storage->dbh->{PrintError} = 0;
262 'Koha::Exceptions::Object::FKConstraint',
263 'Exception is thrown correctly';
266 "Broken FK constraint",
267 'Exception message is correct'
272 'Exception field is correct'
275 $patron = $builder->build_object({ class => 'Koha::Patrons' });
276 $api_key = $builder->build_object({ class => 'Koha::ApiKeys' });
278 my $new_api_key = Koha::ApiKey->new({
279 patron_id => $patron_id,
280 secret => $api_key->secret,
284 { $new_api_key->store }
285 'Koha::Exceptions::Object::DuplicateID',
286 'Exception is thrown correctly';
291 'Exception message is correct'
297 'Exception field is correct'
300 $schema->storage->dbh->{PrintError} = $print_error;
303 $api_key->set({ secret => 'Manuel' });
304 my $ret = $api_key->store;
305 is( ref($ret), 'Koha::ApiKey', 'store() returns the object on success' );
307 $schema->storage->txn_rollback;
310 subtest 'unblessed_all_relateds' => sub {
313 $schema->storage->txn_begin;
315 # FIXME It's very painful to create an issue in tests!
316 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
317 C4::Context->_new_userenv('xxx');
318 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Midway Public Library', '', '', '');
319 my $patron_category = $builder->build(
321 source => 'Category',
323 category_type => 'P',
325 BlockExpiredPatronOpacActions => -1, # Pick the pref value
330 firstname => 'firstname',
331 surname => 'surname',
332 categorycode => $patron_category->{categorycode},
333 branchcode => $library->branchcode,
335 my $patron = Koha::Patron->new($patron_data)->store;
336 my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
337 my $biblio = Koha::Biblios->find( $biblionumber );
338 my $item = $builder->build_object(
340 class => 'Koha::Items',
342 homebranch => $library->branchcode,
343 holdingbranch => $library->branchcode,
344 biblionumber => $biblio->biblionumber,
351 my $issue = AddIssue( $patron->unblessed, $item->barcode, DateTime->now->subtract( days => 1 ) );
352 my $overdues = Koha::Patrons->find( $patron->id )->get_overdues; # Koha::Patron->get_overdue prefetches
353 my $overdue = $overdues->next->unblessed_all_relateds;
354 is( $overdue->{issue_id}, $issue->issue_id, 'unblessed_all_relateds has field from the original table (issues)' );
355 is( $overdue->{title}, $biblio->title, 'unblessed_all_relateds has field from other tables (biblio)' );
356 is( $overdue->{homebranch}, $item->homebranch, 'unblessed_all_relateds has field from other tables (items)' );
358 $schema->storage->txn_rollback;