From 93a2ec8e6f7c63a90be13def6c1643bfe1cc04f6 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Fri, 22 Jul 2016 15:57:26 +0100
Subject: [PATCH] Bug 16966: GetBorrowersWithIssuesHistoryOlderThan - Highlight
 the existing issue in tests

If IndependentBranches is set, the code is buggy. This patch only
highlight the bug by providing a test.

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 t/db_dependent/Circulation/AnonymiseIssueHistory.t | 42 +++++++++++++++++++---
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/t/db_dependent/Circulation/AnonymiseIssueHistory.t b/t/db_dependent/Circulation/AnonymiseIssueHistory.t
index a171897..e013abc 100644
--- a/t/db_dependent/Circulation/AnonymiseIssueHistory.t
+++ b/t/db_dependent/Circulation/AnonymiseIssueHistory.t
@@ -18,7 +18,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 4;
+use Test::More tests => 5;
 
 use C4::Context;
 use C4::Circulation;
@@ -56,9 +56,10 @@ t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber}
 
 subtest 'patron privacy is 1 (default)' => sub {
     plan tests => 4;
+    t::lib::Mocks::mock_preference('IndependentBranches', 0);
     my $patron = $builder->build(
         {   source => 'Borrower',
-            value  => { privacy => 1, }
+            value  => { privacy => 1, branchcode => $userenv_patron->{branchcode} }
         }
     );
     my $item = $builder->build(
@@ -94,10 +95,11 @@ subtest 'patron privacy is 1 (default)' => sub {
 
 subtest 'patron privacy is 0 (forever)' => sub {
     plan tests => 3;
+    t::lib::Mocks::mock_preference('IndependentBranches', 0);
 
     my $patron = $builder->build(
         {   source => 'Borrower',
-            value  => { privacy => 0, }
+            value  => { privacy => 0, branchcode => $userenv_patron->{branchcode} }
         }
     );
     my $item = $builder->build(
@@ -133,9 +135,10 @@ t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
 
 subtest 'AnonymousPatron is not defined' => sub {
     plan tests => 4;
+    t::lib::Mocks::mock_preference('IndependentBranches', 0);
     my $patron = $builder->build(
         {   source => 'Borrower',
-            value  => { privacy => 1, }
+            value  => { privacy => 1, branchcode => $userenv_patron->{branchcode} }
         }
     );
     my $item = $builder->build(
@@ -168,6 +171,37 @@ subtest 'AnonymousPatron is not defined' => sub {
     is( $borrowernumber_used_to_anonymised, undef, 'With AnonymousPatron is not defined, the issue should have been anonymised anyway' );
 };
 
+subtest 'Logged in librarian is not superlibrarian & IndependentBranches' => sub {
+    plan tests => 1;
+    t::lib::Mocks::mock_preference('IndependentBranches', 1);
+    my $patron = $builder->build(
+        {   source => 'Borrower',
+            value  => { privacy => 1 } # Another branchcode than the logged in librarian
+        }
+    );
+    my $item = $builder->build(
+        {   source => 'Item',
+            value  => {
+                itemlost  => 0,
+                withdrawn => 0,
+            },
+        }
+    );
+    my $issue = $builder->build(
+        {   source => 'Issue',
+            value  => {
+                borrowernumber => $patron->{borrowernumber},
+                itemnumber     => $item->{itemnumber},
+            },
+        }
+    );
+
+    my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
+    my $patrons_to_anonymise = C4::Members::GetBorrowersWithIssuesHistoryOlderThan( '2010-10-11' );
+    my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
+    is( scalar(@$patrons_to_anonymise), $rows_affected, , 'AnonymiseIssueHistory should affect at least 1 row' );
+};
+
 subtest 'Test StoreLastBorrower' => sub {
     plan tests => 6;
 
-- 
2.9.3