From 2de072f3aff0cc1c63da80a9081d28544fb0c0f8 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Thu, 11 Sep 2014 14:23:41 +0200
Subject: [PATCH] Bug 12872: Unit tests for ZebraExclude preference
Content-Type: text/plain; charset=utf-8

Test plan:
Run ZebraExclude.t

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
---
 t/db_dependent/ZebraExclude.t |  160 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 160 insertions(+), 0 deletions(-)
 create mode 100644 t/db_dependent/ZebraExclude.t

diff --git a/t/db_dependent/ZebraExclude.t b/t/db_dependent/ZebraExclude.t
new file mode 100644
index 0000000..776c754
--- /dev/null
+++ b/t/db_dependent/ZebraExclude.t
@@ -0,0 +1,160 @@
+#!/usr/bin/perl
+
+# Copyright 2014 Rijksmuseum
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use C4::Context;
+use C4::Biblio;
+use misc::migration_tools::ZebraExclude;
+
+use MARC::Record;
+use MARC::File::XML;
+use Test::More tests => 2;
+
+my $dbh = C4::Context->dbh;
+$dbh->{AutoCommit} = 0;
+$dbh->{RaiseError} = 1;
+
+subtest '01parse' => sub {
+    plan tests => 17;
+    test_parsing_pref();
+};
+subtest '02exclude' => sub {
+    plan tests => 12;
+    test_exclude();
+};
+
+$dbh->rollback;
+
+sub test_parsing_pref {
+    my ($excl, @a, @b, $hash, $reg);
+
+    $dbh->do("DELETE FROM systempreferences where variable='ZebraExclude'");
+    is( defined load_zebra_exclude(), '', 'Test without pref');
+
+    C4::Context->set_preference('ZebraExclude', '590,952e');
+    $excl = load_zebra_exclude();
+    @a= @{$excl->{list}};
+    is( @a, 1, 'Expect one parsed line');
+    ($hash, $reg) = @{$a[0]};
+    is( defined $reg, '', 'No regex in first line' );
+    @b= @{$hash->{field}};
+    is( @b, 1, 'Expect one whole field' );
+    is( $b[0], '590', 'Should be 590' );
+    @b= @{$hash->{subfield}};
+    is( @b, 1, 'Expect one subfield' );
+    is( $b[0][0] eq '952' && $b[0][1] eq 'e', 1, 'Should be 952e' );
+
+    C4::Context->set_preference('ZebraExclude', "\n/\// : 590, 8XX, nothing\n");
+    $excl = load_zebra_exclude();
+    @a= @{$excl->{list}};
+    is( @a, 1, 'Expect one parsed line');
+    ($hash, $reg) = @{$a[0]};
+    is( $reg, '/', 'Check regex /' );
+    @b= @{$hash->{field}};
+    is( @b, 1, 'Expect one whole field' );
+    @b= @{$hash->{wildcard}};
+    is( @b, 1, 'Expect one wildcard' );
+
+    C4::Context->set_preference('ZebraExclude', '/Garbage[/: 9XX');
+    $excl = load_zebra_exclude();
+    @a= @{$excl->{list}};
+    ($hash, $reg) = @{$a[0]};
+    is( $reg, undef, 'Wrong regex with only [' );
+
+    C4::Context->set_preference('ZebraExclude',
+        "/_skip_/: XXX\n".
+        "//: 590, 920, 952e\n"
+    );
+    $excl = load_zebra_exclude();
+    @a= @{$excl->{list}};
+    is( @a, 2, 'Expect two parsed lines');
+    ($hash, $reg) = @{$a[0]};
+    is( $reg, '_skip_', 'Check regex _skip_' );
+    ($hash, $reg) = @{$a[1]};
+    @b= @{$hash->{wildcard}};
+    is( @b, 0, 'Expect no wildcard for line 2' );
+    @b= @{$hash->{field}};
+    is( @b, 2, 'Expect two whole fields' );
+    @b= @{$hash->{subfield}};
+    is( @b, 1, 'Expect one subfield' );
+}
+
+sub test_exclude {
+    my ( $marc, $rv, $excl, $c1, $c2, @f );
+
+    C4::Context->set_preference('ZebraExclude', '590,952e');
+    $excl = load_zebra_exclude();
+    $marc= mybiblio();
+    $c1 = scalar $marc->fields;
+    $rv = exclude_fields( $marc, $excl );
+    $c2 = scalar $marc->fields;
+    is( $rv, 1, 'Checked return value' );
+    is( $c1-$c2, 2, 'Expected two fields less' );
+    is( $marc->field('590'), undef, 'No field 590' );
+    @f = $marc->field('952');
+    is( @f, 1, 'One field 952' );
+    is( $marc->subfield('952', 'o'), '123 A 456', 'Checked 952$o' );
+
+    C4::Context->set_preference('ZebraExclude', '/Garbage/:XXX');
+    $excl = load_zebra_exclude();
+    $marc= mybiblio();
+    $rv = exclude_fields( $marc, $excl );
+    is( $rv, undef, 'Checked return value with Garbage' );
+    C4::Context->set_preference('ZebraExclude', '/G@rb@ge/:XXX');
+    $excl = load_zebra_exclude();
+    $marc= mybiblio();
+    $c1 = scalar $marc->fields;
+    $rv = exclude_fields( $marc, $excl );
+    $c2 = scalar $marc->fields;
+    is( $rv, 1, 'Checked return value with G@rb@ge' );
+    is( $c1, $c2, 'Checked number of fields for G@rb@ge' );
+
+    C4::Context->set_preference('ZebraExclude',
+        "/local note/: 590a\n".
+        "/Author2/: 1XX\n".
+        "/Author1/: 245, 3XX, 9XX\n"
+    );
+    $excl = load_zebra_exclude();
+    $marc= mybiblio();
+    $c1 = scalar $marc->fields;
+    $rv = exclude_fields( $marc, $excl );
+    $c2 = scalar $marc->fields;
+    is( $c1-$c2, 5, 'Expected 5 fields less' );
+    is( $marc->field('245'), undef, 'No field 245' );
+    is( $marc->field('952'), undef, 'No fields 952' );
+    is( defined $marc->field('999'), 1, 'Field 999 should still be there' );
+}
+
+sub mybiblio {
+    my $marc= MARC::Record->new;
+    my @fld= (
+        MARC::Field->new('001', 12345),
+        MARC::Field->new('100', '', '', a => 'Author1' ),
+        MARC::Field->new('245', '', '', a => 'Just a title' ),
+        MARC::Field->new('500', '', '', a => 'Just a note' ),
+        MARC::Field->new('590', '', '', a => 'Just a local note' ),
+        MARC::Field->new('920', '', '', a => 'Garbage' ),
+        MARC::Field->new('952', '', '', e => 'acqsource', o => '123 A 456' ),
+        MARC::Field->new('952', '', '', e => 'only sub' ),
+        MARC::Field->new('999', '', '', c => 12345, d => 12345 ),
+    );
+    $marc->append_fields(@fld);
+    return $marc;
+}
-- 
1.7.7.6