Bug 31503: (QA follow-up) Fix number of tests in rebase
[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 => 13;
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', value => { lastseen => undef } } );
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     t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', '' );
56     $ar->request()->discard_changes;
57
58     is( $ar->status, Koha::ArticleRequest::Status::Requested );
59     ok( defined $ar->created_on, 'created_on is set' );
60
61     is( $ar->debit_id, undef, 'No fee linked' );
62     is( $patron->account->balance, 0, 'No outstanding fees' );
63     $patron->discard_changes;
64     is( $patron->lastseen, undef, 'Patron activity not tracked when article is not a valid trigger' );
65
66     # set a fee amount
67     $amount = 10;
68
69     $ar = Koha::ArticleRequest->new(
70         {
71             borrowernumber => $patron->id,
72             biblionumber   => $item->biblionumber,
73             itemnumber     => $item->id,
74         }
75     );
76
77     t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'article' );
78     $ar->request()->discard_changes;
79
80     is( $ar->status, Koha::ArticleRequest::Status::Requested );
81     is( $ar->itemnumber, $item->id, 'itemnumber set' );
82     ok( defined $ar->created_on, 'created_on is set' );
83
84     ok( defined $ar->debit_id, 'Fee linked' );
85     is( $patron->account->balance, $amount, 'Outstanding fees with the right value' );
86     $patron->discard_changes;
87     isnt( $patron->lastseen, undef, 'Patron activity tracked when article is a valid trigger' );
88
89     $schema->storage->txn_rollback;
90 };
91
92 subtest 'set_pending() tests' => sub {
93
94     plan tests => 3;
95
96     $schema->storage->txn_begin;
97
98     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
99     my $biblio = $builder->build_sample_biblio;
100
101     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
102     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
103
104     my $ar = Koha::ArticleRequest->new(
105         {
106             borrowernumber => $patron->id,
107             biblionumber   => $biblio->id,
108         }
109     );
110
111     $ar->set_pending()->discard_changes;
112
113     is( $ar->status, Koha::ArticleRequest::Status::Pending );
114     ok( defined $ar->created_on, 'created_on is set' );
115
116     $schema->storage->txn_rollback;
117 };
118
119 subtest 'process() tests' => sub {
120
121     plan tests => 2;
122
123     $schema->storage->txn_begin;
124
125     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
126     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
127
128     my $ar = $builder->build_object(
129         {   class => 'Koha::ArticleRequests',
130             value => { status => Koha::ArticleRequest::Status::Requested }
131         }
132     );
133
134     $ar->process()->discard_changes;
135
136     is( $ar->status, Koha::ArticleRequest::Status::Processing );
137
138     $schema->storage->txn_rollback;
139 };
140
141 subtest 'complete() tests' => sub {
142
143     plan tests => 2;
144
145     $schema->storage->txn_begin;
146
147     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
148     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
149
150     my $ar = $builder->build_object(
151         {   class => 'Koha::ArticleRequests',
152             value => { status => Koha::ArticleRequest::Status::Requested }
153         }
154     );
155
156     $ar->complete()->discard_changes;
157
158     is( $ar->status, Koha::ArticleRequest::Status::Completed );
159
160     $schema->storage->txn_rollback;
161 };
162
163 subtest 'cancel() tests' => sub {
164
165     plan tests => 11;
166
167     $schema->storage->txn_begin;
168
169     my $amount = 11;
170
171     my $patron_mock = Test::MockModule->new('Koha::Patron');
172     $patron_mock->mock( 'article_request_fee', sub { return $amount; } );
173
174     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
175     my $item   = $builder->build_sample_item;
176
177     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
178     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
179
180     my $ar = Koha::ArticleRequest->new(
181         {
182             borrowernumber => $patron->id,
183             biblionumber   => $item->biblionumber,
184             itemnumber     => $item->id,
185         }
186     );
187
188     $ar->request()->discard_changes;
189
190     is( $ar->status, Koha::ArticleRequest::Status::Requested );
191     is( $ar->itemnumber, $item->id, 'itemnumber set' );
192     ok( defined $ar->debit_id, 'Fee linked' );
193     is( $patron->account->balance, $amount, 'Outstanding fees with the right value' );
194
195     my $payed_amount = 5;
196     $patron->account->pay({ amount => $payed_amount, interface => 'intranet', lines => [ $ar->debit ] });
197     is( $patron->account->balance, $amount - $payed_amount, 'Outstanding fees with the right value' );
198
199     my $reason = "Hey, ho";
200     my $notes  = "Let's go!";
201
202     $ar->cancel({ cancellation_reason => $reason, notes => $notes })->discard_changes;
203
204     is( $ar->status, Koha::ArticleRequest::Status::Canceled );
205     is( $ar->cancellation_reason, $reason, 'Cancellation reason stored correctly' );
206     is( $ar->notes, $notes, 'Notes stored correctly' );
207
208     is( $patron->account->balance, -$payed_amount, 'The patron has a credit balance' );
209
210     $schema->storage->txn_rollback;
211 };