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
;
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
"Test update method" => sub {
219 $schema->storage->txn_begin;
221 my $branchcode = $builder->build({ source
=> 'Branch' })->{branchcode
};
222 my $library = Koha
::Libraries
->find( $branchcode );
223 $library->update({ branchname
=> 'New_Name', branchcity
=> 'AMS' });
224 is
( $library->branchname, 'New_Name', 'Changed name with update' );
225 is
( $library->branchcity, 'AMS', 'Changed city too' );
226 is
( $library->is_changed, 0, 'Change should be stored already' );
229 branchcity
=> 'NYC', not_a_column
=> 53, branchname
=> 'Name3',
231 fail
( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
233 ok
( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
234 $library->discard_changes; #requery after failing update
236 # Check if the columns are not updated
237 is
( $library->branchcity, 'AMS', 'First column not updated' );
238 is
( $library->branchname, 'New_Name', 'Third column not updated' );
240 $schema->storage->txn_rollback;
243 subtest
'store() tests' => sub {
247 # Using Koha::ApiKey to test Koha::Object>-store
248 # Simple object with foreign keys and unique key
250 $schema->storage->txn_begin;
252 # Create a patron to make sure its ID doesn't exist on the DB
253 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
254 my $patron_id = $patron->id;
257 my $api_key = Koha
::ApiKey
->new({ patron_id
=> $patron_id, secret
=> 'a secret', description
=> 'a description' });
259 my $print_error = $schema->storage->dbh->{PrintError
};
260 $schema->storage->dbh->{PrintError
} = 0;
263 'Koha::Exceptions::Object::FKConstraint',
264 'Exception is thrown correctly';
267 "Broken FK constraint",
268 'Exception message is correct'
273 'Exception field is correct'
276 $patron = $builder->build_object({ class => 'Koha::Patrons' });
277 $api_key = $builder->build_object({ class => 'Koha::ApiKeys' });
279 my $new_api_key = Koha
::ApiKey
->new({
280 patron_id
=> $patron_id,
281 secret
=> $api_key->secret,
282 description
=> 'a description',
286 { $new_api_key->store }
287 'Koha::Exceptions::Object::DuplicateID',
288 'Exception is thrown correctly';
293 'Exception message is correct'
299 'Exception field is correct'
302 $schema->storage->dbh->{PrintError
} = $print_error;
305 $api_key->set({ secret
=> 'Manuel' });
306 my $ret = $api_key->store;
307 is
( ref($ret), 'Koha::ApiKey', 'store() returns the object on success' );
309 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
310 my $patron_category = $builder->build_object(
312 class => 'Koha::Patron::Categories',
313 value
=> { category_type
=> 'P', enrolmentfee
=> 0 }
320 categorycode
=> $patron_category->categorycode,
321 branchcode
=> $library->branchcode,
322 dateofbirth
=> "", # date will be set to NULL
323 sms_provider_id
=> "", # Integer will be set to NULL
324 privacy
=> "", # privacy cannot be NULL but has a default value
328 is
( $@
, '', 'No error should be raised by ->store if empty strings are passed' );
329 is
( $patron->privacy, 1, 'Default value for privacy should be set to 1' );
330 is
( $patron->dateofbirth, undef, 'dateofbirth must have been set to undef');
331 is
( $patron->sms_provider_id, undef, 'sms_provider_id must have been set to undef');
333 my $itemtype = eval {
336 itemtype
=> 'IT4test',
343 is
( $@
, '', 'No error should be raised by ->store if empty strings are passed' );
344 is
( $itemtype->rentalcharge, undef, 'decimal DEFAULT NULL should default to null');
345 is
( $itemtype->notforloan, undef, 'int DEFAULT NULL should default to null');
346 is
( $itemtype->hideinopac, 0, 'int NOT NULL DEFAULT 0 should default to 0');
348 subtest
'Bad value tests' => sub {
352 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
354 my $print_error = $schema->storage->dbh->{PrintError
};
355 $schema->storage->dbh->{PrintError
} = 0;
358 { $patron->lastseen('wrong_value')->store; }
359 'Koha::Exceptions::Object::BadValue',
360 'Exception thrown correctly';
362 $schema->storage->dbh->{PrintError
} = $print_error;
365 $schema->storage->txn_rollback;
368 subtest
'unblessed_all_relateds' => sub {
371 $schema->storage->txn_begin;
373 # FIXME It's very painful to create an issue in tests!
374 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
375 t
::lib
::Mocks
::mock_userenv
({ branchcode
=> $library->branchcode });
377 my $patron_category = $builder->build(
379 source
=> 'Category',
381 category_type
=> 'P',
383 BlockExpiredPatronOpacActions
=> -1, # Pick the pref value
388 firstname
=> 'firstname',
389 surname
=> 'surname',
390 categorycode
=> $patron_category->{categorycode
},
391 branchcode
=> $library->branchcode,
393 my $patron = Koha
::Patron
->new($patron_data)->store;
394 my ($biblionumber) = AddBiblio
( MARC
::Record
->new, '' );
395 my $biblio = Koha
::Biblios
->find( $biblionumber );
396 my $item = $builder->build_object(
398 class => 'Koha::Items',
400 homebranch
=> $library->branchcode,
401 holdingbranch
=> $library->branchcode,
402 biblionumber
=> $biblio->biblionumber,
409 my $issue = AddIssue
( $patron->unblessed, $item->barcode, DateTime
->now->subtract( days
=> 1 ) );
410 my $overdues = Koha
::Patrons
->find( $patron->id )->get_overdues; # Koha::Patron->get_overdue prefetches
411 my $overdue = $overdues->next->unblessed_all_relateds;
412 is
( $overdue->{issue_id
}, $issue->issue_id, 'unblessed_all_relateds has field from the original table (issues)' );
413 is
( $overdue->{title
}, $biblio->title, 'unblessed_all_relateds has field from other tables (biblio)' );
414 is
( $overdue->{homebranch
}, $item->homebranch, 'unblessed_all_relateds has field from other tables (items)' );
416 $schema->storage->txn_rollback;