Bug 5404: Move the test to a new IsMarcStructureInternal sub
[koha.git] / t / db_dependent / Biblio.t
index 137946e..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,6 +38,20 @@ my $context = new Test::MockModule('C4::Context');
 
 mock_marcfromkohafield();
 
+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 {
 
     # Undef C4::Biblio::inverted_field_map to avoid problems introduced
@@ -137,6 +152,12 @@ sub run_tests {
     $issns = GetMarcISSN( $marc_record, $marcflavour );
     is( scalar @$issns, 4,
         'GetMARCISSN handles records with multiple ISSN fields (count correct)');
+    # Create an empty ISSN
+    $field = create_issn_field( "", $marcflavour );
+    $marc_record->append_fields($field);
+    $issns = GetMarcISSN( $marc_record, $marcflavour );
+    is( scalar @$issns, 4,
+        'GetMARCISSN skips empty ISSN fields (Bug 12674)');
 
     ## Testing GetMarcControlnumber
     my $controlnumber;
@@ -178,8 +199,23 @@ sub run_tests {
         is( $isbns->[$i], $more_isbns[$i],
             "(GetMarcISBN) Corretly retrieves ISBN #". ($i + 1));
     }
+
+
     is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
         "GetMarcPrice returns the correct value");
+    my $newincbiblioitemnumber=$biblioitemnumber+1;
+    $dbh->do("UPDATE biblioitems SET biblioitemnumber = ? WHERE biblionumber = ?;", undef, $newincbiblioitemnumber, $biblionumber );
+    my $updatedrecord = GetMarcBiblio($biblionumber, 0);
+    my $frameworkcode = GetFrameworkCode($biblionumber);
+    my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
+    die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
+    my $biblioitemnumbertotest;
+    if ( $biblioitem_tag < 10 ) {
+        $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->data();
+    } else {
+        $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
+    }
+    is ($newincbiblioitemnumber, $biblioitemnumbertotest);
 }
 
 sub mock_marcfromkohafield {
@@ -246,22 +282,65 @@ sub create_issn_field {
 }
 
 subtest 'MARC21' => sub {
-    plan tests => 26;
+    plan tests => 28;
     run_tests('MARC21');
     $dbh->rollback;
 };
 
 subtest 'UNIMARC' => sub {
-    plan tests => 26;
+    plan tests => 28;
     run_tests('UNIMARC');
     $dbh->rollback;
 };
 
 subtest 'NORMARC' => sub {
-    plan tests => 26;
+    plan tests => 28;
     run_tests('NORMARC');
     $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;