From 7d4dbf56742715f1edd9e6d72f5b902140b52eb8 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Tue, 6 Feb 2018 16:47:12 +0100
Subject: [PATCH] Bug 14769: Add tests for _thesaurus_info in
 ControlledIndicators.t

Apart from adding tests for this sub, we make the following change
too and support it with a test.

The fallback to $code in the construction
    $thes_mapping{ $code } // $code // '4'
is removed by this patch. When $code is not in $thes_mapping, we cannot
assume that it is a valid code for a biblio indicator. In this case it is
more safe to mark it as 4 (Source not specified).

Test plan:
Run t/Koha/Authority/ControlledIndicators.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>

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

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
---
 Koha/Authority/ControlledIndicators.pm  |  2 +-
 t/Koha/Authority/ControlledIndicators.t | 69 ++++++++++++++++++++++++++++++++-
 2 files changed, 69 insertions(+), 2 deletions(-)

diff --git a/Koha/Authority/ControlledIndicators.pm b/Koha/Authority/ControlledIndicators.pm
index 87ae796..64a8734 100644
--- a/Koha/Authority/ControlledIndicators.pm
+++ b/Koha/Authority/ControlledIndicators.pm
@@ -129,7 +129,7 @@ sub _thesaurus_info {
         ? substr($record->field('008')->data, 11, 1)
         : q{};
     my %thes_mapping = ( a => 0, b => 1, c => 2, d => 3, k => 5, n => 4, r => 7, s => 7, v => 6, z => 7, '|' => 4 );
-    my $ind = $thes_mapping{ $code } // $code // '4';
+    my $ind = $thes_mapping{ $code } // '4';
 
     # Determine optional subfield $2
     my $sub2;
diff --git a/t/Koha/Authority/ControlledIndicators.t b/t/Koha/Authority/ControlledIndicators.t
index 17ce49c..8552507 100644
--- a/t/Koha/Authority/ControlledIndicators.t
+++ b/t/Koha/Authority/ControlledIndicators.t
@@ -4,7 +4,7 @@ use Modern::Perl;
 use Data::Dumper qw/Dumper/;
 use MARC::Record;
 use MARC::Field;
-use Test::More tests => 1;
+use Test::More tests => 2;
 
 use t::lib::Mocks;
 use Koha::Authority::ControlledIndicators;
@@ -71,3 +71,70 @@ marc21,800,ind1:,
     });
     is_deeply( $res, {}, 'Cache cleared' );
 };
+
+subtest "Tests for sub _thesaurus_info" => sub {
+    plan tests => 10;
+
+    t::lib::Mocks::mock_preference('AuthorityControlledIndicators', q|marc21,600,ignored,ind2:thesaurus|);
+    my $oInd = Koha::Authority::ControlledIndicators->new;
+
+    my $record = MARC::Record->new;
+    $record->append_fields(
+        MARC::Field->new( '008', (' 'x11).'a' ),
+        MARC::Field->new( '040', '', '', f => 'very_special' ),
+    );
+
+    # Case 1: LOC code a in auth record should become 0 in 6XX ind2
+    my $res = $oInd->get({
+        flavour => "MARC21",
+        report_tag  => '100',
+        auth_record => $record,
+        biblio_tag  => '600',
+    });
+    is( $res->{ind2}, '0', 'Indicator matched for LOC headings' );
+    is( $res->{sub2}, undef, 'No subfield 2' );
+
+    # Case 2: Code n (Not applicable) should become 4 (source not specified)
+    $record->field('008')->update( (' 'x11).'n' );
+    $res = $oInd->get({
+        flavour => "MARC21",
+        report_tag  => '100',
+        auth_record => $record,
+        biblio_tag  => '600',
+    });
+    is( $res->{ind2}, '4', 'Source not specified' );
+    is( $res->{sub2}, undef, 'No subfield 2' );
+
+    # Case 3: AAT thesaurus (and subfield $2)
+    $record->field('008')->update( (' 'x11).'r' );
+    $res = $oInd->get({
+        flavour => "MARC21",
+        report_tag  => '100',
+        auth_record => $record,
+        biblio_tag  => '600',
+    });
+    is( $res->{ind2}, '7', 'AAT, see subfield 2' );
+    is( $res->{sub2}, 'aat', 'Subfield 2 verified' );
+
+    # Case 4: Code z triggers a fetch from 040$f (and subfield $2)
+    $record->field('008')->update( (' 'x11).'z' );
+    $res = $oInd->get({
+        flavour => "MARC21",
+        report_tag  => '100',
+        auth_record => $record,
+        biblio_tag  => '600',
+    });
+    is( $res->{ind2}, '7', 'Code z, see subfield 2' );
+    is( $res->{sub2}, 'very_special', 'Subfield 2 from 040$f' );
+
+    # Case 5: Code e does not belong in 008/11
+    $record->field('008')->update( (' 'x11).'e' );
+    $res = $oInd->get({
+        flavour => "MARC21",
+        report_tag  => '100',
+        auth_record => $record,
+        biblio_tag  => '600',
+    });
+    is( $res->{ind2}, '4', 'Code e triggers not specified' );
+    is( $res->{sub2}, undef, 'No subfield 2' );
+};
-- 
2.1.4