|
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 |
}; |