Bug 24329: Add a test
[koha.git] / 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 => 12;
21 use Test::Exception;
22 use Test::Warn;
23 use DateTime;
24
25 use C4::Context;
26 use C4::Circulation; # AddIssue
27 use C4::Biblio; # AddBiblio
28
29 use Koha::Database;
30 use Koha::DateUtils qw( dt_from_string );
31 use Koha::Libraries;
32 use Koha::Patrons;
33 use Koha::ApiKeys;
34
35 use Scalar::Util qw( isvstring );
36 use Try::Tiny;
37
38 use t::lib::TestBuilder;
39 use t::lib::Mocks;
40
41 BEGIN {
42     use_ok('Koha::Object');
43     use_ok('Koha::Patron');
44 }
45
46 my $schema  = Koha::Database->new->schema;
47 my $builder = t::lib::TestBuilder->new();
48
49 subtest 'is_changed / make_column_dirty' => sub {
50     plan tests => 11;
51
52     $schema->storage->txn_begin;
53
54     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
55     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
56
57     my $object = Koha::Patron->new();
58     $object->categorycode( $categorycode );
59     $object->branchcode( $branchcode );
60     $object->surname("Test Surname");
61     $object->store();
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" );
67
68     $object->store();
69     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
70
71     $object->set({ firstname => 'Test Firstname' });
72     is( $object->is_changed(), 1, "Object is changed after Set" );
73     $object->store();
74     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
75
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" );
80     $object->store;
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" );
85
86     $schema->storage->txn_rollback;
87 };
88
89 subtest 'in_storage' => sub {
90     plan tests => 6;
91
92     $schema->storage->txn_begin;
93
94     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
95     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
96
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");
102     $object->store();
103     is( $object->in_storage, 1, "Object is now stored" );
104     $object->surname("another surname");
105     is( $object->in_storage, 1 );
106
107     my $borrowernumber = $object->borrowernumber;
108     my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
109     is( $patron->surname(), "Test Surname", "Object found in database" );
110
111     $object->delete();
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" );
115
116     $schema->storage->txn_rollback;
117 };
118
119 subtest 'id' => sub {
120     plan tests => 1;
121
122     $schema->storage->txn_begin;
123
124     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
125     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
126
127     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
128     is( $patron->id, $patron->borrowernumber );
129
130     $schema->storage->txn_rollback;
131 };
132
133 subtest 'get_column' => sub {
134     plan tests => 1;
135
136     $schema->storage->txn_begin;
137
138     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
139     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
140
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' );
143
144     $schema->storage->txn_rollback;
145 };
146
147 subtest 'discard_changes' => sub {
148     plan tests => 1;
149
150     $schema->storage->txn_begin;
151
152     my $patron = $builder->build( { source => 'Borrower' } );
153     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
154     $patron->dateexpiry(dt_from_string);
155     $patron->discard_changes;
156     is(
157         dt_from_string( $patron->dateexpiry ),
158         dt_from_string->truncate( to => 'day' ),
159         'discard_changes should refresh the object'
160     );
161
162     $schema->storage->txn_rollback;
163 };
164
165 subtest 'TO_JSON tests' => sub {
166
167     plan tests => 8;
168
169     $schema->storage->txn_begin;
170
171     my $dt = dt_from_string();
172     my $borrowernumber = $builder->build(
173         { source => 'Borrower',
174           value => { lost => 1,
175                      sms_provider_id => undef,
176                      gonenoaddress => 0,
177                      updated_on => $dt,
178                      lastseen   => $dt, } })->{borrowernumber};
179
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};
185
186     ok( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
187     is( $lost, 1, 'Boolean attribute value is correct (true)' );
188
189     ok( $gonenoaddress->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
190     is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
191
192     is( $patron->TO_JSON->{sms_provider_id}, undef, 'Undef values should not be casted to 0' );
193
194     ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
195
196     my $rfc3999_regex = qr/
197             (?<year>\d{4})
198             -
199             (?<month>\d{2})
200             -
201             (?<day>\d{2})
202             ([Tt\s])
203             (?<hour>\d{2})
204             :
205             (?<minute>\d{2})
206             :
207             (?<second>\d{2})
208             (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))
209         /xms;
210     like( $updated_on, $rfc3999_regex, "Date-time $updated_on formatted correctly");
211     like( $lastseen, $rfc3999_regex, "Date-time $updated_on formatted correctly");
212
213     $schema->storage->txn_rollback;
214 };
215
216 subtest "to_api() tests" => sub {
217
218     plan tests => 11;
219
220     $schema->storage->txn_begin;
221
222     my $city = $builder->build_object({ class => 'Koha::Cities' });
223
224     # THE mapping
225     # cityid       => 'city_id',
226     # city_country => 'country',
227     # city_name    => 'name',
228     # city_state   => 'state',
229     # city_zipcode => 'postal_code'
230
231     my $api_city = $city->to_api;
232
233     is( $api_city->{city_id},     $city->cityid,       'Attribute translated correctly' );
234     is( $api_city->{country},     $city->city_country, 'Attribute translated correctly' );
235     is( $api_city->{name},        $city->city_name,    'Attribute translated correctly' );
236     is( $api_city->{state},       $city->city_state,   'Attribute translated correctly' );
237     is( $api_city->{postal_code}, $city->city_zipcode, 'Attribute translated correctly' );
238
239     # Lets emulate an undef
240     my $city_class = Test::MockModule->new('Koha::City');
241     $city_class->mock( 'to_api_mapping',
242         sub {
243             return {
244                 cityid       => 'city_id',
245                 city_country => 'country',
246                 city_name    => 'name',
247                 city_state   => 'state',
248                 city_zipcode => undef
249             };
250         }
251     );
252
253     $api_city = $city->to_api;
254
255     is( $api_city->{city_id},     $city->cityid,       'Attribute translated correctly' );
256     is( $api_city->{country},     $city->city_country, 'Attribute translated correctly' );
257     is( $api_city->{name},        $city->city_name,    'Attribute translated correctly' );
258     is( $api_city->{state},       $city->city_state,   'Attribute translated correctly' );
259     ok( !exists $api_city->{postal_code}, 'Attribute removed' );
260
261     # Pick a class that won't have a mapping for the API
262     my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
263     is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no to_api_method present, return TO_JSON' );
264
265     $schema->storage->txn_rollback;
266 };
267
268 subtest "Test update method" => sub {
269     plan tests => 6;
270
271     $schema->storage->txn_begin;
272
273     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
274     my $library = Koha::Libraries->find( $branchcode );
275     $library->update({ branchname => 'New_Name', branchcity => 'AMS' });
276     is( $library->branchname, 'New_Name', 'Changed name with update' );
277     is( $library->branchcity, 'AMS', 'Changed city too' );
278     is( $library->is_changed, 0, 'Change should be stored already' );
279     try {
280         $library->update({
281             branchcity => 'NYC', not_a_column => 53, branchname => 'Name3',
282         });
283         fail( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
284     } catch {
285         ok( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
286         $library->discard_changes; #requery after failing update
287     };
288     # Check if the columns are not updated
289     is( $library->branchcity, 'AMS', 'First column not updated' );
290     is( $library->branchname, 'New_Name', 'Third column not updated' );
291
292     $schema->storage->txn_rollback;
293 };
294
295 subtest 'store() tests' => sub {
296
297     plan tests => 16;
298
299     # Using Koha::ApiKey to test Koha::Object>-store
300     # Simple object with foreign keys and unique key
301
302     $schema->storage->txn_begin;
303
304     # Create a patron to make sure its ID doesn't exist on the DB
305     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
306     my $patron_id = $patron->id;
307     $patron->delete;
308
309     my $api_key = Koha::ApiKey->new({ patron_id => $patron_id, secret => 'a secret', description => 'a description' });
310
311     my $print_error = $schema->storage->dbh->{PrintError};
312     $schema->storage->dbh->{PrintError} = 0;
313     throws_ok
314         { $api_key->store }
315         'Koha::Exceptions::Object::FKConstraint',
316         'Exception is thrown correctly';
317     is(
318         $@->message,
319         "Broken FK constraint",
320         'Exception message is correct'
321     );
322     is(
323         $@->broken_fk,
324         'patron_id',
325         'Exception field is correct'
326     );
327
328     $patron = $builder->build_object({ class => 'Koha::Patrons' });
329     $api_key = $builder->build_object({ class => 'Koha::ApiKeys' });
330
331     my $new_api_key = Koha::ApiKey->new({
332         patron_id => $patron_id,
333         secret => $api_key->secret,
334         description => 'a description',
335     });
336
337     throws_ok
338         { $new_api_key->store }
339         'Koha::Exceptions::Object::DuplicateID',
340         'Exception is thrown correctly';
341
342     is(
343         $@->message,
344         'Duplicate ID',
345         'Exception message is correct'
346     );
347
348     is(
349        $@->duplicate_id,
350        'secret',
351        'Exception field is correct'
352     );
353
354     $schema->storage->dbh->{PrintError} = $print_error;
355
356     # Successful test
357     $api_key->set({ secret => 'Manuel' });
358     my $ret = $api_key->store;
359     is( ref($ret), 'Koha::ApiKey', 'store() returns the object on success' );
360
361     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
362     my $patron_category = $builder->build_object(
363         {
364             class => 'Koha::Patron::Categories',
365             value => { category_type => 'P', enrolmentfee => 0 }
366         }
367     );
368
369     $patron = eval {
370         Koha::Patron->new(
371             {
372                 categorycode    => $patron_category->categorycode,
373                 branchcode      => $library->branchcode,
374                 dateofbirth     => "", # date will be set to NULL
375                 sms_provider_id => "", # Integer will be set to NULL
376                 privacy         => "", # privacy cannot be NULL but has a default value
377             }
378         )->store;
379     };
380     is( $@, '', 'No error should be raised by ->store if empty strings are passed' );
381     is( $patron->privacy, 1, 'Default value for privacy should be set to 1' );
382     is( $patron->dateofbirth,     undef, 'dateofbirth must have been set to undef');
383     is( $patron->sms_provider_id, undef, 'sms_provider_id must have been set to undef');
384
385     my $itemtype = eval {
386         Koha::ItemType->new(
387             {
388                 itemtype        => 'IT4test',
389                 rentalcharge    => "",
390                 notforloan      => "",
391                 hideinopac      => "",
392             }
393         )->store;
394     };
395     is( $@, '', 'No error should be raised by ->store if empty strings are passed' );
396     is( $itemtype->rentalcharge, undef, 'decimal DEFAULT NULL should default to null');
397     is( $itemtype->notforloan, undef, 'int DEFAULT NULL should default to null');
398     is( $itemtype->hideinopac, 0, 'int NOT NULL DEFAULT 0 should default to 0');
399
400     subtest 'Bad value tests' => sub {
401
402         plan tests => 3;
403
404         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
405
406         my $print_error = $schema->storage->dbh->{PrintError};
407         $schema->storage->dbh->{PrintError} = 0;
408
409         try {
410             $patron->lastseen('wrong_value')->store;
411         } catch {
412             ok( $_->isa('Koha::Exceptions::Object::BadValue'), 'Exception thrown correctly' );
413             like( $_->property, qr/(borrowers\.)?lastseen/, 'Column should be the expected one' ); # The table name is not always displayed, it depends on the DBMS version
414             is( $_->value, 'wrong_value', 'Value should be the expected one' );
415         };
416
417         $schema->storage->dbh->{PrintError} = $print_error;
418     };
419
420     $schema->storage->txn_rollback;
421 };
422
423 subtest 'unblessed_all_relateds' => sub {
424     plan tests => 3;
425
426     $schema->storage->txn_begin;
427
428     # FIXME It's very painful to create an issue in tests!
429     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
430     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
431
432     my $patron_category = $builder->build(
433         {
434             source => 'Category',
435             value  => {
436                 category_type                 => 'P',
437                 enrolmentfee                  => 0,
438                 BlockExpiredPatronOpacActions => -1, # Pick the pref value
439             }
440         }
441     );
442     my $patron_data = {
443         firstname =>  'firstname',
444         surname => 'surname',
445         categorycode => $patron_category->{categorycode},
446         branchcode => $library->branchcode,
447     };
448     my $patron = Koha::Patron->new($patron_data)->store;
449     my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
450     my $biblio = Koha::Biblios->find( $biblionumber );
451     my $item = $builder->build_object(
452         {
453             class => 'Koha::Items',
454             value => {
455                 homebranch    => $library->branchcode,
456                 holdingbranch => $library->branchcode,
457                 biblionumber  => $biblio->biblionumber,
458                 itemlost      => 0,
459                 withdrawn     => 0,
460             }
461         }
462     );
463
464     my $issue = AddIssue( $patron->unblessed, $item->barcode, DateTime->now->subtract( days => 1 ) );
465     my $overdues = Koha::Patrons->find( $patron->id )->get_overdues; # Koha::Patron->get_overdue prefetches
466     my $overdue = $overdues->next->unblessed_all_relateds;
467     is( $overdue->{issue_id}, $issue->issue_id, 'unblessed_all_relateds has field from the original table (issues)' );
468     is( $overdue->{title}, $biblio->title, 'unblessed_all_relateds has field from other tables (biblio)' );
469     is( $overdue->{homebranch}, $item->homebranch, 'unblessed_all_relateds has field from other tables (items)' );
470
471     $schema->storage->txn_rollback;
472 };