From 39b4226683d52ad8248ba3e5e1916c81ff9653fd Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Thu, 11 May 2023 12:52:23 +0000
Subject: [PATCH] Bug 33718: Fix the mock of Koha::Config->get in mock_config

It should not mock calls for other sections than 'config' in
koha-conf.xml.

Test plan:
Without this patch:
[1] Enable CatalogModuleRelink and LinkerRelink. This will trigger
a SearchAuthorities call when creating a sample biblio.
Note: SearchAuthorities calls Zconn and gets back information from
a wrong part of koha-conf.xml.
[2] Run t/db_dependent/Koha/Pseudonymization.t
You should see something like:
  {UNKNOWN}: Can't use string ("authorities") as a HASH ref while "strict refs" in use at /usr/share/koha/C4/Context.pm line 587. at /usr/share/koha/C4/Biblio.pm line 302

With this patch:
[3] Run t/db_dependent/Koha/Pseudonymization.t. Should pass now.
[4] git grep -l mock_config | xargs -i{} prove {}
    Exclude Mocks.pm.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: David Nind <david@davidnind.com>
---
 t/lib/Mocks.pm | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/t/lib/Mocks.pm b/t/lib/Mocks.pm
index 68d8829ca6..bca34f4e0b 100644
--- a/t/lib/Mocks.pm
+++ b/t/lib/Mocks.pm
@@ -39,20 +39,24 @@ my %configs;
 
 Mock the configuration I<$config_entry> with the specified I<$value>.
 
+NOTE: We are only mocking config entries here, so no entries from other
+sections of koha-conf.xml. Bug 33718 fixed the section parameter of
+mocked Koha::Config->get calls for other sections (not cached).
+
 =cut
 
 sub mock_config {
+    my ( $config_entry, $value ) = @_;
     my $koha_config = Test::MockModule->new('Koha::Config');
-    my ( $conf, $value ) = @_;
-    $configs{$conf} = $value;
+    $configs{$config_entry} = $value;
     $koha_config->mock('get', sub {
-        my ( $self, $conf ) = @_;
-        if ( exists $configs{$conf} ) {
-            return $configs{$conf}
-        } else {
-            my $method = $koha_config->original('get');
-            return $method->($self, $conf);
+        my ( $self, $key, $section ) = @_;
+        $section ||= 'config';
+        if( $section eq 'config' && exists $configs{$key} ) {
+            return $configs{$key};
         }
+        my $method = $koha_config->original('get');
+        return $method->( $self, $key, $section );
     });
 }
 
-- 
2.30.2