Bug 31196: Remove 'default_value_for_mod_marc-' clear_from_cache calls
[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 => 36;
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 )->request();
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 $rule = Koha::CirculationRules->set_rule(
114     {
115         categorycode => undef,
116         itemtype     => undef,
117         branchcode   => undef,
118         rule_name    => 'article_requests',
119         rule_value   => 'yes',
120     }
121 );
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' );
126 $rule->delete();
127
128 $rule = Koha::CirculationRules->set_rule(
129     {
130         categorycode => undef,
131         itemtype     => undef,
132         branchcode   => undef,
133         rule_name    => 'article_requests',
134         rule_value   => 'bib_only',
135     }
136 );
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' );
141 $rule->delete();
142
143 $rule = Koha::CirculationRules->set_rule(
144     {
145         categorycode => undef,
146         itemtype     => undef,
147         branchcode   => undef,
148         rule_name    => 'article_requests',
149         rule_value   => 'item_only',
150     }
151 );
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' );
156 $rule->delete();
157
158 $rule = Koha::CirculationRules->set_rule(
159     {
160         categorycode => undef,
161         itemtype     => undef,
162         branchcode   => undef,
163         rule_name    => 'article_requests',
164         rule_value   => 'no',
165     }
166 );
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' );
171 $rule->delete();
172
173 subtest 'search_limited' => sub {
174     plan tests => 2;
175     my $nb_article_requests = Koha::ArticleRequests->count;
176
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' );
185 };
186
187 subtest 'may_article_request' => sub {
188     plan tests => 4;
189
190     # mocking
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 },
195         'S'  => { '*'  => 1 },
196         'PT' => { 'BK' => 1 },
197     });
198
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' );
205
206     # Cleanup
207     $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
208 };
209
210 $schema->storage->txn_rollback();