From 538e55afef38fa3ce1d917d926c8b5d56126b0e8 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 15 Sep 2021 15:56:59 -0300 Subject: [PATCH] Bug 29033: Add C4::Context->multivalue_preference I've seen several places in which a syspref is retrieved and then splitted using split and the fact they are pipe-separated strings. It seems it would be simple (and handy) to add a method to do that. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/Context.t => SUCCESS: Tests pass, a pipe-separated syspref is correctly retrieved as an arrayref. 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Nind Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- C4/Context.pm | 16 ++++++++++++++++ t/Context.t | 16 +++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/C4/Context.pm b/C4/Context.pm index 6406f340ec..091e4783a4 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -353,6 +353,22 @@ sub yaml_preference { return $yaml; } +=head2 multivalue_preference + +Retrieves the required system preference value, and splits it +into pieces using the I (|) symbol as separator. + +=cut + +sub multivalue_preference { + my ( $self, $preference ) = @_; + + my $syspref = $self->preference($preference) // q{}; + my $values = [ split qr{\|}, $syspref ]; + + return $values; +} + =head2 enable_syspref_cache C4::Context->enable_syspref_cache(); diff --git a/t/Context.t b/t/Context.t index e8115df8f0..9903719e8d 100755 --- a/t/Context.t +++ b/t/Context.t @@ -18,7 +18,7 @@ use Modern::Perl; use DBI; -use Test::More tests => 34; +use Test::More tests => 35; use Test::MockModule; use Test::Warn; use YAML::XS; @@ -58,6 +58,20 @@ subtest 'yaml_preference() tests' => sub { $context->unmock( 'preference' ); }; +subtest 'multivalue_preference() tests' => sub { + + plan tests => 3; + + t::lib::Mocks::mock_preference( 'MultiValuedSyspref', '' ); + is_deeply( C4::Context->multivalue_preference('MultiValuedSyspref'), [] ); + + t::lib::Mocks::mock_preference( 'MultiValuedSyspref', 'some' ); + is_deeply( C4::Context->multivalue_preference('MultiValuedSyspref'), ['some'] ); + + t::lib::Mocks::mock_preference( 'MultiValuedSyspref', 'some|more|values' ); + is_deeply( C4::Context->multivalue_preference('MultiValuedSyspref'), [ 'some', 'more', 'values' ] ); +}; + subtest 'needs_install() tests' => sub { plan tests => 2; -- 2.39.5