From 391f31b224a058d5f3395cdaa1fb67b0fe7f447b 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 --- 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 241589e7b5..74b55114ec 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -350,6 +350,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 156ae778bb..e2cc96ede7 100755 --- a/t/Context.t +++ b/t/Context.t @@ -18,7 +18,7 @@ use Modern::Perl; use DBI; -use Test::More tests => 31; +use Test::More tests => 32; use Test::MockModule; use Test::Warn; use YAML::XS; @@ -52,6 +52,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.30.2