Bug 17594: Make Koha::Object->discard_changes available
[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 => 7;
21 use Test::Warn;
22
23 use C4::Context;
24 use Koha::Database;
25 use Koha::DateUtils qw( dt_from_string );
26
27 use t::lib::TestBuilder;
28
29 BEGIN {
30     use_ok('Koha::Object');
31     use_ok('Koha::Patron');
32 }
33
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36
37 my $categorycode = $schema->resultset('Category')->first()->categorycode();
38 my $branchcode = $schema->resultset('Branch')->first()->branchcode();
39
40 subtest 'is_changed' => sub {
41     plan tests => 6;
42     my $object = Koha::Patron->new();
43     $object->categorycode( $categorycode );
44     $object->branchcode( $branchcode );
45     $object->surname("Test Surname");
46     $object->store();
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" );
52
53     $object->store();
54     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
55
56     $object->set({ firstname => 'Test Firstname' });
57     is( $object->is_changed(), 1, "Object is changed after Set" );
58     $object->store();
59     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
60 };
61
62 subtest 'in_storage' => sub {
63     plan tests => 6;
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");
69     $object->store();
70     is( $object->in_storage, 1, "Object is now stored" );
71     $object->surname("another surname");
72     is( $object->in_storage, 1 );
73
74     my $borrowernumber = $object->borrowernumber;
75     my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
76     is( $patron->surname(), "Test Surname", "Object found in database" );
77
78     $object->delete();
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" );
82 };
83
84 subtest 'id' => sub {
85     plan tests => 1;
86     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
87     is( $patron->id, $patron->borrowernumber );
88 };
89
90 subtest 'get_column' => sub {
91     plan tests => 1;
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' );
94 };
95
96 subtest 'discard_changes' => sub {
97     plan tests => 1;
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;
103     is(
104         dt_from_string( $patron->dateexpiry ),
105         dt_from_string->truncate( to => 'day' ),
106         'discard_changes should refresh the object'
107     );
108 };
109
110 1;