Bug 18477: Add tests
[koha.git] / t / db_dependent / ArticleRequests.t
blob4d5631563332efb5b60becfdf5e8fd7706a47599
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use POSIX qw(strftime);
22 use Test::More tests => 54;
24 use t::lib::TestBuilder;
26 use Koha::Database;
27 use Koha::Biblio;
28 use Koha::Notice::Messages;
29 use Koha::Patron;
30 use Koha::Library;
32 BEGIN {
33 use_ok('Koha::ArticleRequest');
34 use_ok('Koha::ArticleRequests');
35 use_ok('Koha::ArticleRequest::Status');
38 my $schema = Koha::Database->new()->schema();
39 $schema->storage->txn_begin();
40 my $builder = t::lib::TestBuilder->new;
42 my $dbh = C4::Context->dbh;
43 $dbh->{RaiseError} = 1;
45 $dbh->do("DELETE FROM issuingrules");
47 my $biblio = Koha::Biblio->new()->store();
48 ok( $biblio->id, 'Koha::Biblio created' );
50 my $biblioitem = $schema->resultset('Biblioitem')->new(
52 biblionumber => $biblio->id
54 )->insert();
55 ok( $biblioitem->id, 'biblioitem created' );
57 my $itype = $builder->build({ source => 'Itemtype' });
58 my $item = Koha::Item->new(
60 biblionumber => $biblio->id,
61 biblioitemnumber => $biblioitem->id,
62 itype => $itype->{itemtype},
64 )->store();
65 ok( $item->id, 'Koha::Item created' );
67 my $branch = $builder->build({ source => 'Branch' });
68 my $category = $builder->build({ source => 'Category' });
69 my $patron = Koha::Patron->new(
71 categorycode => $category->{categorycode},
72 branchcode => $branch->{branchcode},
74 )->store();
75 ok( $patron->id, 'Koha::Patron created' );
77 # store
78 Koha::Notice::Messages->delete;
79 my $article_request_title = 'an article request title';
80 my $article_request = Koha::ArticleRequest->new(
82 borrowernumber => $patron->id,
83 biblionumber => $biblio->id,
84 itemnumber => $item->id,
85 title => $article_request_title,
87 )->store();
89 my $notify_message = Koha::Notice::Messages->search->next;
90 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Pending);
91 # Default AR_PROCESSING template content "Title: <<article_requests.title>>"
92 like( $notify_message->content, qr{Title: $article_request_title}, 'Values from article_requests table must be fetched for the notification' );
94 $article_request = Koha::ArticleRequests->find( $article_request->id );
95 ok( $article_request->id, 'Koha::ArticleRequest created' );
96 is( $article_request->status, Koha::ArticleRequest::Status::Pending, 'New article request has status of Open' );
97 is( $article_request->updated_on, undef, 'New article request has not an updated_on date set yet' );
99 # process
100 Koha::Notice::Messages->delete;
101 $article_request->process();
102 $notify_message = Koha::Notice::Messages->search->next;
103 is( $notify_message->letter_code, "AR_".Koha::ArticleRequest::Status::Processing);
104 is( $article_request->status, Koha::ArticleRequest::Status::Processing, '$ar->process() changes status to Processing' );
105 isnt( $article_request->updated_on, undef, 'Updated article request has an updated_on date set' );
107 # complete
108 $article_request->complete();
109 is( $article_request->status, Koha::ArticleRequest::Status::Completed, '$ar->complete() changes status to Completed' );
111 # cancel
112 $article_request->cancel();
113 is( $article_request->status, Koha::ArticleRequest::Status::Canceled, '$ar->complete() changes status to Canceled' );
114 $article_request->status(Koha::ArticleRequest::Status::Pending);
115 $article_request->store();
117 is( $article_request->biblio->id, $biblio->id, '$ar->biblio() gets corresponding Koha::Biblio object' );
118 is( $article_request->item->id, $item->id, '$ar->item() gets corresponding Koha::Item object' );
119 is( $article_request->borrower->id, $patron->id, '$ar->borrower() gets corresponding Koha::Patron object' );
121 my $ar = $patron->article_requests();
122 is( ref($ar), 'Koha::ArticleRequests', '$patron->article_requests returns Koha::ArticleRequests object' );
123 is( $ar->next->id, $article_request->id, 'Returned article request matches' );
125 is( $patron->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
126 $article_request->process();
127 is( $patron->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
128 $article_request->complete();
129 is( $patron->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
130 $article_request->cancel();
131 is( $patron->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
133 $article_request->status(Koha::ArticleRequest::Status::Pending);
134 $article_request->store();
136 is( $patron->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
137 $article_request->process();
138 is( $patron->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
139 $article_request->complete();
140 $article_request->cancel();
141 is( $patron->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
143 $article_request->status(Koha::ArticleRequest::Status::Pending);
144 $article_request->store();
146 $ar = $biblio->article_requests();
147 is( ref($ar), 'Koha::ArticleRequests', '$biblio->article_requests returns Koha::ArticleRequests object' );
148 is( $ar->next->id, $article_request->id, 'Returned article request matches' );
150 is( $biblio->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
151 $article_request->process();
152 is( $biblio->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
153 $article_request->complete();
154 is( $biblio->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
155 $article_request->cancel();
156 is( $biblio->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
158 $article_request->status(Koha::ArticleRequest::Status::Pending);
159 $article_request->store();
161 is( $biblio->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
162 $article_request->process();
163 is( $biblio->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
164 $article_request->complete();
165 $article_request->cancel();
166 is( $biblio->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
168 my $rule;
169 $rule = $schema->resultset('Issuingrule')
170 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'yes' } )->insert();
171 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type yes' );
172 is( $biblio->article_request_type($patron), 'yes', 'Biblio article request type is yes' );
173 ok( $item->can_article_request($patron), 'Item is requestable with rule type yes' );
174 is( $item->article_request_type($patron), 'yes', 'Item article request type is yes' );
175 $rule->delete();
177 $rule = $schema->resultset('Issuingrule')
178 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'bib_only' } )->insert();
179 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type bib_only' );
180 is( $biblio->article_request_type($patron), 'bib_only', 'Biblio article request type is bib_only' );
181 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type bib_only' );
182 is( $item->article_request_type($patron), 'bib_only', 'Item article request type is bib_only' );
183 $rule->delete();
185 $rule = $schema->resultset('Issuingrule')
186 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'item_only' } )->insert();
187 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type item_only' );
188 is( $biblio->article_request_type($patron), 'item_only', 'Biblio article request type is item_only' );
189 ok( $item->can_article_request($patron), 'Item is not requestable with rule type item_only' );
190 is( $item->article_request_type($patron), 'item_only', 'Item article request type is item_only' );
191 $rule->delete();
193 $rule = $schema->resultset('Issuingrule')
194 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'no' } )->insert();
195 ok( !$biblio->can_article_request($patron), 'Record is requestable with rule type no' );
196 is( $biblio->article_request_type($patron), 'no', 'Biblio article request type is no' );
197 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type no' );
198 is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
199 $rule->delete();
201 $schema->storage->txn_rollback();