Bug 16966: move parameters to hashref
[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 => 8;
21 use Test::Warn;
22
23 use C4::Context;
24 use Koha::Database;
25 use Koha::DateUtils qw( dt_from_string );
26
27 use Scalar::Util qw( isvstring );
28
29 use t::lib::TestBuilder;
30
31 BEGIN {
32     use_ok('Koha::Object');
33     use_ok('Koha::Patron');
34 }
35
36 my $schema  = Koha::Database->new->schema;
37 my $builder = t::lib::TestBuilder->new();
38
39 subtest 'is_changed' => sub {
40     plan tests => 6;
41
42     $schema->storage->txn_begin;
43
44     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
45     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
46
47     my $object = Koha::Patron->new();
48     $object->categorycode( $categorycode );
49     $object->branchcode( $branchcode );
50     $object->surname("Test Surname");
51     $object->store();
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" );
57
58     $object->store();
59     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
60
61     $object->set({ firstname => 'Test Firstname' });
62     is( $object->is_changed(), 1, "Object is changed after Set" );
63     $object->store();
64     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
65
66     $schema->storage->txn_rollback;
67 };
68
69 subtest 'in_storage' => sub {
70     plan tests => 6;
71
72     $schema->storage->txn_begin;
73
74     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
75     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
76
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");
82     $object->store();
83     is( $object->in_storage, 1, "Object is now stored" );
84     $object->surname("another surname");
85     is( $object->in_storage, 1 );
86
87     my $borrowernumber = $object->borrowernumber;
88     my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
89     is( $patron->surname(), "Test Surname", "Object found in database" );
90
91     $object->delete();
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" );
95
96     $schema->storage->txn_rollback;
97 };
98
99 subtest 'id' => sub {
100     plan tests => 1;
101
102     $schema->storage->txn_begin;
103
104     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
105     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
106
107     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
108     is( $patron->id, $patron->borrowernumber );
109
110     $schema->storage->txn_rollback;
111 };
112
113 subtest 'get_column' => sub {
114     plan tests => 1;
115
116     $schema->storage->txn_begin;
117
118     my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
119     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
120
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' );
123
124     $schema->storage->txn_rollback;
125 };
126
127 subtest 'discard_changes' => sub {
128     plan tests => 1;
129
130     $schema->storage->txn_begin;
131
132     my $patron = $builder->build( { source => 'Borrower' } );
133     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
134     $patron->dateexpiry(dt_from_string);
135     $patron->discard_changes;
136     is(
137         dt_from_string( $patron->dateexpiry ),
138         dt_from_string->truncate( to => 'day' ),
139         'discard_changes should refresh the object'
140     );
141
142     $schema->storage->txn_rollback;
143 };
144
145 subtest 'TO_JSON tests' => sub {
146
147     plan tests => 5;
148
149     $schema->storage->txn_begin;
150
151     my $borrowernumber = $builder->build(
152         { source => 'Borrower',
153           value => { lost => 1,
154                      gonenoaddress => 0 } })->{borrowernumber};
155
156     my $patron = Koha::Patrons->find($borrowernumber);
157     my $lost = $patron->TO_JSON()->{lost};
158     my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
159
160     ok( $lost->isa('Mojo::JSON::_Bool'), 'Boolean attribute type is correct' );
161     is( $lost, 1, 'Boolean attribute value is correct (true)' );
162
163     ok( $gonenoaddress->isa('Mojo::JSON::_Bool'), 'Boolean attribute type is correct' );
164     is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
165
166     ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
167
168     $schema->storage->txn_rollback;
169 };
170
171
172
173 1;