Bug 5404: Move the test to a new IsMarcStructureInternal sub
[koha.git] / t / db_dependent / Biblio.t
index 0413b90..e23dc34 100755 (executable)
 
 use Modern::Perl;
 
-use Test::More tests => 4;
+use Test::More tests => 6;
 use Test::MockModule;
 
+use List::MoreUtils qw( uniq );
 use MARC::Record;
 use t::lib::Mocks qw( mock_preference );
 
@@ -37,11 +38,19 @@ my $context = new Test::MockModule('C4::Context');
 
 mock_marcfromkohafield();
 
-my $currency = new Test::MockModule('C4::Budgets');
-$currency->mock( 'GetCurrency', sub {
-    return { symbol   => '$',   isocode  => 'USD',
-             currency => 'USD', active => 1 };
-});
+my $currency = new Test::MockModule('Koha::Acquisition::Currencies');
+$currency->mock(
+    'get_active',
+    sub {
+        return Koha::Acquisition::Currency->new(
+            {   symbol   => '$',
+                isocode  => 'USD',
+                currency => 'USD',
+                active   => 1,
+            }
+        );
+    }
+);
 
 sub run_tests {
 
@@ -290,5 +299,48 @@ subtest 'NORMARC' => sub {
     $dbh->rollback;
 };
 
+subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
+    plan tests => 23;
+
+    my @columns = qw(
+        tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
+        authorised_value authtypecode value_builder isurl hidden frameworkcode
+        seealso link defaultvalue maxlength
+    );
+
+    # biblio.biblionumber must be mapped so this should return something
+    my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
+
+    ok(defined $marc_subfield_structure, "There is a result");
+    is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
+    foreach my $col (@columns) {
+        ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
+    }
+    is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
+    like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
+
+    # foo.bar does not exist so this should return undef
+    $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar', '');
+    is($marc_subfield_structure, undef, "invalid kohafield returns undef");
+};
+
+subtest 'IsMarcStructureInternal' => sub {
+    plan tests => 6;
+    my $tagslib = GetMarcStructure();
+    my @internals;
+    for my $tag ( sort keys %$tagslib ) {
+        next unless $tag;
+        for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
+            push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
+        }
+    }
+    @internals = uniq @internals;
+    is( scalar(@internals), 4, '');
+    is( grep( /^lib$/, @internals ), 1, '' );
+    is( grep( /^tab$/, @internals ), 1, '' );
+    is( grep( /^mandatory$/, @internals ), 1, '' );
+    is( grep( /^repeatable$/, @internals ), 1, '' );
+    is( grep( /^a$/, @internals ), 0, '' );
+};
 
 1;