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

(-)a/t/Koha/REST/Plugin/Query.t (-5 / +67 lines)
Lines 318-329 get '/dbic_extended_attributes_join' => sub { Link Here
318
    ];
318
    ];
319
    my $attributes = { 'prefetch' => ['extended_attributes'] };
319
    my $attributes = { 'prefetch' => ['extended_attributes'] };
320
320
321
    $c->dbic_extended_attributes_join(
321
    my $result_set = Koha::Patrons->new;
322
323
    $c->render( json => { 'attributes' => $attributes, 'filtered_params' => $filtered_params }, status => 200 );
324
};
325
326
get '/dbic_extended_attributes_join_multiple_values' => sub {
327
    my ( $c, $args ) = @_;
328
329
    my $filtered_params = [
322
        {
330
        {
323
            'filtered_params' => $filtered_params,
331
            '-and' => [
324
            'attributes'      => $attributes
332
                [
333
                    {
334
                        'extended_attributes.attribute' => { 'like' => 'abc%' },
335
                        'extended_attributes.code'      => 'CODE_1'
336
                    }
337
                ],
338
                [
339
                    {
340
                        'extended_attributes.code'      => 'CODE_2',
341
                        'extended_attributes.attribute' => { 'like' => '123%' }
342
                    }
343
                ]
344
            ]
325
        }
345
        }
326
    );
346
    ];
347
    my $attributes = { 'prefetch' => ['extended_attributes'] };
348
349
    my $result_set = Koha::Patrons->new;
327
350
328
    $c->render( json => { 'attributes' => $attributes, 'filtered_params' => $filtered_params }, status => 200 );
351
    $c->render( json => { 'attributes' => $attributes, 'filtered_params' => $filtered_params }, status => 200 );
329
};
352
};
Lines 337-343 sub to_model { Link Here
337
360
338
# The tests
361
# The tests
339
362
340
use Test::More tests => 8;
363
use Test::More tests => 9;
341
use Test::Mojo;
364
use Test::Mojo;
342
365
343
subtest 'extract_reserved_params() tests' => sub {
366
subtest 'extract_reserved_params() tests' => sub {
Lines 634-636 subtest 'dbic_extended_attributes_join() tests' => sub { Link Here
634
        ]
657
        ]
635
    );
658
    );
636
};
659
};
660
661
subtest 'dbic_extended_attributes_join() tests' => sub {
662
663
    plan tests => 4;
664
665
    my $t = Test::Mojo->new;
666
667
    $t->get_ok( '/dbic_extended_attributes_join_multiple_values' => { 'x-koha-embed' => 'extended_attributes' } )
668
        ->json_has(
669
        '/attributes' => {
670
            'join' => [
671
                'extended_attributes_CODE_1',
672
                'extended_attributes_CODE_2'
673
            ],
674
            'prefetch' => ['extended_attributes']
675
        }
676
        );
677
678
    $t->get_ok( '/dbic_extended_attributes_join' => { 'x-koha-embed' => 'extended_attributes' } )->json_has(
679
        '/filtered_params' => [
680
            {
681
                '-and' => [
682
                    [
683
                        {
684
                            'extended_attributes_CODE_1.code'      => 'CODE_1',
685
                            'extended_attributes_CODE_1.attribute' => { 'like' => 'abc%' }
686
                        }
687
                    ],
688
                    [
689
                        {
690
                            'extended_attributes_CODE_2.code'      => 'CODE_2',
691
                            'extended_attributes_CODE_2.attribute' => { 'like' => 'abc%' }
692
                        }
693
                    ],
694
                ]
695
            }
696
        ]
697
    );
698
};
(-)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