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 POSIX qw(strftime);
22 use Test::More tests => 54;
25 use t::lib::TestBuilder;
30 use Koha::Notice::Messages;
32 use Koha::Library::Group;
33 use Koha::CirculationRules;
37 use_ok('Koha::ArticleRequest');
38 use_ok('Koha::ArticleRequests');
39 use_ok('Koha::ArticleRequest::Status');
42 my $schema = Koha::Database->new()->schema();
43 $schema->storage->txn_begin();
44 my $builder = t::lib::TestBuilder->new;
45 our $cache = Koha::Caches->get_instance;
47 my $dbh = C4::Context->dbh;
49 $dbh->do("DELETE FROM circulation_rules");
51 my $item = $builder->build_sample_item;
52 my $biblio = $item->biblio;
54 my $branch = $builder->build({ source => 'Branch' });
55 my $category = $builder->build({ source => 'Category' });
56 my $patron = $builder->build_object({
57 class => 'Koha::Patrons',
59 categorycode => $category->{categorycode},
60 branchcode => $branch->{branchcode},
61 flags => 1,# superlibrarian
64 ok( $patron->id, 'Koha::Patron created' );
65 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 } });
68 Koha::Notice::Messages->delete;
69 my $article_request_title = 'an article request title';
70 my $article_request = Koha::ArticleRequest->new(
72 borrowernumber => $patron->id,
73 biblionumber => $item->biblionumber,
74 itemnumber => $item->itemnumber,
75 title => $article_request_title,
79 my $notify_message = Koha::Notice::Messages->search->next;
80 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Requested);
81 # Default AR_PROCESSING template content "Title: <<article_requests.title>>"
82 like( $notify_message->content, qr{Title: $article_request_title}, 'Values from article_requests table must be fetched for the notification' );
84 $article_request = Koha::ArticleRequests->find( $article_request->id );
85 ok( $article_request->id, 'Koha::ArticleRequest created' );
86 is( $article_request->status, Koha::ArticleRequest::Status::Requested, 'New article request has status of Open' );
87 isnt( $article_request->created_on, undef, 'New article request has created_on date set' );
88 isnt( $article_request->updated_on, undef, 'New article request has updated_on date set' );
91 Koha::Notice::Messages->delete;
92 $article_request->process();
93 $notify_message = Koha::Notice::Messages->search->next;
94 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Processing);
95 is( $article_request->status, Koha::ArticleRequest::Status::Processing, '$ar->process() changes status to Processing' );
96 isnt( $article_request->updated_on, undef, 'Updated article request has an updated_on date set' );
99 $article_request->complete();
100 is( $article_request->status, Koha::ArticleRequest::Status::Completed, '$ar->complete() changes status to Completed' );
103 $article_request->cancel();
104 is( $article_request->status, Koha::ArticleRequest::Status::Canceled, '$ar->complete() changes status to Canceled' );
105 $article_request->set_pending();
107 is( $article_request->biblio->id, $biblio->id, '$ar->biblio() gets corresponding Koha::Biblio object' );
108 is( $article_request->item->id, $item->id, '$ar->item() gets corresponding Koha::Item object' );
109 is( $article_request->borrower->id, $patron->id, '$ar->borrower() gets corresponding Koha::Patron object' );
111 my $ar = $patron->article_requests();
112 is( ref($ar), 'Koha::ArticleRequests', '$patron->article_requests returns Koha::ArticleRequests object' );
113 is( $ar->next->id, $article_request->id, 'Returned article request matches' );
115 is( $patron->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
116 $article_request->process();
117 is( $patron->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
118 $article_request->complete();
119 is( $patron->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
120 $article_request->cancel();
121 is( $patron->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
123 $article_request->set_pending();
125 is( $patron->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
126 $article_request->process();
127 is( $patron->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
128 $article_request->complete();
129 $article_request->cancel();
130 is( $patron->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
132 $article_request->set_pending();
134 $ar = $biblio->article_requests();
135 is( ref($ar), 'Koha::ArticleRequests', '$biblio->article_requests returns Koha::ArticleRequests object' );
136 is( $ar->next->id, $article_request->id, 'Returned article request matches' );
138 is( $biblio->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
139 $article_request->process();
140 is( $biblio->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
141 $article_request->complete();
142 is( $biblio->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
143 $article_request->cancel();
144 is( $biblio->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
146 $article_request->status(Koha::ArticleRequest::Status::Pending);
147 $article_request->store();
149 is( $biblio->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
150 $article_request->process();
151 is( $biblio->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
152 $article_request->complete();
153 $article_request->cancel();
154 is( $biblio->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
156 my $rule = Koha::CirculationRules->set_rule(
158 categorycode => undef,
161 rule_name => 'article_requests',
165 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type yes' );
166 is( $biblio->article_request_type($patron), 'yes', 'Biblio article request type is yes' );
167 ok( $item->can_article_request($patron), 'Item is requestable with rule type yes' );
168 is( $item->article_request_type($patron), 'yes', 'Item article request type is yes' );
171 $rule = Koha::CirculationRules->set_rule(
173 categorycode => undef,
176 rule_name => 'article_requests',
177 rule_value => 'bib_only',
180 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type bib_only' );
181 is( $biblio->article_request_type($patron), 'bib_only', 'Biblio article request type is bib_only' );
182 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type bib_only' );
183 is( $item->article_request_type($patron), 'bib_only', 'Item article request type is bib_only' );
186 $rule = Koha::CirculationRules->set_rule(
188 categorycode => undef,
191 rule_name => 'article_requests',
192 rule_value => 'item_only',
195 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type item_only' );
196 is( $biblio->article_request_type($patron), 'item_only', 'Biblio article request type is item_only' );
197 ok( $item->can_article_request($patron), 'Item is not requestable with rule type item_only' );
198 is( $item->article_request_type($patron), 'item_only', 'Item article request type is item_only' );
201 $rule = Koha::CirculationRules->set_rule(
203 categorycode => undef,
206 rule_name => 'article_requests',
210 ok( !$biblio->can_article_request($patron), 'Record is requestable with rule type no' );
211 is( $biblio->article_request_type($patron), 'no', 'Biblio article request type is no' );
212 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type no' );
213 is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
216 subtest 'search_limited' => sub {
218 my $nb_article_requests = Koha::ArticleRequests->count;
220 my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
221 my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
222 Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->branchcode })->store();
223 Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron_2->branchcode })->store();
224 t::lib::Mocks::mock_userenv( { patron => $patron } ); # Is superlibrarian
225 is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' );
226 t::lib::Mocks::mock_userenv( { patron => $patron_2 } ); # Is restricted
227 is( Koha::ArticleRequests->search_limited->count, 0, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' );
230 subtest 'may_article_request' => sub {
234 t::lib::Mocks::mock_preference('ArticleRequests', 1);
235 t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
236 $cache->set_in_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY, {
237 '*' => { 'CR' => 1 },
239 'PT' => { 'BK' => 1 },
242 my $itemtype = Koha::ItemTypes->find('CR') // Koha::ItemType->new({ itemtype => 'CR' })->store;
243 is( $itemtype->may_article_request, 1, 'SER/* should be true' );
244 is( $itemtype->may_article_request({ categorycode => 'S' }), 1, 'SER/S should be true' );
245 is( $itemtype->may_article_request({ categorycode => 'PT' }), '', 'SER/PT should be false' );
246 t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'always');
247 is( $itemtype->may_article_request({ categorycode => 'PT' }), '1', 'Result should be true when LinkControl is set to always' );
250 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
253 $schema->storage->txn_rollback();