From fcebb5e5ea6354b327ad807ec5fd0838fc2eda29 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Wed, 6 Jan 2016 10:41:21 +0000
Subject: [PATCH] Bug 4941: Make tests non DB dependent

Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 t/Koha.t              | 44 +++++++++++++++++++++++++++++++++++++++-----
 t/db_dependent/Koha.t | 21 +--------------------
 2 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/t/Koha.t b/t/Koha.t
index 3593368..7decc9e 100755
--- a/t/Koha.t
+++ b/t/Koha.t
@@ -25,7 +25,7 @@ use Module::Load::Conditional qw/check_install/;
 
 BEGIN {
     if ( check_install( module => 'Test::DBIx::Class' ) ) {
-        plan tests => 30;
+        plan tests => 31;
     } else {
         plan skip_all => "Need Test::DBIx::Class"
     }
@@ -38,15 +38,19 @@ use Test::DBIx::Class {
     connect_info => ['dbi:SQLite:dbname=:memory:','',''],
     connect_opts => { name_sep => '.', quote_char => '`', },
     fixture_class => '::Populate',
-}, 'AuthorisedValue' ;
+}, 'AuthorisedValue', 'Branch' ;
 
 sub fixtures {
-    my ( $data ) = @_;
+    my ( $av, $libraries ) = @_;
     fixtures_ok [
         AuthorisedValue => [
             [ 'category', 'authorised_value' ],
-            @$data,
+            @$av,
         ],
+        Branch => [
+            ['branchcode', 'branchname'],
+            @$libraries,
+        ]
     ], 'add fixtures';
 }
 
@@ -57,7 +61,10 @@ my $authorised_values = [
     ['LOC', 'LOC'],
     ['RELTERMS', 'RELTERMS'],
 ];
-fixtures($authorised_values);
+my $libraries = [
+    ['XXX_test', 'my branchname XXX'],
+];
+fixtures($authorised_values, $libraries);
 
 is ( IsAuthorisedValueCategory('LOC'), 1, 'LOC is a valid authorized value category');
 is ( IsAuthorisedValueCategory('something'), 0, 'something is not a valid authorized value category');
@@ -119,4 +126,31 @@ is( C4::Koha::GetNormalizedISBN('9781250075345 (hardcover) | 1250075343 (hardcov
 is( C4::Koha::GetNormalizedISBN('9781250067128 | 125006712X'), '125006712X', 'Test GetNormalizedISBN' );
 is( C4::Koha::GetNormalizedISBN('9780373211463 | 0373211465'), '0373211465', 'Test GetNormalizedISBN' );
 
+
+subtest 'getFacets() tests' => sub {
+    plan tests => 5;
+
+    is ( Koha::Libraries->search->count, 1, 'There should be only 1 library (singleBranchMode on)' );
+    my $facets = C4::Koha::getFacets();
+    is(
+        scalar( grep { defined $_->{idx} && $_->{idx} eq 'location' } @$facets ),
+        1,
+        'location facet present with singleBranchMode on (bug 10078)'
+    );
+
+    $libraries = [
+        ['YYY_test', 'my branchname YYY'],
+        ['ZZZ_test', 'my branchname XXX'],
+    ];
+    fixtures($authorised_values, $libraries);
+    is ( Koha::Libraries->search->count, 3, 'There should be only more than 1 library (singleBranchMode off)' );
+
+    $facets = C4::Koha::getFacets();
+    is(
+        scalar( grep { defined $_->{idx} && $_->{idx} eq 'location' } @$facets ),
+        1,
+        'location facet present with singleBranchMode off (bug 10078)'
+    );
+};
+
 1;
diff --git a/t/db_dependent/Koha.t b/t/db_dependent/Koha.t
index c013434..27a1361 100644
--- a/t/db_dependent/Koha.t
+++ b/t/db_dependent/Koha.t
@@ -8,7 +8,7 @@ use warnings;
 use C4::Context;
 use Koha::DateUtils qw(dt_from_string);
 
-use Test::More tests => 10;
+use Test::More tests => 9;
 use DateTime::Format::MySQL;
 
 BEGIN {
@@ -236,25 +236,6 @@ subtest 'Date and ISBN tests' => sub {
 
 };
 
-subtest 'getFacets() tests' => sub {
-    plan tests => 2;
-
-    C4::Context->set_preference('singleBranchMode', 0);
-    my $facets = C4::Koha::getFacets();
-    is(
-        scalar( grep { defined $_->{idx} && $_->{idx} eq 'location' } @$facets ),
-        1,
-        'location facet present with singleBranchMode off (bug 10078)'
-    );
-    C4::Context->set_preference('singleBranchMode', 1);
-    $facets = C4::Koha::getFacets();
-    is(
-        scalar( grep { defined $_->{idx} && $_->{idx} eq 'location' } @$facets ),
-        1,
-        'location facet present with singleBranchMode on (bug 10078)'
-    );
-};
-
 subtest 'GetFrameworksLoop() tests' => sub {
     plan tests => 6;
 
-- 
2.1.4