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 => 56;
24 use t::lib::TestBuilder;
29 use Koha::Notice::Messages;
31 use Koha::Library::Group;
32 use Koha::IssuingRules;
35 use_ok('Koha::ArticleRequest');
36 use_ok('Koha::ArticleRequests');
37 use_ok('Koha::ArticleRequest::Status');
40 my $schema = Koha::Database->new()->schema();
41 $schema->storage->txn_begin();
42 my $builder = t::lib::TestBuilder->new;
44 my $dbh = C4::Context->dbh;
45 $dbh->{RaiseError} = 1;
47 $dbh->do("DELETE FROM issuingrules");
49 my $biblio = Koha::Biblio->new()->store();
50 ok( $biblio->id, 'Koha::Biblio created' );
52 my $biblioitem = $schema->resultset('Biblioitem')->new(
54 biblionumber => $biblio->id
57 ok( $biblioitem->id, 'biblioitem created' );
59 my $itype = $builder->build({ source => 'Itemtype' });
60 my $item = Koha::Item->new(
62 biblionumber => $biblio->id,
63 biblioitemnumber => $biblioitem->id,
64 itype => $itype->{itemtype},
67 ok( $item->id, 'Koha::Item created' );
69 my $branch = $builder->build({ source => 'Branch' });
70 my $category = $builder->build({ source => 'Category' });
71 my $patron = $builder->build_object({
72 class => 'Koha::Patrons',
74 categorycode => $category->{categorycode},
75 branchcode => $branch->{branchcode},
76 flags => 1,# superlibrarian
79 ok( $patron->id, 'Koha::Patron created' );
80 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 } });
83 Koha::Notice::Messages->delete;
84 my $article_request_title = 'an article request title';
85 my $article_request = Koha::ArticleRequest->new(
87 borrowernumber => $patron->id,
88 biblionumber => $biblio->id,
89 itemnumber => $item->id,
90 title => $article_request_title,
94 my $notify_message = Koha::Notice::Messages->search->next;
95 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Pending);
96 # Default AR_PROCESSING template content "Title: <<article_requests.title>>"
97 like( $notify_message->content, qr{Title: $article_request_title}, 'Values from article_requests table must be fetched for the notification' );
99 $article_request = Koha::ArticleRequests->find( $article_request->id );
100 ok( $article_request->id, 'Koha::ArticleRequest created' );
101 is( $article_request->status, Koha::ArticleRequest::Status::Pending, 'New article request has status of Open' );
102 is( $article_request->updated_on, undef, 'New article request has not an updated_on date set yet' );
105 Koha::Notice::Messages->delete;
106 $article_request->process();
107 $notify_message = Koha::Notice::Messages->search->next;
108 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Processing);
109 is( $article_request->status, Koha::ArticleRequest::Status::Processing, '$ar->process() changes status to Processing' );
110 isnt( $article_request->updated_on, undef, 'Updated article request has an updated_on date set' );
113 $article_request->complete();
114 is( $article_request->status, Koha::ArticleRequest::Status::Completed, '$ar->complete() changes status to Completed' );
117 $article_request->cancel();
118 is( $article_request->status, Koha::ArticleRequest::Status::Canceled, '$ar->complete() changes status to Canceled' );
119 $article_request->status(Koha::ArticleRequest::Status::Pending);
120 $article_request->store();
122 is( $article_request->biblio->id, $biblio->id, '$ar->biblio() gets corresponding Koha::Biblio object' );
123 is( $article_request->item->id, $item->id, '$ar->item() gets corresponding Koha::Item object' );
124 is( $article_request->borrower->id, $patron->id, '$ar->borrower() gets corresponding Koha::Patron object' );
126 my $ar = $patron->article_requests();
127 is( ref($ar), 'Koha::ArticleRequests', '$patron->article_requests returns Koha::ArticleRequests object' );
128 is( $ar->next->id, $article_request->id, 'Returned article request matches' );
130 is( $patron->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
131 $article_request->process();
132 is( $patron->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
133 $article_request->complete();
134 is( $patron->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
135 $article_request->cancel();
136 is( $patron->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
138 $article_request->status(Koha::ArticleRequest::Status::Pending);
139 $article_request->store();
141 is( $patron->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
142 $article_request->process();
143 is( $patron->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
144 $article_request->complete();
145 $article_request->cancel();
146 is( $patron->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
148 $article_request->status(Koha::ArticleRequest::Status::Pending);
149 $article_request->store();
151 $ar = $biblio->article_requests();
152 is( ref($ar), 'Koha::ArticleRequests', '$biblio->article_requests returns Koha::ArticleRequests object' );
153 is( $ar->next->id, $article_request->id, 'Returned article request matches' );
155 is( $biblio->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
156 $article_request->process();
157 is( $biblio->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
158 $article_request->complete();
159 is( $biblio->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
160 $article_request->cancel();
161 is( $biblio->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
163 $article_request->status(Koha::ArticleRequest::Status::Pending);
164 $article_request->store();
166 is( $biblio->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
167 $article_request->process();
168 is( $biblio->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
169 $article_request->complete();
170 $article_request->cancel();
171 is( $biblio->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
174 $rule = $schema->resultset('Issuingrule')
175 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'yes' } )->insert();
176 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type yes' );
177 is( $biblio->article_request_type($patron), 'yes', 'Biblio article request type is yes' );
178 ok( $item->can_article_request($patron), 'Item is requestable with rule type yes' );
179 is( $item->article_request_type($patron), 'yes', 'Item article request type is yes' );
182 $rule = $schema->resultset('Issuingrule')
183 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'bib_only' } )->insert();
184 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type bib_only' );
185 is( $biblio->article_request_type($patron), 'bib_only', 'Biblio article request type is bib_only' );
186 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type bib_only' );
187 is( $item->article_request_type($patron), 'bib_only', 'Item article request type is bib_only' );
190 $rule = $schema->resultset('Issuingrule')
191 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'item_only' } )->insert();
192 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type item_only' );
193 is( $biblio->article_request_type($patron), 'item_only', 'Biblio article request type is item_only' );
194 ok( $item->can_article_request($patron), 'Item is not requestable with rule type item_only' );
195 is( $item->article_request_type($patron), 'item_only', 'Item article request type is item_only' );
198 $rule = $schema->resultset('Issuingrule')
199 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'no' } )->insert();
200 ok( !$biblio->can_article_request($patron), 'Record is requestable with rule type no' );
201 is( $biblio->article_request_type($patron), 'no', 'Biblio article request type is no' );
202 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type no' );
203 is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
206 subtest 'search_limited' => sub {
208 C4::Context->_new_userenv('xxx');
209 my $nb_article_requests = Koha::ArticleRequests->count;
211 my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
212 my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
213 Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->branchcode })->store();
214 Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron_2->branchcode })->store();
215 set_logged_in_user( $patron ); # Is superlibrarian
216 is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' );
217 set_logged_in_user( $patron_2 ); # Is restricted
218 is( Koha::ArticleRequests->search_limited->count, 0, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' );
221 subtest 'may_article_request' => sub {
225 t::lib::Mocks::mock_preference('ArticleRequests', 1);
226 $Koha::IssuingRules::last_article_requestable_guesses = {
227 '*' => { 'CR' => 1 },
229 'PT' => { 'BK' => 1 },
232 # tests for class method call
233 is( Koha::Biblio->may_article_request({ itemtype => 'CR' }), 1, 'SER/* should be true' );
234 is( Koha::Biblio->may_article_request({ itemtype => 'CR', categorycode => 'S' }), 1, 'SER/S should be true' );
235 is( Koha::Biblio->may_article_request({ itemtype => 'CR', categorycode => 'PT' }), '', 'SER/PT should be false' );
237 # tests for instance method call
238 my $builder = t::lib::TestBuilder->new;
239 my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
240 my $biblioitem = $builder->build_object({ class => 'Koha::Biblioitems', value => { biblionumber => $biblio->biblionumber, itemtype => 'BK' }});
241 is( $biblio->may_article_request, '', 'BK/* false' );
242 is( $biblio->may_article_request({ categorycode => 'S' }), 1, 'BK/S true' );
243 is( $biblio->may_article_request({ categorycode => 'PT' }), 1, 'BK/PT true' );
246 $Koha::IssuingRules::last_article_requestable_guesses = undef;
249 $schema->storage->txn_rollback();
251 sub set_logged_in_user {
253 C4::Context->set_userenv(
254 $patron->borrowernumber, $patron->userid,
255 $patron->cardnumber, 'firstname',
256 'surname', $patron->library->branchcode,
257 'Midway Public Library', $patron->flags,