Bug 29083: Update article requests-related Koha::Patron methods to use relationships
[koha.git] / t / db_dependent / ArticleRequests.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use POSIX qw(strftime);
21
22 use Test::More tests => 45;
23 use Test::MockModule;
24
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 use Koha::Database;
29 use Koha::Biblio;
30 use Koha::Notice::Messages;
31 use Koha::Patron;
32 use Koha::Library::Group;
33 use Koha::CirculationRules;
34 use Koha::Caches;
35 use Koha::DateUtils qw( dt_from_string );
36 use Try::Tiny;
37
38 BEGIN {
39     use_ok('Koha::ArticleRequest');
40     use_ok('Koha::ArticleRequests');
41     use_ok('Koha::ArticleRequest::Status');
42 }
43
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;
48
49 my $dbh = C4::Context->dbh;
50
51 $dbh->do("DELETE FROM circulation_rules");
52
53 my $item = $builder->build_sample_item;
54 my $biblio = $item->biblio;
55
56 my $branch   = $builder->build({ source => 'Branch' });
57 my $category = $builder->build({ source => 'Category' });
58 my $patron   = $builder->build_object({
59     class => 'Koha::Patrons',
60     value => {
61         categorycode => $category->{categorycode},
62         branchcode   => $branch->{branchcode},
63         flags        => 1,# superlibrarian
64     },
65 });
66 ok( $patron->id, 'Koha::Patron created' );
67 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 } });
68
69 # store
70 Koha::Notice::Messages->delete;
71 my $article_request_title = 'an article request title';
72 my $article_request = Koha::ArticleRequest->new(
73     {
74         borrowernumber => $patron->id,
75         biblionumber   => $item->biblionumber,
76         itemnumber     => $item->itemnumber,
77         title          => $article_request_title,
78     }
79 )->store();
80
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' );
85
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' );
91
92 # process
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' );
99
100 # complete
101 $article_request->complete();
102 is( $article_request->status, Koha::ArticleRequest::Status::Completed, '$ar->complete() changes status to Completed' );
103
104 # cancel
105 $article_request->cancel();
106 is( $article_request->status, Koha::ArticleRequest::Status::Canceled, '$ar->complete() changes status to Canceled' );
107 $article_request->set_pending();
108
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' );
112
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' );
116
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' );
124
125 $article_request->status(Koha::ArticleRequest::Status::Pending);
126 $article_request->store();
127
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' );
134
135 my $rule = Koha::CirculationRules->set_rule(
136     {
137         categorycode => undef,
138         itemtype     => undef,
139         branchcode   => undef,
140         rule_name    => 'article_requests',
141         rule_value   => 'yes',
142     }
143 );
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' );
148 $rule->delete();
149
150 $rule = Koha::CirculationRules->set_rule(
151     {
152         categorycode => undef,
153         itemtype     => undef,
154         branchcode   => undef,
155         rule_name    => 'article_requests',
156         rule_value   => 'bib_only',
157     }
158 );
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' );
163 $rule->delete();
164
165 $rule = Koha::CirculationRules->set_rule(
166     {
167         categorycode => undef,
168         itemtype     => undef,
169         branchcode   => undef,
170         rule_name    => 'article_requests',
171         rule_value   => 'item_only',
172     }
173 );
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' );
178 $rule->delete();
179
180 $rule = Koha::CirculationRules->set_rule(
181     {
182         categorycode => undef,
183         itemtype     => undef,
184         branchcode   => undef,
185         rule_name    => 'article_requests',
186         rule_value   => 'no',
187     }
188 );
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' );
193 $rule->delete();
194
195 subtest 'search_limited' => sub {
196     plan tests => 2;
197     my $nb_article_requests = Koha::ArticleRequests->count;
198
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' );
207 };
208
209 subtest 'may_article_request' => sub {
210     plan tests => 4;
211
212     # mocking
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 },
217         'S'  => { '*'  => 1 },
218         'PT' => { 'BK' => 1 },
219     });
220
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' );
227
228     # Cleanup
229     $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
230 };
231
232 $schema->storage->txn_rollback();