From c76281f3fcb4f634a1e1a8f74b9663763c874dfd Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 6 Apr 2016 13:12:51 +0200 Subject: [PATCH] Bug 16213: Allow to select hold's itemtype when using API Test plan: 1/ Use your usual "REST testing" tool to place a title-level hold with an itemtype. The request should look like this: POST /api/v1/holds { "borrowernumber": 1234, "biblionumber": 456, "branchcode": "CPL", "itemtype": "A" } 2/ Check that the hold was placed and the itemtype is correctly selected 3/ prove t/db_dependent/api/v1/holds.t Signed-off-by: Benjamin Rokseth Signed-off-by: Josef Moravec Signed-off-by: Jonathan Druart --- Koha/REST/V1/Hold.pm | 4 ++- api/v1/swagger/paths/holds.json | 4 +++ t/db_dependent/api/v1/holds.t | 46 ++++++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Koha/REST/V1/Hold.pm b/Koha/REST/V1/Hold.pm index 3ab15cd194..2927a7ad0a 100644 --- a/Koha/REST/V1/Hold.pm +++ b/Koha/REST/V1/Hold.pm @@ -50,6 +50,8 @@ sub add { my $itemnumber = $body->{itemnumber}; my $branchcode = $body->{branchcode}; my $expirationdate = $body->{expirationdate}; + my $itemtype = $body->{itemtype}; + my $borrower = Koha::Patrons->find($borrowernumber); unless ($borrower) { return $c->render( status => 404, @@ -103,7 +105,7 @@ sub add { my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber, $biblionumber, undef, $priority, undef, $expirationdate, undef, - $biblio->title, $itemnumber); + $biblio->title, $itemnumber, undef, $itemtype); unless ($reserve_id) { return $c->render( status => 500, openapi => { diff --git a/api/v1/swagger/paths/holds.json b/api/v1/swagger/paths/holds.json index b2f542ac40..e89511dddc 100644 --- a/api/v1/swagger/paths/holds.json +++ b/api/v1/swagger/paths/holds.json @@ -190,6 +190,10 @@ "description": "Hold end date", "type": "string", "format": "date" + }, + "itemtype": { + "description": "Limit reserve on one itemtype (ignored for item-level holds)", + "type": "string" } } } diff --git a/t/db_dependent/api/v1/holds.t b/t/db_dependent/api/v1/holds.t index 8397f30a58..f74bf12c63 100644 --- a/t/db_dependent/api/v1/holds.t +++ b/t/db_dependent/api/v1/holds.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 4; +use Test::More tests => 5; use Test::Mojo; use t::lib::TestBuilder; use t::lib::Mocks; @@ -26,6 +26,7 @@ use DateTime; use C4::Context; use C4::Reserves; +use C4::Items; use Koha::Database; use Koha::DateUtils; @@ -49,6 +50,7 @@ my $tx; my $categorycode = $builder->build({ source => 'Category' })->{categorycode}; my $branchcode = $builder->build({ source => 'Branch' })->{branchcode}; +my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype}; # User without any permissions my $nopermission = $builder->build({ @@ -123,10 +125,14 @@ $session3->param('lasttime', time()); $session3->flush; my $biblionumber = create_biblio('RESTful Web APIs'); -my $itemnumber = create_item($biblionumber, 'TEST000001'); +my $item = create_item($biblionumber, 'TEST000001'); +my $itemnumber = $item->{itemnumber}; +$item->{itype} = $itemtype; +C4::Items::ModItem($item, $biblionumber, $itemnumber); my $biblionumber2 = create_biblio('RESTful Web APIs'); -my $itemnumber2 = create_item($biblionumber2, 'TEST000002'); +my $item2 = create_item($biblionumber2, 'TEST000002'); +my $itemnumber2 = $item2->{itemnumber}; my $dbh = C4::Context->dbh; $dbh->do('DELETE FROM reserves'); @@ -331,6 +337,38 @@ subtest "Test endpoints with permission" => sub { ->json_like('/error', qr/tooManyReserves/); }; + +subtest 'Reserves with itemtype' => sub { + plan tests => 9; + + my $post_data = { + borrowernumber => int($patron_1->borrowernumber), + biblionumber => int($biblionumber), + branchcode => $branchcode, + itemtype => $itemtype, + }; + + $tx = $t->ua->build_tx(DELETE => "/api/v1/holds/$reserve_id"); + $tx->req->cookies({name => 'CGISESSID', value => $session3->id}); + $t->request_ok($tx) + ->status_is(200); + + $tx = $t->ua->build_tx(POST => "/api/v1/holds" => json => $post_data); + $tx->req->cookies({name => 'CGISESSID', value => $session3->id}); + $t->request_ok($tx) + ->status_is(201) + ->json_has('/reserve_id'); + + $reserve_id = $t->tx->res->json->{reserve_id}; + + $tx = $t->ua->build_tx(GET => "/api/v1/holds?borrowernumber=" . $patron_1->borrowernumber); + $tx->req->cookies({name => 'CGISESSID', value => $session->id}); + $t->request_ok($tx) + ->status_is(200) + ->json_is('/0/reserve_id', $reserve_id) + ->json_is('/0/itemtype', $itemtype); +}; + $schema->storage->txn_rollback; sub create_biblio { @@ -357,5 +395,5 @@ sub create_item { } ); - return $item->{itemnumber}; + return $item; } -- 2.39.5