From 149ec9014c75871eefb6808052bcee19896ff5ea Mon Sep 17 00:00:00 2001
From: Johanna Raisa <johanna.raisa@gmail.com>
Date: Tue, 9 Aug 2022 11:58:59 +0300
Subject: [PATCH] Bug 31326: Koha::Biblio->get_components_query fetches too
 many component parts

This patch adds double quotes to rcn and cni when searching component parts
with get_components_query.

Test plan:
1) Apply the patch
2) prove Koha/t/db_dependent/Koha/Biblio.t

Sponsored-by: Koha-Suomi Oy
---
 Koha/Biblio.pm               | 4 ++--
 t/db_dependent/Koha/Biblio.t | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm
index 5a1be3cfe2..4ca26b12bd 100644
--- a/Koha/Biblio.pm
+++ b/Koha/Biblio.pm
@@ -595,12 +595,12 @@ sub get_components_query {
 
             if ( !defined($pf003) ) {
                 # search for 773$w='Host001'
-                $searchstr .= "rcn:" . $pf001->data();
+                $searchstr .= "rcn:\"" . $pf001->data()."\"";
             }
             else {
                 $searchstr .= "(";
                 # search for (773$w='Host001' and 003='Host003') or 773$w='(Host003)Host001'
-                $searchstr .= "(rcn:" . $pf001->data() . " AND cni:" . $pf003->data() . ")";
+                $searchstr .= "(rcn:\"" . $pf001->data() . "\" AND cni:\"" . $pf003->data() . "\")";
                 $searchstr .= " OR rcn:\"" . $pf003->data() . " " . $pf001->data() . "\"";
                 $searchstr .= ")";
             }
diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t
index df9dffe0bd..6d3da04e89 100755
--- a/t/db_dependent/Koha/Biblio.t
+++ b/t/db_dependent/Koha/Biblio.t
@@ -583,7 +583,7 @@ subtest 'get_components_query' => sub {
     $biblio = Koha::Biblios->find( $biblio->biblionumber);
 
     ( $comp_q, $comp_s ) = $biblio->get_components_query;
-    is($comp_q, "(rcn:$biblionumber AND (bib-level:a OR bib-level:b))", "UseControlNumber enabled without MarcOrgCode");
+    is($comp_q, "(rcn:\"$biblionumber\" AND (bib-level:a OR bib-level:b))", "UseControlNumber enabled without MarcOrgCode");
     is($comp_s, "author_az", "UseControlNumber enabled without MarcOrgCode sort is correct");
 
     my $marc_003_field = MARC::Field->new('003', 'OSt');
@@ -594,7 +594,7 @@ subtest 'get_components_query' => sub {
     t::lib::Mocks::mock_preference( 'ComponentSortField', 'title' );
     t::lib::Mocks::mock_preference( 'ComponentSortOrder', 'asc' );
     ( $comp_q, $comp_s ) = $biblio->get_components_query;
-    is($comp_q, "(((rcn:$biblionumber AND cni:OSt) OR rcn:\"OSt $biblionumber\") AND (bib-level:a OR bib-level:b))", "UseControlNumber enabled with MarcOrgCode");
+    is($comp_q, "(((rcn:\"$biblionumber\" AND cni:\"OSt\") OR rcn:\"OSt $biblionumber\") AND (bib-level:a OR bib-level:b))", "UseControlNumber enabled with MarcOrgCode");
     is($comp_s, "title_asc", "UseControlNumber enabled with MarcOrgCode sort if correct");
 };
 
-- 
2.25.1