From 5b53e7aee51c6b133ce31f1473f0a29a3334c832 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Fri, 14 Oct 2022 06:31:23 +0000
Subject: [PATCH] Bug 31784: Add Libraries->singleBranchMode

Test plan:
Run t/db_dependent/Koha/Libraries.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: David Nind <david@davidnind.com>
---
 Koha/Libraries.pm               | 18 ++++++++++++++++++
 t/db_dependent/Koha/Libraries.t | 16 +++++++++++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/Koha/Libraries.pm b/Koha/Libraries.pm
index 20ceb8aa34..b4fd772164 100644
--- a/Koha/Libraries.pm
+++ b/Koha/Libraries.pm
@@ -64,6 +64,24 @@ sub search_filtered {
     return $self->SUPER::search( $params, $attributes );
 }
 
+=head3 singleLibraryMode
+
+    Aka singleBranchMode.
+
+    my $boolean = Koha::Libraries->singleLibaryMode;
+
+    Returns 1 if there is only one library marked public.
+
+=cut
+
+sub singleLibraryMode { # formerly called singleBranchMode
+    my $self = shift;
+    if( $self->search({ public => 1 })->count == 1 ) { # Historically we test number==1 instead of number<2
+        return 1;
+    }
+    return 0;
+}
+
 =head3 type
 
 =cut
diff --git a/t/db_dependent/Koha/Libraries.t b/t/db_dependent/Koha/Libraries.t
index 86c68fd5c7..a730de4900 100755
--- a/t/db_dependent/Koha/Libraries.t
+++ b/t/db_dependent/Koha/Libraries.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 12;
+use Test::More tests => 13;
 
 use C4::Biblio;
 use C4::Context;
@@ -365,3 +365,17 @@ subtest 'outgoing_transfers' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'singleLibrarymode aka singleBranchMode' => sub {
+    plan tests => 3;
+    $schema->storage->txn_begin;
+
+    Koha::Libraries->new->update({ public => 0 });
+    is( Koha::Libraries->singleLibraryMode, 0, 'No public libraries' );
+    my $library1 = $builder->build_object({ class => 'Koha::Libraries', value => { public => 1 } });
+    is( Koha::Libraries->singleLibraryMode, 1, 'One public library' );
+    my $library2 = $builder->build_object({ class => 'Koha::Libraries', value => { public => 1 } });
+    is( Koha::Libraries->singleLibraryMode, 0, 'Two public libraries' );
+
+    $schema->storage->txn_rollback;
+};
-- 
2.30.2