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