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 => 36;
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 $rule = Koha::CirculationRules->set_rule(
115 categorycode => undef,
118 rule_name => 'article_requests',
122 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type yes' );
123 is( $biblio->article_request_type($patron), 'yes', 'Biblio article request type is yes' );
124 ok( $item->can_article_request($patron), 'Item is requestable with rule type yes' );
125 is( $item->article_request_type($patron), 'yes', 'Item article request type is yes' );
128 $rule = Koha::CirculationRules->set_rule(
130 categorycode => undef,
133 rule_name => 'article_requests',
134 rule_value => 'bib_only',
137 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type bib_only' );
138 is( $biblio->article_request_type($patron), 'bib_only', 'Biblio article request type is bib_only' );
139 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type bib_only' );
140 is( $item->article_request_type($patron), 'bib_only', 'Item article request type is bib_only' );
143 $rule = Koha::CirculationRules->set_rule(
145 categorycode => undef,
148 rule_name => 'article_requests',
149 rule_value => 'item_only',
152 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type item_only' );
153 is( $biblio->article_request_type($patron), 'item_only', 'Biblio article request type is item_only' );
154 ok( $item->can_article_request($patron), 'Item is not requestable with rule type item_only' );
155 is( $item->article_request_type($patron), 'item_only', 'Item article request type is item_only' );
158 $rule = Koha::CirculationRules->set_rule(
160 categorycode => undef,
163 rule_name => 'article_requests',
167 ok( !$biblio->can_article_request($patron), 'Record is requestable with rule type no' );
168 is( $biblio->article_request_type($patron), 'no', 'Biblio article request type is no' );
169 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type no' );
170 is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
173 subtest 'search_limited' => sub {
175 my $nb_article_requests = Koha::ArticleRequests->count;
177 my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
178 my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
179 Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->branchcode })->store();
180 Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron_2->branchcode })->store();
181 t::lib::Mocks::mock_userenv( { patron => $patron } ); # Is superlibrarian
182 is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' );
183 t::lib::Mocks::mock_userenv( { patron => $patron_2 } ); # Is restricted
184 is( Koha::ArticleRequests->search_limited->count, 0, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' );
187 subtest 'may_article_request' => sub {
191 t::lib::Mocks::mock_preference('ArticleRequests', 1);
192 t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
193 $cache->set_in_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY, {
194 '*' => { 'CR' => 1 },
196 'PT' => { 'BK' => 1 },
199 my $itemtype = Koha::ItemTypes->find('CR') // Koha::ItemType->new({ itemtype => 'CR' })->store;
200 is( $itemtype->may_article_request, 1, 'SER/* should be true' );
201 is( $itemtype->may_article_request({ categorycode => 'S' }), 1, 'SER/S should be true' );
202 is( $itemtype->may_article_request({ categorycode => 'PT' }), '', 'SER/PT should be false' );
203 t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'always');
204 is( $itemtype->may_article_request({ categorycode => 'PT' }), '1', 'Result should be true when LinkControl is set to always' );
207 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
210 $schema->storage->txn_rollback();