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 AutoLinkBiblios, 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>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
This commit is contained in:
Marcel de Rooy 2023-05-11 12:52:23 +00:00 committed by Jonathan Druart
parent 45d99e7a0b
commit 50d3c3c0a8

View file

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