From 4b24f538bd2ea9b4660cda528e90d7576b7d3870 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 18 Feb 2022 16:07:02 -0300 Subject: [PATCH] Bug 30133: Fix pagination in /biblios/:bibio_id/pickup_locations This patch does the same thing we did for items on the previous patch, but for biblios. The tests are almost the same as well. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/api/v1/biblios.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Jonathan Druart Signed-off-by: Fridolin Somers Signed-off-by: Kyle M Hall --- Koha/REST/V1/Biblios.pm | 18 ++++++-------- t/db_dependent/api/v1/biblios.t | 44 ++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm index 839d117f56..007a2ed2c9 100644 --- a/Koha/REST/V1/Biblios.pm +++ b/Koha/REST/V1/Biblios.pm @@ -337,11 +337,9 @@ sub pickup_locations { return try { - my $ps_set = $biblio->pickup_locations( { patron => $patron } ); + my $pl_set = $biblio->pickup_locations( { patron => $patron } ); - my $pickup_locations = $c->objects->search( $ps_set ); my @response = (); - if ( C4::Context->preference('AllowHoldPolicyOverride') ) { my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } ); @@ -350,21 +348,19 @@ sub pickup_locations { @response = map { my $library = $_; $library->{needs_override} = ( - any { $_->{library_id} eq $library->{library_id} } - @{$pickup_locations} + any { $_->branchcode eq $library->{library_id} } + @{$pl_set->as_list} ) ? Mojo::JSON->false : Mojo::JSON->true; $library; } @{$libraries}; - - return $c->render( - status => 200, - openapi => \@response - ); } + else { - @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations}; + my $pickup_locations = $c->objects->search($pl_set); + @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations}; + } return $c->render( status => 200, diff --git a/t/db_dependent/api/v1/biblios.t b/t/db_dependent/api/v1/biblios.t index 32f7137555..5c836c39d8 100755 --- a/t/db_dependent/api/v1/biblios.t +++ b/t/db_dependent/api/v1/biblios.t @@ -383,7 +383,7 @@ subtest 'get_public() tests' => sub { subtest 'pickup_locations() tests' => sub { - plan tests => 15; + plan tests => 16; $schema->storage->txn_begin; @@ -476,6 +476,48 @@ subtest 'pickup_locations() tests' => sub { . "patron_id=" . $patron->id . "&_order_by=marc_org_code" ) ->json_is( [ $library_1_api, $library_2_api, $library_3_api, $library_5_api ] ); + subtest 'Pagination and AllowHoldPolicyOverride tests' => sub { + + plan tests => 27; + + t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 ); + + $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->id . "/pickup_locations?" . "patron_id=" . $patron->id . "&_order_by=marc_org_code" . "&_per_page=1" ) + ->json_is( [$library_1_api] ) + ->header_is( 'X-Total-Count', '4', '4 is the count for libraries with pickup_location=1' ) + ->header_is( 'X-Base-Total-Count', '4', '4 is the count for libraries with pickup_location=1' ) + ->header_unlike( 'Link', qr|rel="prev"| ) + ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="next"# ) + ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# ) + ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1).*>\; rel="last"# ); + + $t->get_ok( "//$userid:$password@/api/v1/biblios/" + . $biblio->id + . "/pickup_locations?" + . "patron_id=" + . $patron->id + . "&_order_by=marc_org_code" + . "&_per_page=1&_page=3" ) # force the needs_override=1 check + ->json_is( [$library_3_api] ) + ->header_is( 'X-Total-Count', '4', '4 is the count for libraries with pickup_location=1' ) + ->header_is( 'X-Base-Total-Count', '4', '4 is the count for libraries with pickup_location=1' ) + ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="prev"# ) + ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1.*)>\; rel="next"# ) + ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# ) + ->header_like( 'Link', qr#(_per_page=1.*\&_page=4.*|_page=4.*\&_per_page=1).*>\; rel="last"# ); + + t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 ); + + $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->id . "/pickup_locations?" . "patron_id=" . $patron->id . "&_order_by=marc_org_code" . "&_per_page=1" ) + ->json_is( [$library_1_api] ) + ->header_is( 'X-Total-Count', '2' ) + ->header_is( 'X-Base-Total-Count', '2' ) + ->header_unlike( 'Link', qr|rel="prev"| ) + ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1.*)>\; rel="next"# ) + ->header_like( 'Link', qr#(_per_page=1.*\&_page=1.*|_page=1.*\&_per_page=1).*>\; rel="first"# ) + ->header_like( 'Link', qr#(_per_page=1.*\&_page=2.*|_page=2.*\&_per_page=1).*>\; rel="last"# ); + }; + my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' }); my $deleted_patron_id = $deleted_patron->id; $deleted_patron->delete; -- 2.39.5