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', value => { lastseen => undef } } );
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 t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', '' );
56 $ar->request()->discard_changes;
58 is( $ar->status, Koha::ArticleRequest::Status::Requested );
59 ok( defined $ar->created_on, 'created_on is set' );
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' );
69 $ar = Koha::ArticleRequest->new(
71 borrowernumber => $patron->id,
72 biblionumber => $item->biblionumber,
73 itemnumber => $item->id,
77 t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'article' );
78 $ar->request()->discard_changes;
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' );
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' );
89 $schema->storage->txn_rollback;
92 subtest 'set_pending() tests' => sub {
96 $schema->storage->txn_begin;
98 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
99 my $biblio = $builder->build_sample_biblio;
101 my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
102 $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
104 my $ar = Koha::ArticleRequest->new(
106 borrowernumber => $patron->id,
107 biblionumber => $biblio->id,
111 $ar->set_pending()->discard_changes;
113 is( $ar->status, Koha::ArticleRequest::Status::Pending );
114 ok( defined $ar->created_on, 'created_on is set' );
116 $schema->storage->txn_rollback;
119 subtest 'process() tests' => sub {
123 $schema->storage->txn_begin;
125 my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
126 $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
128 my $ar = $builder->build_object(
129 { class => 'Koha::ArticleRequests',
130 value => { status => Koha::ArticleRequest::Status::Requested }
134 $ar->process()->discard_changes;
136 is( $ar->status, Koha::ArticleRequest::Status::Processing );
138 $schema->storage->txn_rollback;
141 subtest 'complete() tests' => sub {
145 $schema->storage->txn_begin;
147 my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
148 $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
150 my $ar = $builder->build_object(
151 { class => 'Koha::ArticleRequests',
152 value => { status => Koha::ArticleRequest::Status::Requested }
156 $ar->complete()->discard_changes;
158 is( $ar->status, Koha::ArticleRequest::Status::Completed );
160 $schema->storage->txn_rollback;
163 subtest 'cancel() tests' => sub {
167 $schema->storage->txn_begin;
171 my $patron_mock = Test::MockModule->new('Koha::Patron');
172 $patron_mock->mock( 'article_request_fee', sub { return $amount; } );
174 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
175 my $item = $builder->build_sample_item;
177 my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
178 $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
180 my $ar = Koha::ArticleRequest->new(
182 borrowernumber => $patron->id,
183 biblionumber => $item->biblionumber,
184 itemnumber => $item->id,
188 $ar->request()->discard_changes;
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' );
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' );
199 my $reason = "Hey, ho";
200 my $notes = "Let's go!";
202 $ar->cancel({ cancellation_reason => $reason, notes => $notes })->discard_changes;
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' );
208 is( $patron->account->balance, -$payed_amount, 'The patron has a credit balance' );
210 $schema->storage->txn_rollback;