3 # This file is part of Koha.
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
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.
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.
20 use Test::More tests => 39;
33 my $dbh = C4::Context->dbh;
34 $dbh->{AutoCommit} = 0;
35 $dbh->{RaiseError} = 1;
37 my $t = Test::Mojo->new('Koha::REST::V1');
39 my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
40 my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
42 my $borrower = Koha::Patron->new;
43 $borrower->categorycode( $categorycode );
44 $borrower->branchcode( $branchcode );
45 $borrower->surname("Test Surname");
47 my $borrowernumber = $borrower->borrowernumber;
49 my $borrower2 = Koha::Patron->new;
50 $borrower2->categorycode( $categorycode );
51 $borrower2->branchcode( $branchcode );
52 $borrower2->surname("Test Surname 2");
54 my $borrowernumber2 = $borrower2->borrowernumber;
56 my $biblionumber = create_biblio('RESTful Web APIs');
57 my $itemnumber = create_item($biblionumber, 'TEST000001');
59 $dbh->do('DELETE FROM reserves');
61 my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber,
62 $biblionumber, undef, 1, undef, undef, undef, '', $itemnumber);
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);
68 $t->get_ok('/api/v1/holds')
74 $t->get_ok('/api/v1/holds?priority=2')
76 ->json_is('/0/borrowernumber', $borrowernumber2)
79 my $suspend_until = DateTime->now->add(days => 10)->ymd;
82 suspend_until => $suspend_until,
84 $t->put_ok("/api/v1/holds/$reserve_id" => json => $put_data)
86 ->json_is('/reserve_id', $reserve_id)
87 ->json_is('/suspend_until', $suspend_until . ' 00:00:00')
88 ->json_is('/priority', 2);
90 $t->delete_ok("/api/v1/holds/$reserve_id")
93 $t->put_ok("/api/v1/holds/$reserve_id" => json => $put_data)
97 $t->delete_ok("/api/v1/holds/$reserve_id")
102 $t->get_ok("/api/v1/holds?borrowernumber=$borrowernumber")
106 my $inexisting_borrowernumber = $borrowernumber2 + 1;
107 $t->get_ok("/api/v1/holds?borrowernumber=$inexisting_borrowernumber")
111 $dbh->do('DELETE FROM issuingrules');
113 INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
115 }, {}, '*', '*', '*', 1);
117 my $expirationdate = DateTime->now->add(days => 10)->ymd;
119 borrowernumber => int($borrowernumber),
120 biblionumber => int($biblionumber),
121 itemnumber => int($itemnumber),
122 branchcode => $branchcode,
123 expirationdate => $expirationdate,
125 $t->post_ok("/api/v1/holds" => json => $post_data)
127 ->json_has('/reserve_id');
129 $reserve_id = $t->tx->res->json->{reserve_id};
131 $t->get_ok("/api/v1/holds?borrowernumber=$borrowernumber")
133 ->json_is('/0/reserve_id', $reserve_id)
134 ->json_is('/0/expirationdate', $expirationdate)
135 ->json_is('/0/branchcode', $branchcode);
137 $t->post_ok("/api/v1/holds" => json => $post_data)
139 ->json_like('/error', qr/tooManyReserves/);
147 my $record = new MARC::Record;
148 $record->append_fields(
149 new MARC::Field('200', ' ', ' ', a => $title),
152 my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
154 return $biblionumber;
158 my ($biblionumber, $barcode) = @_;
164 my $itemnumber = C4::Items::AddItem($item, $biblionumber);