From 3a909e78af2efe9b5302d4c6b5f4e1cfd7aeda11 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 7 Feb 2024 14:36:01 +0100 Subject: [PATCH] Bug 35963: Fix bundled items table ordering and filtering Using the many-to-many relationship results in the table alias `me` to be used for table `item_bundles` instead of the expected table `items`. This causes ordering and filtering to fail for columns Callnumber and Barcode. Using Koha::Items->search does not have this problem. This patch also disables ordering by status because it does not work (error message is: "Cannot find Koha::Object class for return_claim'") Test plan: 1. Create an item bundle https://koha-community.org/manual/23.11/en/html/circulation.html#circulating-bundles 2. Add at least 2 items to this bundle 3. Verify that ordering/filtering by callnumber or barcode works 4. Run `prove t/db_dependent/api/v1/items/bundled_items.t` --- Koha/REST/V1/Items.pm | 11 ++++- .../prog/en/modules/catalogue/detail.tt | 2 +- t/db_dependent/api/v1/items/bundled_items.t | 48 +++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 t/db_dependent/api/v1/items/bundled_items.t diff --git a/Koha/REST/V1/Items.pm b/Koha/REST/V1/Items.pm index 5db12b669d1..69333c1bdf6 100644 --- a/Koha/REST/V1/Items.pm +++ b/Koha/REST/V1/Items.pm @@ -294,8 +294,15 @@ sub bundled_items { } return try { - my $items_set = $item->bundle_items; - my $items = $c->objects->search( $items_set ); + my $items_set = Koha::Items->search( + { + 'item_bundles_item.host' => $item_id, + }, + { + join => 'item_bundles_item', + } + ); + my $items = $c->objects->search($items_set); return $c->render( status => 200, openapi => $items diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt index 9764f45735d..48f96e6c87f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt @@ -1907,7 +1907,7 @@ "data": "lost_status:last_seen_date:return_claim.patron", "title": _("Status"), "searchable": false, - "orderable": true, + "orderable": false, "render": function(data, type, row, meta) { if ( row.lost_status == bundle_lost_value ) { let out = '' + _("Last seen") + ': ' + $date(row.last_seen_date) + ''; diff --git a/t/db_dependent/api/v1/items/bundled_items.t b/t/db_dependent/api/v1/items/bundled_items.t new file mode 100644 index 00000000000..cab4b23e9ab --- /dev/null +++ b/t/db_dependent/api/v1/items/bundled_items.t @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +use Modern::Perl; + +use Test::More tests => 1; +use Test::MockModule; +use Test::Mojo; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::Database; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + +my $t = Test::Mojo->new('Koha::REST::V1'); + +subtest 'order by me.barcode should return 200' => sub { + plan tests => 2; + + $schema->storage->txn_begin; + + my $bundle = $builder->build_sample_item; + my $item = $builder->build_sample_item; + $bundle->add_to_bundle($item); + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 4 } + } + ); + + my $password = 'thePassword123'; + + $patron->set_password( { password => $password, skip_validation => 1 } ); + + my $userid = $patron->userid; + my $itemnumber = $bundle->itemnumber; + + $t->get_ok( "//$userid:$password@/api/v1/items/$itemnumber/bundled_items?_order_by=+me.barcode" ) + ->status_is(200); + + $schema->storage->txn_rollback; +}; -- 2.30.2