From 0bd7922513ace59589d6a97454cb91d9045fdd5e Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi 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 --- 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. + +=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