Bug 14334: Remove AutoCommit from tests
[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 => 11;
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
40 BEGIN {
41     use_ok('Koha::Object');
42     use_ok('Koha::Patron');
43 }
44
45 my $schema  = Koha::Database->new->schema;
46 my $builder = t::lib::TestBuilder->new();
47
48 subtest 'is_changed / make_column_dirty' => sub {
49     plan tests => 11;
50
51     $schema->storage->txn_begin;
52
53     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
54     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
55
56     my $object = Koha::Patron->new();
57     $object->categorycode( $categorycode );
58     $object->branchcode( $branchcode );
59     $object->surname("Test Surname");
60     $object->store();
61     is( $object->is_changed(), 0, "Object is unchanged" );
62     $object->surname("Test Surname");
63     is( $object->is_changed(), 0, "Object is still unchanged" );
64     $object->surname("Test Surname 2");
65     is( $object->is_changed(), 1, "Object is changed" );
66
67     $object->store();
68     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
69
70     $object->set({ firstname => 'Test Firstname' });
71     is( $object->is_changed(), 1, "Object is changed after Set" );
72     $object->store();
73     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
74
75     # Test make_column_dirty
76     is( $object->make_column_dirty('firstname'), '', 'make_column_dirty returns empty string on success' );
77     is( $object->make_column_dirty('firstname'), 1, 'make_column_dirty returns 1 if already dirty' );
78     is( $object->is_changed, 1, "Object is changed after make dirty" );
79     $object->store;
80     is( $object->is_changed, 0, "Store clears dirty mark" );
81     $object->make_column_dirty('firstname');
82     $object->discard_changes;
83     is( $object->is_changed, 0, "Discard clears dirty mark too" );
84
85     $schema->storage->txn_rollback;
86 };
87
88 subtest 'in_storage' => sub {
89     plan tests => 6;
90
91     $schema->storage->txn_begin;
92
93     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
94     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
95
96     my $object = Koha::Patron->new();
97     is( $object->in_storage, 0, "Object is not in storage" );
98     $object->categorycode( $categorycode );
99     $object->branchcode( $branchcode );
100     $object->surname("Test Surname");
101     $object->store();
102     is( $object->in_storage, 1, "Object is now stored" );
103     $object->surname("another surname");
104     is( $object->in_storage, 1 );
105
106     my $borrowernumber = $object->borrowernumber;
107     my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
108     is( $patron->surname(), "Test Surname", "Object found in database" );
109
110     $object->delete();
111     $patron = $schema->resultset('Borrower')->find( $borrowernumber );
112     ok( ! $patron, "Object no longer found in database" );
113     is( $object->in_storage, 0, "Object is not in storage" );
114
115     $schema->storage->txn_rollback;
116 };
117
118 subtest 'id' => sub {
119     plan tests => 1;
120
121     $schema->storage->txn_begin;
122
123     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
124     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
125
126     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
127     is( $patron->id, $patron->borrowernumber );
128
129     $schema->storage->txn_rollback;
130 };
131
132 subtest 'get_column' => sub {
133     plan tests => 1;
134
135     $schema->storage->txn_begin;
136
137     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
138     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
139
140     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
141     is( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
142
143     $schema->storage->txn_rollback;
144 };
145
146 subtest 'discard_changes' => sub {
147     plan tests => 1;
148
149     $schema->storage->txn_begin;
150
151     my $patron = $builder->build( { source => 'Borrower' } );
152     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
153     $patron->dateexpiry(dt_from_string);
154     $patron->discard_changes;
155     is(
156         dt_from_string( $patron->dateexpiry ),
157         dt_from_string->truncate( to => 'day' ),
158         'discard_changes should refresh the object'
159     );
160
161     $schema->storage->txn_rollback;
162 };
163
164 subtest 'TO_JSON tests' => sub {
165
166     plan tests => 8;
167
168     $schema->storage->txn_begin;
169
170     my $dt = dt_from_string();
171     my $borrowernumber = $builder->build(
172         { source => 'Borrower',
173           value => { lost => 1,
174                      sms_provider_id => undef,
175                      gonenoaddress => 0,
176                      updated_on => $dt,
177                      lastseen   => $dt, } })->{borrowernumber};
178
179     my $patron = Koha::Patrons->find($borrowernumber);
180     my $lost = $patron->TO_JSON()->{lost};
181     my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
182     my $updated_on = $patron->TO_JSON->{updated_on};
183     my $lastseen = $patron->TO_JSON->{lastseen};
184
185     ok( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
186     is( $lost, 1, 'Boolean attribute value is correct (true)' );
187
188     ok( $gonenoaddress->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
189     is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
190
191     is( $patron->TO_JSON->{sms_provider_id}, undef, 'Undef values should not be casted to 0' );
192
193     ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
194
195     my $rfc3999_regex = qr/
196             (?<year>\d{4})
197             -
198             (?<month>\d{2})
199             -
200             (?<day>\d{2})
201             ([Tt\s])
202             (?<hour>\d{2})
203             :
204             (?<minute>\d{2})
205             :
206             (?<second>\d{2})
207             (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))
208         /xms;
209     like( $updated_on, $rfc3999_regex, "Date-time $updated_on formatted correctly");
210     like( $lastseen, $rfc3999_regex, "Date-time $updated_on formatted correctly");
211
212     $schema->storage->txn_rollback;
213 };
214
215 subtest "Test update method" => sub {
216     plan tests => 6;
217
218     $schema->storage->txn_begin;
219
220     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
221     my $library = Koha::Libraries->find( $branchcode );
222     $library->update({ branchname => 'New_Name', branchcity => 'AMS' });
223     is( $library->branchname, 'New_Name', 'Changed name with update' );
224     is( $library->branchcity, 'AMS', 'Changed city too' );
225     is( $library->is_changed, 0, 'Change should be stored already' );
226     try {
227         $library->update({
228             branchcity => 'NYC', not_a_column => 53, branchname => 'Name3',
229         });
230         fail( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
231     } catch {
232         ok( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
233         $library->discard_changes; #requery after failing update
234     };
235     # Check if the columns are not updated
236     is( $library->branchcity, 'AMS', 'First column not updated' );
237     is( $library->branchname, 'New_Name', 'Third column not updated' );
238
239     $schema->storage->txn_rollback;
240 };
241
242 subtest 'store() tests' => sub {
243
244     plan tests => 7;
245
246     # Using Koha::ApiKey to test Koha::Object>-store
247     # Simple object with foreign keys and unique key
248
249     $schema->storage->txn_begin;
250
251     # Create a patron to make sure its ID doesn't exist on the DB
252     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
253     my $patron_id = $patron->id;
254     $patron->delete;
255
256     my $api_key = Koha::ApiKey->new({ patron_id => $patron_id, secret => 'a secret', description => 'a description' });
257
258     my $print_error = $schema->storage->dbh->{PrintError};
259     $schema->storage->dbh->{PrintError} = 0;
260     throws_ok
261         { $api_key->store }
262         'Koha::Exceptions::Object::FKConstraint',
263         'Exception is thrown correctly';
264     is(
265         $@->message,
266         "Broken FK constraint",
267         'Exception message is correct'
268     );
269     is(
270         $@->broken_fk,
271         'patron_id',
272         'Exception field is correct'
273     );
274
275     $patron = $builder->build_object({ class => 'Koha::Patrons' });
276     $api_key = $builder->build_object({ class => 'Koha::ApiKeys' });
277
278     my $new_api_key = Koha::ApiKey->new({
279         patron_id => $patron_id,
280         secret => $api_key->secret,
281         description => 'a description',
282     });
283
284     throws_ok
285         { $new_api_key->store }
286         'Koha::Exceptions::Object::DuplicateID',
287         'Exception is thrown correctly';
288
289     is(
290         $@->message,
291         'Duplicate ID',
292         'Exception message is correct'
293     );
294
295     is(
296        $@->duplicate_id,
297        'secret',
298        'Exception field is correct'
299     );
300
301     $schema->storage->dbh->{PrintError} = $print_error;
302
303     # Successful test
304     $api_key->set({ secret => 'Manuel' });
305     my $ret = $api_key->store;
306     is( ref($ret), 'Koha::ApiKey', 'store() returns the object on success' );
307
308     $schema->storage->txn_rollback;
309 };
310
311 subtest 'unblessed_all_relateds' => sub {
312     plan tests => 3;
313
314     $schema->storage->txn_begin;
315
316     # FIXME It's very painful to create an issue in tests!
317     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
318     C4::Context->_new_userenv('xxx');
319     C4::Context->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Midway Public Library', '', '', '');
320     my $patron_category = $builder->build(
321         {
322             source => 'Category',
323             value  => {
324                 category_type                 => 'P',
325                 enrolmentfee                  => 0,
326                 BlockExpiredPatronOpacActions => -1, # Pick the pref value
327             }
328         }
329     );
330     my $patron_data = {
331         firstname =>  'firstname',
332         surname => 'surname',
333         categorycode => $patron_category->{categorycode},
334         branchcode => $library->branchcode,
335     };
336     my $patron = Koha::Patron->new($patron_data)->store;
337     my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
338     my $biblio = Koha::Biblios->find( $biblionumber );
339     my $item = $builder->build_object(
340         {
341             class => 'Koha::Items',
342             value => {
343                 homebranch    => $library->branchcode,
344                 holdingbranch => $library->branchcode,
345                 biblionumber  => $biblio->biblionumber,
346                 itemlost      => 0,
347                 withdrawn     => 0,
348             }
349         }
350     );
351
352     my $issue = AddIssue( $patron->unblessed, $item->barcode, DateTime->now->subtract( days => 1 ) );
353     my $overdues = Koha::Patrons->find( $patron->id )->get_overdues; # Koha::Patron->get_overdue prefetches
354     my $overdue = $overdues->next->unblessed_all_relateds;
355     is( $overdue->{issue_id}, $issue->issue_id, 'unblessed_all_relateds has field from the original table (issues)' );
356     is( $overdue->{title}, $biblio->title, 'unblessed_all_relateds has field from other tables (biblio)' );
357     is( $overdue->{homebranch}, $item->homebranch, 'unblessed_all_relateds has field from other tables (items)' );
358
359     $schema->storage->txn_rollback;
360 };