Bug 18624: Regression test for 'any' vs 'all'
[koha.git] / t / db_dependent / ArticleRequests.t
bloba9466a669355f2fd1058e21b8127f38e805f66fd
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 => 49;
24 use t::lib::TestBuilder;
26 use Koha::Database;
27 use Koha::Biblio;
28 use Koha::Patron;
29 use Koha::Library;
31 BEGIN {
32 use_ok('Koha::ArticleRequest');
33 use_ok('Koha::ArticleRequests');
34 use_ok('Koha::ArticleRequest::Status');
37 my $schema = Koha::Database->new()->schema();
38 $schema->storage->txn_begin();
39 my $builder = t::lib::TestBuilder->new;
41 my $dbh = C4::Context->dbh;
42 $dbh->{RaiseError} = 1;
44 $dbh->do("DELETE FROM issuingrules");
46 my $biblio = Koha::Biblio->new()->store();
47 ok( $biblio->id, 'Koha::Biblio created' );
49 my $biblioitem = $schema->resultset('Biblioitem')->new(
51 biblionumber => $biblio->id
53 )->insert();
54 ok( $biblioitem->id, 'biblioitem created' );
56 my $itype = $builder->build({ source => 'Itemtype' });
57 my $item = Koha::Item->new(
59 biblionumber => $biblio->id,
60 biblioitemnumber => $biblioitem->id,
61 itype => $itype->{itemtype},
63 )->store();
64 ok( $item->id, 'Koha::Item created' );
66 my $branch = $builder->build({ source => 'Branch' });
67 my $category = $builder->build({ source => 'Category' });
68 my $patron = Koha::Patron->new(
70 categorycode => $category->{categorycode},
71 branchcode => $branch->{branchcode},
73 )->store();
74 ok( $patron->id, 'Koha::Patron created' );
76 my $article_request = Koha::ArticleRequest->new(
78 borrowernumber => $patron->id,
79 biblionumber => $biblio->id,
80 itemnumber => $item->id,
82 )->store();
83 $article_request = Koha::ArticleRequests->find( $article_request->id );
84 ok( $article_request->id, 'Koha::ArticleRequest created' );
86 is( $article_request->status, Koha::ArticleRequest::Status::Pending, 'New article request has status of Open' );
87 $article_request->process();
88 is( $article_request->status, Koha::ArticleRequest::Status::Processing, '$ar->process() changes status to Processing' );
89 $article_request->complete();
90 is( $article_request->status, Koha::ArticleRequest::Status::Completed, '$ar->complete() changes status to Completed' );
91 $article_request->cancel();
92 is( $article_request->status, Koha::ArticleRequest::Status::Canceled, '$ar->complete() changes status to Canceled' );
93 $article_request->status(Koha::ArticleRequest::Status::Pending);
94 $article_request->store();
96 is( $article_request->biblio->id, $biblio->id, '$ar->biblio() gets corrosponding Koha::Biblio object' );
97 is( $article_request->item->id, $item->id, '$ar->item() gets corrosponding Koha::Item object' );
98 is( $article_request->borrower->id, $patron->id, '$ar->borrower() gets corrosponding Koha::Patron object' );
100 my $ar = $patron->article_requests();
101 is( ref($ar), 'Koha::ArticleRequests', '$patron->article_requests returns Koha::ArticleRequests object' );
102 is( $ar->next->id, $article_request->id, 'Returned article request matches' );
104 is( $patron->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
105 $article_request->process();
106 is( $patron->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
107 $article_request->complete();
108 is( $patron->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
109 $article_request->cancel();
110 is( $patron->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
112 $article_request->status(Koha::ArticleRequest::Status::Pending);
113 $article_request->store();
115 is( $patron->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
116 $article_request->process();
117 is( $patron->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
118 $article_request->complete();
119 $article_request->cancel();
120 is( $patron->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
122 $article_request->status(Koha::ArticleRequest::Status::Pending);
123 $article_request->store();
125 $ar = $biblio->article_requests();
126 is( ref($ar), 'Koha::ArticleRequests', '$biblio->article_requests returns Koha::ArticleRequests object' );
127 is( $ar->next->id, $article_request->id, 'Returned article request matches' );
129 is( $biblio->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
130 $article_request->process();
131 is( $biblio->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
132 $article_request->complete();
133 is( $biblio->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
134 $article_request->cancel();
135 is( $biblio->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
137 $article_request->status(Koha::ArticleRequest::Status::Pending);
138 $article_request->store();
140 is( $biblio->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
141 $article_request->process();
142 is( $biblio->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
143 $article_request->complete();
144 $article_request->cancel();
145 is( $biblio->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
147 my $rule;
148 $rule = $schema->resultset('Issuingrule')
149 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'yes' } )->insert();
150 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type yes' );
151 is( $biblio->article_request_type($patron), 'yes', 'Biblio article request type is yes' );
152 ok( $item->can_article_request($patron), 'Item is requestable with rule type yes' );
153 is( $item->article_request_type($patron), 'yes', 'Item article request type is yes' );
154 $rule->delete();
156 $rule = $schema->resultset('Issuingrule')
157 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'bib_only' } )->insert();
158 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type bib_only' );
159 is( $biblio->article_request_type($patron), 'bib_only', 'Biblio article request type is bib_only' );
160 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type bib_only' );
161 is( $item->article_request_type($patron), 'bib_only', 'Item article request type is bib_only' );
162 $rule->delete();
164 $rule = $schema->resultset('Issuingrule')
165 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'item_only' } )->insert();
166 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type item_only' );
167 is( $biblio->article_request_type($patron), 'item_only', 'Biblio article request type is item_only' );
168 ok( $item->can_article_request($patron), 'Item is not requestable with rule type item_only' );
169 is( $item->article_request_type($patron), 'item_only', 'Item article request type is item_only' );
170 $rule->delete();
172 $rule = $schema->resultset('Issuingrule')
173 ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'no' } )->insert();
174 ok( !$biblio->can_article_request($patron), 'Record is requestable with rule type no' );
175 is( $biblio->article_request_type($patron), 'no', 'Biblio article request type is no' );
176 ok( !$item->can_article_request($patron), 'Item is not requestable with rule type no' );
177 is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
178 $rule->delete();
180 $schema->storage->txn_rollback();