Bug 22273: DBRev 19.12.00.031
[koha.git] / t / db_dependent / ArticleRequests.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 POSIX qw(strftime);
21
22 use Test::More tests => 57;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use Koha::Database;
28 use Koha::Biblio;
29 use Koha::Notice::Messages;
30 use Koha::Patron;
31 use Koha::Library::Group;
32 use Koha::CirculationRules;
33 use Koha::Caches;
34
35 BEGIN {
36     use_ok('Koha::ArticleRequest');
37     use_ok('Koha::ArticleRequests');
38     use_ok('Koha::ArticleRequest::Status');
39 }
40
41 my $schema = Koha::Database->new()->schema();
42 $schema->storage->txn_begin();
43 my $builder = t::lib::TestBuilder->new;
44 our $cache = Koha::Caches->get_instance;
45
46 my $dbh = C4::Context->dbh;
47 $dbh->{RaiseError} = 1;
48
49 $dbh->do("DELETE FROM circulation_rules");
50
51 my $biblio = Koha::Biblio->new()->store();
52 ok( $biblio->id, 'Koha::Biblio created' );
53
54 my $biblioitem = $schema->resultset('Biblioitem')->new(
55     {
56         biblionumber => $biblio->id
57     }
58 )->insert();
59 ok( $biblioitem->id, 'biblioitem created' );
60
61 my $itype = $builder->build({ source => 'Itemtype' });
62 my $item = Koha::Item->new(
63     {
64         biblionumber     => $biblio->id,
65         biblioitemnumber => $biblioitem->id,
66         itype => $itype->{itemtype},
67     }
68 )->store();
69 ok( $item->id, 'Koha::Item created' );
70
71 my $branch   = $builder->build({ source => 'Branch' });
72 my $category = $builder->build({ source => 'Category' });
73 my $patron   = $builder->build_object({
74     class => 'Koha::Patrons',
75     value => {
76         categorycode => $category->{categorycode},
77         branchcode   => $branch->{branchcode},
78         flags        => 1,# superlibrarian
79     },
80 });
81 ok( $patron->id, 'Koha::Patron created' );
82 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 } });
83
84 # store
85 Koha::Notice::Messages->delete;
86 my $article_request_title = 'an article request title';
87 my $article_request = Koha::ArticleRequest->new(
88     {
89         borrowernumber => $patron->id,
90         biblionumber   => $biblio->id,
91         itemnumber     => $item->id,
92         title          => $article_request_title,
93     }
94 )->store();
95
96 my $notify_message = Koha::Notice::Messages->search->next;
97 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Pending);
98 # Default AR_PROCESSING template content "Title: <<article_requests.title>>"
99 like( $notify_message->content, qr{Title: $article_request_title}, 'Values from article_requests table must be fetched for the notification' );
100
101 $article_request = Koha::ArticleRequests->find( $article_request->id );
102 ok( $article_request->id, 'Koha::ArticleRequest created' );
103 is( $article_request->status, Koha::ArticleRequest::Status::Pending, 'New article request has status of Open' );
104 isnt( $article_request->created_on, undef, 'New article request has created_on date set' );
105 isnt( $article_request->updated_on, undef, 'New article request has updated_on date set' );
106
107 # process
108 Koha::Notice::Messages->delete;
109 $article_request->process();
110 $notify_message = Koha::Notice::Messages->search->next;
111 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Processing);
112 is( $article_request->status, Koha::ArticleRequest::Status::Processing, '$ar->process() changes status to Processing' );
113 isnt( $article_request->updated_on, undef, 'Updated article request has an updated_on date set' );
114
115 # complete
116 $article_request->complete();
117 is( $article_request->status, Koha::ArticleRequest::Status::Completed, '$ar->complete() changes status to Completed' );
118
119 # cancel
120 $article_request->cancel();
121 is( $article_request->status, Koha::ArticleRequest::Status::Canceled, '$ar->complete() changes status to Canceled' );
122 $article_request->status(Koha::ArticleRequest::Status::Pending);
123 $article_request->store();
124
125 is( $article_request->biblio->id,   $biblio->id, '$ar->biblio() gets corresponding Koha::Biblio object' );
126 is( $article_request->item->id,     $item->id,   '$ar->item() gets corresponding Koha::Item object' );
127 is( $article_request->borrower->id, $patron->id, '$ar->borrower() gets corresponding Koha::Patron object' );
128
129 my $ar = $patron->article_requests();
130 is( ref($ar),      'Koha::ArticleRequests', '$patron->article_requests returns Koha::ArticleRequests object' );
131 is( $ar->next->id, $article_request->id,    'Returned article request matches' );
132
133 is( $patron->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
134 $article_request->process();
135 is( $patron->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
136 $article_request->complete();
137 is( $patron->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
138 $article_request->cancel();
139 is( $patron->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
140
141 $article_request->status(Koha::ArticleRequest::Status::Pending);
142 $article_request->store();
143
144 is( $patron->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
145 $article_request->process();
146 is( $patron->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
147 $article_request->complete();
148 $article_request->cancel();
149 is( $patron->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
150
151 $article_request->status(Koha::ArticleRequest::Status::Pending);
152 $article_request->store();
153
154 $ar = $biblio->article_requests();
155 is( ref($ar),      'Koha::ArticleRequests', '$biblio->article_requests returns Koha::ArticleRequests object' );
156 is( $ar->next->id, $article_request->id,    'Returned article request matches' );
157
158 is( $biblio->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
159 $article_request->process();
160 is( $biblio->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
161 $article_request->complete();
162 is( $biblio->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
163 $article_request->cancel();
164 is( $biblio->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
165
166 $article_request->status(Koha::ArticleRequest::Status::Pending);
167 $article_request->store();
168
169 is( $biblio->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
170 $article_request->process();
171 is( $biblio->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
172 $article_request->complete();
173 $article_request->cancel();
174 is( $biblio->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
175
176 my $rule = Koha::CirculationRules->set_rule(
177     {
178         categorycode => undef,
179         itemtype     => undef,
180         branchcode   => undef,
181         rule_name    => 'article_requests',
182         rule_value   => 'yes',
183     }
184 );
185 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type yes' );
186 is( $biblio->article_request_type($patron), 'yes', 'Biblio article request type is yes' );
187 ok( $item->can_article_request($patron),   'Item is requestable with rule type yes' );
188 is( $item->article_request_type($patron), 'yes', 'Item article request type is yes' );
189 $rule->delete();
190
191 $rule = Koha::CirculationRules->set_rule(
192     {
193         categorycode => undef,
194         itemtype     => undef,
195         branchcode   => undef,
196         rule_name    => 'article_requests',
197         rule_value   => 'bib_only',
198     }
199 );
200 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type bib_only' );
201 is( $biblio->article_request_type($patron), 'bib_only', 'Biblio article request type is bib_only' );
202 ok( !$item->can_article_request($patron),  'Item is not requestable with rule type bib_only' );
203 is( $item->article_request_type($patron), 'bib_only', 'Item article request type is bib_only' );
204 $rule->delete();
205
206 $rule = Koha::CirculationRules->set_rule(
207     {
208         categorycode => undef,
209         itemtype     => undef,
210         branchcode   => undef,
211         rule_name    => 'article_requests',
212         rule_value   => 'item_only',
213     }
214 );
215 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type item_only' );
216 is( $biblio->article_request_type($patron), 'item_only', 'Biblio article request type is item_only' );
217 ok( $item->can_article_request($patron),   'Item is not requestable with rule type item_only' );
218 is( $item->article_request_type($patron), 'item_only', 'Item article request type is item_only' );
219 $rule->delete();
220
221 $rule = Koha::CirculationRules->set_rule(
222     {
223         categorycode => undef,
224         itemtype     => undef,
225         branchcode   => undef,
226         rule_name    => 'article_requests',
227         rule_value   => 'no',
228     }
229 );
230 ok( !$biblio->can_article_request($patron), 'Record is requestable with rule type no' );
231 is( $biblio->article_request_type($patron), 'no', 'Biblio article request type is no' );
232 ok( !$item->can_article_request($patron),   'Item is not requestable with rule type no' );
233 is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
234 $rule->delete();
235
236 subtest 'search_limited' => sub {
237     plan tests => 2;
238     my $nb_article_requests = Koha::ArticleRequests->count;
239
240     my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
241     my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
242     Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->branchcode })->store();
243     Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron_2->branchcode })->store();
244     t::lib::Mocks::mock_userenv( { patron => $patron } ); # Is superlibrarian
245     is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' );
246     t::lib::Mocks::mock_userenv( { patron => $patron_2 } ); # Is restricted
247     is( Koha::ArticleRequests->search_limited->count, 0, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' );
248 };
249
250 subtest 'may_article_request' => sub {
251     plan tests => 4;
252
253     # mocking
254     t::lib::Mocks::mock_preference('ArticleRequests', 1);
255     t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
256     $cache->set_in_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY, {
257         '*'  => { 'CR' => 1 },
258         'S'  => { '*'  => 1 },
259         'PT' => { 'BK' => 1 },
260     });
261
262     my $itemtype = Koha::ItemTypes->find('CR') // Koha::ItemType->new({ itemtype => 'CR' })->store;
263     is( $itemtype->may_article_request, 1, 'SER/* should be true' );
264     is( $itemtype->may_article_request({ categorycode => 'S' }), 1, 'SER/S should be true' );
265     is( $itemtype->may_article_request({ categorycode => 'PT' }), '', 'SER/PT should be false' );
266     t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'always');
267     is( $itemtype->may_article_request({ categorycode => 'PT' }), '1', 'Result should be true when LinkControl is set to always' );
268
269     # Cleanup
270     $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
271 };
272
273 $schema->storage->txn_rollback();