Bug 19926: Add tests for Koha::Object->unblessed_all_relateds
[koha.git] / t / db_dependent / Koha / Object.t
blob125394c234a6978e2fb84f03749ce49230920711
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 11;
21 use Test::Exception;
22 use Test::Warn;
23 use DateTime;
25 use C4::Context;
26 use C4::Biblio; # AddBiblio
27 use C4::Circulation; # AddIssue
28 use C4::Members;# AddMember
29 use Koha::Database;
30 use Koha::DateUtils qw( dt_from_string );
31 use Koha::Libraries;
33 use Scalar::Util qw( isvstring );
34 use Try::Tiny;
36 use t::lib::TestBuilder;
38 BEGIN {
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' => sub {
47 plan tests => 6;
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");
58 $object->store();
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" );
65 $object->store();
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" );
70 $object->store();
71 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
73 $schema->storage->txn_rollback;
76 subtest 'in_storage' => sub {
77 plan tests => 6;
79 $schema->storage->txn_begin;
81 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
82 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
84 my $object = Koha::Patron->new();
85 is( $object->in_storage, 0, "Object is not in storage" );
86 $object->categorycode( $categorycode );
87 $object->branchcode( $branchcode );
88 $object->surname("Test Surname");
89 $object->store();
90 is( $object->in_storage, 1, "Object is now stored" );
91 $object->surname("another surname");
92 is( $object->in_storage, 1 );
94 my $borrowernumber = $object->borrowernumber;
95 my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
96 is( $patron->surname(), "Test Surname", "Object found in database" );
98 $object->delete();
99 $patron = $schema->resultset('Borrower')->find( $borrowernumber );
100 ok( ! $patron, "Object no longer found in database" );
101 is( $object->in_storage, 0, "Object is not in storage" );
103 $schema->storage->txn_rollback;
106 subtest 'id' => sub {
107 plan tests => 1;
109 $schema->storage->txn_begin;
111 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
112 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
114 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
115 is( $patron->id, $patron->borrowernumber );
117 $schema->storage->txn_rollback;
120 subtest 'get_column' => sub {
121 plan tests => 1;
123 $schema->storage->txn_begin;
125 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
126 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
128 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
129 is( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
131 $schema->storage->txn_rollback;
134 subtest 'discard_changes' => sub {
135 plan tests => 1;
137 $schema->storage->txn_begin;
139 my $patron = $builder->build( { source => 'Borrower' } );
140 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
141 $patron->dateexpiry(dt_from_string);
142 $patron->discard_changes;
144 dt_from_string( $patron->dateexpiry ),
145 dt_from_string->truncate( to => 'day' ),
146 'discard_changes should refresh the object'
149 $schema->storage->txn_rollback;
152 subtest 'TO_JSON tests' => sub {
154 plan tests => 7;
156 $schema->storage->txn_begin;
158 my $dt = dt_from_string();
159 my $borrowernumber = $builder->build(
160 { source => 'Borrower',
161 value => { lost => 1,
162 gonenoaddress => 0,
163 updated_on => $dt,
164 lastseen => $dt, } })->{borrowernumber};
166 my $patron = Koha::Patrons->find($borrowernumber);
167 my $lost = $patron->TO_JSON()->{lost};
168 my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
169 my $updated_on = $patron->TO_JSON->{updated_on};
170 my $lastseen = $patron->TO_JSON->{lastseen};
172 ok( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
173 is( $lost, 1, 'Boolean attribute value is correct (true)' );
175 ok( $gonenoaddress->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
176 is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
178 ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
180 my $rfc3999_regex = qr/
181 (?<year>\d{4})
183 (?<month>\d{2})
185 (?<day>\d{2})
186 ([Tt\s])
187 (?<hour>\d{2})
189 (?<minute>\d{2})
191 (?<second>\d{2})
192 (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))
193 /xms;
194 like( $updated_on, $rfc3999_regex, "Date-time $updated_on formatted correctly");
195 like( $lastseen, $rfc3999_regex, "Date-time $updated_on formatted correctly");
197 $schema->storage->txn_rollback;
200 subtest "Test update method" => sub {
201 plan tests => 6;
203 $schema->storage->txn_begin;
205 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
206 my $library = Koha::Libraries->find( $branchcode );
207 $library->update({ branchname => 'New_Name', branchcity => 'AMS' });
208 is( $library->branchname, 'New_Name', 'Changed name with update' );
209 is( $library->branchcity, 'AMS', 'Changed city too' );
210 is( $library->is_changed, 0, 'Change should be stored already' );
211 try {
212 $library->update({
213 branchcity => 'NYC', not_a_column => 53, branchname => 'Name3',
215 fail( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
216 } catch {
217 ok( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
218 $library->discard_changes; #requery after failing update
220 # Check if the columns are not updated
221 is( $library->branchcity, 'AMS', 'First column not updated' );
222 is( $library->branchname, 'New_Name', 'Third column not updated' );
224 $schema->storage->txn_rollback;
227 subtest 'store() tests' => sub {
229 plan tests => 10;
231 $schema->storage->txn_begin;
233 # Create a category to make sure its ID doesn't exist on the DB
234 my $category = $builder->build_object({ class => 'Koha::Patron::Categories' });
235 my $category_id = $category->id;
236 $category->delete;
238 my $patron = Koha::Patron->new({ categorycode => $category_id });
240 my $print_error = $schema->storage->dbh->{PrintError};
241 $schema->storage->dbh->{PrintError} = 0;
242 throws_ok
243 { $patron->store }
244 'Koha::Exceptions::Object::FKConstraint',
245 'Exception is thrown correctly';
247 $@->message,
248 "Broken FK constraint",
249 'Exception message is correct'
252 $@->broken_fk,
253 'categorycode',
254 'Exception field is correct'
257 my $library = $builder->build_object({ class => 'Koha::Libraries' });
258 $category = $builder->build_object({ class => 'Koha::Patron::Categories' });
259 $patron = $builder->build_object({ class => 'Koha::Patrons' });
261 my $new_patron = Koha::Patron->new({
262 branchcode => $library->id,
263 cardnumber => $patron->cardnumber,
264 categorycode => $category->id
267 throws_ok
268 { $new_patron->store }
269 'Koha::Exceptions::Object::DuplicateID',
270 'Exception is thrown correctly';
273 $@->message,
274 'Duplicate ID',
275 'Exception message is correct'
279 $@->duplicate_id,
280 'cardnumber',
281 'Exception field is correct'
284 $new_patron = Koha::Patron->new({
285 branchcode => $library->id,
286 userid => $patron->userid,
287 categorycode => $category->id
290 throws_ok
291 { $new_patron->store }
292 'Koha::Exceptions::Object::DuplicateID',
293 'Exception is thrown correctly';
296 $@->message,
297 'Duplicate ID',
298 'Exception message is correct'
302 $@->duplicate_id,
303 'userid',
304 'Exception field is correct'
307 $schema->storage->dbh->{PrintError} = $print_error;
309 # Successful test
310 $patron->set({ firstname => 'Manuel' });
311 my $ret = $patron->store;
312 is( ref($ret), 'Koha::Patron', 'store() returns the object on success' );
314 $schema->storage->txn_rollback;
317 subtest 'unblessed_all_relateds' => sub {
318 plan tests => 3;
320 $schema->storage->txn_begin;
322 # FIXME It's very painful to create an issue in tests!
323 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
324 C4::Context->_new_userenv('xxx');
325 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Midway Public Library', '', '', '');
326 my $patron_category = $builder->build(
328 source => 'Category',
329 value => {
330 category_type => 'P',
331 enrolmentfee => 0,
332 BlockExpiredPatronOpacActions => -1, # Pick the pref value
336 my $patron_data = {
337 firstname => 'firstname',
338 surname => 'surname',
339 categorycode => $patron_category->{categorycode},
340 branchcode => $library->branchcode,
342 my $borrowernumber = C4::Members::AddMember(%$patron_data);
343 my $patron = Koha::Patrons->find( $borrowernumber );
344 my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
345 my $biblio = Koha::Biblios->find( $biblionumber );
346 my $item = $builder->build_object(
348 class => 'Koha::Items',
349 value => {
350 homebranch => $library->branchcode,
351 holdingbranch => $library->branchcode,
352 biblionumber => $biblio->biblionumber,
353 itemlost => 0,
354 withdrawn => 0,
359 my $issue = AddIssue( $patron->unblessed, $item->barcode, DateTime->now->subtract( days => 1 ) );
360 my $overdues = Koha::Patrons->find( $patron->id )->get_overdues; # Koha::Patron->get_overdue prefetches
361 my $overdue = $overdues->next->unblessed_all_relateds;
362 is( $overdue->{issue_id}, $issue->issue_id, 'unblessed_all_relateds has field from the original table (issues)' );
363 is( $overdue->{title}, $biblio->title, 'unblessed_all_relateds has field from other tables (biblio)' );
364 is( $overdue->{homebranch}, $item->homebranch, 'unblessed_all_relateds has field from other tables (items)' );
366 $schema->storage->txn_rollback;