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 => 30;
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 my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber,
60 $biblionumber, undef, 1, undef, undef, undef, '', $itemnumber);
62 # Add another reserve to be able to change first reserve's rank
63 C4::Reserves::AddReserve($branchcode, $borrowernumber2,
64 $biblionumber, undef, 2, undef, undef, undef, '', $itemnumber);
66 my $suspend_until = DateTime->now->add(days => 10)->ymd;
69 suspend_until => $suspend_until,
71 $t->put_ok("/api/v1/reserves/$reserve_id" => json => $put_data)
73 ->json_is('/reserve_id', $reserve_id)
74 ->json_is('/suspend_until', $suspend_until . ' 00:00:00')
75 ->json_is('/priority', 2);
77 $t->delete_ok("/api/v1/reserves/$reserve_id")
80 $t->put_ok("/api/v1/reserves/$reserve_id" => json => $put_data)
84 $t->delete_ok("/api/v1/reserves/$reserve_id")
89 $t->get_ok("/api/v1/reserves?borrowernumber=$borrowernumber")
93 my $inexisting_borrowernumber = $borrowernumber2 + 1;
94 $t->get_ok("/api/v1/reserves?borrowernumber=$inexisting_borrowernumber")
98 $dbh->do('DELETE FROM issuingrules');
100 INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
102 }, {}, '*', '*', '*', 1);
104 my $expirationdate = DateTime->now->add(days => 10)->ymd;
106 borrowernumber => int($borrowernumber),
107 biblionumber => int($biblionumber),
108 itemnumber => int($itemnumber),
109 branchcode => $branchcode,
110 expirationdate => $expirationdate,
112 $t->post_ok("/api/v1/reserves" => json => $post_data)
114 ->json_has('/reserve_id');
116 $reserve_id = $t->tx->res->json->{reserve_id};
118 $t->get_ok("/api/v1/reserves?borrowernumber=$borrowernumber")
120 ->json_is('/0/reserve_id', $reserve_id)
121 ->json_is('/0/expirationdate', $expirationdate)
122 ->json_is('/0/branchcode', $branchcode);
124 $t->post_ok("/api/v1/reserves" => json => $post_data)
126 ->json_like('/error', qr/tooManyReserves/);
134 my $record = new MARC::Record;
135 $record->append_fields(
136 new MARC::Field('200', ' ', ' ', a => $title),
139 my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
141 return $biblionumber;
145 my ($biblionumber, $barcode) = @_;
151 my $itemnumber = C4::Items::AddItem($item, $biblionumber);