Bugzilla – Attachment 152765 Details for
Bug 33974
Add ability to search biblios endpoint using any biblioitem attribute
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 33974: Add Koha::Biblios->api_query_fixer
Bug-33974-Add-KohaBiblios-apiqueryfixer.patch (text/plain), 6.08 KB, created by
Nick Clemens (kidclamp)
on 2023-06-27 16:04:19 UTC
(
hide
)
Description:
Bug 33974: Add Koha::Biblios->api_query_fixer
Filename:
MIME Type:
Creator:
Nick Clemens (kidclamp)
Created:
2023-06-27 16:04:19 UTC
Size:
6.08 KB
patch
obsolete
>From d26f0a9ee2d29a076e68e56401f391340ba2dfb5 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Fri, 16 Jun 2023 12:48:14 -0300 >Subject: [PATCH] Bug 33974: Add Koha::Biblios->api_query_fixer > >This patch adds the `api_query_fixer` method to the class, and adds >tests to validate its behavior. > >To test: >1. Apply this patch >2. Run: > $ ktd --shell > k$ prove t/db_dependent/Koha/Biblios.t >=> SUCCESS: Tests pass! >3. Sign off :-D > >Signed-off-by: Sam Lau <samalau@gmail.com> >Signed-off-by: Nick Clemens <nick@bywatersolutions.com> >--- > Koha/Biblios.pm | 28 +++++++++++++- > t/db_dependent/Koha/Biblios.t | 69 ++++++++++++++++++++++++++++++++++- > 2 files changed, 95 insertions(+), 2 deletions(-) > >diff --git a/Koha/Biblios.pm b/Koha/Biblios.pm >index 2e105214d0..e43b63fbff 100644 >--- a/Koha/Biblios.pm >+++ b/Koha/Biblios.pm >@@ -72,7 +72,33 @@ sub pickup_locations { > > =head2 Internal methods > >-=head3 type >+=head3 api_query_fixer >+ >+ $query_string = $biblios->api_query_fixer( $query_string, $context, $no_quotes ); >+ >+Method that takes care of adjusting I<$query_string> as required. An optional I<$context> parameter >+will be used to prefix the relevant query atoms if present. A I<$no_quotes> boolean parameter >+can be passed to choose not to use quotes on matching. This is particularly useful in the context of I<order_by>. >+ >+=cut >+ >+sub api_query_fixer { >+ my ( $self, $query, $context, $no_quotes ) = @_; >+ >+ my $quotes = $no_quotes ? '' : '"'; >+ >+ if ($context) { >+ $query =~ >+ s/${quotes}${context}\.(age_restriction|cn_class|cn_item|cn_sort|cn_source|cn_suffix|collection_issn|collection_title|collection_volume|ean|edition_statement|illustrations|isbn|issn|item_type|lc_control_number|notes|number|pages|publication_place|publication_year|publisher|material_size|serial_total_issues|url|volume|volume_date|volume_description)${quotes}/${quotes}${context}\.biblioitem\.$1${quotes}/g; >+ } else { >+ $query =~ >+ s/${quotes}(age_restriction|cn_class|cn_item|cn_sort|cn_source|cn_suffix|collection_issn|collection_title|collection_volume|ean|edition_statement|illustrations|isbn|issn|item_type|lc_control_number|notes|number|pages|publication_place|publication_year|publisher|material_size|serial_total_issues|url|volume|volume_date|volume_description)${quotes}/${quotes}biblioitem\.$1${quotes}/g; >+ } >+ >+ return $query; >+} >+ >+=head3 _type > > =cut > >diff --git a/t/db_dependent/Koha/Biblios.t b/t/db_dependent/Koha/Biblios.t >index 185c0e4117..fde249ffe5 100755 >--- a/t/db_dependent/Koha/Biblios.t >+++ b/t/db_dependent/Koha/Biblios.t >@@ -19,12 +19,13 @@ > > use Modern::Perl; > >-use Test::More tests => 6; >+use Test::More tests => 7; > > use Test::Exception; > use Test::MockModule; > > use MARC::Field; >+use Mojo::JSON qw(encode_json); > > use C4::Items; > use C4::Biblio qw( AddBiblio ModBiblio GetMarcFromKohaField ); >@@ -322,3 +323,69 @@ subtest 'pickup_locations() tests' => sub { > > $schema->storage->txn_rollback; > }; >+ >+subtest 'api_query_fixer() tests' => sub { >+ >+ plan tests => 2; >+ >+ my $rs = Koha::Biblios->new; >+ >+ subtest 'JSON query tests' => sub { >+ >+ plan tests => 6; >+ >+ my $query = encode_json( { collection_issn => { "-like" => "\%asd" } } ); >+ is( >+ $rs->api_query_fixer($query), '{"biblioitem.collection_issn":{"-like":"%asd"}}', >+ 'Query adapted for biblioitem attributes' >+ ); >+ $query = encode_json( { author => { "-like" => "\%asd" } } ); >+ is( $rs->api_query_fixer($query), $query, 'Query unchanged for non-biblioitem attributes' ); >+ $query = encode_json( { author => { "-like" => "an age_restriction" } } ); >+ is( $rs->api_query_fixer($query), $query, 'Query unchanged because quotes are expected for the match' ); >+ >+ $query = encode_json( { "biblio.collection_issn" => { "-like" => "\%asd" } } ); >+ is( >+ $rs->api_query_fixer( $query, 'biblio' ), '{"biblio.biblioitem.collection_issn":{"-like":"%asd"}}', >+ 'Query adapted for biblioitem attributes, context is kept, match using context' >+ ); >+ $query = encode_json( { collection_issn => { "-like" => "\%asd" } } ); >+ is( $rs->api_query_fixer( $query, 'biblio' ), $query, 'Query unchanged because no match for context' ); >+ $query = encode_json( { author => { "-like" => "a biblio.age_restriction" } } ); >+ is( >+ $rs->api_query_fixer( $query, 'biblio' ), $query, >+ 'Query unchanged because quotes are expected for the match' >+ ); >+ }; >+ >+ subtest 'order_by tests' => sub { >+ >+ plan tests => 6; >+ >+ my $query = encode_json( { collection_issn => { "-like" => "\%asd" } } ); >+ is( >+ $rs->api_query_fixer( $query, undef, 1 ), '{"biblioitem.collection_issn":{"-like":"%asd"}}', >+ 'Query adapted for biblioitem attributes' >+ ); >+ $query = encode_json( { author => { "-like" => "\%asd" } } ); >+ is( $rs->api_query_fixer( $query, undef, 1 ), $query, 'Query unchanged for non-biblioitem attributes' ); >+ $query = encode_json( { author => { "-like" => "an age_restriction" } } ); >+ is( >+ $rs->api_query_fixer( $query, undef, 1 ), '{"author":{"-like":"an biblioitem.age_restriction"}}', >+ 'Query changed because quotes are not expected for the match' >+ ); >+ >+ $query = encode_json( { "banana.collection_issn" => { "-like" => "\%asd" } } ); >+ is( >+ $rs->api_query_fixer( $query, 'banana', 1 ), '{"banana.biblioitem.collection_issn":{"-like":"%asd"}}', >+ 'Query adapted for biblioitem attributes' >+ ); >+ $query = encode_json( { author => { "-like" => "\%asd" } } ); >+ is( $rs->api_query_fixer( $query, 'banana', 1 ), $query, 'Query unchanged for non-biblioitem attributes' ); >+ $query = encode_json( { author => { "-like" => "a banana.age_restriction" } } ); >+ is( >+ $rs->api_query_fixer( $query, 'banana', 1 ), '{"author":{"-like":"a banana.biblioitem.age_restriction"}}', >+ 'Query changed because quotes are not expected for the match' >+ ); >+ } >+}; >-- >2.30.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 33974
:
152433
|
152434
|
152435
|
152487
|
152488
|
152489
|
152765
|
152766
|
152767
|
152983
|
152984
|
152985
|
152986
|
152988
|
153104