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> Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> (cherry picked from commit60e8998e91) Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> (cherry picked from commit949ba13a6e) Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
This commit is contained in:
parent
f906e1bb65
commit
bb9139ee0d
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue