Bug 24815: Add additional tests
[koha.git] / t / db_dependent / Koha / Cash / Register.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 4;
23
24 use Test::Exception;
25
26 use Koha::Database;
27
28 use t::lib::TestBuilder;
29
30 my $builder = t::lib::TestBuilder->new;
31 my $schema  = Koha::Database->new->schema;
32
33 subtest 'library' => sub {
34     plan tests => 2;
35
36     $schema->storage->txn_begin;
37
38     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
39     my $register = $builder->build_object(
40         {
41             class => 'Koha::Cash::Registers',
42             value => { branch => $library->branchcode },
43         }
44     );
45
46     is( ref( $register->library ),
47         'Koha::Library',
48         'Koha::Cash::Register->library should return a Koha::Library' );
49
50     is( $register->library->id,
51         $library->id,
52         'Koha::Cash::Register->library returns the correct Koha::Library' );
53
54     $schema->storage->txn_rollback;
55 };
56
57 subtest 'accountlines' => sub {
58     plan tests => 5;
59
60     $schema->storage->txn_begin;
61
62     my $register =
63       $builder->build_object( { class => 'Koha::Cash::Registers' } );
64
65     my $accountlines = $register->accountlines;
66     is( ref($accountlines), 'Koha::Account::Lines',
67 'Koha::Cash::Register->accountlines should always return a Koha::Account::Lines set'
68     );
69     is( $accountlines->count, 0,
70 'Koha::Cash::Register->accountlines should always return the correct number of accountlines'
71     );
72
73     my $accountline1 = $builder->build_object(
74         {
75             class => 'Koha::Account::Lines',
76             value => { register_id => $register->id },
77         }
78     );
79     my $accountline2 = $builder->build_object(
80         {
81             class => 'Koha::Account::Lines',
82             value => { register_id => $register->id },
83         }
84     );
85
86     $accountlines = $register->accountlines;
87     is( ref($accountlines), 'Koha::Account::Lines',
88 'Koha::Cash::Register->accountlines should return a set of Koha::Account::Lines'
89     );
90     is( $accountlines->count, 2,
91 'Koha::Cash::Register->accountlines should return the correct number of accountlines'
92     );
93
94     $accountline1->delete;
95     is( $register->accountlines->next->id, $accountline2->id,
96 'Koha::Cash::Register->accountlines should return the correct acocuntlines'
97     );
98
99     $schema->storage->txn_rollback;
100 };
101
102 subtest 'branch_default' => sub {
103     plan tests => 3;
104
105     $schema->storage->txn_begin;
106     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
107     my $register1 = $builder->build_object(
108         {
109             class => 'Koha::Cash::Registers',
110             value => { branch => $library->branchcode, branch_default => 1 },
111         }
112     );
113     my $register2 = $builder->build_object(
114         {
115             class => 'Koha::Cash::Registers',
116             value => { branch => $library->branchcode, branch_default => 0 },
117         }
118     );
119
120     subtest 'store' => sub {
121         plan tests => 2;
122
123         $register1->name('Test till 1');
124         ok( $register1->store(),
125             "Store works as expected when branch_default is not changed" );
126
127         $register1->branch_default(0);
128         throws_ok { $register1->store(); }
129         'Koha::Exceptions::Object::ReadOnlyProperty',
130           'Exception thrown if direct update to branch_default is attempted';
131
132     };
133
134     subtest 'make_default' => sub {
135         plan tests => 3;
136
137         ok( $register2->make_default, 'Koha::Register->make_default ran' );
138
139         $register1 = $register1->get_from_storage;
140         $register2 = $register2->get_from_storage;
141         is( $register1->branch_default, 0, 'register1 was unset as expected' );
142         is( $register2->branch_default, 1, 'register2 was set as expected' );
143     };
144
145     subtest 'drop_default' => sub {
146         plan tests => 2;
147
148         ok( $register2->drop_default, 'Koha::Register->drop_default ran' );
149
150         $register2 = $register2->get_from_storage;
151         is( $register2->branch_default, 0, 'register2 was unset as expected' );
152     };
153
154     $schema->storage->txn_rollback;
155 };
156
157 subtest 'cashup' => sub {
158     plan tests => 4;
159
160     $schema->storage->txn_begin;
161
162     my $register =
163       $builder->build_object( { class => 'Koha::Cash::Registers' } );
164     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
165
166     my $cashup1;
167     subtest 'add_cashup' => sub {
168         plan tests => 6;
169
170         ok(
171             $cashup1 = $register->add_cashup(
172                 { manager_id => $patron->id, amount => '12.00' }
173             ),
174             'call successfull'
175         );
176
177         is(
178             ref($cashup1),
179             'Koha::Cash::Register::Action',
180             'return is Koha::Cash::Register::Action'
181         );
182         is( $cashup1->code, 'CASHUP',
183             'CASHUP code set in Koha::Cash::Register::Action' );
184         is( $cashup1->manager_id, $patron->id,
185             'manager_id set correctly in Koha::Cash::Register::Action' );
186         is( $cashup1->amount, '12.000000',
187             'amount set correctly in Koha::Cash::Register::Action' );
188         isnt( $cashup1->timestamp, undef,
189             'timestamp set in Koha::Cash::Register::Action' );
190     };
191
192     subtest 'last_cashup' => sub {
193         plan tests => 3;
194
195         my $cashup2 =
196           $register->add_cashup( { manager_id => $patron->id, amount => '6.00' } );
197
198         my $last_cashup = $register->last_cashup;
199         is(
200             ref($last_cashup),
201             'Koha::Cash::Register::Action',
202             'A cashup was returned when one existed'
203         );
204         is( $last_cashup->id, $cashup2->id,
205             'The most recent cashup was returned' );
206         $cashup1->delete;
207         $cashup2->delete;
208         $last_cashup = $register->last_cashup;
209         is( $last_cashup, undef, 'undef is returned when no cashup exists' );
210     };
211
212     subtest 'cashups' => sub {
213         plan tests => 4;
214
215         my $cashups = $register->cashups;
216         is( ref($cashups), 'Koha::Cash::Register::Actions',
217 'Koha::Cash::Register->cashups should always return a Koha::Cash::Register::Actions set'
218         );
219         is( $cashups->count, 0,
220 'Koha::Cash::Register->cashups should always return the correct number of cashups'
221         );
222
223         my $cashup3 =
224           $register->add_cashup( { manager_id => $patron->id, amount => '6.00' } );
225
226         $cashups = $register->cashups;
227         is( ref($cashups), 'Koha::Cash::Register::Actions',
228 'Koha::Cash::Register->cashups should return a Koha::Cash::Register::Actions set'
229         );
230         is( $cashups->count, 1,
231 'Koha::Cash::Register->cashups should return the correct number of cashups'
232         );
233
234         $cashup3->delete;
235     };
236
237     subtest 'outstanding_accountlines' => sub {
238         plan tests => 6;
239
240         my $accountlines = $register->outstanding_accountlines;
241         is( ref($accountlines), 'Koha::Account::Lines',
242 'Koha::Cash::Register->outstanding_accountlines should always return a Koha::Account::Lines set'
243         );
244         is( $accountlines->count, 0,
245 'Koha::Cash::Register->outstanding_accountlines should always return the correct number of accountlines'
246         );
247
248         my $accountline1 = $builder->build_object(
249             {
250                 class => 'Koha::Account::Lines',
251                 value => { register_id => $register->id, date => \'NOW() - INTERVAL 5 MINUTE' },
252             }
253         );
254         my $accountline2 = $builder->build_object(
255             {
256                 class => 'Koha::Account::Lines',
257                 value => { register_id => $register->id, date => \'NOW() - INTERVAL 5 MINUTE'},
258             }
259         );
260
261         $accountlines = $register->outstanding_accountlines;
262         is( $accountlines->count, 2, 'No cashup, all accountlines returned' );
263
264         my $cashup3 =
265           $register->add_cashup( { manager_id => $patron->id, amount => '2.50' } );
266
267         $accountlines = $register->outstanding_accountlines;
268         is( $accountlines->count, 0, 'Cashup added, no accountlines returned' );
269
270         my $accountline3 = $builder->build_object(
271             {
272                 class => 'Koha::Account::Lines',
273                 value => { register_id => $register->id },
274             }
275         );
276
277         # Fake the cashup timestamp to make sure it's before the accountline we just added,
278         # we can't trust that these two actions are more than a second apart in a test
279         $cashup3->timestamp(\'NOW() - INTERVAL 2 MINUTE')->store;
280
281         $accountlines = $register->outstanding_accountlines;
282         is( $accountlines->count, 1,
283             'Accountline added, one accountline returned' );
284         is( $accountlines->next->id,
285             $accountline3->id, 'Correct accountline returned' );
286     };
287
288     $schema->storage->txn_rollback;
289 };