From e66dba8ff30ce994d4a1a3f5a236ee5b522f995e Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 2 Nov 2020 19:51:38 -0300 Subject: [PATCH] Bug 20212: Controller changes regression tests This patchset did a copy and paste of the objects->search helper inside the controller, to handle a very special case that wasn't worth generalizing: the fact that the biblio object (that this patchset makes embeddable) is actually built from two tables, and we want to filter by fields in both tables. This requires us to translate: - Any filter reference to fields in biblioitems we allow to search on, through an allow list. - For filtering to work, we need to tweak the stashed 'embed' object so the prefetch is done correctly This patchset also adds a new query parameter: only_active. It is used to only request active orders (see the spec for more details). This patch adds tests for: - Filtering by a column in bilbioitems (this implies correctly prefetching the bilbio => biblioitems relation, and translating the filtering parameter. - Using the only_active query parameter to retrieve only active orders To test: 1. Apply this patchset 2. Run: $ kshell k$ prove t/db_dependent/api/v1/acquisitions_orders.t => SUCCESS: Tests pass! 3. Verify the tests check for the explained behaviours. => SUCCESS: They do! 4. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Martin Renvoize --- t/db_dependent/api/v1/acquisitions_orders.t | 35 +++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/api/v1/acquisitions_orders.t b/t/db_dependent/api/v1/acquisitions_orders.t index 6335515a80..6700f0edb0 100755 --- a/t/db_dependent/api/v1/acquisitions_orders.t +++ b/t/db_dependent/api/v1/acquisitions_orders.t @@ -24,6 +24,8 @@ use Test::Warn; use t::lib::TestBuilder; use t::lib::Mocks; +use JSON qw( encode_json ); + use Koha::Acquisition::Orders; use Koha::Database; @@ -35,7 +37,7 @@ t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); my $t = Test::Mojo->new('Koha::REST::V1'); subtest 'list() tests' => sub { - plan tests => 8; + plan tests => 18; $schema->storage->txn_begin; @@ -48,11 +50,20 @@ subtest 'list() tests' => sub { my $userid = $patron->userid; my $basket = $builder->build_object({ class => 'Koha::Acquisition::Baskets' }); + + my $biblio = $builder->build_sample_biblio; + my $biblioitem = $biblio->biblioitem; + $biblioitem->set({ isbn => 'SOMETHING' })->store; + # Create test context my $order = $builder->build_object( { class => 'Koha::Acquisition::Orders', - value => { basketno => $basket->basketno, orderstatus => 'new' } + value => { + basketno => $basket->basketno, + orderstatus => 'new', + biblionumber => $biblio->biblionumber + } } ); my $another_order = $order->unblessed; # create a copy of $order but make @@ -98,6 +109,26 @@ subtest 'list() tests' => sub { } }; + my $query = { "biblio.isbn" => { "-like" => "\%SOMETHING\%" } }; + + $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?q=" . encode_json($query) => {'x-koha-embed' => 'biblio'} ) + ->status_is( 200, "Embeddig biblio.isbn doesn't make it explode" ) + ->json_has( "/0/biblio", "biblio object correctly embedded" ) + ->json_is( "/0/biblio/isbn", 'SOMETHING', 'Filtering by a biblioitems column works!' ); + + my $result = $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?biblio_id=" . $biblio->biblionumber ) + ->status_is( 200 ); + + is( scalar @{ $result->tx->res->json}, 2, 'Two orders fetched' ); + + # Mark $another_order as cancelled + $another_order->set({ orderstatus => 'cancelled' })->store; + + $result = $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?only_active=1&biblio_id=" . $biblio->biblionumber ) + ->status_is( 200, "only_active parameter accepted" ); + + is( scalar @{ $result->tx->res->json}, 1, 'Only one order is active' ); + # Warn on unsupported query parameter $t->get_ok( "//$userid:$password@/api/v1/acquisitions/orders?order_blah=blah" ) ->status_is(400) -- 2.20.1