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::Biblio; # AddBiblio
27 use C4::Circulation; # AddIssue
29 use Koha::DateUtils qw( dt_from_string );
33 use Scalar::Util qw( isvstring );
36 use t::lib::TestBuilder;
39 use_ok('Koha::Object');
40 use_ok('Koha::Patron');
43 my $schema = Koha::Database->new->schema;
44 my $builder = t::lib::TestBuilder->new();
46 subtest 'is_changed / make_column_dirty' => sub {
49 $schema->storage->txn_begin;
51 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
52 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
54 my $object = Koha::Patron->new();
55 $object->categorycode( $categorycode );
56 $object->branchcode( $branchcode );
57 $object->surname("Test Surname");
59 is( $object->is_changed(), 0, "Object is unchanged" );
60 $object->surname("Test Surname");
61 is( $object->is_changed(), 0, "Object is still unchanged" );
62 $object->surname("Test Surname 2");
63 is( $object->is_changed(), 1, "Object is changed" );
66 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
68 $object->set({ firstname => 'Test Firstname' });
69 is( $object->is_changed(), 1, "Object is changed after Set" );
71 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
73 # Test make_column_dirty
74 is( $object->make_column_dirty('firstname'), '', 'make_column_dirty returns empty string on success' );
75 is( $object->make_column_dirty('firstname'), 1, 'make_column_dirty returns 1 if already dirty' );
76 is( $object->is_changed, 1, "Object is changed after make dirty" );
78 is( $object->is_changed, 0, "Store clears dirty mark" );
79 $object->make_column_dirty('firstname');
80 $object->discard_changes;
81 is( $object->is_changed, 0, "Discard clears dirty mark too" );
83 $schema->storage->txn_rollback;
86 subtest 'in_storage' => sub {
89 $schema->storage->txn_begin;
91 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
92 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
94 my $object = Koha::Patron->new();
95 is( $object->in_storage, 0, "Object is not in storage" );
96 $object->categorycode( $categorycode );
97 $object->branchcode( $branchcode );
98 $object->surname("Test Surname");
100 is( $object->in_storage, 1, "Object is now stored" );
101 $object->surname("another surname");
102 is( $object->in_storage, 1 );
104 my $borrowernumber = $object->borrowernumber;
105 my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
106 is( $patron->surname(), "Test Surname", "Object found in database" );
109 $patron = $schema->resultset('Borrower')->find( $borrowernumber );
110 ok( ! $patron, "Object no longer found in database" );
111 is( $object->in_storage, 0, "Object is not in storage" );
113 $schema->storage->txn_rollback;
116 subtest 'id' => sub {
119 $schema->storage->txn_begin;
121 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
122 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
124 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
125 is( $patron->id, $patron->borrowernumber );
127 $schema->storage->txn_rollback;
130 subtest 'get_column' => sub {
133 $schema->storage->txn_begin;
135 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
136 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
138 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
139 is( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
141 $schema->storage->txn_rollback;
144 subtest 'discard_changes' => sub {
147 $schema->storage->txn_begin;
149 my $patron = $builder->build( { source => 'Borrower' } );
150 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
151 $patron->dateexpiry(dt_from_string);
152 $patron->discard_changes;
154 dt_from_string( $patron->dateexpiry ),
155 dt_from_string->truncate( to => 'day' ),
156 'discard_changes should refresh the object'
159 $schema->storage->txn_rollback;
162 subtest 'TO_JSON tests' => sub {
166 $schema->storage->txn_begin;
168 my $dt = dt_from_string();
169 my $borrowernumber = $builder->build(
170 { source => 'Borrower',
171 value => { lost => 1,
172 sms_provider_id => undef,
175 lastseen => $dt, } })->{borrowernumber};
177 my $patron = Koha::Patrons->find($borrowernumber);
178 my $lost = $patron->TO_JSON()->{lost};
179 my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
180 my $updated_on = $patron->TO_JSON->{updated_on};
181 my $lastseen = $patron->TO_JSON->{lastseen};
183 ok( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
184 is( $lost, 1, 'Boolean attribute value is correct (true)' );
186 ok( $gonenoaddress->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
187 is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
189 is( $patron->TO_JSON->{sms_provider_id}, undef, 'Undef values should not be casted to 0' );
191 ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
193 my $rfc3999_regex = qr/
205 (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))
207 like( $updated_on, $rfc3999_regex, "Date-time $updated_on formatted correctly");
208 like( $lastseen, $rfc3999_regex, "Date-time $updated_on formatted correctly");
210 $schema->storage->txn_rollback;
213 subtest "Test update method" => sub {
216 $schema->storage->txn_begin;
218 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
219 my $library = Koha::Libraries->find( $branchcode );
220 $library->update({ branchname => 'New_Name', branchcity => 'AMS' });
221 is( $library->branchname, 'New_Name', 'Changed name with update' );
222 is( $library->branchcity, 'AMS', 'Changed city too' );
223 is( $library->is_changed, 0, 'Change should be stored already' );
226 branchcity => 'NYC', not_a_column => 53, branchname => 'Name3',
228 fail( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
230 ok( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
231 $library->discard_changes; #requery after failing update
233 # Check if the columns are not updated
234 is( $library->branchcity, 'AMS', 'First column not updated' );
235 is( $library->branchname, 'New_Name', 'Third column not updated' );
237 $schema->storage->txn_rollback;
240 subtest 'store() tests' => sub {
244 $schema->storage->txn_begin;
246 # Create a category to make sure its ID doesn't exist on the DB
247 my $category = $builder->build_object({ class => 'Koha::Patron::Categories' });
248 my $category_id = $category->id;
251 my $patron = Koha::Patron->new({ categorycode => $category_id });
253 my $print_error = $schema->storage->dbh->{PrintError};
254 $schema->storage->dbh->{PrintError} = 0; # FIXME This does not longer work - because of the transaction in Koha::Patron->store?
257 'Koha::Exceptions::Object::FKConstraint',
258 'Exception is thrown correctly';
261 "Broken FK constraint",
262 'Exception message is correct'
267 'Exception field is correct'
270 my $library = $builder->build_object({ class => 'Koha::Libraries' });
271 $category = $builder->build_object({ class => 'Koha::Patron::Categories' });
272 $patron = $builder->build_object({ class => 'Koha::Patrons' });
274 my $new_patron = Koha::Patron->new({
275 branchcode => $library->id,
276 cardnumber => $patron->cardnumber,
277 categorycode => $category->id
281 { $new_patron->store }
282 'Koha::Exceptions::Object::DuplicateID',
283 'Exception is thrown correctly';
288 'Exception message is correct'
294 'Exception field is correct'
297 $new_patron = Koha::Patron->new({
298 branchcode => $library->id,
299 userid => $patron->userid,
300 categorycode => $category->id
304 { $new_patron->store }
305 'Koha::Exceptions::Object::DuplicateID',
306 'Exception is thrown correctly';
311 'Exception message is correct'
317 'Exception field is correct'
320 $schema->storage->dbh->{PrintError} = $print_error;
323 $patron->set({ firstname => 'Manuel' });
324 my $ret = $patron->store;
325 is( ref($ret), 'Koha::Patron', 'store() returns the object on success' );
327 $schema->storage->txn_rollback;
330 subtest 'unblessed_all_relateds' => sub {
333 $schema->storage->txn_begin;
335 # FIXME It's very painful to create an issue in tests!
336 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
337 C4::Context->_new_userenv('xxx');
338 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Midway Public Library', '', '', '');
339 my $patron_category = $builder->build(
341 source => 'Category',
343 category_type => 'P',
345 BlockExpiredPatronOpacActions => -1, # Pick the pref value
350 firstname => 'firstname',
351 surname => 'surname',
352 categorycode => $patron_category->{categorycode},
353 branchcode => $library->branchcode,
355 my $patron = Koha::Patron->new($patron_data)->store;
356 my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
357 my $biblio = Koha::Biblios->find( $biblionumber );
358 my $item = $builder->build_object(
360 class => 'Koha::Items',
362 homebranch => $library->branchcode,
363 holdingbranch => $library->branchcode,
364 biblionumber => $biblio->biblionumber,
371 my $issue = AddIssue( $patron->unblessed, $item->barcode, DateTime->now->subtract( days => 1 ) );
372 my $overdues = Koha::Patrons->find( $patron->id )->get_overdues; # Koha::Patron->get_overdue prefetches
373 my $overdue = $overdues->next->unblessed_all_relateds;
374 is( $overdue->{issue_id}, $issue->issue_id, 'unblessed_all_relateds has field from the original table (issues)' );
375 is( $overdue->{title}, $biblio->title, 'unblessed_all_relateds has field from other tables (biblio)' );
376 is( $overdue->{homebranch}, $item->homebranch, 'unblessed_all_relateds has field from other tables (items)' );
378 $schema->storage->txn_rollback;