Bug 31555: change holds GET permission to place_holds
[koha-ffzg.git] / t / db_dependent / Biblio.t
index 21c39ae..67cba63 100755 (executable)
@@ -30,10 +30,10 @@ use Koha::Database;
 use Koha::Caches;
 use Koha::MarcSubfieldStructures;
 
-use C4::Linker::Default;
+use C4::Linker::Default qw( get_link );
 
 BEGIN {
-    use_ok('C4::Biblio');
+    use_ok('C4::Biblio', qw( AddBiblio GetMarcFromKohaField BiblioAutoLink GetMarcSubfieldStructure GetMarcSubfieldStructureFromKohaField LinkBibHeadingsToAuthorities GetBiblioData ModBiblio GetMarcISSN GetMarcControlnumber GetMarcISBN GetMarcPrice GetFrameworkCode GetMarcUrls IsMarcStructureInternal GetMarcStructure GetXmlBiblio DelBiblio ));
 }
 
 my $schema = Koha::Database->new->schema;
@@ -44,7 +44,7 @@ Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
 my $builder = t::lib::TestBuilder->new;
 
 subtest 'AddBiblio' => sub {
-    plan tests => 4;
+    plan tests => 5;
 
     my $marcflavour = 'MARC21';
     t::lib::Mocks::mock_preference( 'marcflavour', $marcflavour );
@@ -67,6 +67,20 @@ subtest 'AddBiblio' => sub {
     );
     is( Koha::Biblios->count, $nb_biblios,
         'No biblio should have been added if something went wrong' );
+
+    t::lib::Mocks::mock_preference( 'AutoLinkBiblios', $marcflavour );
+    t::lib::Mocks::mock_preference( 'AutoCreateAuthorities', $marcflavour );
+    t::lib::Mocks::mock_preference( 'autoControlNumber', "OFF" );
+
+    my $mock_biblio = Test::MockModule->new("C4::Biblio");
+    $mock_biblio->mock( BiblioAutoLink => sub {
+        my $record = shift;
+        my $frameworkcode = shift;
+        warn "My biblionumber is ".$record->subfield('999','c')." and my frameworkcode is $frameworkcode";
+    });
+    warning_like { $builder->build_sample_biblio(); }
+        qr/My biblionumber is \d+ and my frameworkcode is /, "The biblionumber is correctly passed to BiblioAutoLink";
+
 };
 
 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
