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

(-)a/t/Koha/REST/Plugin/Query.t (-5 / +66 lines)
Lines 331-342 get '/dbic_extended_attributes_join' => sub { Link Here
331
    ];
331
    ];
332
    my $attributes = { 'prefetch' => ['extended_attributes'] };
332
    my $attributes = { 'prefetch' => ['extended_attributes'] };
333
333
334
    $c->dbic_extended_attributes_join(
334
    my $result_set = Koha::Patrons->new;
335
336
    $c->render( json => { 'attributes' => $attributes, 'filtered_params' => $filtered_params }, status => 200 );
337
};
338
339
get '/dbic_extended_attributes_join_multiple_values' => sub {
340
    my ( $c, $args ) = @_;
341
342
    my $filtered_params = [
335
        {
343
        {
336
            'filtered_params' => $filtered_params,
344
            '-and' => [
337
            'attributes'      => $attributes
345
                [
346
                    {
347
                        'extended_attributes.attribute' => { 'like' => 'abc%' },
348
                        'extended_attributes.code'      => 'CODE_1'
349
                    }
350
                ],
351
                [
352
                    {
353
                        'extended_attributes.code'      => 'CODE_2',
354
                        'extended_attributes.attribute' => { 'like' => '123%' }
355
                    }
356
                ]
357
            ]
338
        }
358
        }
339
    );
359
    ];
360
    my $attributes = { 'prefetch' => ['extended_attributes'] };
361
362
    my $result_set = Koha::Patrons->new;
340
363
341
    $c->render( json => { 'attributes' => $attributes, 'filtered_params' => $filtered_params }, status => 200 );
364
    $c->render( json => { 'attributes' => $attributes, 'filtered_params' => $filtered_params }, status => 200 );
