From b42479aba974621e79024524cda830a121cfd534 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 6 Jan 2020 13:00:26 -0300 Subject: [PATCH] Bug 18731: Use the stashed koha.embed in objects.search This patch makes the objects.search helper use the koha.embed structure that is embedded in the authenticate_api_request step. This way, any controller using it will benefit from automatic embed handling. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/REST/Plugin/Objects.t => SUCCESS: Tests pass! 3. Sign off :-D --- Koha/REST/Plugin/Objects.pm | 4 ++- t/db_dependent/Koha/REST/Plugin/Objects.t | 36 ++++++++++++++++++----- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index e8f0616f94..4ce9c5134d 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -51,6 +51,8 @@ sub register { # Extract reserved params my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args); + # Look for embeds + my $embed = $c->stash('koha.embed'); # Merge sorting into query attributes $c->dbic_merge_sorting( @@ -87,7 +89,7 @@ sub register { }); } - return $objects->to_api; + return $objects->to_api({ embed => $embed }); } ); } diff --git a/t/db_dependent/Koha/REST/Plugin/Objects.t b/t/db_dependent/Koha/REST/Plugin/Objects.t index c1a76de564..2e162b21bc 100644 --- a/t/db_dependent/Koha/REST/Plugin/Objects.t +++ b/t/db_dependent/Koha/REST/Plugin/Objects.t @@ -16,6 +16,8 @@ # along with Koha; if not, see . use Modern::Perl; + +use Koha::Acquisition::Orders; use Koha::Cities; # Dummy app for testing the plugin @@ -34,24 +36,30 @@ get '/cities' => sub { $c->render( status => 200, json => $cities ); }; +get '/orders' => sub { + my $c = shift; + $c->stash('koha.embed', ( { fund => {} } ) ); + $c->validation->output($c->req->params->to_hash); + my $orders = $c->objects->search(Koha::Acquisition::Orders->new); + $c->render( status => 200, json => $orders ); +}; + # The tests -use Test::More tests => 2; +use Test::More tests => 3; use Test::Mojo; use t::lib::TestBuilder; use Koha::Database; -my $schema = Koha::Database->new()->schema(); - +my $t = Test::Mojo->new; +my $schema = Koha::Database->new()->schema(); my $builder = t::lib::TestBuilder->new; subtest 'objects.search helper' => sub { plan tests => 34; - my $t = Test::Mojo->new; - $schema->storage->txn_begin; # Remove existing cities to have more control on the search restuls @@ -128,8 +136,6 @@ subtest 'objects.search helper, sorting on mapped column' => sub { plan tests => 14; - my $t = Test::Mojo->new; - $schema->storage->txn_begin; # Have complete control over the existing cities to ease testing @@ -155,4 +161,18 @@ subtest 'objects.search helper, sorting on mapped column' => sub { ->json_is('/1/name' => 'A'); $schema->storage->txn_rollback; -} +}; + +subtest 'objects.search helper, embed' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $order = $builder->build_object({ class => 'Koha::Acquisition::Orders' }); + + $t->get_ok('/orders?order_id=' . $order->ordernumber) + ->json_is('/0',$order->to_api({ embed => ( { fund => {} } ) })); + + $schema->storage->txn_rollback; +}; -- 2.24.1