@@ -154,10 +168,10 @@ subtest "GetMarcFromKohaField" => sub {
 };
 
 subtest "Authority creation with default linker" => sub {
-    plan tests => 2;
+    plan tests => 4;
     # Automatic authority creation
     t::lib::Mocks::mock_preference('LinkerModule', 'Default');
-    t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
+    t::lib::Mocks::mock_preference('AutoLinkBiblios', 1);
     t::lib::Mocks::mock_preference('AutoCreateAuthorities', 1);
     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
     my $linker = C4::Linker::Default->new({});
@@ -175,6 +189,10 @@ subtest "Authority creation with default linker" => sub {
     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");
+
+    ($num_changed,$results) = LinkBibHeadingsToAuthorities($linker, $marc_record, "",undef);
+    is( $num_changed, 0, "We shouldn't link or create a new record using cached result");
+    ok( !defined $results->{added}, "If we have multiple matches, we shouldn't create a new record on second instance");
 };
 
 
@@ -230,6 +248,12 @@ sub run_tests {
 
     my $marcflavour = shift;
     t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
+    # Authority tests don't interact well with Elasticsearch at the moment due to the fact that there's currently no way to
+    # roll back ES index changes.
+    t::lib::Mocks::mock_preference('SearchEngine', 'Zebra');
+    t::lib::Mocks::mock_preference('autoControlNumber', 'OFF');
+
+    t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
 
     my $isbn = '0590353403';
     my $title = 'Foundation';
@@ -251,8 +275,10 @@ sub run_tests {
     is( $data->{ title }, undef,
         '(GetBiblioData) Title field is empty in fresh biblio.');
 
+    my $biblio = Koha::Biblios->find($biblionumber);
+
     my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
-    my $marc = GetMarcBiblio({ biblionumber => $biblionumber });
+    my $marc = $biblio->metadata->record;
     is( $marc->subfield( $isbn_field, $isbn_subfield ), $isbn, );
 
     # Add title
@@ -263,7 +289,7 @@ sub run_tests {
     is( $data->{ title }, $title,
         'ModBiblio correctly added the title field, and GetBiblioData.');
     is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
-    $marc = GetMarcBiblio({ biblionumber => $biblionumber });
+    $marc = $biblio->get_from_storage->metadata->record;
     my ( $title_field, $title_subfield ) = get_title_field();
     is( $marc->subfield( $title_field, $title_subfield ), $title, );
 
@@ -400,12 +426,8 @@ sub run_tests {
 
     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 => $biblionumber,
-        embed_items  => 0 });
     my $frameworkcode = GetFrameworkCode($biblionumber);
+    my $updatedrecord = $biblio->metadata->record;
     my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
     die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
     my $biblioitemnumbertotest;
@@ -414,16 +436,6 @@ sub run_tests {
     } else {
         $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
     }
-    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(
@@ -437,7 +449,7 @@ sub run_tests {
         'GetMarcUrls prefixed a MARC21 URL with http://' );
 
     # Automatic authority creation
-    t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
+    t::lib::Mocks::mock_preference('AutoLinkBiblios', 1);
     t::lib::Mocks::mock_preference('AutoCreateAuthorities', 1);
     my $authorities_mod = Test::MockModule->new( 'C4::Heading' );
     $authorities_mod->mock(
@@ -465,7 +477,7 @@ sub run_tests {
     my $authid = $field->subfield('9');
     ok($authid, 'ModBiblio adds authority id');
 
-    use_ok('C4::AuthoritiesMarc');
+    use_ok('C4::AuthoritiesMarc', qw( GetAuthority ));
     my $auth_record = C4::AuthoritiesMarc::GetAuthority($authid);
     ok($auth_record, 'Authority record successfully retrieved');
 
@@ -481,7 +493,7 @@ sub run_tests {
     is($field->subfield($author_relator_subfield), undef, 'Authority does not contain relator subfield');
 
     # Reset settings
-    t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 0);
+    t::lib::Mocks::mock_preference('AutoLinkBiblios', 0);
     t::lib::Mocks::mock_preference('AutoCreateAuthorities', 0);
 }
 
@@ -586,14 +598,14 @@ sub create_author_field {
 }
 
 subtest 'MARC21' => sub {
-    plan tests => 48;
+    plan tests => 46;
     run_tests('MARC21');
     $schema->storage->txn_rollback;
     $schema->storage->txn_begin;
 };
 
 subtest 'UNIMARC' => sub {
-    plan tests => 48;
+    plan tests => 46;
 
     # 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;
@@ -603,13 +615,6 @@ subtest 'UNIMARC' => sub {
     $schema->storage->txn_begin;
 };
 
-subtest 'NORMARC' => sub {
-    plan tests => 48;
-    run_tests('NORMARC');
-    $schema->storage->txn_rollback;
-    $schema->storage->txn_begin;
-};
-
 subtest 'IsMarcStructureInternal' => sub {
     plan tests => 9;
     my $tagslib = GetMarcStructure();
@@ -635,6 +640,8 @@ subtest 'IsMarcStructureInternal' => sub {
 subtest 'deletedbiblio_metadata' => sub {
     plan tests => 2;
 
+    t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
+
     my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
     my $biblio_metadata = C4::Biblio::GetXmlBiblio( $biblionumber );
     C4::Biblio::DelBiblio( $biblionumber );
@@ -645,8 +652,11 @@ subtest 'deletedbiblio_metadata' => sub {
 };
 
 subtest 'DelBiblio' => sub {
+
     plan tests => 5;
 
+    t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
+
     my ($biblionumber, $biblioitemnumber) = C4::Biblio::AddBiblio(MARC::Record->new, '');
     my $deleted = C4::Biblio::DelBiblio( $biblionumber );
     is( $deleted, undef, 'DelBiblio returns undef is the biblio has been deleted correctly - Must be 1 instead'); # FIXME We should return 1 instead!
@@ -698,7 +708,8 @@ subtest 'MarcFieldForCreatorAndModifier' => sub {
     my $record = MARC::Record->new();
     my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
 
-    $record = GetMarcBiblio({biblionumber => $biblionumber});
+    my $biblio = Koha::Biblios->find($biblionumber);
+    $record = $biblio->metadata->record;
     is($record->subfield('998', 'a'), 123, '998$a = 123');
     is($record->subfield('998', 'b'), 'John Doe', '998$b = John Doe');
     is($record->subfield('998', 'c'), 123, '998$c = 123');
@@ -707,7 +718,7 @@ subtest 'MarcFieldForCreatorAndModifier' => sub {
     $c4_context->mock('userenv', sub { return { number => 321, firstname => 'Jane', surname => 'Doe'}; });
     C4::Biblio::ModBiblio($record, $biblionumber, '');
 
-    $record = GetMarcBiblio({biblionumber => $biblionumber});
+    $record = $biblio->get_from_storage->metadata->record;
     is($record->subfield('998', 'a'), 123, '998$a = 123');
     is($record->subfield('998', 'b'), 'John Doe', '998$b = John Doe');
     is($record->subfield('998', 'c'), 321, '998$c = 321');
@@ -717,7 +728,7 @@ subtest 'MarcFieldForCreatorAndModifier' => sub {
 subtest 'ModBiblio called from linker test' => sub {
     plan tests => 2;
     my $called = 0;
-    t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
+    t::lib::Mocks::mock_preference('AutoLinkBiblios', 1);
     my $biblio_mod = Test::MockModule->new( 'C4::Biblio' );
     $biblio_mod->mock( 'LinkBibHeadingsToAuthorities', sub {
         $called = 1;
@@ -727,7 +738,7 @@ subtest 'ModBiblio called from linker test' => sub {
     C4::Biblio::ModBiblio($record,$biblionumber,'');
     is($called,1,"We called to link bibs because not from linker");
     $called = 0;
-    C4::Biblio::ModBiblio($record,$biblionumber,'',1);
+    C4::Biblio::ModBiblio($record,$biblionumber,'',{ disable_autolink => 1 });
     is($called,0,"We didn't call to link bibs because from linker");
 };
 
@@ -745,13 +756,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},
@@ -767,8 +779,8 @@ subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
     );
 
     #Add test for this case using verbose
-    ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef, 1);
-    my $details = $results->{details};
+    $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");
 
@@ -781,11 +793,10 @@ subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
     );
 
     # 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},
@@ -801,7 +812,8 @@ subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
     );
 
     # The same example With verbose
-    ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef, 1);
+    $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");
 
@@ -813,6 +825,34 @@ subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
     );
 };
 
+subtest 'autoControlNumber tests' => sub {
+
+    plan tests => 3;
+
+    t::lib::Mocks::mock_preference('autoControlNumber', 'OFF');
+
+    my $record = MARC::Record->new();
+    my ($biblio_id) = C4::Biblio::AddBiblio($record, '');
+    my $biblio = Koha::Biblios->find($biblio_id);
+
+    $record = $biblio->metadata->record;
+    is($record->field('001'), undef, '001 not set when pref is off');
+
+    t::lib::Mocks::mock_preference('autoControlNumber', 'biblionumber');
+    C4::Biblio::ModBiblio($record, $biblio_id, "", { skip_record_index => 1, disable_autolink => 1 });
+    $biblio->discard_changes;
+    $record = $biblio->metadata->record;
+    is($record->field('001')->as_string(), $biblio_id, '001 set to biblionumber when pref set and field is blank');
+
+    $record->field('001')->update('Not biblionumber');
+    C4::Biblio::ModBiblio($record, $biblio_id, "", { skip_record_index => 1, disable_autolink => 1 });
+    $biblio->discard_changes;
+    $record = $biblio->metadata->record;
+    is($record->field('001')->as_string(), 'Not biblionumber', '001 not set to biblionumber when pref set and field exists');
+
+};
+
+
 # Cleanup
 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
 $schema->storage->txn_rollback;