Bug 20145: Do not insert 0000-00-00 in patron tests (and more)
[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 => 10;
21 use Test::Exception;
22 use Test::Warn;
23
24 use C4::Context;
25 use Koha::Database;
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Libraries;
28
29 use Scalar::Util qw( isvstring );
30 use Try::Tiny;
31
32 use t::lib::TestBuilder;
33
34 BEGIN {
35     use_ok('Koha::Object');
36     use_ok('Koha::Patron');
37 }
38
39 my $schema  = Koha::Database->new->schema;
40 my $builder = t::lib::TestBuilder->new();
41
42 subtest 'is_changed' => sub {
43     plan tests => 6;
44
45     $schema->storage->txn_begin;
46
47     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
48     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
49
50     my $object = Koha::Patron->new();
51     $object->categorycode( $categorycode );
52     $object->branchcode( $branchcode );
53     $object->surname("Test Surname");
54     $object->store();
55     is( $object->is_changed(), 0, "Object is unchanged" );
56     $object->surname("Test Surname");
57     is( $object->is_changed(), 0, "Object is still unchanged" );
58     $object->surname("Test Surname 2");
59     is( $object->is_changed(), 1, "Object is changed" );
60
61     $object->store();
62     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
63
64     $object->set({ firstname => 'Test Firstname' });
65     is( $object->is_changed(), 1, "Object is changed after Set" );
66     $object->store();
67     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
68
69     $schema->storage->txn_rollback;
70 };
71
72 subtest 'in_storage' => sub {
73     plan tests => 6;
74
75     $schema->storage->txn_begin;
76
77     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
78     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
79
80     my $object = Koha::Patron->new();
81     is( $object->in_storage, 0, "Object is not in storage" );
82     $object->categorycode( $categorycode );
83     $object->branchcode( $branchcode );
84     $object->surname("Test Surname");
85     $object->store();
86     is( $object->in_storage, 1, "Object is now stored" );
87     $object->surname("another surname");
88     is( $object->in_storage, 1 );
89
90     my $borrowernumber = $object->borrowernumber;
91     my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
92     is( $patron->surname(), "Test Surname", "Object found in database" );
93
94     $object->delete();
95     $patron = $schema->resultset('Borrower')->find( $borrowernumber );
96     ok( ! $patron, "Object no longer found in database" );
97     is( $object->in_storage, 0, "Object is not in storage" );
98
99     $schema->storage->txn_rollback;
100 };
101
102 subtest 'id' => sub {
103     plan tests => 1;
104
105     $schema->storage->txn_begin;
106
107     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
108     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
109
110     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
111     is( $patron->id, $patron->borrowernumber );
112
113     $schema->storage->txn_rollback;
114 };
115
116 subtest 'get_column' => sub {
117     plan tests => 1;
118
119     $schema->storage->txn_begin;
120
121     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
122     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
123
124     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
125     is( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
126
127     $schema->storage->txn_rollback;
128 };
129
130 subtest 'discard_changes' => sub {
131     plan tests => 1;
132
133     $schema->storage->txn_begin;
134
135     my $patron = $builder->build( { source => 'Borrower' } );
136     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
137     $patron->dateexpiry(dt_from_string);
138     $patron->discard_changes;
139     is(
140         dt_from_string( $patron->dateexpiry ),
141         dt_from_string->truncate( to => 'day' ),
142         'discard_changes should refresh the object'
143     );
144
145     $schema->storage->txn_rollback;
146 };
147
148 subtest 'TO_JSON tests' => sub {
149
150     plan tests => 7;
151
152     $schema->storage->txn_begin;
153
154     my $dt = dt_from_string();
155     my $borrowernumber = $builder->build(
156         { source => 'Borrower',
157           value => { lost => 1,
158                      gonenoaddress => 0,
159                      updated_on => $dt,
160                      lastseen   => $dt, } })->{borrowernumber};
161
162     my $patron = Koha::Patrons->find($borrowernumber);
163     my $lost = $patron->TO_JSON()->{lost};
164     my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
165     my $updated_on = $patron->TO_JSON->{updated_on};
166     my $lastseen = $patron->TO_JSON->{lastseen};
167
168     ok( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
169     is( $lost, 1, 'Boolean attribute value is correct (true)' );
170
171     ok( $gonenoaddress->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
172     is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
173
174     ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
175
176     my $rfc3999_regex = qr/
177             (?<year>\d{4})
178             -
179             (?<month>\d{2})
180             -
181             (?<day>\d{2})
182             ([Tt\s])
183             (?<hour>\d{2})
184             :
185             (?<minute>\d{2})
186             :
187             (?<second>\d{2})
188             (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))
189         /xms;
190     like( $updated_on, $rfc3999_regex, "Date-time $updated_on formatted correctly");
191     like( $lastseen, $rfc3999_regex, "Date-time $updated_on formatted correctly");
192
193     $schema->storage->txn_rollback;
194 };
195
196 subtest "Test update method" => sub {
197     plan tests => 6;
198
199     $schema->storage->txn_begin;
200
201     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
202     my $library = Koha::Libraries->find( $branchcode );
203     $library->update({ branchname => 'New_Name', branchcity => 'AMS' });
204     is( $library->branchname, 'New_Name', 'Changed name with update' );
205     is( $library->branchcity, 'AMS', 'Changed city too' );
206     is( $library->is_changed, 0, 'Change should be stored already' );
207     try {
208         $library->update({
209             branchcity => 'NYC', not_a_column => 53, branchname => 'Name3',
210         });
211         fail( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
212     } catch {
213         ok( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
214         $library->discard_changes; #requery after failing update
215     };
216     # Check if the columns are not updated
217     is( $library->branchcity, 'AMS', 'First column not updated' );
218     is( $library->branchname, 'New_Name', 'Third column not updated' );
219
220     $schema->storage->txn_rollback;
221 };
222
223 subtest 'store() tests' => sub {
224
225     plan tests => 10;
226
227     $schema->storage->txn_begin;
228
229     # Create a category to make sure its ID doesn't exist on the DB
230     my $category = $builder->build_object({ class => 'Koha::Patron::Categories' });
231     my $category_id = $category->id;
232     $category->delete;
233
234     my $patron = Koha::Patron->new({ categorycode => $category_id });
235
236     my $print_error = $schema->storage->dbh->{PrintError};
237     $schema->storage->dbh->{PrintError} = 0;
238     throws_ok
239         { $patron->store }
240         'Koha::Exceptions::Object::FKConstraint',
241         'Exception is thrown correctly';
242     is(
243         $@->message,
244         "Broken FK constraint",
245         'Exception message is correct'
246     );
247     is(
248         $@->broken_fk,
249         'categorycode',
250         'Exception field is correct'
251     );
252
253     my $library = $builder->build_object({ class => 'Koha::Libraries' });
254     $category   = $builder->build_object({ class => 'Koha::Patron::Categories' });
255     $patron     = $builder->build_object({ class => 'Koha::Patrons' });
256
257     my $new_patron = Koha::Patron->new({
258         branchcode   => $library->id,
259         cardnumber   => $patron->cardnumber,
260         categorycode => $category->id
261     });
262
263     throws_ok
264         { $new_patron->store }
265         'Koha::Exceptions::Object::DuplicateID',
266         'Exception is thrown correctly';
267
268     is(
269         $@->message,
270         'Duplicate ID',
271         'Exception message is correct'
272     );
273
274     is(
275        $@->duplicate_id,
276        'cardnumber',
277        'Exception field is correct'
278     );
279
280     $new_patron = Koha::Patron->new({
281         branchcode   => $library->id,
282         userid       => $patron->userid,
283         categorycode => $category->id
284     });
285
286     throws_ok
287         { $new_patron->store }
288         'Koha::Exceptions::Object::DuplicateID',
289         'Exception is thrown correctly';
290
291     is(
292         $@->message,
293         'Duplicate ID',
294         'Exception message is correct'
295     );
296
297     is(
298        $@->duplicate_id,
299        'userid',
300        'Exception field is correct'
301     );
302
303     $schema->storage->dbh->{PrintError} = $print_error;
304
305     # Successful test
306     $patron->set({ firstname => 'Manuel' });
307     my $ret = $patron->store;
308     is( ref($ret), 'Koha::Patron', 'store() returns the object on success' );
309
310     $schema->storage->txn_rollback;
311 };