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 => 8;
25 use Koha::DateUtils qw( dt_from_string );
27 use Scalar::Util qw( isvstring );
29 use t::lib::TestBuilder;
32 use_ok('Koha::Object');
33 use_ok('Koha::Patron');
36 my $schema = Koha::Database->new->schema;
37 my $builder = t::lib::TestBuilder->new();
39 subtest 'is_changed' => sub {
42 $schema->storage->txn_begin;
44 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
45 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
47 my $object = Koha::Patron->new();
48 $object->categorycode( $categorycode );
49 $object->branchcode( $branchcode );
50 $object->surname("Test Surname");
52 is( $object->is_changed(), 0, "Object is unchanged" );
53 $object->surname("Test Surname");
54 is( $object->is_changed(), 0, "Object is still unchanged" );
55 $object->surname("Test Surname 2");
56 is( $object->is_changed(), 1, "Object is changed" );
59 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
61 $object->set({ firstname => 'Test Firstname' });
62 is( $object->is_changed(), 1, "Object is changed after Set" );
64 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
66 $schema->storage->txn_rollback;
69 subtest 'in_storage' => sub {
72 $schema->storage->txn_begin;
74 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
75 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
77 my $object = Koha::Patron->new();
78 is( $object->in_storage, 0, "Object is not in storage" );
79 $object->categorycode( $categorycode );
80 $object->branchcode( $branchcode );
81 $object->surname("Test Surname");
83 is( $object->in_storage, 1, "Object is now stored" );
84 $object->surname("another surname");
85 is( $object->in_storage, 1 );
87 my $borrowernumber = $object->borrowernumber;
88 my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
89 is( $patron->surname(), "Test Surname", "Object found in database" );
92 $patron = $schema->resultset('Borrower')->find( $borrowernumber );
93 ok( ! $patron, "Object no longer found in database" );
94 is( $object->in_storage, 0, "Object is not in storage" );
96 $schema->storage->txn_rollback;
102 $schema->storage->txn_begin;
104 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
105 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
107 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
108 is( $patron->id, $patron->borrowernumber );
110 $schema->storage->txn_rollback;
113 subtest 'get_column' => sub {
116 $schema->storage->txn_begin;
118 my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
119 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
121 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
122 is( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
124 $schema->storage->txn_rollback;
127 subtest 'discard_changes' => sub {
130 $schema->storage->txn_begin;
132 my $patron = $builder->build( { source => 'Borrower' } );
133 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
134 $patron->dateexpiry(dt_from_string);
135 $patron->discard_changes;
137 dt_from_string( $patron->dateexpiry ),
138 dt_from_string->truncate( to => 'day' ),
139 'discard_changes should refresh the object'
142 $schema->storage->txn_rollback;
145 subtest 'TO_JSON tests' => sub {
149 $schema->storage->txn_begin;
151 my $borrowernumber = $builder->build(
152 { source => 'Borrower',
153 value => { lost => 1,
154 gonenoaddress => 0 } })->{borrowernumber};
156 my $patron = Koha::Patrons->find($borrowernumber);
157 my $lost = $patron->TO_JSON()->{lost};
158 my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
160 ok( $lost->isa('Mojo::JSON::_Bool'), 'Boolean attribute type is correct' );
161 is( $lost, 1, 'Boolean attribute value is correct (true)' );
163 ok( $gonenoaddress->isa('Mojo::JSON::_Bool'), 'Boolean attribute type is correct' );
164 is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
166 ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
168 $schema->storage->txn_rollback;