Bug 14598: (followup) Remove unused and non-existent C4::ItemType include
[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 => 49;
23 use Koha::Database;
24
25 use Koha::Biblio;
26 use Koha::Patron;
27 use Koha::Library;
28
29 BEGIN {
30     use_ok('Koha::ArticleRequest');
31     use_ok('Koha::ArticleRequests');
32     use_ok('Koha::ArticleRequest::Status');
33 }
34
35 my $schema = Koha::Database->new()->schema();
36 $schema->storage->txn_begin();
37
38 my $dbh = C4::Context->dbh;
39 $dbh->{RaiseError} = 1;
40
41 $dbh->do("DELETE FROM issuingrules");
42
43 my $biblio = Koha::Biblio->new()->store();
44 ok( $biblio->id, 'Koha::Biblio created' );
45
46 my $biblioitem = $schema->resultset('Biblioitem')->new(
47     {
48         biblionumber => $biblio->id
49     }
50 )->insert();
51 ok( $biblioitem->id, 'biblioitem created' );
52
53 my $item = Koha::Item->new(
54     {
55         biblionumber     => $biblio->id,
56         biblioitemnumber => $biblioitem->id,
57         itype => $schema->resultset('Itemtype')->search()->next()->itemtype(),
58     }
59 )->store();
60 ok( $item->id, 'Koha::Item created' );
61
62 my $branch   = Koha::Libraries->search()->next();
63 my $category = $schema->resultset('Category')->next();
64 my $patron   = Koha::Patron->new(
65     {
66         categorycode => $category->id,
67         branchcode   => $branch->id,
68     }
69 )->store();
70 ok( $patron->id, 'Koha::Patron created' );
71
72 my $article_request = Koha::ArticleRequest->new(
73     {
74         borrowernumber => $patron->id,
75         biblionumber   => $biblio->id,
76         itemnumber     => $item->id,
77     }
78 )->store();
79 $article_request = Koha::ArticleRequests->find( $article_request->id );
80 ok( $article_request->id, 'Koha::ArticleRequest created' );
81
82 is( $article_request->status, Koha::ArticleRequest::Status::Pending, 'New article request has status of Open' );
83 $article_request->process();
84 is( $article_request->status, Koha::ArticleRequest::Status::Processing, '$ar->process() changes status to Processing' );
85 $article_request->complete();
86 is( $article_request->status, Koha::ArticleRequest::Status::Completed, '$ar->complete() changes status to Completed' );
87 $article_request->cancel();
88 is( $article_request->status, Koha::ArticleRequest::Status::Canceled, '$ar->complete() changes status to Canceled' );
89 $article_request->status(Koha::ArticleRequest::Status::Pending);
90 $article_request->store();
91
92 is( $article_request->biblio->id,   $biblio->id, '$ar->biblio() gets corrosponding Koha::Biblio object' );
93 is( $article_request->item->id,     $item->id,   '$ar->item() gets corrosponding Koha::Item object' );
94 is( $article_request->borrower->id, $patron->id, '$ar->borrower() gets corrosponding Koha::Patron object' );
95
96 my $ar = $patron->article_requests();
97 is( ref($ar),      'Koha::ArticleRequests', '$patron->article_requests returns Koha::ArticleRequests object' );
98 is( $ar->next->id, $article_request->id,    'Returned article request matches' );
99
100 is( $patron->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
101 $article_request->process();
102 is( $patron->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
103 $article_request->complete();
104 is( $patron->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
105 $article_request->cancel();
106 is( $patron->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
107
108 $article_request->status(Koha::ArticleRequest::Status::Pending);
109 $article_request->store();
110
111 is( $patron->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
112 $article_request->process();
113 is( $patron->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
114 $article_request->complete();
115 $article_request->cancel();
116 is( $patron->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
117
118 $article_request->status(Koha::ArticleRequest::Status::Pending);
119 $article_request->store();
120
121 $ar = $biblio->article_requests();
122 is( ref($ar),      'Koha::ArticleRequests', '$biblio->article_requests returns Koha::ArticleRequests object' );
123 is( $ar->next->id, $article_request->id,    'Returned article request matches' );
124
125 is( $biblio->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
126 $article_request->process();
127 is( $biblio->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
128 $article_request->complete();
129 is( $biblio->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
130 $article_request->cancel();
131 is( $biblio->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
132
133 $article_request->status(Koha::ArticleRequest::Status::Pending);
134 $article_request->store();
135
136 is( $biblio->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
137 $article_request->process();
138 is( $biblio->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
139 $article_request->complete();
140 $article_request->cancel();
141 is( $biblio->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
142
143 my $rule;
144 $rule = $schema->resultset('Issuingrule')
145   ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'yes' } )->insert();
146 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type yes' );
147 is( $biblio->article_request_type($patron), 'yes', 'Biblio article request type is yes' );
148 ok( $item->can_article_request($patron),   'Item is requestable with rule type yes' );
149 is( $item->article_request_type($patron), 'yes', 'Item article request type is yes' );
150 $rule->delete();
151
152 $rule = $schema->resultset('Issuingrule')
153   ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'bib_only' } )->insert();
154 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type bib_only' );
155 is( $biblio->article_request_type($patron), 'bib_only', 'Biblio article request type is bib_only' );
156 ok( !$item->can_article_request($patron),  'Item is not requestable with rule type bib_only' );
157 is( $item->article_request_type($patron), 'bib_only', 'Item article request type is bib_only' );
158 $rule->delete();
159
160 $rule = $schema->resultset('Issuingrule')
161   ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'item_only' } )->insert();
162 ok( $biblio->can_article_request($patron), 'Record is requestable with rule type item_only' );
163 is( $biblio->article_request_type($patron), 'item_only', 'Biblio article request type is item_only' );
164 ok( $item->can_article_request($patron),   'Item is not requestable with rule type item_only' );
165 is( $item->article_request_type($patron), 'item_only', 'Item article request type is item_only' );
166 $rule->delete();
167
168 $rule = $schema->resultset('Issuingrule')
169   ->new( { categorycode => '*', itemtype => '*', branchcode => '*', article_requests => 'no' } )->insert();
170 ok( !$biblio->can_article_request($patron), 'Record is requestable with rule type no' );
171 is( $biblio->article_request_type($patron), 'no', 'Biblio article request type is no' );
172 ok( !$item->can_article_request($patron),   'Item is not requestable with rule type no' );
173 is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
174 $rule->delete();
175
176 $schema->storage->txn_rollback();