Bug 18290: Fix t/db_dependent/Koha/Object.t, Mojo::JSON::Bool is a JSON::PP::Boolean :)
[koha.git] / t / db_dependent / Koha / Object.t
bloba506b89ccc48d1179048d342b541a4f58a371d99
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 9;
21 use Test::Warn;
23 use C4::Context;
24 use Koha::Database;
25 use Koha::DateUtils qw( dt_from_string );
26 use Koha::Libraries;
28 use Scalar::Util qw( isvstring );
29 use Try::Tiny;
31 use t::lib::TestBuilder;
33 BEGIN {
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 {
42 plan tests => 6;
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");
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" );
60 $object->store();
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" );
65 $object->store();
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 {
72 plan tests => 6;
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");
84 $object->store();
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" );
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" );
98 $schema->storage->txn_rollback;
101 subtest 'id' => sub {
102 plan tests => 1;
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 {
116 plan tests => 1;
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 {
130 plan tests => 1;
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 {
149 plan tests => 5;
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 {
174 plan tests => 6;
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' );
184 try {
185 $library->update({
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' );
189 } catch {
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;