3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 5;
23 use t::lib::TestBuilder;
26 use Koha::ArticleRequests;
28 my $schema = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
31 subtest 'request() tests' => sub {
35 $schema->storage->txn_begin;
39 my $patron_mock = Test::MockModule->new('Koha::Patron');
40 $patron_mock->mock( 'article_request_fee', sub { return $amount; } );
42 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
43 my $item = $builder->build_sample_item;
45 my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
46 $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
48 my $ar = Koha::ArticleRequest->new(
50 borrowernumber => $patron->id,
51 biblionumber => $item->biblionumber,
55 $ar->request()->discard_changes;
57 is( $ar->status, Koha::ArticleRequest::Status::Requested );
58 ok( defined $ar->created_on, 'created_on is set' );
60 is( $ar->debit_id, undef, 'No fee linked' );
61 is( $patron->account->balance, 0, 'No outstanding fees' );
66 $ar = Koha::ArticleRequest->new(
68 borrowernumber => $patron->id,
69 biblionumber => $item->biblionumber,
70 itemnumber => $item->id,
74 $ar->request()->discard_changes;
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' );
80 ok( defined $ar->debit_id, 'Fee linked' );
81 is( $patron->account->balance, $amount, 'Outstanding fees with the right value' );
83 $schema->storage->txn_rollback;
86 subtest 'set_pending() tests' => sub {
90 $schema->storage->txn_begin;
92 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
93 my $biblio = $builder->build_sample_biblio;
95 my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
96 $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
98 my $ar = Koha::ArticleRequest->new(
100 borrowernumber => $patron->id,
101 biblionumber => $biblio->id,
105 $ar->set_pending()->discard_changes;
107 is( $ar->status, Koha::ArticleRequest::Status::Pending );
108 ok( defined $ar->created_on, 'created_on is set' );
110 $schema->storage->txn_rollback;
113 subtest 'process() tests' => sub {
117 $schema->storage->txn_begin;
119 my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
120 $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
122 my $ar = $builder->build_object(
123 { class => 'Koha::ArticleRequests',
124 value => { status => Koha::ArticleRequest::Status::Requested }
128 $ar->process()->discard_changes;
130 is( $ar->status, Koha::ArticleRequest::Status::Processing );
132 $schema->storage->txn_rollback;
135 subtest 'complete() tests' => sub {
139 $schema->storage->txn_begin;
141 my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
142 $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
144 my $ar = $builder->build_object(
145 { class => 'Koha::ArticleRequests',
146 value => { status => Koha::ArticleRequest::Status::Requested }
150 $ar->complete()->discard_changes;
152 is( $ar->status, Koha::ArticleRequest::Status::Completed );
154 $schema->storage->txn_rollback;
157 subtest 'cancel() tests' => sub {
161 $schema->storage->txn_begin;
165 my $patron_mock = Test::MockModule->new('Koha::Patron');
166 $patron_mock->mock( 'article_request_fee', sub { return $amount; } );
168 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
169 my $item = $builder->build_sample_item;
171 my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
172 $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
174 my $ar = Koha::ArticleRequest->new(
176 borrowernumber => $patron->id,
177 biblionumber => $item->biblionumber,
178 itemnumber => $item->id,
182 $ar->request()->discard_changes;
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' );
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' );
193 my $reason = "Hey, ho";
194 my $notes = "Let's go!";
196 $ar->cancel({ cancellation_reason => $reason, notes => $notes })->discard_changes;
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' );
202 is( $patron->account->balance, -$payed_amount, 'The patron has a credit balance' );
204 $schema->storage->txn_rollback;