Bug 18174: Add update to Koha::Object
[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 => 9;
21 use Test::Warn;
22
23 use C4::Context;
24 use Koha::Database;
25 use Koha::DateUtils qw( dt_from_string );
26 use Koha::Libraries;
27
28 use Scalar::Util qw( isvstring );
29 use Try::Tiny;
30
31 use t::lib::TestBuilder;
32
33 BEGIN {
34     use_ok('Koha::Object');
35     use_ok('Koha::Patron');
36 }
37
38 my $schema  = Koha::Database->new->schema;
39 my $builder = t::lib::TestBuilder->new();
40
41 subtest 'is_changed' => sub {
42     plan tests => 6;
43
44     $schema->storage->txn_begin;
45
46     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
47     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
48
49     my $object = Koha::Patron->new();
50     $object->categorycode( $categorycode );
51     $object->branchcode( $branchcode );
52     $object->surname("Test Surname");
53     $object->store();
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" );
59
60     $object->store();
61     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
62
63     $object->set({ firstname => 'Test Firstname' });
64     is( $object->is_changed(), 1, "Object is changed after Set" );
65     $object->store();
66     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
67
68     $schema->storage->txn_rollback;
69 };
70
71 subtest 'in_storage' => sub {
72     plan tests => 6;
73
74     $schema->storage->txn_begin;
75
76     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
77     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
78
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");
84     $object->store();
85     is( $object->in_storage, 1, "Object is now stored" );
86     $object->surname("another surname");
87     is( $object->in_storage, 1 );
88
89     my $borrowernumber = $object->borrowernumber;
90     my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
91     is( $patron->surname(), "Test Surname", "Object found in database" );
92
93     $object->delete();
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" );
97
98     $schema->storage->txn_rollback;
99 };
100
101 subtest 'id' => sub {
102     plan tests => 1;
103
104     $schema->storage->txn_begin;
105
106     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
107     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
108
109     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
110     is( $patron->id, $patron->borrowernumber );
111
112     $schema->storage->txn_rollback;
113 };
114
115 subtest 'get_column' => sub {
116     plan tests => 1;
117
118     $schema->storage->txn_begin;
119
120     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
121     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
122
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' );
125
126     $schema->storage->txn_rollback;
127 };
128
129 subtest 'discard_changes' => sub {
130     plan tests => 1;
131
132     $schema->storage->txn_begin;
133
134     my $patron = $builder->build( { source => 'Borrower' } );
135     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
136     $patron->dateexpiry(dt_from_string);
137     $patron->discard_changes;
138     is(
139         dt_from_string( $patron->dateexpiry ),
140         dt_from_string->truncate( to => 'day' ),
141         'discard_changes should refresh the object'
142     );
143
144     $schema->storage->txn_rollback;
145 };
146
147 subtest 'TO_JSON tests' => sub {
148
149     plan tests => 5;
150
151     $schema->storage->txn_begin;
152
153     my $borrowernumber = $builder->build(
154         { source => 'Borrower',
155           value => { lost => 1,
156                      gonenoaddress => 0 } })->{borrowernumber};
157
158     my $patron = Koha::Patrons->find($borrowernumber);
159     my $lost = $patron->TO_JSON()->{lost};
160     my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
161
162     ok( $lost->isa('Mojo::JSON::_Bool'), 'Boolean attribute type is correct' );
163     is( $lost, 1, 'Boolean attribute value is correct (true)' );
164
165     ok( $gonenoaddress->isa('Mojo::JSON::_Bool'), 'Boolean attribute type is correct' );
166     is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
167
168     ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
169
170     $schema->storage->txn_rollback;
171 };
172
173 subtest "Test update method" => sub {
174     plan tests => 6;
175
176     $schema->storage->txn_begin;
177
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' );
184     try {
185         $library->update({
186             branchcity => 'NYC', not_a_column => 53, branchname => 'Name3',
187         });
188         fail( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
189     } catch {
190         ok( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
191         $library->discard_changes; #requery after failing update
192     };
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' );
196
197     $schema->storage->txn_rollback;
198 };