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 => 55;
24 use t::lib::TestBuilder;
28 use Koha::Notice::Messages;
31 use t::lib::TestBuilder;
34 use_ok('Koha::ArticleRequest');
35 use_ok('Koha::ArticleRequests');
36 use_ok('Koha::ArticleRequest::Status');
39 my $schema = Koha::Database->new()->schema();
40 $schema->storage->txn_begin();
41 my $builder = t::lib::TestBuilder->new;
43 my $dbh = C4::Context->dbh;
44 $dbh->{RaiseError} = 1;
46 $dbh->do("DELETE FROM issuingrules");
48 my $biblio = Koha::Biblio->new()->store();
49 ok( $biblio->id, 'Koha::Biblio created' );
51 my $biblioitem = $schema->resultset('Biblioitem')->new(
53 biblionumber => $biblio->id
56 ok( $biblioitem->id, 'biblioitem created' );
58 my $itype = $builder->build({ source => 'Itemtype' });
59 my $item = Koha::Item->new(
61 biblionumber => $biblio->id,
62 biblioitemnumber => $biblioitem->id,
63 itype => $itype->{itemtype},
66 ok( $item->id, 'Koha::Item created' );
68 my $branch = $builder->build({ source => 'Branch' });
69 my $category = $builder->build({ source => 'Category' });
70 my $patron = $builder->build_object({
71 class => 'Koha::Patrons',
73 categorycode => $category->{categorycode},
74 branchcode => $branch->{branchcode},
75 flags => 1,# superlibrarian
78 ok( $patron->id, 'Koha::Patron created' );
79 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 } });
82 Koha::Notice::Messages->delete;
83 my $article_request_title = 'an article request title';
84 my $article_request = Koha::ArticleRequest->new(
86 borrowernumber => $patron->id,
87 biblionumber => $biblio->id,
88 itemnumber => $item->id,
89 title => $article_request_title,
93 my $notify_message = Koha::Notice::Messages->search->next;
94 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Pending);
95 # Default AR_PROCESSING template content "Title: <<article_requests.title>>"
96 like( $notify_message->content, qr{Title: $article_request_title}, 'Values from article_requests table must be fetched for the notification' );
98 $article_request = Koha::ArticleRequests->find( $article_request->id );
99 ok( $article_request->id, 'Koha::ArticleRequest created' );
100 is( $article_request->status, Koha::ArticleRequest::Status::Pending, 'New article request has status of Open' );
101 is( $article_request->updated_on, undef, 'New article request has not an updated_on date set yet' );
104 Koha::Notice::Messages->delete;
105 $article_request->process();
106 $notify_message = Koha::Notice::Messages->search->next;
107 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Processing);
108 is( $article_request->status, Koha::ArticleRequest::Status::Processing, '$ar->process() changes status to Processing' );
109 isnt( $article_request->updated_on, undef, 'Updated article request has an updated_on date set' );
112 $article_request->complete();
113 is( $article_request->status, Koha::ArticleRequest::Status::Completed, '$ar->complete() changes status to Completed' );
116 $article_request->cancel();
117 is( $article_request->status, Koha::ArticleRequest::Status::Canceled, '$ar->complete() changes status to Canceled' );
118 $article_request->status(Koha::ArticleRequest::Status::Pending);
119 $article_request->store();
121 is( $article_request->biblio->id, $biblio->id, '$ar->biblio() gets corresponding Koha::Biblio object' );
122 is( $article_request->item->id, $item->id, '$ar->item() gets corresponding Koha::Item object' );
123 is( $article_request->borrower->id, $patron->id, '$ar->borrower() gets corresponding Koha::Patron object' );
125 my $ar = $patron->article_requests();
126 is( ref($ar), 'Koha::ArticleRequests', '$patron->article_requests returns Koha::ArticleRequests object' );
127 is( $ar->next->id, $article_request->id, 'Returned article request matches' );
129 is( $patron->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
130 $article_request->process();
131 is( $patron->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
132 $article_request->complete();
133 is( $patron->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
134 $article_request->cancel();
135 is( $patron->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
137 $article_request->status(Koha::ArticleRequest::Status::Pending);
138 $article_request->store();
140 is( $patron->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
141 $article_request->process();
142 is( $patron->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
143 $article_request->complete();
144 $article_request->cancel();
145 is( $patron->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
147 $article_request->status(Koha::ArticleRequest::Status::Pending);
148 $article_request->store();
150 $ar = $biblio->article_requests();
151 is( ref($ar), 'Koha::ArticleRequests', '$biblio->article_requests returns Koha::ArticleRequests object' );
152 is( $ar->next->id, $article_request->id, 'Returned article request matches' );
154 is( $biblio->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
155 $article_request->process();
156 is( $biblio->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
157 $article_request->complete();
158 is( $biblio->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
159 $article_request->cancel();
160 is( $biblio->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
162 $article_request->status(Koha::ArticleRequest::Status::Pending);
163 $article_request->store();
165 is( $biblio->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
166 $article_request->process();
167 is( $biblio->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
168 $article_request->complete();
169 $article_request->cancel();
170 is( $biblio->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
173 $rule = $schema->resultset('Issuingrule')
174 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'yes' } )->insert();
175 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type yes' );
176 is( $biblio->article_request_type($patron), 'yes', 'Biblio article request type is yes' );
177 ok( $item->can_article_request($patron), 'Item is requestable with rule type yes' );
178 is( $item->article_request_type($patron), 'yes', 'Item article request type is yes' );
181 $rule = $schema->resultset('Issuingrule')
182 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'bib_only' } )->insert();
183 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type bib_only' );
184 is( $biblio->article_request_type($patron), 'bib_only', 'Biblio article request type is bib_only' );
185 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type bib_only' );
186 is( $item->article_request_type($patron), 'bib_only', 'Item article request type is bib_only' );
189 $rule = $schema->resultset('Issuingrule')
190 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'item_only' } )->insert();
191 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type item_only' );
192 is( $biblio->article_request_type($patron), 'item_only', 'Biblio article request type is item_only' );
193 ok( $item->can_article_request($patron), 'Item is not requestable with rule type item_only' );
194 is( $item->article_request_type($patron), 'item_only', 'Item article request type is item_only' );
197 $rule = $schema->resultset('Issuingrule')
198 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'no' } )->insert();
199 ok( !$biblio->can_article_request($patron), 'Record is requestable with rule type no' );
200 is( $biblio->article_request_type($patron), 'no', 'Biblio article request type is no' );
201 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type no' );
202 is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
205 subtest 'search_limited' => sub {
207 C4::Context->_new_userenv('xxx');
208 my $nb_article_requests = Koha::ArticleRequests->count;
210 my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
211 my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
212 Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->branchcode })->store();
213 Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron_2->branchcode })->store();
214 set_logged_in_user( $patron ); # Is superlibrarian
215 is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' );
216 set_logged_in_user( $patron_2 ); # Is restricted
217 is( Koha::ArticleRequests->search_limited->count, 0, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' );
220 $schema->storage->txn_rollback();
222 sub set_logged_in_user {
224 C4::Context->set_userenv(
225 $patron->borrowernumber, $patron->userid,
226 $patron->cardnumber, 'firstname',
227 'surname', $patron->library->branchcode,
228 'Midway Public Library', $patron->flags,