From b62a810cb5f70998ff5ec31574c90087e63dfddc Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Thu, 25 Jul 2024 15:27:42 +0000 Subject: [PATCH] Bug 37389: Add tests prove t/Koha/REST/Plugin/Query.t prove t/db_dependent/Koha/Objects/Mixin/ExtendedAttributes.t Co-authored-by: Martin Renvoize --- t/Koha/REST/Plugin/Query.t | 72 ++++++- .../Koha/Objects/Mixin/ExtendedAttributes.t | 189 ++++++++++++++++++ 2 files changed, 256 insertions(+), 5 deletions(-) create mode 100644 t/db_dependent/Koha/Objects/Mixin/ExtendedAttributes.t diff --git a/t/Koha/REST/Plugin/Query.t b/t/Koha/REST/Plugin/Query.t index 2f816f4f14..8a458723f9 100755 --- a/t/Koha/REST/Plugin/Query.t +++ b/t/Koha/REST/Plugin/Query.t @@ -318,12 +318,35 @@ get '/dbic_extended_attributes_join' => sub { ]; my $attributes = { 'prefetch' => ['extended_attributes'] }; - $c->dbic_extended_attributes_join( + my $result_set = Koha::Patrons->new; + + $c->render( json => { 'attributes' => $attributes, 'filtered_params' => $filtered_params }, status => 200 ); +}; + +get '/dbic_extended_attributes_join_multiple_values' => sub { + my ( $c, $args ) = @_; + + my $filtered_params = [ { - 'filtered_params' => $filtered_params, - 'attributes' => $attributes + '-and' => [ + [ + { + 'extended_attributes.attribute' => { 'like' => 'abc%' }, + 'extended_attributes.code' => 'CODE_1' + } + ], + [ + { + 'extended_attributes.code' => 'CODE_2', + 'extended_attributes.attribute' => { 'like' => '123%' } + } + ] + ] } - ); + ]; + my $attributes = { 'prefetch' => ['extended_attributes'] }; + + my $result_set = Koha::Patrons->new; $c->render( json => { 'attributes' => $attributes, 'filtered_params' => $filtered_params }, status => 200 ); }; @@ -337,7 +360,7 @@ sub to_model { # The tests -use Test::More tests => 8; +use Test::More tests => 9; use Test::Mojo; subtest 'extract_reserved_params() tests' => sub { @@ -634,3 +657,42 @@ subtest 'dbic_extended_attributes_join() tests' => sub { ] ); }; + +subtest 'dbic_extended_attributes_join() tests' => sub { + + plan tests => 4; + + my $t = Test::Mojo->new; + + $t->get_ok( '/dbic_extended_attributes_join_multiple_values' => { 'x-koha-embed' => 'extended_attributes' } ) + ->json_has( + '/attributes' => { + 'join' => [ + 'extended_attributes_CODE_1', + 'extended_attributes_CODE_2' + ], + 'prefetch' => ['extended_attributes'] + } + ); + + $t->get_ok( '/dbic_extended_attributes_join' => { 'x-koha-embed' => 'extended_attributes' } )->json_has( + '/filtered_params' => [ + { + '-and' => [ + [ + { + 'extended_attributes_CODE_1.code' => 'CODE_1', + 'extended_attributes_CODE_1.attribute' => { 'like' => 'abc%' } + } + ], + [ + { + 'extended_attributes_CODE_2.code' => 'CODE_2', + 'extended_attributes_CODE_2.attribute' => { 'like' => 'abc%' } + } + ], + ] + } + ] + ); +}; diff --git a/t/db_dependent/Koha/Objects/Mixin/ExtendedAttributes.t b/t/db_dependent/Koha/Objects/Mixin/ExtendedAttributes.t new file mode 100644 index 0000000000..6d84c4c0cf --- /dev/null +++ b/t/db_dependent/Koha/Objects/Mixin/ExtendedAttributes.t @@ -0,0 +1,189 @@ +#!/usr/bin/perl + +# Copyright 2024 Koha Development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 2; +use C4::Context; + +use Koha::Database; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; + +subtest 'extended_attributes patrons join searches() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $builder = t::lib::TestBuilder->new; + + my $patron1 = $builder->build( { source => 'Borrower' } )->{borrowernumber}; + my $patron2 = $builder->build( { source => 'Borrower' } )->{borrowernumber}; + + my $attribute_type_1 = $builder->build( + { + source => 'BorrowerAttributeType', + value => { repeatable => 1, is_date => 0, code => 'CODE_1' }, + } + ); + my $attribute_type_2 = $builder->build( + { + source => 'BorrowerAttributeType', + value => { repeatable => 1, is_date => 0, code => 'CODE_2' } + } + ); + + my $attr1 = Koha::Patron::Attribute->new( + { + borrowernumber => $patron1, + code => $attribute_type_1->{code}, + attribute => 'Bar' + } + )->store; + my $attr2 = Koha::Patron::Attribute->new( + { + borrowernumber => $patron1, + code => $attribute_type_2->{code}, + attribute => 'Foo' + } + )->store; + + my $patrons_search = Koha::Patrons->search( + [ + '-and'=>[ + { + 'extended_attributes.attribute' => { 'like' => '%Bar%' }, + 'extended_attributes.code' => $attr1->code + }, + { + 'extended_attributes.attribute' => { 'like' => '%Foo%' }, + 'extended_attributes.code' => $attr2->code + } + ], + ], + { 'prefetch' => ['extended_attributes'] } + ); + + is( $patrons_search->count, 1, "Patrons extended_attribute 'AND' query works." ); + + my $patrons_search2 = Koha::Patrons->search( + [ + '-and' => [ + { + 'extended_attributes.attribute' => { 'like' => '%Bar%' }, + 'extended_attributes.code' => $attr1->code + }, + { + 'extended_attributes.attribute' => { 'like' => '%Bar%' }, + 'extended_attributes.code' => $attr2->code + } + ], + ], + { 'prefetch' => ['extended_attributes'] } + ); + + is( $patrons_search2->count, 0, "Second patrons extended_attribute 'AND' query works." ); + + + my $patrons_search3 = Koha::Patrons->search( + [ + [ + { + 'extended_attributes.attribute' => { 'like' => '%Bar%' }, + 'extended_attributes.code' => $attr1->code + } + ], + [ + { + 'extended_attributes.attribute' => { 'like' => '%Foo%' }, + 'extended_attributes.code' => $attr2->code + } + ], + [ + { + 'extended_attributes.attribute' => { 'like' => '%Foo%' }, + 'extended_attributes.code' => $attr1->code + } + ], + ], + { 'prefetch' => ['extended_attributes'] } + ); + + is( $patrons_search3->count, 1, "Patrons extended_attribute 'OR' search works" ); + + $schema->storage->txn_rollback; +}; + +subtest 'extended_attributes ill requests join searches() tests' => sub { + + plan tests => 1; + + $schema->storage->txn_begin; + + my $builder = t::lib::TestBuilder->new; + + # ILL::Requests + my $illrequest1 = $builder->build( { source => 'Illrequest' } )->{illrequest_id}; + my $illrequest2 = $builder->build( { source => 'Illrequest' } )->{illrequest_id}; + + my $illrequest_attribute1 = 'author'; + + my $ill_attr1 = Koha::ILL::Request::Attribute->new( + { + illrequest_id => $illrequest1, + type => 'author', + value => 'Pedro' + } + )->store; + my $ill_attr2 = Koha::ILL::Request::Attribute->new( + { + illrequest_id => $illrequest2, + type => 'author', + value => 'Pedro' + } + )->store; + + my $ill_requests = Koha::ILL::Requests->search( + [ + [ + { + 'extended_attributes.value' => { 'like' => '%Pedro%' }, + 'extended_attributes.type' => $illrequest_attribute1 + } + ], + [ + { + 'extended_attributes.value' => { 'like' => '%field2 value%' }, + 'extended_attributes.type' => $illrequest_attribute1 + } + ], + ], + { 'prefetch' => ['extended_attributes'] } + ); + + is( $ill_requests->count, 2, "ILL requests extended_attribute search works" ); + + $schema->storage->txn_rollback; + +}; -- 2.39.2