Bug 27268: Move GetMarcNotes to Koha namespace
[srvgit] / t / db_dependent / Biblio.t
index 1d12d29..4e69b3e 100755 (executable)
@@ -17,8 +17,9 @@
 
 use Modern::Perl;
 
-use Test::More tests => 13;
+use Test::More tests => 15;
 use Test::MockModule;
+use Test::Warn;
 use List::MoreUtils qw( uniq );
 use MARC::Record;
 
@@ -29,6 +30,8 @@ use Koha::Database;
 use Koha::Caches;
 use Koha::MarcSubfieldStructures;
 
+use C4::Linker::Default;
+
 BEGIN {
     use_ok('C4::Biblio');
 }
@@ -40,6 +43,32 @@ Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
 
 my $builder = t::lib::TestBuilder->new;
 
+subtest 'AddBiblio' => sub {
+    plan tests => 4;
+
+    my $marcflavour = 'MARC21';
+    t::lib::Mocks::mock_preference( 'marcflavour', $marcflavour );
+    my $record = MARC::Record->new();
+
+    my ( $f, $sf ) = GetMarcFromKohaField('biblioitems.lccn');
+    my $lccn_field = MARC::Field->new( $f, ' ', ' ',
+        $sf => 'ThisisgoingtobetoomanycharactersfortheLCCNfield' );
+    $record->append_fields($lccn_field);
+
+    my $nb_biblios = Koha::Biblios->count;
+    my ( $biblionumber, $biblioitemnumber );
+    warnings_like { ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' ) }
+        [ qr/Data too long for column 'lccn'/, qr/Data too long for column 'lccn'/ ],
+        "expected warnings when adding too long LCCN";
+    is( $biblionumber, undef,
+        'AddBiblio returns undef for biblionumber if something went wrong' );
+    is( $biblioitemnumber, undef,
+        'AddBiblio returns undef for biblioitemnumber if something went wrong'
+    );
+    is( Koha::Biblios->count, $nb_biblios,
+        'No biblio should have been added if something went wrong' );
+};
+
 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
     plan tests => 25;
 
@@ -124,8 +153,34 @@ subtest "GetMarcFromKohaField" => sub {
     is( $retval[0].$retval[1], '399a', 'Including 399a' );
 };
 
+subtest "Authority creation with default linker" => sub {
+    plan tests => 2;
+    # Automatic authority creation
+    t::lib::Mocks::mock_preference('LinkerModule', 'Default');
+    t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
+    t::lib::Mocks::mock_preference('AutoCreateAuthorities', 1);
+    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
+    my $linker = C4::Linker::Default->new({});
+    my $authorities_mod = Test::MockModule->new( 'C4::Heading' );
+    $authorities_mod->mock(
+        'authorities',
+        sub {
+            my $results = [{ authid => 'original' },{ authid => 'duplicate' }];
+            return $results;
+        }
+    );
+    my $marc_record = MARC::Record->new();
+    my $field = MARC::Field->new(655, ' ', ' ','a' => 'Magical realism');
+    $marc_record->append_fields( $field );
+    my ($num_changed,$results) = LinkBibHeadingsToAuthorities($linker, $marc_record, "",undef);
+    is( $num_changed, 0, "We shouldn't link or create a new record");
+    ok( !defined $results->{added}, "If we have multiple matches, we shouldn't create a new record");
+};
+
+
+
 # Mocking variables
-my $biblio_module = new Test::MockModule('C4::Biblio');
+my $biblio_module = Test::MockModule->new('C4::Biblio');
 $biblio_module->mock(
     'GetMarcSubfieldStructure',
     sub {
@@ -157,7 +212,7 @@ $biblio_module->mock(
       }
 );
 
-my $currency = new Test::MockModule('Koha::Acquisition::Currencies');
+my $currency = Test::MockModule->new('Koha::Acquisition::Currencies');
 $currency->mock(
     'get_active',
     sub {
@@ -361,15 +416,6 @@ sub run_tests {
     }
     is ($newincbiblioitemnumber, $biblioitemnumbertotest, 'Check newincbiblioitemnumber');
 
-    # test for GetMarcNotes
-    my $a1= GetMarcNotes( $marc_record, $marcflavour );
-    my $field2 = MARC::Field->new( $marcflavour eq 'UNIMARC'? 300: 555, 0, '', a=> 'Some text', u=> 'http://url-1.com', u=> 'nohttp://something_else' );
-    $marc_record->append_fields( $field2 );
-    my $a2= GetMarcNotes( $marc_record, $marcflavour );
-    is( ( $marcflavour eq 'UNIMARC' && @$a2 == @$a1 + 1 ) ||
-        ( $marcflavour ne 'UNIMARC' && @$a2 == @$a1 + 3 ), 1,
-        'Check the number of returned notes of GetMarcNotes' );
-
     # test for GetMarcUrls
     $marc_record->append_fields(
         MARC::Field->new( '856', '', '', u => ' https://koha-community.org ' ),
@@ -384,12 +430,12 @@ sub run_tests {
     # Automatic authority creation
     t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
     t::lib::Mocks::mock_preference('AutoCreateAuthorities', 1);
-    my $authorities_mod = Test::MockModule->new( 'C4::AuthoritiesMarc' );
+    my $authorities_mod = Test::MockModule->new( 'C4::Heading' );
     $authorities_mod->mock(
-        'SearchAuthorities',
+        'authorities',
         sub {
             my @results;
-            return \@results, 0;
+            return \@results;
         }
     );
     $success = 0;
@@ -531,14 +577,14 @@ sub create_author_field {
 }
 
 subtest 'MARC21' => sub {
-    plan tests => 48;
+    plan tests => 47;
     run_tests('MARC21');
     $schema->storage->txn_rollback;
     $schema->storage->txn_begin;
 };
 
 subtest 'UNIMARC' => sub {
-    plan tests => 48;
+    plan tests => 47;
 
     # Mock the auth type data for UNIMARC
     $dbh->do("UPDATE auth_types SET auth_tag_to_report = '106' WHERE auth_tag_to_report = '100'") or die $dbh->errstr;
@@ -549,7 +595,7 @@ subtest 'UNIMARC' => sub {
 };
 
 subtest 'NORMARC' => sub {
-    plan tests => 48;
+    plan tests => 47;
     run_tests('NORMARC');
     $schema->storage->txn_rollback;
     $schema->storage->txn_begin;
@@ -677,7 +723,7 @@ subtest 'ModBiblio called from linker test' => sub {
 };
 
 subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
-    plan tests => 6;
+    plan tests => 12;
 
     # Set up mocks to ensure authorities are generated
     my $biblio_mod = Test::MockModule->new( 'C4::Linker::Default' );
@@ -690,13 +736,14 @@ subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
     t::lib::Mocks::mock_preference('AutoCreateAuthorities', '1');
 
     my $linker = C4::Linker::Default->new();
-    my $record = MARC::Record->new();
+    my $biblio = $builder->build_sample_biblio();
+    my $record = $biblio->metadata->record;
 
     # Generate a record including all valid subfields and an invalid one 'e'
     my $field = MARC::Field->new('650','','','a' => 'Beach city', 'b' => 'Weirdness', 'v' => 'Fiction', 'x' => 'Books', 'y' => '21st Century', 'z' => 'Fish Stew Pizza', 'e' => 'Depicted');
 
     $record->append_fields($field);
-    my ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef);
+    my ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef,650);
 
     is( $num_headings_changed, 1, 'We changed the one we passed' );
     is_deeply( $results->{added},
@@ -711,12 +758,25 @@ subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
         "The generated record contains the correct subfields"
     );
 
+    #Add test for this case using verbose
+    $record->field('650')->delete_subfield('9');
+    ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef, 650, 1);
+    is( $num_headings_changed, 1, 'We changed the one we passed' );
+    is( $results->{details}->[0]->{status}, 'CREATED', "We added an authority record for the heading using verbose");
+
+    # Now we check the authority record itself
+    $authority = GetAuthority($results->{details}->[0]->{authid});
+
+    is( $authority->field('150')->as_string(),
+         "Beach city Weirdness Fiction Books 21st Century Fish Stew Pizza",
+         "The generated record contains the correct subfields when using verbose"
+    );
+
     # Example series link with volume and punctuation
-    $record = MARC::Record->new();
     $field = MARC::Field->new('800','','','a' => 'Tolkien, J. R. R.', 'q' => '(John Ronald Reuel),', 'd' => '1892-1973.', 't' => 'Lord of the rings ;', 'v' => '1');
     $record->append_fields($field);
 
-    ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef);
+    ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef, 800);
 
     is( $num_headings_changed, 1, 'We changed the one we passed' );
     is_deeply( $results->{added},
@@ -731,8 +791,18 @@ subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
         "The generated record contains the correct subfields"
     );
 
+    # The same example With verbose
+    $record->field('800')->delete_subfield('9');
+    ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef, 800, 1);
+    is( $num_headings_changed, 1, 'We changed the one we passed' );
+    is( $results->{details}->[0]->{status}, 'CREATED', "We added an authority record for the heading using verbose");
 
-
+    # Now we check the authority record itself
+    $authority = GetAuthority($results->{details}->[0]->{authid});
+    is( $authority->field('100')->as_string(),
+         "Tolkien, J. R. R. (John Ronald Reuel), 1892-1973. Lord of the rings",
+         "The generated record contains the correct subfields"
+    );
 };
 
 # Cleanup