From fffd006ab1176bb969926734a30575f913575394 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 7 Nov 2013 12:33:37 +0100 Subject: [PATCH] Bug 10855: A partial search should return the subscriptions If a search on an additional fields is done using a partial string ("foo" and the defined value is "foobar"), the subscription should appear in the result list. Test plan: Try to search a part of the string for an additional field. --- C4/Serials.pm | 1 + Koha/AdditionalField.pm | 5 +++-- t/db_dependent/AdditionalField.t | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/C4/Serials.pm b/C4/Serials.pm index 4de2b65..1ef8413 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -657,6 +657,7 @@ sub SearchSubscriptions { $matching_record_ids_for_additional_fields = Koha::AdditionalField->get_matching_record_ids({ fields => $args->{additional_fields}, tablename => 'subscription', + exact_match => 0, }); return () unless @$matching_record_ids_for_additional_fields; } diff --git a/Koha/AdditionalField.pm b/Koha/AdditionalField.pm index 4b38a45..fe45b18 100644 --- a/Koha/AdditionalField.pm +++ b/Koha/AdditionalField.pm @@ -204,6 +204,7 @@ sub get_matching_record_ids { my $fields = $args->{fields} // []; my $tablename = $args->{tablename}; + my $exact_match = $args->{exact_match} // 1; return [] unless @$fields; my $dbh = C4::Context->dbh; @@ -222,13 +223,13 @@ sub get_matching_record_ids { WHERE afv.field_id = af.id AND af.name = ? AND af.tablename = ? - AND value = ? + AND value LIKE ? ) AS field$i USING (id) WHERE field$i.id IS NOT NULL ) AS values$i |; $subquery .= ' USING (record_id)' if $i > 1; push @subqueries, $subquery; - push @args, $field->{name}, $tablename, $field->{value}; + push @args, $field->{name}, $tablename, ( $exact_match ? $field->{value} : "%$field->{value}%" ); } $query .= join( ' LEFT JOIN ', @subqueries ) . ' WHERE 1'; for my $j ( 1 .. $i ) { diff --git a/t/db_dependent/AdditionalField.t b/t/db_dependent/AdditionalField.t index 861c15a..df307a4 100644 --- a/t/db_dependent/AdditionalField.t +++ b/t/db_dependent/AdditionalField.t @@ -1,7 +1,7 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 34; +use Test::More tests => 37; use C4::Context; use Koha::AdditionalField; @@ -276,4 +276,19 @@ is ( $exists, 1, "get_matching_record_ids: field common: common_value matches su $exists = grep /not_existent_id/, @$matching_record_ids; is ( $exists, 0, "get_matching_record_ids: field common: common_value does not inexistent id" ); +$fields = [ + { + name => 'common', + value => q|common|, + } +]; +$matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields, exact_match => 0 }); +$exists = grep /$subscriptionid1/, @$matching_record_ids; +is ( $exists, 1, "get_matching_record_ids: field common: common% matches subscription1" ); +$exists = grep /$subscriptionid2/, @$matching_record_ids; +is ( $exists, 1, "get_matching_record_ids: field common: common% matches subscription2 too" ); +$exists = grep /not_existent_id/, @$matching_record_ids; +is ( $exists, 0, "get_matching_record_ids: field common: common% does not inexistent id" ); + + $dbh->rollback; -- 1.7.10.4