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 => 7;
25 use Koha::DateUtils qw( dt_from_string );
27 use t::lib::TestBuilder;
30 use_ok('Koha::Object');
31 use_ok('Koha::Patron');
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
37 my $categorycode = $schema->resultset('Category')->first()->categorycode();
38 my $branchcode = $schema->resultset('Branch')->first()->branchcode();
40 subtest 'is_changed' => sub {
42 my $object = Koha::Patron->new();
43 $object->categorycode( $categorycode );
44 $object->branchcode( $branchcode );
45 $object->surname("Test Surname");
47 is( $object->is_changed(), 0, "Object is unchanged" );
48 $object->surname("Test Surname");
49 is( $object->is_changed(), 0, "Object is still unchanged" );
50 $object->surname("Test Surname 2");
51 is( $object->is_changed(), 1, "Object is changed" );
54 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
56 $object->set({ firstname => 'Test Firstname' });
57 is( $object->is_changed(), 1, "Object is changed after Set" );
59 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
62 subtest 'in_storage' => sub {
64 my $object = Koha::Patron->new();
65 is( $object->in_storage, 0, "Object is not in storage" );
66 $object->categorycode( $categorycode );
67 $object->branchcode( $branchcode );
68 $object->surname("Test Surname");
70 is( $object->in_storage, 1, "Object is now stored" );
71 $object->surname("another surname");
72 is( $object->in_storage, 1 );
74 my $borrowernumber = $object->borrowernumber;
75 my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
76 is( $patron->surname(), "Test Surname", "Object found in database" );
79 $patron = $schema->resultset('Borrower')->find( $borrowernumber );
80 ok( ! $patron, "Object no longer found in database" );
81 is( $object->in_storage, 0, "Object is not in storage" );
86 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
87 is( $patron->id, $patron->borrowernumber );
90 subtest 'get_column' => sub {
92 my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
93 is( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
96 subtest 'discard_changes' => sub {
98 my $builder = t::lib::TestBuilder->new;
99 my $patron = $builder->build( { source => 'Borrower' } );
100 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
101 $patron->dateexpiry(dt_from_string);
102 $patron->discard_changes;
104 dt_from_string( $patron->dateexpiry ),
105 dt_from_string->truncate( to => 'day' ),
106 'discard_changes should refresh the object'