Bug 19564: (QA follow-up) Make commented out tests pass
[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 => 55;
23
24 use t::lib::TestBuilder;
25
26 use Koha::Database;
27 use Koha::Biblio;
28 use Koha::Notice::Messages;
29 use Koha::Patron;
30
31 use t::lib::TestBuilder;
32
33 BEGIN {
34     use_ok('Koha::ArticleRequest');
35     use_ok('Koha::ArticleRequests');
36     use_ok('Koha::ArticleRequest::Status');
37 }
38
39 my $schema = Koha::Database->new()->schema();
40 $schema->storage->txn_begin();
41 my $builder = t::lib::TestBuilder->new;
42
43 my $dbh = C4::Context->dbh;
44 $dbh->{RaiseError} = 1;
45
46 $dbh->do("DELETE FROM issuingrules");
47
48 my $biblio = Koha::Biblio->new()->store();
49 ok( $biblio->id, 'Koha::Biblio created' );
50
51 my $biblioitem = $schema->resultset('Biblioitem')->new(
52     {
53         biblionumber => $biblio->id
54     }
55 )->insert();
56 ok( $biblioitem->id, 'biblioitem created' );
57
58 my $itype = $builder->build({ source => 'Itemtype' });
59 my $item = Koha::Item->new(
60     {
61         biblionumber     => $biblio->id,
62         biblioitemnumber => $biblioitem->id,
63         itype => $itype->{itemtype},
64     }
65 )->store();
66 ok( $item->id, 'Koha::Item created' );
67
68 my $branch   = $builder->build({ source => 'Branch' });
69 my $category = $builder->build({ source => 'Category' });
70 my $patron   = Koha::Patron->new(
71     {
72         categorycode => $category->{categorycode},
73         branchcode   => $branch->{branchcode},
74         flags        => 1,# superlibrarian
75     }
76 )->store();
77 ok( $patron->id, 'Koha::Patron created' );
78 my $patron_2 = $builder->build({ source => 'Borrower', value => { flags => 0 } });
79 $patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
80
81 my $nb_article_requests = Koha::ArticleRequests->count;
82
83 # store
84 Koha::Notice::Messages->delete;
85 my $article_request_title = 'an article request title';
86 my $article_request = Koha::ArticleRequest->new(
87     {
88         borrowernumber => $patron->id,
89         biblionumber   => $biblio->id,
90         itemnumber     => $item->id,
91         title          => $article_request_title,
92     }
93 )->store();
94
95 my $notify_message = Koha::Notice::Messages->search->next;
96 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Pending);
97 # Default AR_PROCESSING template content "Title: <<article_requests.title>>"
98 like( $notify_message->content, qr{Title: $article_request_title}, 'Values from article_requests table must be fetched for the notification' );
99
100 $article_request = Koha::ArticleRequests->find( $article_request->id );
101 ok( $article_request->id, 'Koha::ArticleRequest created' );
102 is( $article_request->status, Koha::ArticleRequest::Status::Pending, 'New article request has status of Open' );
103 is( $article_request->updated_on, undef, 'New article request has not an updated_on date set yet' );
104
105 # process
106 Koha::Notice::Messages->delete;
107 $article_request->process();
108 $notify_message = Koha::Notice::Messages->search->next;
109 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Processing);
110 is( $article_request->status, Koha::ArticleRequest::Status::Processing, '$ar->process() changes status to Processing' );
111 isnt( $article_request->updated_on, undef, 'Updated article request has an updated_on date set' );
112
113 # complete
114 $article_request->complete();
115 is( $article_request->status, Koha::ArticleRequest::Status::Completed, '$ar->complete() changes status to Completed' );
116
117 # cancel
118 $article_request->cancel();
119 is( $article_request->status, Koha::ArticleRequest::Status::Canceled, '$ar->complete() changes status to Canceled' );
120 $article_request->status(Koha::ArticleRequest::Status::Pending);
121 $article_request->store();
122
123 is( $article_request->biblio->id,   $biblio->id, '$ar->biblio() gets corresponding Koha::Biblio object' );
124 is( $article_request->item->id,     $item->id,   '$ar->item() gets corresponding Koha::Item object' );
125 is( $article_request->borrower->id, $patron->id, '$ar->borrower() gets corresponding Koha::Patron object' );
126
127 my $ar = $patron->article_requests();
128 is( ref($ar),      'Koha::ArticleRequests', '$patron->article_requests returns Koha::ArticleRequests object' );
129 is( $ar->next->id, $article_request->id,    'Returned article request matches' );
130
131 is( $patron->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
132 $article_request->process();
133 is( $patron->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
134 $article_request->complete();
135 is( $patron->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
136 $article_request->cancel();
137 is( $patron->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
138
139 $article_request->status(Koha::ArticleRequest::Status::Pending);
140 $article_request->store();
141
142 is( $patron->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
143 $article_request->process();
144 is( $patron->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
145 $article_request->complete();
146 $article_request->cancel();
147 is( $patron->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
148
149 $article_request->status(Koha::ArticleRequest::Status::Pending);
150 $article_request->store();
151
152 $ar = $biblio->article_requests();
153 is( ref($ar),      'Koha::ArticleRequests', '$biblio->article_requests returns Koha::ArticleRequests object' );
154 is( $ar->next->id, $article_request->id,    'Returned article request matches' );
155
156 is( $biblio->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
157 $article_request->process();
158 is( $biblio->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
159 $article_request->complete();
160 is( $biblio->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
161 $article_request->cancel();
162 is( $biblio->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
163
164 $article_request->status(Koha::ArticleRequest::Status::Pending);
165 $article_request->store();
166
167 is( $biblio->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
168 $article_request->process();
169 is( $biblio->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
170 $article_request->complete();
171 $article_request->cancel();
172 is( $biblio->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
173
174 my $rule;
175 $rule = $schema->resultset('Issuingrule')
176   ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'yes' } )->insert();
177 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type yes' );
178 is( $biblio->article_request_type($patron), 'yes', 'Biblio article request type is yes' );
179 ok( $item->can_article_request($patron),   'Item is requestable with rule type yes' );
180 is( $item->article_request_type($patron), 'yes', 'Item article request type is yes' );
181 $rule->delete();
182
183 $rule = $schema->resultset('Issuingrule')
184   ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'bib_only' } )->insert();
185 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type bib_only' );
186 is( $biblio->article_request_type($patron), 'bib_only', 'Biblio article request type is bib_only' );
187 ok( !$item->can_article_request($patron),  'Item is not requestable with rule type bib_only' );
188 is( $item->article_request_type($patron), 'bib_only', 'Item article request type is bib_only' );
189 $rule->delete();
190
191 $rule = $schema->resultset('Issuingrule')
192   ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'item_only' } )->insert();
193 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type item_only' );
194 is( $biblio->article_request_type($patron), 'item_only', 'Biblio article request type is item_only' );
195 ok( $item->can_article_request($patron),   'Item is not requestable with rule type item_only' );
196 is( $item->article_request_type($patron), 'item_only', 'Item article request type is item_only' );
197 $rule->delete();
198
199 $rule = $schema->resultset('Issuingrule')
200   ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'no' } )->insert();
201 ok( !$biblio->can_article_request($patron), 'Record is requestable with rule type no' );
202 is( $biblio->article_request_type($patron), 'no', 'Biblio article request type is no' );
203 ok( !$item->can_article_request($patron),   'Item is not requestable with rule type no' );
204 is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
205 $rule->delete();
206
207 subtest 'search_limited' => sub {
208     plan tests => 4;
209     C4::Context->_new_userenv('xxx');
210     my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
211     my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
212     Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->branchcode })->store();
213     Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron_2->branchcode })->store();
214     set_logged_in_user( $patron ); # Is superlibrarian
215     is( Koha::ArticleRequests->count, $nb_article_requests + 1, 'Koha::ArticleRequests should return all article requests' );
216     is( Koha::ArticleRequests->search_limited->count, $nb_article_requests + 1, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' );
217     set_logged_in_user( $patron_2 ); # Is restricted
218     is( Koha::ArticleRequests->count, $nb_article_requests + 1, 'Koha::ArticleRequests should return all article requests' );
219     is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' );
220 };
221
222 $schema->storage->txn_rollback();
223
224 sub set_logged_in_user {
225     my ($patron) = @_;
226     C4::Context->set_userenv(
227         $patron->borrowernumber, $patron->userid,
228         $patron->cardnumber,     'firstname',
229         'surname',               $patron->library->branchcode,
230         'Midway Public Library', $patron->flags,
231         '',                      ''
232     );
233 }