Bug 28445: Use the task queue for the batch delete and update items tool
[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 => 3;
34
35     $schema->storage->txn_begin;
36
37     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
38     my $biblio = $builder->build_sample_biblio;
39
40     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
41     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
42
43     my $ar = Koha::ArticleRequest->new(
44         {
45             borrowernumber => $patron->id,
46             biblionumber   => $biblio->id,
47         }
48     );
49
50     $ar->request()->discard_changes;
51
52     is( $ar->status, Koha::ArticleRequest::Status::Requested );
53     ok( defined $ar->created_on, 'created_on is set' );
54
55     $schema->storage->txn_rollback;
56 };
57
58 subtest 'set_pending() tests' => sub {
59
60     plan tests => 3;
61
62     $schema->storage->txn_begin;
63
64     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
65     my $biblio = $builder->build_sample_biblio;
66
67     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
68     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
69
70     my $ar = Koha::ArticleRequest->new(
71         {
72             borrowernumber => $patron->id,
73             biblionumber   => $biblio->id,
74         }
75     );
76
77     $ar->set_pending()->discard_changes;
78
79     is( $ar->status, Koha::ArticleRequest::Status::Pending );
80     ok( defined $ar->created_on, 'created_on is set' );
81
82     $schema->storage->txn_rollback;
83 };
84
85 subtest 'process() tests' => sub {
86
87     plan tests => 2;
88
89     $schema->storage->txn_begin;
90
91     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
92     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
93
94     my $ar = $builder->build_object(
95         {   class => 'Koha::ArticleRequests',
96             value => { status => Koha::ArticleRequest::Status::Requested }
97         }
98     );
99
100     $ar->process()->discard_changes;
101
102     is( $ar->status, Koha::ArticleRequest::Status::Processing );
103
104     $schema->storage->txn_rollback;
105 };
106
107 subtest 'complete() tests' => sub {
108
109     plan tests => 2;
110
111     $schema->storage->txn_begin;
112
113     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
114     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
115
116     my $ar = $builder->build_object(
117         {   class => 'Koha::ArticleRequests',
118             value => { status => Koha::ArticleRequest::Status::Requested }
119         }
120     );
121
122     $ar->complete()->discard_changes;
123
124     is( $ar->status, Koha::ArticleRequest::Status::Completed );
125
126     $schema->storage->txn_rollback;
127 };
128
129 subtest 'cancel() tests' => sub {
130
131     plan tests => 4;
132
133     $schema->storage->txn_begin;
134
135     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
136     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
137
138     my $ar = $builder->build_object(
139         {   class => 'Koha::ArticleRequests',
140             value => { status => Koha::ArticleRequest::Status::Requested }
141         }
142     );
143
144     my $reason = "Hey, ho";
145     my $notes  = "Let's go!";
146
147     $ar->cancel({ cancellation_reason => $reason, notes => $notes })->discard_changes;
148
149     is( $ar->status, Koha::ArticleRequest::Status::Canceled );
150     is( $ar->cancellation_reason, $reason, 'Cancellation reason stored correctly' );
151     is( $ar->notes, $notes, 'Notes stored correctly' );
152
153     $schema->storage->txn_rollback;
154 };