52177bf3bb365cc0363f23971a459942d511062a
[koha.git] / t / db_dependent / api / v1 / holds.t
1 #!/usr/bin/env perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Test::More tests => 39;
21 use Test::Mojo;
22
23 use DateTime;
24
25 use C4::Context;
26 use C4::Biblio;
27 use C4::Items;
28 use C4::Reserves;
29
30 use Koha::Database;
31 use Koha::Patron;
32
33 my $dbh = C4::Context->dbh;
34 $dbh->{AutoCommit} = 0;
35 $dbh->{RaiseError} = 1;
36
37 my $t = Test::Mojo->new('Koha::REST::V1');
38
39 my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
40 my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
41
42 my $borrower = Koha::Patron->new;
43 $borrower->categorycode( $categorycode );
44 $borrower->branchcode( $branchcode );
45 $borrower->surname("Test Surname");
46 $borrower->store;
47 my $borrowernumber = $borrower->borrowernumber;
48
49 my $borrower2 = Koha::Patron->new;
50 $borrower2->categorycode( $categorycode );
51 $borrower2->branchcode( $branchcode );
52 $borrower2->surname("Test Surname 2");
53 $borrower2->store;
54 my $borrowernumber2 = $borrower2->borrowernumber;
55
56 my $biblionumber = create_biblio('RESTful Web APIs');
57 my $itemnumber = create_item($biblionumber, 'TEST000001');
58
59 $dbh->do('DELETE FROM reserves');
60
61 my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber,
62     $biblionumber, undef, 1, undef, undef, undef, '', $itemnumber);
63
64 # Add another reserve to be able to change first reserve's rank
65 C4::Reserves::AddReserve($branchcode, $borrowernumber2,
66     $biblionumber, undef, 2, undef, undef, undef, '', $itemnumber);
67
68 $t->get_ok('/api/v1/holds')
69     ->status_is(200)
70     ->json_has('/0')
71     ->json_has('/1')
72     ->json_hasnt('/2');
73
74 $t->get_ok('/api/v1/holds?priority=2')
75     ->status_is(200)
76     ->json_is('/0/borrowernumber', $borrowernumber2)
77     ->json_hasnt('/1');
78
79 my $suspend_until = DateTime->now->add(days => 10)->ymd;
80 my $put_data = {
81     priority => 2,
82     suspend_until => $suspend_until,
83 };
84 $t->put_ok("/api/v1/holds/$reserve_id" => json => $put_data)
85   ->status_is(200)
86   ->json_is('/reserve_id', $reserve_id)
87   ->json_is('/suspend_until', $suspend_until . ' 00:00:00')
88   ->json_is('/priority', 2);
89
90 $t->delete_ok("/api/v1/holds/$reserve_id")
91   ->status_is(200);
92
93 $t->put_ok("/api/v1/holds/$reserve_id" => json => $put_data)
94   ->status_is(404)
95   ->json_has('/error');
96
97 $t->delete_ok("/api/v1/holds/$reserve_id")
98   ->status_is(404)
99   ->json_has('/error');
100
101
102 $t->get_ok("/api/v1/holds?borrowernumber=$borrowernumber")
103   ->status_is(200)
104   ->json_is([]);
105
106 my $inexisting_borrowernumber = $borrowernumber2 + 1;
107 $t->get_ok("/api/v1/holds?borrowernumber=$inexisting_borrowernumber")
108   ->status_is(200)
109   ->json_is([]);
110
111 $dbh->do('DELETE FROM issuingrules');
112 $dbh->do(q{
113     INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
114     VALUES (?, ?, ?, ?)
115 }, {}, '*', '*', '*', 1);
116
117 my $expirationdate = DateTime->now->add(days => 10)->ymd;
118 my $post_data = {
119     borrowernumber => int($borrowernumber),
120     biblionumber => int($biblionumber),
121     itemnumber => int($itemnumber),
122     branchcode => $branchcode,
123     expirationdate => $expirationdate,
124 };
125 $t->post_ok("/api/v1/holds" => json => $post_data)
126   ->status_is(201)
127   ->json_has('/reserve_id');
128
129 $reserve_id = $t->tx->res->json->{reserve_id};
130
131 $t->get_ok("/api/v1/holds?borrowernumber=$borrowernumber")
132   ->status_is(200)
133   ->json_is('/0/reserve_id', $reserve_id)
134   ->json_is('/0/expirationdate', $expirationdate)
135   ->json_is('/0/branchcode', $branchcode);
136
137 $t->post_ok("/api/v1/holds" => json => $post_data)
138   ->status_is(403)
139   ->json_like('/error', qr/tooManyReserves/);
140
141
142 $dbh->rollback;
143
144 sub create_biblio {
145     my ($title) = @_;
146
147     my $record = new MARC::Record;
148     $record->append_fields(
149         new MARC::Field('200', ' ', ' ', a => $title),
150     );
151
152     my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
153
154     return $biblionumber;
155 }
156
157 sub create_item {
158     my ($biblionumber, $barcode) = @_;
159
160     my $item = {
161         barcode => $barcode,
162     };
163
164     my $itemnumber = C4::Items::AddItem($item, $biblionumber);
165
166     return $itemnumber;
167 }