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

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Tomas Cohen Arazi 2020-01-06 13:00:26 -03:00 committed by Martin Renvoize
parent 5d8ae2e6c8
commit 021f5c95d7
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
2 changed files with 31 additions and 9 deletions

View File

@ -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 });
}
);
}

View File

@ -16,6 +16,8 @@
# along with Koha; if not, see <http://www.gnu.org/licenses>.
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;
};