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>.
20 use Test::More tests => 10;
26 use Koha::DateUtils qw( dt_from_string );
29 use Scalar::Util qw( isvstring );
32 use t::lib::TestBuilder;
35 use_ok('Koha::Object');
36 use_ok('Koha::Patron');
39 my $schema = Koha::Database->new->schema;
40 my $builder = t::lib::TestBuilder->new();
42 subtest 'is_changed' => sub {
45 $schema->storage->txn_begin;
47 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
48 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
50 my $object = Koha::Patron->new();
51 $object->categorycode( $categorycode );
52 $object->branchcode( $branchcode );
53 $object->surname("Test Surname");
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" );
62 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
64 $object->set({ firstname => 'Test Firstname' });
65 is( $object->is_changed(), 1, "Object is changed after Set" );
67 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
69 $schema->storage->txn_rollback;
72 subtest 'in_storage' => sub {
75 $schema->storage->txn_begin;
77 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
78 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
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");
86 is( $object->in_storage, 1, "Object is now stored" );
87 $object->surname("another surname");
88 is( $object->in_storage, 1 );
90 my $borrowernumber = $object->borrowernumber;
91 my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
92 is( $patron->surname(), "Test Surname", "Object found in database" );
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" );
99 $schema->storage->txn_rollback;
102 subtest 'id' => sub {
105 $schema->storage->txn_begin;
107 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
108 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
110 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
111 is( $patron->id, $patron->borrowernumber );
113 $schema->storage->txn_rollback;
116 subtest 'get_column' => sub {
119 $schema->storage->txn_begin;
121 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
122 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
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' );
127 $schema->storage->txn_rollback;
130 subtest 'discard_changes' => sub {
133 $schema->storage->txn_begin;
135 my $patron = $builder->build( { source => 'Borrower' } );
136 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
137 $patron->dateexpiry(dt_from_string);
138 $patron->discard_changes;
140 dt_from_string( $patron->dateexpiry ),
141 dt_from_string->truncate( to => 'day' ),
142 'discard_changes should refresh the object'
145 $schema->storage->txn_rollback;
148 subtest 'TO_JSON tests' => sub {
152 $schema->storage->txn_begin;
154 my $dt = dt_from_string();
155 my $borrowernumber = $builder->build(
156 { source => 'Borrower',
157 value => { lost => 1,
160 lastseen => $dt, } })->{borrowernumber};
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};
168 ok( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
169 is( $lost, 1, 'Boolean attribute value is correct (true)' );
171 ok( $gonenoaddress->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
172 is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
174 ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
176 my $rfc3999_regex = qr/
188 (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))
190 like( $updated_on, $rfc3999_regex, "Date-time $updated_on formatted correctly");
191 like( $lastseen, $rfc3999_regex, "Date-time $updated_on formatted correctly");
193 $schema->storage->txn_rollback;
196 subtest "Test update method" => sub {
199 $schema->storage->txn_begin;
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' );
209 branchcity => 'NYC', not_a_column => 53, branchname => 'Name3',
211 fail( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
213 ok( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
214 $library->discard_changes; #requery after failing update
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' );
220 $schema->storage->txn_rollback;
223 subtest 'store() tests' => sub {
227 $schema->storage->txn_begin;
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;
234 my $patron = Koha::Patron->new({ categorycode => $category_id });
236 my $print_error = $schema->storage->dbh->{PrintError};
237 $schema->storage->dbh->{PrintError} = 0;
240 'Koha::Exceptions::Object::FKConstraint',
241 'Exception is thrown correctly';
244 "Broken FK constraint",
245 'Exception message is correct'
250 'Exception field is correct'
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' });
257 my $new_patron = Koha::Patron->new({
258 branchcode => $library->id,
259 cardnumber => $patron->cardnumber,
260 categorycode => $category->id
264 { $new_patron->store }
265 'Koha::Exceptions::Object::DuplicateID',
266 'Exception is thrown correctly';
271 'Exception message is correct'
277 'Exception field is correct'
280 $new_patron = Koha::Patron->new({
281 branchcode => $library->id,
282 userid => $patron->userid,
283 categorycode => $category->id
287 { $new_patron->store }
288 'Koha::Exceptions::Object::DuplicateID',
289 'Exception is thrown correctly';
294 'Exception message is correct'
300 'Exception field is correct'
303 $schema->storage->dbh->{PrintError} = $print_error;
306 $patron->set({ firstname => 'Manuel' });
307 my $ret = $patron->store;
308 is( ref($ret), 'Koha::Patron', 'store() returns the object on success' );
310 $schema->storage->txn_rollback;