342
};
365
};
Lines 350-356 sub to_model { Link Here
350
373
351
# The tests
374
# The tests
352
375
353
use Test::More tests => 9;
376
use Test::More tests => 10;
354
use Test::Mojo;
377
use Test::Mojo;
355
378
356
subtest 'extract_reserved_params() tests' => sub {
379
subtest 'extract_reserved_params() tests' => sub {
Lines 684-688 subtest 'dbic_validate_operators' => sub { Link Here
684
    # Invalid queries
707
    # Invalid queries
685
    $q = [ { "-and" => [ [ { "biblio_id" => { "like(sleep(1/100000))or" => "%a%" } } ] ] } ];
708
    $q = [ { "-and" => [ [ { "biblio_id" => { "like(sleep(1/100000))or" => "%a%" } } ] ] } ];
686
    $t->get_ok( '/dbic_validate_operators' => json => { q => $q } )->status_is(400);
709
    $t->get_ok( '/dbic_validate_operators' => json => { q => $q } )->status_is(400);
710
};
687
711
712
subtest 'dbic_extended_attributes_join() tests' => sub {
713
714
    plan tests => 4;
715
716
    my $t = Test::Mojo->new;
717
718
    $t->get_ok( '/dbic_extended_attributes_join_multiple_values' => { 'x-koha-embed' => 'extended_attributes' } )
719
        ->json_has(
720
        '/attributes' => {
721
            'join' => [
722
                'extended_attributes_CODE_1',
723
                'extended_attributes_CODE_2'
724
            ],
725
            'prefetch' => ['extended_attributes']
726
        }
727
        );
728
729
    $t->get_ok( '/dbic_extended_attributes_join' => { 'x-koha-embed' => 'extended_attributes' } )->json_has(
730
        '/filtered_params' => [
731
            {
732
                '-and' => [
733
                    [
734
                        {
735
                            'extended_attributes_CODE_1.code'      => 'CODE_1',
736
                            'extended_attributes_CODE_1.attribute' => { 'like' => 'abc%' }
737
                        }
738
                    ],
739
                    [
740
                        {
741
                            'extended_attributes_CODE_2.code'      => 'CODE_2',
742
                            'extended_attributes_CODE_2.attribute' => { 'like' => 'abc%' }
743
                        }
744
                    ],
745
                ]
746
            }
747
        ]
748
    );
688
};
749
};
(-)a/t/db_dependent/Koha/Objects/Mixin/ExtendedAttributes.t (-1 / +189 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2024 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 2;
23
use C4::Context;
24
25
use Koha::Database;
26
27
use t::lib::TestBuilder;
28
29
my $schema = Koha::Database->new->schema;
30
$schema->storage->txn_begin;
31
my $builder = t::lib::TestBuilder->new;
32
33
subtest 'extended_attributes patrons join searches() tests' => sub {
34
35
    plan tests => 3;
36
37
    $schema->storage->txn_begin;
38
39
    my $builder = t::lib::TestBuilder->new;
40
41
    my $patron1 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
42
    my $patron2 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
43
44
    my $attribute_type_1 = $builder->build(
45
        {
46
            source => 'BorrowerAttributeType',
47
            value => { repeatable => 1, is_date => 0, code => 'CODE_1' },
48
        }
49
    );
50
    my $attribute_type_2 = $builder->build(
51
        {
52
            source => 'BorrowerAttributeType',
53
            value => { repeatable => 1, is_date => 0, code => 'CODE_2' }
54
        }
55
    );
56
57
    my $attr1 = Koha::Patron::Attribute->new(
58
        {
59
            borrowernumber => $patron1,
60
            code           => $attribute_type_1->{code},
61
            attribute      => 'Bar'
62
        }
63
    )->store;
64
    my $attr2 = Koha::Patron::Attribute->new(
65
        {
66
            borrowernumber => $patron1,
67
            code           => $attribute_type_2->{code},
68
            attribute      => 'Foo'
69
        }
70
    )->store;
71
72
    my $patrons_search = Koha::Patrons->search(
73
        [
74
            '-and'=>[
75
                {
76
                    'extended_attributes.attribute' => { 'like' => '%Bar%' },
77
                    'extended_attributes.code'      => $attr1->code
78
                },
79
                {
80
                    'extended_attributes.attribute' => { 'like' => '%Foo%' },
81
                    'extended_attributes.code'      => $attr2->code
82
                }
83
            ],
84
        ],
85
        { 'prefetch' => ['extended_attributes'] }
86
    );
87
88
    is( $patrons_search->count, 1, "Patrons extended_attribute 'AND' query works." );
89
90
    my $patrons_search2 = Koha::Patrons->search(
91
        [
92
            '-and' => [
93
                {
94
                    'extended_attributes.attribute' => { 'like' => '%Bar%' },
95
                    'extended_attributes.code'      => $attr1->code
96
                },
97
                {
98
                    'extended_attributes.attribute' => { 'like' => '%Bar%' },
99
                    'extended_attributes.code'      => $attr2->code
100
                }
101
            ],
102
        ],
103
        { 'prefetch' => ['extended_attributes'] }
104
    );
105
106
    is( $patrons_search2->count, 0, "Second patrons extended_attribute 'AND' query works." );
107
108
109
    my $patrons_search3 = Koha::Patrons->search(
110
        [
111
            [
112
                {
113
                    'extended_attributes.attribute' => { 'like' => '%Bar%' },
114
                    'extended_attributes.code'      => $attr1->code
115
                }
116
            ],
117
            [
118
                {
119
                    'extended_attributes.attribute' => { 'like' => '%Foo%' },
120
                    'extended_attributes.code'      => $attr2->code
121
                }
122
            ],
123
            [
124
                {
125
                    'extended_attributes.attribute' => { 'like' => '%Foo%' },
126
                    'extended_attributes.code'      => $attr1->code
127
                }
128
            ],
129
        ],
130
        { 'prefetch' => ['extended_attributes'] }
131
    );
132
133
    is( $patrons_search3->count, 1, "Patrons extended_attribute 'OR' search works" );
134
135
    $schema->storage->txn_rollback;
136
};
137
138
subtest 'extended_attributes ill requests join searches() tests' => sub {
139
140
    plan tests => 1;
141
142
    $schema->storage->txn_begin;
143
144
    my $builder = t::lib::TestBuilder->new;
145
146
    # ILL::Requests
147
    my $illrequest1 = $builder->build( { source => 'Illrequest' } )->{illrequest_id};
148
    my $illrequest2 = $builder->build( { source => 'Illrequest' } )->{illrequest_id};
149
150
    my $illrequest_attribute1 = 'author';
151
152
    my $ill_attr1 = Koha::ILL::Request::Attribute->new(
153
        {
154
            illrequest_id => $illrequest1,
155
            type          => 'author',
156
            value         => 'Pedro'
157
        }
158
    )->store;
159
    my $ill_attr2 = Koha::ILL::Request::Attribute->new(
160
        {
161
            illrequest_id => $illrequest2,
162
            type          => 'author',
163
            value         => 'Pedro'
164
        }
165
    )->store;
166
167
    my $ill_requests = Koha::ILL::Requests->search(
168
        [
169
            [
170
                {
171
                    'extended_attributes.value' => { 'like' => '%Pedro%' },
172
                    'extended_attributes.type'  => $illrequest_attribute1
173
                }
174
            ],
175
            [
176
                {
177
                    'extended_attributes.value' => { 'like' => '%field2 value%' },
178
                    'extended_attributes.type'  => $illrequest_attribute1
179
                }
180
            ],
181
        ],
182
        { 'prefetch' => ['extended_attributes'] }
183
    );
184
185
    is( $ill_requests->count, 2, "ILL requests extended_attribute search works" );
186
187
    $schema->storage->txn_rollback;
188
189
};

Return to bug 37389