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
=> 9;
25 use Koha
::DateUtils
qw( dt_from_string );
28 use Scalar
::Util
qw( isvstring );
31 use t
::lib
::TestBuilder
;
34 use_ok
('Koha::Object');
35 use_ok
('Koha::Patron');
38 my $schema = Koha
::Database
->new->schema;
39 my $builder = t
::lib
::TestBuilder
->new();
41 subtest
'is_changed' => sub {
44 $schema->storage->txn_begin;
46 my $categorycode = $builder->build({ source
=> 'Category' })->{categorycode
};
47 my $branchcode = $builder->build({ source
=> 'Branch' })->{branchcode
};
49 my $object = Koha
::Patron
->new();
50 $object->categorycode( $categorycode );
51 $object->branchcode( $branchcode );
52 $object->surname("Test Surname");
54 is
( $object->is_changed(), 0, "Object is unchanged" );
55 $object->surname("Test Surname");
56 is
( $object->is_changed(), 0, "Object is still unchanged" );
57 $object->surname("Test Surname 2");
58 is
( $object->is_changed(), 1, "Object is changed" );
61 is
( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
63 $object->set({ firstname
=> 'Test Firstname' });
64 is
( $object->is_changed(), 1, "Object is changed after Set" );
66 is
( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
68 $schema->storage->txn_rollback;
71 subtest
'in_storage' => sub {
74 $schema->storage->txn_begin;
76 my $categorycode = $builder->build({ source
=> 'Category' })->{categorycode
};
77 my $branchcode = $builder->build({ source
=> 'Branch' })->{branchcode
};
79 my $object = Koha
::Patron
->new();
80 is
( $object->in_storage, 0, "Object is not in storage" );
81 $object->categorycode( $categorycode );
82 $object->branchcode( $branchcode );
83 $object->surname("Test Surname");
85 is
( $object->in_storage, 1, "Object is now stored" );
86 $object->surname("another surname");
87 is
( $object->in_storage, 1 );
89 my $borrowernumber = $object->borrowernumber;
90 my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
91 is
( $patron->surname(), "Test Surname", "Object found in database" );
94 $patron = $schema->resultset('Borrower')->find( $borrowernumber );
95 ok
( ! $patron, "Object no longer found in database" );
96 is
( $object->in_storage, 0, "Object is not in storage" );
98 $schema->storage->txn_rollback;
101 subtest
'id' => sub {
104 $schema->storage->txn_begin;
106 my $categorycode = $builder->build({ source
=> 'Category' })->{categorycode
};
107 my $branchcode = $builder->build({ source
=> 'Branch' })->{branchcode
};
109 my $patron = Koha
::Patron
->new({categorycode
=> $categorycode, branchcode
=> $branchcode })->store;
110 is
( $patron->id, $patron->borrowernumber );
112 $schema->storage->txn_rollback;
115 subtest
'get_column' => sub {
118 $schema->storage->txn_begin;
120 my $categorycode = $builder->build({ source
=> 'Category' })->{categorycode
};
121 my $branchcode = $builder->build({ source
=> 'Branch' })->{branchcode
};
123 my $patron = Koha
::Patron
->new({categorycode
=> $categorycode, branchcode
=> $branchcode })->store;
124 is
( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
126 $schema->storage->txn_rollback;
129 subtest
'discard_changes' => sub {
132 $schema->storage->txn_begin;
134 my $patron = $builder->build( { source
=> 'Borrower' } );
135 $patron = Koha
::Patrons
->find( $patron->{borrowernumber
} );
136 $patron->dateexpiry(dt_from_string
);
137 $patron->discard_changes;
139 dt_from_string
( $patron->dateexpiry ),
140 dt_from_string
->truncate( to
=> 'day' ),
141 'discard_changes should refresh the object'
144 $schema->storage->txn_rollback;
147 subtest
'TO_JSON tests' => sub {
151 $schema->storage->txn_begin;
153 my $borrowernumber = $builder->build(
154 { source
=> 'Borrower',
155 value
=> { lost
=> 1,
156 gonenoaddress
=> 0 } })->{borrowernumber
};
158 my $patron = Koha
::Patrons
->find($borrowernumber);
159 my $lost = $patron->TO_JSON()->{lost
};
160 my $gonenoaddress = $patron->TO_JSON->{gonenoaddress
};
162 ok
( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
163 is
( $lost, 1, 'Boolean attribute value is correct (true)' );
165 ok
( $gonenoaddress->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
166 is
( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
168 ok
( !isvstring
($patron->borrowernumber), 'Integer values are not coded as strings' );
170 $schema->storage->txn_rollback;
173 subtest
"Test update method" => sub {
176 $schema->storage->txn_begin;
178 my $branchcode = $builder->build({ source
=> 'Branch' })->{branchcode
};
179 my $library = Koha
::Libraries
->find( $branchcode );
180 $library->update({ branchname
=> 'New_Name', branchcity
=> 'AMS' });
181 is
( $library->branchname, 'New_Name', 'Changed name with update' );
182 is
( $library->branchcity, 'AMS', 'Changed city too' );
183 is
( $library->is_changed, 0, 'Change should be stored already' );
186 branchcity
=> 'NYC', not_a_column
=> 53, branchname
=> 'Name3',
188 fail
( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
190 ok
( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
191 $library->discard_changes; #requery after failing update
193 # Check if the columns are not updated
194 is
( $library->branchcity, 'AMS', 'First column not updated' );
195 is
( $library->branchname, 'New_Name', 'Third column not updated' );
197 $schema->storage->txn_rollback;