View | Details | Raw Unified | Return to bug 33974
Collapse All | Expand All

(-)a/Koha/Biblios.pm (-1 / +27 lines)
Lines 72-78 sub pickup_locations { Link Here
72
72
73
=head2 Internal methods
73
=head2 Internal methods
74
74
75
=head3 type
75
=head3 api_query_fixer
76
77
    $query_string = $biblios->api_query_fixer( $query_string, $context, $no_quotes );
78
79
Method that takes care of adjusting I<$query_string> as required. An optional I<$context> parameter
80
will be used to prefix the relevant query atoms if present. A I<$no_quotes> boolean parameter
81
can be passed to choose not to use quotes on matching. This is particularly useful in the context of I<order_by>.
82
83
=cut
84
85
sub api_query_fixer {
86
    my ( $self, $query, $context, $no_quotes ) = @_;
87
88
    my $quotes = $no_quotes ? '' : '"';
89
90
    if ($context) {
91
        $query =~
92
            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;
93
    } else {
94
        $query =~
95
            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;
96
    }
97
98
    return $query;
99
}
100
101
=head3 _type
76
102
77
=cut
103
=cut
78
104
(-)a/t/db_dependent/Koha/Biblios.t (-2 / +68 lines)
Lines 19-30 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 6;
22
use Test::More tests => 7;
23
23
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
26
26
27
use MARC::Field;
27
use MARC::Field;
28
use Mojo::JSON qw(encode_json);
28
29
29
use C4::Items;
30
use C4::Items;
30
use C4::Biblio qw( AddBiblio ModBiblio GetMarcFromKohaField );
31
use C4::Biblio qw( AddBiblio ModBiblio GetMarcFromKohaField );
Lines 322-324 subtest 'pickup_locations() tests' => sub { Link Here
322
323
323
    $schema->storage->txn_rollback;
324
    $schema->storage->txn_rollback;
324
};
325
};
325
- 
326
327
subtest 'api_query_fixer() tests' => sub {
328
329
    plan tests => 2;
330
331
    my $rs = Koha::Biblios->new;
332
333
    subtest 'JSON query tests' => sub {
334
335
        plan tests => 6;
336
337
        my $query = encode_json( { collection_issn => { "-like" => "\%asd" } } );
338
        is(
339
            $rs->api_query_fixer($query), '{"biblioitem.collection_issn":{"-like":"%asd"}}',
340
            'Query adapted for biblioitem attributes'
341
        );
342
        $query = encode_json( { author => { "-like" => "\%asd" } } );
343
        is( $rs->api_query_fixer($query), $query, 'Query unchanged for non-biblioitem attributes' );
344
        $query = encode_json( { author => { "-like" => "an age_restriction" } } );
345
        is( $rs->api_query_fixer($query), $query, 'Query unchanged because quotes are expected for the match' );
346
347
        $query = encode_json( { "biblio.collection_issn" => { "-like" => "\%asd" } } );
348
        is(
349
            $rs->api_query_fixer( $query, 'biblio' ), '{"biblio.biblioitem.collection_issn":{"-like":"%asd"}}',
350
            'Query adapted for biblioitem attributes, context is kept, match using context'
351
        );
352
        $query = encode_json( { collection_issn => { "-like" => "\%asd" } } );
353
        is( $rs->api_query_fixer( $query, 'biblio' ), $query, 'Query unchanged because no match for context' );
354
        $query = encode_json( { author => { "-like" => "a biblio.age_restriction" } } );
355
        is(
356
            $rs->api_query_fixer( $query, 'biblio' ), $query,
357
            'Query unchanged because quotes are expected for the match'
358
        );
359
    };
360
361
    subtest 'order_by tests' => sub {
362
363
        plan tests => 6;
364
365
        my $query = encode_json( { collection_issn => { "-like" => "\%asd" } } );
366
        is(
367
            $rs->api_query_fixer( $query, undef, 1 ), '{"biblioitem.collection_issn":{"-like":"%asd"}}',
368
            'Query adapted for biblioitem attributes'
369
        );
370
        $query = encode_json( { author => { "-like" => "\%asd" } } );
371
        is( $rs->api_query_fixer( $query, undef, 1 ), $query, 'Query unchanged for non-biblioitem attributes' );
372
        $query = encode_json( { author => { "-like" => "an age_restriction" } } );
373
        is(
374
            $rs->api_query_fixer( $query, undef, 1 ), '{"author":{"-like":"an biblioitem.age_restriction"}}',
375
            'Query changed because quotes are not expected for the match'
376
        );
377
378
        $query = encode_json( { "banana.collection_issn" => { "-like" => "\%asd" } } );
379
        is(
380
            $rs->api_query_fixer( $query, 'banana', 1 ), '{"banana.biblioitem.collection_issn":{"-like":"%asd"}}',
381
            'Query adapted for biblioitem attributes'
382
        );
383
        $query = encode_json( { author => { "-like" => "\%asd" } } );
384
        is( $rs->api_query_fixer( $query, 'banana', 1 ), $query, 'Query unchanged for non-biblioitem attributes' );
385
        $query = encode_json( { author => { "-like" => "a banana.age_restriction" } } );
386
        is(
387
            $rs->api_query_fixer( $query, 'banana', 1 ), '{"author":{"-like":"a banana.biblioitem.age_restriction"}}',
388
            'Query changed because quotes are not expected for the match'
389
        );
390
    }
391
};

Return to bug 33974