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 => 45;
25 use t::lib::TestBuilder;
30 use Koha::Notice::Messages;
32 use Koha::Library::Group;
33 use Koha::CirculationRules;
35 use Koha::DateUtils qw( dt_from_string );
39 use_ok('Koha::ArticleRequest');
40 use_ok('Koha::ArticleRequests');
41 use_ok('Koha::ArticleRequest::Status');
44 my $schema = Koha::Database->new()->schema();
45 $schema->storage->txn_begin();
46 my $builder = t::lib::TestBuilder->new;
47 our $cache = Koha::Caches->get_instance;
49 my $dbh = C4::Context->dbh;
51 $dbh->do("DELETE FROM circulation_rules");
53 my $item = $builder->build_sample_item;
54 my $biblio = $item->biblio;
56 my $branch = $builder->build({ source => 'Branch' });
57 my $category = $builder->build({ source => 'Category' });
58 my $patron = $builder->build_object({
59 class => 'Koha::Patrons',
61 categorycode => $category->{categorycode},
62 branchcode => $branch->{branchcode},
63 flags => 1,# superlibrarian
66 ok( $patron->id, 'Koha::Patron created' );
67 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 } });
70 Koha::Notice::Messages->delete;
71 my $article_request_title = 'an article request title';
72 my $article_request = Koha::ArticleRequest->new(
74 borrowernumber => $patron->id,
75 biblionumber => $item->biblionumber,
76 itemnumber => $item->itemnumber,
77 title => $article_request_title,
81 my $notify_message = Koha::Notice::Messages->search->next;
82 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Requested);
83 # Default AR_PROCESSING template content "Title: <<article_requests.title>>"
84 like( $notify_message->content, qr{Title: $article_request_title}, 'Values from article_requests table must be fetched for the notification' );
86 $article_request = Koha::ArticleRequests->find( $article_request->id );
87 ok( $article_request->id, 'Koha::ArticleRequest created' );
88 is( $article_request->status, Koha::ArticleRequest::Status::Requested, 'New article request has status of Open' );
89 isnt( $article_request->created_on, undef, 'New article request has created_on date set' );
90 isnt( $article_request->updated_on, undef, 'New article request has updated_on date set' );
93 Koha::Notice::Messages->delete;
94 $article_request->process();
95 $notify_message = Koha::Notice::Messages->search->next;
96 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Processing);
97 is( $article_request->status, Koha::ArticleRequest::Status::Processing, '$ar->process() changes status to Processing' );
98 isnt( $article_request->updated_on, undef, 'Updated article request has an updated_on date set' );
101 $article_request->complete();
102 is( $article_request->status, Koha::ArticleRequest::Status::Completed, '$ar->complete() changes status to Completed' );
105 $article_request->cancel();
106 is( $article_request->status, Koha::ArticleRequest::Status::Canceled, '$ar->complete() changes status to Canceled' );
107 $article_request->set_pending();
109 is( $article_request->biblio->id, $biblio->id, '$ar->biblio() gets corresponding Koha::Biblio object' );
110 is( $article_request->item->id, $item->id, '$ar->item() gets corresponding Koha::Item object' );
111 is( $article_request->borrower->id, $patron->id, '$ar->borrower() gets corresponding Koha::Patron object' );
113 my $ar = $biblio->article_requests();
114 is( ref($ar), 'Koha::ArticleRequests', '$biblio->article_requests returns Koha::ArticleRequests object' );
115 is( $ar->next->id, $article_request->id, 'Returned article request matches' );
117 is( $biblio->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
118 $article_request->process();
119 is( $biblio->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
120 $article_request->complete();
121 is( $biblio->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
122 $article_request->cancel();
123 is( $biblio->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
125 $article_request->status(Koha::ArticleRequest::Status::Pending);
126 $article_request->store();
128 is( $biblio->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
129 $article_request->process();
130 is( $biblio->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
131 $article_request->complete();
132 $article_request->cancel();
133 is( $biblio->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
135 my $rule = Koha::CirculationRules->set_rule(
137 categorycode => undef,
140 rule_name => 'article_requests',
144 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type yes' );
145 is( $biblio->article_request_type($patron), 'yes', 'Biblio article request type is yes' );
146 ok( $item->can_article_request($patron), 'Item is requestable with rule type yes' );
147 is( $item->article_request_type($patron), 'yes', 'Item article request type is yes' );
150 $rule = Koha::CirculationRules->set_rule(
152 categorycode => undef,
155 rule_name => 'article_requests',
156 rule_value => 'bib_only',
159 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type bib_only' );
160 is( $biblio->article_request_type($patron), 'bib_only', 'Biblio article request type is bib_only' );
161 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type bib_only' );
162 is( $item->article_request_type($patron), 'bib_only', 'Item article request type is bib_only' );
165 $rule = Koha::CirculationRules->set_rule(
167 categorycode => undef,
170 rule_name => 'article_requests',
171 rule_value => 'item_only',
174 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type item_only' );
175 is( $biblio->article_request_type($patron), 'item_only', 'Biblio article request type is item_only' );
176 ok( $item->can_article_request($patron), 'Item is not requestable with rule type item_only' );
177 is( $item->article_request_type($patron), 'item_only', 'Item article request type is item_only' );
180 $rule = Koha::CirculationRules->set_rule(
182 categorycode => undef,
185 rule_name => 'article_requests',
189 ok( !$biblio->can_article_request($patron), 'Record is requestable with rule type no' );
190 is( $biblio->article_request_type($patron), 'no', 'Biblio article request type is no' );
191 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type no' );
192 is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
195 subtest 'search_limited' => sub {
197 my $nb_article_requests = Koha::ArticleRequests->count;
199 my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
200 my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
201 Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->branchcode })->store();
202 Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron_2->branchcode })->store();
203 t::lib::Mocks::mock_userenv( { patron => $patron } ); # Is superlibrarian
204 is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' );
205 t::lib::Mocks::mock_userenv( { patron => $patron_2 } ); # Is restricted
206 is( Koha::ArticleRequests->search_limited->count, 0, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' );
209 subtest 'may_article_request' => sub {
213 t::lib::Mocks::mock_preference('ArticleRequests', 1);
214 t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
215 $cache->set_in_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY, {
216 '*' => { 'CR' => 1 },
218 'PT' => { 'BK' => 1 },
221 my $itemtype = Koha::ItemTypes->find('CR') // Koha::ItemType->new({ itemtype => 'CR' })->store;
222 is( $itemtype->may_article_request, 1, 'SER/* should be true' );
223 is( $itemtype->may_article_request({ categorycode => 'S' }), 1, 'SER/S should be true' );
224 is( $itemtype->may_article_request({ categorycode => 'PT' }), '', 'SER/PT should be false' );
225 t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'always');
226 is( $itemtype->may_article_request({ categorycode => 'PT' }), '1', 'Result should be true when LinkControl is set to always' );
229 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
232 $schema->storage->txn_rollback();