From 5e5b2618c5fb55d9e45f5ac006ad6407f0aea381 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Mon, 8 Feb 2016 17:17:31 +0000
Subject: [PATCH] Bug 15758: Koha::Libraries - Do not select an option if
 selected is defined

This will certainly need a few more tweaks.
---
 Koha/Template/Plugin/Branches.pm          | 4 ++--
 t/db_dependent/Template/Plugin/Branches.t | 4 +++-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/Koha/Template/Plugin/Branches.pm b/Koha/Template/Plugin/Branches.pm
index b41d3e4..3d17edc 100644
--- a/Koha/Template/Plugin/Branches.pm
+++ b/Koha/Template/Plugin/Branches.pm
@@ -65,8 +65,8 @@ sub all {
       : Koha::Libraries->search_filtered( {}, { order_by => ['branchname'] } )->unblessed;
 
     for my $l ( @$libraries ) {
-        if (       $selected and $l->{branchcode} eq $selected
-            or not $selected and C4::Context->userenv and $l->{branchcode} eq C4::Context->userenv->{branch}
+        if (       defined $selected and $l->{branchcode} eq $selected
+            or not defined $selected and C4::Context->userenv and $l->{branchcode} eq C4::Context->userenv->{branch}
         ) {
             $l->{selected} = 1;
         }
diff --git a/t/db_dependent/Template/Plugin/Branches.t b/t/db_dependent/Template/Plugin/Branches.t
index 8fcc15f..24b6e6c 100644
--- a/t/db_dependent/Template/Plugin/Branches.t
+++ b/t/db_dependent/Template/Plugin/Branches.t
@@ -16,7 +16,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 14;
+use Test::More tests => 15;
 
 use C4::Context;
 use Koha::Database;
@@ -71,6 +71,8 @@ is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and not exists $_->{selected} } @$
 $libraries = $plugin->all( { selected => 'ANOTHERLIB' } );
 is( grep ( { $_->{branchcode} eq 'MYLIBRARY'  and not exists $_->{selected} } @$libraries ), 1, 'With selected parameter, my library should not be preselected' );
 is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and $_->{selected} == 1 } @$libraries ),       1, 'With selected parameter, other library should be preselected' );
+$libraries = $plugin->all( { selected => '' } );
+is( grep ( { exists $_->{selected} } @$libraries ), 0, 'With selected parameter set to an empty string, no library should be preselected' );
 
 C4::Context->set_preference( 'IndependentBranches', 1 );
 $libraries = $plugin->all();
-- 
2.8.1