Bug 5404: Move the test to a new IsMarcStructureInternal sub
[koha.git] / t / db_dependent / Biblio.t
index 2dd8034..e23dc34 100755 (executable)
 
 use Modern::Perl;
 
-use Test::More tests => 3;
+use Test::More tests => 6;
 use Test::MockModule;
-use Data::Dumper;
 
+use List::MoreUtils qw( uniq );
 use MARC::Record;
+use t::lib::Mocks qw( mock_preference );
 
 BEGIN {
     use_ok('C4::Biblio');
@@ -32,14 +33,25 @@ my $dbh = C4::Context->dbh;
 $dbh->{AutoCommit} = 0;
 $dbh->{RaiseError} = 1;
 
-my $global_marcflavour;
 # Mocking variables
-my $original_preference = C4::Context->can( 'preference' );
-my $context             = new Test::MockModule('C4::Context');
+my $context = new Test::MockModule('C4::Context');
 
-mock_preference();
 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
@@ -47,7 +59,7 @@ sub run_tests {
     undef $C4::Biblio::inverted_field_map;
 
     my $marcflavour = shift;
-    $global_marcflavour = $marcflavour;
+    t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
 
     my $isbn = '0590353403';
     my $title = 'Foundation';
@@ -140,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;
@@ -166,7 +184,7 @@ sub run_tests {
     $record_for_isbn->append_fields( $isbn_field );
     $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
     is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
-    is( $isbns->[0]->{ marcisbn }, $isbn, '(GetMarcISBN) The record contains our ISBN');
+    is( $isbns->[0], $isbn, '(GetMarcISBN) The record contains our ISBN');
 
     # We add 3 more ISBNs
     $record_for_isbn = MARC::Record->new();
@@ -178,23 +196,26 @@ sub run_tests {
     $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
     is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
     for my $i (0 .. $#more_isbns) {
-        is( $isbns->[$i]->{ marcisbn }, $more_isbns[$i],
+        is( $isbns->[$i], $more_isbns[$i],
             "(GetMarcISBN) Corretly retrieves ISBN #". ($i + 1));
     }
 
-}
-
-sub mock_preference {
-
-    $context->mock( 'preference', sub {
-        my ( $self, $pref ) = @_;
-        if ( $pref eq 'marcflavour' ) {
-            return $global_marcflavour;
-        } else {
-            &$original_preference(@_);
-        }
-    });
 
+    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 {
@@ -203,7 +224,8 @@ sub mock_marcfromkohafield {
         sub {
             my ( $self ) = shift;
 
-            if ( $global_marcflavour eq 'MARC21' ) {
+            if ( C4::Context->preference('marcflavour') eq 'MARC21' ||
+                 C4::Context->preference('marcflavour') eq 'NORMARC' ) {
 
                 return  {
                 '' => {
@@ -214,7 +236,7 @@ sub mock_marcfromkohafield {
                     'biblioitems.biblioitemnumber' => [ '999', 'd' ]
                     }
                 };
-            } elsif ( $global_marcflavour eq 'UNIMARC' ) {
+            } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
 
                 return {
                 '' => {
@@ -243,6 +265,9 @@ sub create_isbn_field {
 
     my $isbn_field = ( $marcflavour eq 'UNIMARC' ) ? '010' : '020';
     my $field = MARC::Field->new( $isbn_field,'','','a' => $isbn);
+    # Add the price subfield
+    my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c' ;
+    $field->add_subfields( $price_subfield => '$100' );
 
     return $field;
 }
@@ -257,16 +282,65 @@ sub create_issn_field {
 }
 
 subtest 'MARC21' => sub {
-    plan tests => 25;
+    plan tests => 28;
     run_tests('MARC21');
     $dbh->rollback;
 };
 
 subtest 'UNIMARC' => sub {
-    plan tests => 25;
+    plan tests => 28;
     run_tests('UNIMARC');
     $dbh->rollback;
 };
 
+subtest 'NORMARC' => sub {
+    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;