Bug 27944: Add missing tests
[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 => 54;
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
36 BEGIN {
37     use_ok('Koha::ArticleRequest');
38     use_ok('Koha::ArticleRequests');
39     use_ok('Koha::ArticleRequest::Status');
40 }
41
42 my $schema = Koha::Database->new()->schema();
43 $schema->storage->txn_begin();
44 my $builder = t::lib::TestBuilder->new;
45 our $cache = Koha::Caches->get_instance;
46
47 my $dbh = C4::Context->dbh;
48
49 $dbh->do("DELETE FROM circulation_rules");
50
51 my $item = $builder->build_sample_item;
52 my $biblio = $item->biblio;
53
54 my $branch   = $builder->build({ source => 'Branch' });
55 my $category = $builder->build({ source => 'Category' });
56 my $patron   = $builder->build_object({
57     class => 'Koha::Patrons',
58     value => {
59         categorycode => $category->{categorycode},
60         branchcode   => $branch->{branchcode},
61         flags        => 1,# superlibrarian
62     },
63 });
64 ok( $patron->id, 'Koha::Patron created' );
65 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 } });
66
67 # store
68 Koha::Notice::Messages->delete;
69 my $article_request_title = 'an article request title';
70 my $article_request = Koha::ArticleRequest->new(
71     {
72         borrowernumber => $patron->id,
73         biblionumber   => $item->biblionumber,
74         itemnumber     => $item->itemnumber,
75         title          => $article_request_title,
76     }
77 )->store();
78
79 my $notify_message = Koha::Notice::Messages->search->next;
80 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Requested);
81 # Default AR_PROCESSING template content "Title: <<article_requests.title>>"
82 like( $notify_message->content, qr{Title: $article_request_title}, 'Values from article_requests table must be fetched for the notification' );
83
84 $article_request = Koha::ArticleRequests->find( $article_request->id );
85 ok( $article_request->id, 'Koha::ArticleRequest created' );
86 is( $article_request->status, Koha::ArticleRequest::Status::Requested, 'New article request has status of Open' );
87 isnt( $article_request->created_on, undef, 'New article request has created_on date set' );
88 isnt( $article_request->updated_on, undef, 'New article request has updated_on date set' );
89
90 # process
91 Koha::Notice::Messages->delete;
92 $article_request->process();
93 $notify_message = Koha::Notice::Messages->search->next;
94 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Processing);
95 is( $article_request->status, Koha::ArticleRequest::Status::Processing, '$ar->process() changes status to Processing' );
96 isnt( $article_request->updated_on, undef, 'Updated article request has an updated_on date set' );
97
98 # complete
99 $article_request->complete();
100 is( $article_request->status, Koha::ArticleRequest::Status::Completed, '$ar->complete() changes status to Completed' );
101
102 # cancel
103 $article_request->cancel();
104 is( $article_request->status, Koha::ArticleRequest::Status::Canceled, '$ar->complete() changes status to Canceled' );
105 $article_request->set_pending();
106
107 is( $article_request->biblio->id,   $biblio->id, '$ar->biblio() gets corresponding Koha::Biblio object' );
108 is( $article_request->item->id,     $item->id,   '$ar->item() gets corresponding Koha::Item object' );
109 is( $article_request->borrower->id, $patron->id, '$ar->borrower() gets corresponding Koha::Patron object' );
110
111 my $ar = $patron->article_requests();
112 is( ref($ar),      'Koha::ArticleRequests', '$patron->article_requests returns Koha::ArticleRequests object' );
113 is( $ar->next->id, $article_request->id,    'Returned article request matches' );
114
115 is( $patron->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
116 $article_request->process();
117 is( $patron->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
118 $article_request->complete();
119 is( $patron->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
120 $article_request->cancel();
121 is( $patron->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
122
123 $article_request->set_pending();
124
125 is( $patron->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
126 $article_request->process();
127 is( $patron->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
128 $article_request->complete();
129 $article_request->cancel();
130 is( $patron->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
131
132 $article_request->set_pending();
133
134 $ar = $biblio->article_requests();
135 is( ref($ar),      'Koha::ArticleRequests', '$biblio->article_requests returns Koha::ArticleRequests object' );
136 is( $ar->next->id, $article_request->id,    'Returned article request matches' );
137
138 is( $biblio->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
139 $article_request->process();
140 is( $biblio->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
141 $article_request->complete();
142 is( $biblio->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
143 $article_request->cancel();
144 is( $biblio->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
145
146 $article_request->status(Koha::ArticleRequest::Status::Pending);
147 $article_request->store();
148
149 is( $biblio->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
150 $article_request->process();
151 is( $biblio->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
152 $article_request->complete();
153 $article_request->cancel();
154 is( $biblio->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
155
156 my $rule = Koha::CirculationRules->set_rule(
157     {
158         categorycode => undef,
159         itemtype     => undef,
160         branchcode   => undef,
161         rule_name    => 'article_requests',
162         rule_value   => 'yes',
163     }
164 );
165 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type yes' );
166 is( $biblio->article_request_type($patron), 'yes', 'Biblio article request type is yes' );
167 ok( $item->can_article_request($patron),   'Item is requestable with rule type yes' );
168 is( $item->article_request_type($patron), 'yes', 'Item article request type is yes' );
169 $rule->delete();
170
171 $rule = Koha::CirculationRules->set_rule(
172     {
173         categorycode => undef,
174         itemtype     => undef,
175         branchcode   => undef,
176         rule_name    => 'article_requests',
177         rule_value   => 'bib_only',
178     }
179 );
180 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type bib_only' );
181 is( $biblio->article_request_type($patron), 'bib_only', 'Biblio article request type is bib_only' );
182 ok( !$item->can_article_request($patron),  'Item is not requestable with rule type bib_only' );
183 is( $item->article_request_type($patron), 'bib_only', 'Item article request type is bib_only' );
184 $rule->delete();
185
186 $rule = Koha::CirculationRules->set_rule(
187     {
188         categorycode => undef,
189         itemtype     => undef,
190         branchcode   => undef,
191         rule_name    => 'article_requests',
192         rule_value   => 'item_only',
193     }
194 );
195 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type item_only' );
196 is( $biblio->article_request_type($patron), 'item_only', 'Biblio article request type is item_only' );
197 ok( $item->can_article_request($patron),   'Item is not requestable with rule type item_only' );
198 is( $item->article_request_type($patron), 'item_only', 'Item article request type is item_only' );
199 $rule->delete();
200
201 $rule = Koha::CirculationRules->set_rule(
202     {
203         categorycode => undef,
204         itemtype     => undef,
205         branchcode   => undef,
206         rule_name    => 'article_requests',
207         rule_value   => 'no',
208     }
209 );
210 ok( !$biblio->can_article_request($patron), 'Record is requestable with rule type no' );
211 is( $biblio->article_request_type($patron), 'no', 'Biblio article request type is no' );
212 ok( !$item->can_article_request($patron),   'Item is not requestable with rule type no' );
213 is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
214 $rule->delete();
215
216 subtest 'search_limited' => sub {
217     plan tests => 2;
218     my $nb_article_requests = Koha::ArticleRequests->count;
219
220     my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
221     my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
222     Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->branchcode })->store();
223     Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron_2->branchcode })->store();
224     t::lib::Mocks::mock_userenv( { patron => $patron } ); # Is superlibrarian
225     is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' );
226     t::lib::Mocks::mock_userenv( { patron => $patron_2 } ); # Is restricted
227     is( Koha::ArticleRequests->search_limited->count, 0, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' );
228 };
229
230 subtest 'may_article_request' => sub {
231     plan tests => 4;
232
233     # mocking
234     t::lib::Mocks::mock_preference('ArticleRequests', 1);
235     t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
236     $cache->set_in_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY, {
237         '*'  => { 'CR' => 1 },
238         'S'  => { '*'  => 1 },
239         'PT' => { 'BK' => 1 },
240     });
241
242     my $itemtype = Koha::ItemTypes->find('CR') // Koha::ItemType->new({ itemtype => 'CR' })->store;
243     is( $itemtype->may_article_request, 1, 'SER/* should be true' );
244     is( $itemtype->may_article_request({ categorycode => 'S' }), 1, 'SER/S should be true' );
245     is( $itemtype->may_article_request({ categorycode => 'PT' }), '', 'SER/PT should be false' );
246     t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'always');
247     is( $itemtype->may_article_request({ categorycode => 'PT' }), '1', 'Result should be true when LinkControl is set to always' );
248
249     # Cleanup
250     $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
251 };
252
253 $schema->storage->txn_rollback();