Bug 27920: Add ability to update patron expiration dates when importing patrons
[koha.git] / t / db_dependent / Koha / ArticleRequest.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 => 5;
21 use Test::MockModule;
22
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use Koha::ArticleRequests;
27
28 my $schema  = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
30
31 subtest 'request() tests' => sub {
32
33     plan tests => 11;
34
35     $schema->storage->txn_begin;
36
37     my $amount = 0;
38
39     my $patron_mock = Test::MockModule->new('Koha::Patron');
40     $patron_mock->mock( 'article_request_fee', sub { return $amount; } );
41
42     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
43     my $item   = $builder->build_sample_item;
44
45     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
46     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
47
48     my $ar = Koha::ArticleRequest->new(
49         {
50             borrowernumber => $patron->id,
51             biblionumber   => $item->biblionumber,
52         }
53     );
54
55     $ar->request()->discard_changes;
56
57     is( $ar->status, Koha::ArticleRequest::Status::Requested );
58     ok( defined $ar->created_on, 'created_on is set' );
59
60     is( $ar->debit_id, undef, 'No fee linked' );
61     is( $patron->account->balance, 0, 'No outstanding fees' );
62
63     # set a fee amount
64     $amount = 10;
65
66     $ar = Koha::ArticleRequest->new(
67         {
68             borrowernumber => $patron->id,
69             biblionumber   => $item->biblionumber,
70             itemnumber     => $item->id,
71         }
72     );
73
74     $ar->request()->discard_changes;
75
76     is( $ar->status, Koha::ArticleRequest::Status::Requested );
77     is( $ar->itemnumber, $item->id, 'itemnumber set' );
78     ok( defined $ar->created_on, 'created_on is set' );
79
80     ok( defined $ar->debit_id, 'Fee linked' );
81     is( $patron->account->balance, $amount, 'Outstanding fees with the right value' );
82
83     $schema->storage->txn_rollback;
84 };
85
86 subtest 'set_pending() tests' => sub {
87
88     plan tests => 3;
89
90     $schema->storage->txn_begin;
91
92     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
93     my $biblio = $builder->build_sample_biblio;
94
95     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
96     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
97
98     my $ar = Koha::ArticleRequest->new(
99         {
100             borrowernumber => $patron->id,
101             biblionumber   => $biblio->id,
102         }
103     );
104
105     $ar->set_pending()->discard_changes;
106
107     is( $ar->status, Koha::ArticleRequest::Status::Pending );
108     ok( defined $ar->created_on, 'created_on is set' );
109
110     $schema->storage->txn_rollback;
111 };
112
113 subtest 'process() tests' => sub {
114
115     plan tests => 2;
116
117     $schema->storage->txn_begin;
118
119     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
120     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
121
122     my $ar = $builder->build_object(
123         {   class => 'Koha::ArticleRequests',
124             value => { status => Koha::ArticleRequest::Status::Requested }
125         }
126     );
127
128     $ar->process()->discard_changes;
129
130     is( $ar->status, Koha::ArticleRequest::Status::Processing );
131
132     $schema->storage->txn_rollback;
133 };
134
135 subtest 'complete() tests' => sub {
136
137     plan tests => 2;
138
139     $schema->storage->txn_begin;
140
141     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
142     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
143
144     my $ar = $builder->build_object(
145         {   class => 'Koha::ArticleRequests',
146             value => { status => Koha::ArticleRequest::Status::Requested }
147         }
148     );
149
150     $ar->complete()->discard_changes;
151
152     is( $ar->status, Koha::ArticleRequest::Status::Completed );
153
154     $schema->storage->txn_rollback;
155 };
156
157 subtest 'cancel() tests' => sub {
158
159     plan tests => 11;
160
161     $schema->storage->txn_begin;
162
163     my $amount = 11;
164
165     my $patron_mock = Test::MockModule->new('Koha::Patron');
166     $patron_mock->mock( 'article_request_fee', sub { return $amount; } );
167
168     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
169     my $item   = $builder->build_sample_item;
170
171     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
172     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
173
174     my $ar = Koha::ArticleRequest->new(
175         {
176             borrowernumber => $patron->id,
177             biblionumber   => $item->biblionumber,
178             itemnumber     => $item->id,
179         }
180     );
181
182     $ar->request()->discard_changes;
183
184     is( $ar->status, Koha::ArticleRequest::Status::Requested );
185     is( $ar->itemnumber, $item->id, 'itemnumber set' );
186     ok( defined $ar->debit_id, 'Fee linked' );
187     is( $patron->account->balance, $amount, 'Outstanding fees with the right value' );
188
189     my $payed_amount = 5;
190     $patron->account->pay({ amount => $payed_amount, interface => 'intranet', lines => [ $ar->debit ] });
191     is( $patron->account->balance, $amount - $payed_amount, 'Outstanding fees with the right value' );
192
193     my $reason = "Hey, ho";
194     my $notes  = "Let's go!";
195
196     $ar->cancel({ cancellation_reason => $reason, notes => $notes })->discard_changes;
197
198     is( $ar->status, Koha::ArticleRequest::Status::Canceled );
199     is( $ar->cancellation_reason, $reason, 'Cancellation reason stored correctly' );
200     is( $ar->notes, $notes, 'Notes stored correctly' );
201
202     is( $patron->account->balance, -$payed_amount, 'The patron has a credit balance' );
203
204     $schema->storage->txn_rollback;
205 };