Bug 19926: Add tests for Koha::Object->unblessed_all_relateds
[srvgit] / t / db_dependent / Koha / Object.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 11;
21 use Test::Exception;
22 use Test::Warn;
23 use DateTime;
24
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;
32
33 use Scalar::Util qw( isvstring );
34 use Try::Tiny;
35
36 use t::lib::TestBuilder;
37
38 BEGIN {
39     use_ok('Koha::Object');
40     use_ok('Koha::Patron');
41 }
42
43 my $schema  = Koha::Database->new->schema;
44 my $builder = t::lib::TestBuilder->new();
45
46 subtest 'is_changed' => sub {
47     plan tests => 6;
48
49     $schema->storage->txn_begin;
50
51     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
52     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
53
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" );
64
65     $object->store();
66     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
67
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" );
72
73     $schema->storage->txn_rollback;
74 };
75
76 subtest 'in_storage' => sub {
77     plan tests => 6;
78
79     $schema->storage->txn_begin;
80
81     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
82     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
83
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 );
93
94     my $borrowernumber = $object->borrowernumber;
95     my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
96     is( $patron->surname(), "Test Surname", "Object found in database" );
97
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" );
102
103     $schema->storage->txn_rollback;
104 };
105
106 subtest 'id' => sub {
107     plan tests => 1;
108
109     $schema->storage->txn_begin;
110
111     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
112     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
113
114     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
115     is( $patron->id, $patron->borrowernumber );
116
117     $schema->storage->txn_rollback;
118 };
119
120 subtest 'get_column' => sub {
121     plan tests => 1;
122
123     $schema->storage->txn_begin;
124
125     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
126     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
127
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' );
130
131     $schema->storage->txn_rollback;
132 };
133
134 subtest 'discard_changes' => sub {
135     plan tests => 1;
136
137     $schema->storage->txn_begin;
138
139     my $patron = $builder->build( { source => 'Borrower' } );
140     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
141     $patron->dateexpiry(dt_from_string);
142     $patron->discard_changes;
143     is(
144         dt_from_string( $patron->dateexpiry ),
145         dt_from_string->truncate( to => 'day' ),
146         'discard_changes should refresh the object'
147     );
148
149     $schema->storage->txn_rollback;
150 };
151
152 subtest 'TO_JSON tests' => sub {
153
154     plan tests => 7;
155
156     $schema->storage->txn_begin;
157
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};
165
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};
171
172     ok( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
173     is( $lost, 1, 'Boolean attribute value is correct (true)' );
174
175     ok( $gonenoaddress->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
176     is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
177
178     ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
179
180     my $rfc3999_regex = qr/
181             (?<year>\d{4})
182             -
183             (?<month>\d{2})
184             -
185             (?<day>\d{2})
186             ([Tt\s])
187             (?<hour>\d{2})
188             :
189             (?<minute>\d{2})
190             :
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");
196
197     $schema->storage->txn_rollback;
198 };
199
200 subtest "Test update method" => sub {
201     plan tests => 6;
202
203     $schema->storage->txn_begin;
204
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',
214         });
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
219     };
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' );
223
224     $schema->storage->txn_rollback;
225 };
226
227 subtest 'store() tests' => sub {
228
229     plan tests => 10;
230
231     $schema->storage->txn_begin;
232
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;
237
238     my $patron = Koha::Patron->new({ categorycode => $category_id });
239
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';
246     is(
247         $@->message,
248         "Broken FK constraint",
249         'Exception message is correct'
250     );
251     is(
252         $@->broken_fk,
253         'categorycode',
254         'Exception field is correct'
255     );
256
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' });
260
261     my $new_patron = Koha::Patron->new({
262         branchcode   => $library->id,
263         cardnumber   => $patron->cardnumber,
264         categorycode => $category->id
265     });
266
267     throws_ok
268         { $new_patron->store }
269         'Koha::Exceptions::Object::DuplicateID',
270         'Exception is thrown correctly';
271
272     is(
273         $@->message,
274         'Duplicate ID',
275         'Exception message is correct'
276     );
277
278     is(
279        $@->duplicate_id,
280        'cardnumber',
281        'Exception field is correct'
282     );
283
284     $new_patron = Koha::Patron->new({
285         branchcode   => $library->id,
286         userid       => $patron->userid,
287         categorycode => $category->id
288     });
289
290     throws_ok
291         { $new_patron->store }
292         'Koha::Exceptions::Object::DuplicateID',
293         'Exception is thrown correctly';
294
295     is(
296         $@->message,
297         'Duplicate ID',
298         'Exception message is correct'
299     );
300
301     is(
302        $@->duplicate_id,
303        'userid',
304        'Exception field is correct'
305     );
306
307     $schema->storage->dbh->{PrintError} = $print_error;
308
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' );
313
314     $schema->storage->txn_rollback;
315 };
316
317 subtest 'unblessed_all_relateds' => sub {
318     plan tests => 3;
319
320     $schema->storage->txn_begin;
321
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(
327         {
328             source => 'Category',
329             value  => {
330                 category_type                 => 'P',
331                 enrolmentfee                  => 0,
332                 BlockExpiredPatronOpacActions => -1, # Pick the pref value
333             }
334         }
335     );
336     my $patron_data = {
337         firstname =>  'firstname',
338         surname => 'surname',
339         categorycode => $patron_category->{categorycode},
340         branchcode => $library->branchcode,
341     };
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(
347         {
348             class => 'Koha::Items',
349             value => {
350                 homebranch    => $library->branchcode,
351                 holdingbranch => $library->branchcode,
352                 biblionumber  => $biblio->biblionumber,
353                 itemlost      => 0,
354                 withdrawn     => 0,
355             }
356         }
357     );
358
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)' );
365
366     $schema->storage->txn_rollback;
367 };