Bug 24815: Add additional tests
[koha.git] / t / db_dependent / Koha / Cash / Register.t
blob93fe363f3c46e27fb2fb13ef4aee159eec039144
1 #!/usr/bin/perl
3 # Copyright 2019 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
20 use Modern::Perl;
22 use Test::More tests => 4;
24 use Test::Exception;
26 use Koha::Database;
28 use t::lib::TestBuilder;
30 my $builder = t::lib::TestBuilder->new;
31 my $schema = Koha::Database->new->schema;
33 subtest 'library' => sub {
34 plan tests => 2;
36 $schema->storage->txn_begin;
38 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
39 my $register = $builder->build_object(
41 class => 'Koha::Cash::Registers',
42 value => { branch => $library->branchcode },
46 is( ref( $register->library ),
47 'Koha::Library',
48 'Koha::Cash::Register->library should return a Koha::Library' );
50 is( $register->library->id,
51 $library->id,
52 'Koha::Cash::Register->library returns the correct Koha::Library' );
54 $schema->storage->txn_rollback;
57 subtest 'accountlines' => sub {
58 plan tests => 5;
60 $schema->storage->txn_begin;
62 my $register =
63 $builder->build_object( { class => 'Koha::Cash::Registers' } );
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'
69 is( $accountlines->count, 0,
70 'Koha::Cash::Register->accountlines should always return the correct number of accountlines'
73 my $accountline1 = $builder->build_object(
75 class => 'Koha::Account::Lines',
76 value => { register_id => $register->id },
79 my $accountline2 = $builder->build_object(
81 class => 'Koha::Account::Lines',
82 value => { register_id => $register->id },
86 $accountlines = $register->accountlines;
87 is( ref($accountlines), 'Koha::Account::Lines',
88 'Koha::Cash::Register->accountlines should return a set of Koha::Account::Lines'
90 is( $accountlines->count, 2,
91 'Koha::Cash::Register->accountlines should return the correct number of accountlines'
94 $accountline1->delete;
95 is( $register->accountlines->next->id, $accountline2->id,
96 'Koha::Cash::Register->accountlines should return the correct acocuntlines'
99 $schema->storage->txn_rollback;
102 subtest 'branch_default' => sub {
103 plan tests => 3;
105 $schema->storage->txn_begin;
106 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
107 my $register1 = $builder->build_object(
109 class => 'Koha::Cash::Registers',
110 value => { branch => $library->branchcode, branch_default => 1 },
113 my $register2 = $builder->build_object(
115 class => 'Koha::Cash::Registers',
116 value => { branch => $library->branchcode, branch_default => 0 },
120 subtest 'store' => sub {
121 plan tests => 2;
123 $register1->name('Test till 1');
124 ok( $register1->store(),
125 "Store works as expected when branch_default is not changed" );
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';
134 subtest 'make_default' => sub {
135 plan tests => 3;
137 ok( $register2->make_default, 'Koha::Register->make_default ran' );
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' );
145 subtest 'drop_default' => sub {
146 plan tests => 2;
148 ok( $register2->drop_default, 'Koha::Register->drop_default ran' );
150 $register2 = $register2->get_from_storage;
151 is( $register2->branch_default, 0, 'register2 was unset as expected' );
154 $schema->storage->txn_rollback;
157 subtest 'cashup' => sub {
158 plan tests => 4;
160 $schema->storage->txn_begin;
162 my $register =
163 $builder->build_object( { class => 'Koha::Cash::Registers' } );
164 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
166 my $cashup1;
167 subtest 'add_cashup' => sub {
168 plan tests => 6;
171 $cashup1 = $register->add_cashup(
172 { manager_id => $patron->id, amount => '12.00' }
174 'call successfull'
178 ref($cashup1),
179 'Koha::Cash::Register::Action',
180 'return is Koha::Cash::Register::Action'
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' );
192 subtest 'last_cashup' => sub {
193 plan tests => 3;
195 my $cashup2 =
196 $register->add_cashup( { manager_id => $patron->id, amount => '6.00' } );
198 my $last_cashup = $register->last_cashup;
200 ref($last_cashup),
201 'Koha::Cash::Register::Action',
202 'A cashup was returned when one existed'
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' );
212 subtest 'cashups' => sub {
213 plan tests => 4;
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'
219 is( $cashups->count, 0,
220 'Koha::Cash::Register->cashups should always return the correct number of cashups'
223 my $cashup3 =
224 $register->add_cashup( { manager_id => $patron->id, amount => '6.00' } );
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'
230 is( $cashups->count, 1,
231 'Koha::Cash::Register->cashups should return the correct number of cashups'
234 $cashup3->delete;
237 subtest 'outstanding_accountlines' => sub {
238 plan tests => 6;
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'
244 is( $accountlines->count, 0,
245 'Koha::Cash::Register->outstanding_accountlines should always return the correct number of accountlines'
248 my $accountline1 = $builder->build_object(
250 class => 'Koha::Account::Lines',
251 value => { register_id => $register->id, date => \'NOW() - INTERVAL 5 MINUTE' },
254 my $accountline2 = $builder->build_object(
256 class => 'Koha::Account::Lines',
257 value => { register_id => $register->id, date => \'NOW() - INTERVAL 5 MINUTE'},
261 $accountlines = $register->outstanding_accountlines;
262 is( $accountlines->count, 2, 'No cashup, all accountlines returned' );
264 my $cashup3 =
265 $register->add_cashup( { manager_id => $patron->id, amount => '2.50' } );
267 $accountlines = $register->outstanding_accountlines;
268 is( $accountlines->count, 0, 'Cashup added, no accountlines returned' );
270 my $accountline3 = $builder->build_object(
272 class => 'Koha::Account::Lines',
273 value => { register_id => $register->id },
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;
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' );
288 $schema->storage->txn_rollback;