From 0d6c175bf544446a921c04e87e22edf4b856c7f1 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 17 Jan 2018 14:33:33 -0300 Subject: [PATCH] Bug 19686: (followup) Add to_api param for completeness This patch adds (yet) another param to objects.search: a reference to a to_api function to be applied when processing the search results for output. To test: - Apply this patch - Run: $ kshell k$ prove t/db_dependent/Koha/REST/Plugin/Objects.t => SUCCESS: Test count raised, and tests pass! - Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall --- Koha/REST/Plugin/Objects.pm | 21 +++++++---- t/db_dependent/Koha/REST/Plugin/Objects.t | 61 +++++++++++++++++++++++++++++-- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index 057f80e..3653f91 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -30,15 +30,16 @@ Koha::REST::Plugin::Objects =head3 objects.search my $patrons_set = Koha::Patrons->new; - my $patrons = $c->objects->search( $patrons_set, [\&to_model] ); + my $patrons = $c->objects->search( $patrons_set, [\&to_model, \&to_api] ); Performs a database search using given Koha::Objects object and query parameters. -Optionally, it applies the I<$to_model> function reference before building the -query itself. +It (optionally) applies the I<$to_model> function reference before building the +query itself, and (optionally) applies I<$to_api> to the result. -Note: Make sure I<$to_model> doesn't autovivify keys. +Returns an arrayref of the hashrefs representing the resulting objects +for JSON rendering. -Returns a Koha::Objects object +Note: Make sure I<$to_model> and I<$to_api> don't autovivify keys. =cut @@ -47,7 +48,7 @@ sub register { $app->helper( 'objects.search' => sub { - my ( $c, $objects_set, $to_model ) = @_; + my ( $c, $objects_set, $to_model, $to_api ) = @_; my $args = $c->validation->output; my $attributes = {}; @@ -90,7 +91,13 @@ sub register { }); } - return $objects; + my @objects_list = map { + ( defined $to_api ) + ? $to_api->( $_->TO_JSON ) + : $_->TO_JSON + } $objects->as_list; + + return \@objects_list; } ); } diff --git a/t/db_dependent/Koha/REST/Plugin/Objects.t b/t/db_dependent/Koha/REST/Plugin/Objects.t index 5595061..04d7aab 100644 --- a/t/db_dependent/Koha/REST/Plugin/Objects.t +++ b/t/db_dependent/Koha/REST/Plugin/Objects.t @@ -38,11 +38,19 @@ get '/patrons_to_model' => sub { my $c = shift; $c->validation->output($c->req->params->to_hash); my $patrons_set = Koha::Patrons->new; - my $patrons = $c->objects->search( $patrons_set, \&_to_model ); + my $patrons = $c->objects->search( $patrons_set, \&to_model ); $c->render( status => 200, json => $patrons ); }; -sub _to_model { +get '/patrons_to_model_to_api' => sub { + my $c = shift; + $c->validation->output($c->req->params->to_hash); + my $patrons_set = Koha::Patrons->new; + my $patrons = $c->objects->search( $patrons_set, \&to_model, \&to_api ); + $c->render( status => 200, json => $patrons ); +}; + +sub to_model { my $params = shift; if ( exists $params->{nombre} ) { @@ -52,6 +60,16 @@ sub _to_model { return $params; } +sub to_api { + my $params = shift; + + if ( exists $params->{firstname} ) { + $params->{nombre} = delete $params->{firstname}; + } + + return $params; +} + # The tests use Test::More tests => 1; use Test::Mojo; @@ -66,7 +84,7 @@ my $builder = t::lib::TestBuilder->new; subtest 'objects.search helper' => sub { - plan tests => 62; + plan tests => 90; my $t = Test::Mojo->new; @@ -175,5 +193,42 @@ subtest 'objects.search helper' => sub { ->json_is('/1/firstname' => 'Manuela') ->json_is('/2/firstname' => 'Emanuel'); + ## _to_model && _to_api tests + # _match=starts_with + $t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=starts_with') + ->status_is(200) + ->json_has('/0') + ->json_has('/1') + ->json_hasnt('/2') + ->json_is('/0/nombre' => 'Manuel') + ->json_is('/1/nombre' => 'Manuela'); + + # _match=ends_with + $t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=ends_with') + ->status_is(200) + ->json_has('/0') + ->json_has('/1') + ->json_hasnt('/2') + ->json_is('/0/nombre' => 'Manuel') + ->json_is('/1/nombre' => 'Emanuel'); + + # _match=exact + $t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=exact') + ->status_is(200) + ->json_has('/0') + ->json_hasnt('/1') + ->json_is('/0/nombre' => 'Manuel'); + + # _match=contains + $t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=contains') + ->status_is(200) + ->json_has('/0') + ->json_has('/1') + ->json_has('/2') + ->json_hasnt('/3') + ->json_is('/0/nombre' => 'Manuel') + ->json_is('/1/nombre' => 'Manuela') + ->json_is('/2/nombre' => 'Emanuel'); + $schema->storage->txn_rollback; }; -- 2.10.2