use Modern::Perl;
-use Test::More tests => 7;
+use Test::More tests => 10;
use Getopt::Long;
use MARC::Record;
use t::lib::Mocks;
use t::lib::TestBuilder;
-use C4::Biblio;
+use C4::Biblio qw( AddBiblio GetMarcBiblio ModBiblio );
use Koha::Authorities;
+use Koha::Authority::ControlledIndicators;
use Koha::Authority::MergeRequests;
use Koha::Database;
BEGIN {
- use_ok('C4::AuthoritiesMarc');
+ use_ok('C4::AuthoritiesMarc', qw( merge AddAuthority compare_fields DelAuthority ));
}
# Optionally change marc flavour
};
subtest "Test some specific postponed merge issues" => sub {
- plan tests => 4;
+ plan tests => 6;
my $authmarc = MARC::Record->new;
$authmarc->append_fields( MARC::Field->new( '109', '', '', 'a' => 'aa', b => 'bb' ));
$restored_mocks->{auth_mod}->unmock_all;
$biblio = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
is( $biblio->subfield('109', '9'), $id, 'If the 109 is no longer present, another modify merge would not bring it back' );
+
+ # Bug 22437 now removes older postponed A->A merges in DelAuthority
+ $id = AddAuthority( $authmarc, undef, $authtype1 );
+ my $merge = Koha::Authority::MergeRequest->new({ authid => $id })->store;
+ DelAuthority({ authid => $id, skip_merge => 1 });
+ $merge->discard_changes;
+ is( $merge->in_storage, 0, 'Older merge should be removed' );
+ # And even if we don't pass skip_merge
+ $id = AddAuthority( $authmarc, undef, $authtype1 );
+ $merge = Koha::Authority::MergeRequest->new({ authid => $id })->store;
+ DelAuthority({ authid => $id });
+ $merge->discard_changes;
+ is( $merge->in_storage, 0, 'Older merge should be removed again' );
+};
+
+subtest "Graceful resolution of missing reporting tag" => sub {
+ plan tests => 2;
+
+ # Simulate merge with authority in Default fw without reporting tag
+ # We expect data loss in biblio, but we keep $a and the reference in $9
+ # in order to allow a future merge to restore data.
+
+ # Accomplish the above by clearing reporting tag in $authtype2
+ my $fw2 = Koha::Authority::Types->find( $authtype2 );
+ $fw2->auth_tag_to_report('')->store;
+
+ my $authmarc = MARC::Record->new;
+ $authmarc->append_fields( MARC::Field->new( '109', '', '', 'a' => 'aa', b => 'bb' ));
+ my $id1 = AddAuthority( $authmarc, undef, $authtype1 );
+ my $id2 = AddAuthority( $authmarc, undef, $authtype2 );
+
+ my $biblio = MARC::Record->new;
+ $biblio->append_fields(
+ MARC::Field->new( '609', '', '', a => 'aa', 9 => $id1 ),
+ );
+ my ( $biblionumber ) = C4::Biblio::AddBiblio( $biblio, '' );
+
+ # Merge
+ merge({ mergefrom => $id1, MARCfrom => $authmarc, mergeto => $id2, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
+ $biblio = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
+ is( $biblio->subfield('612', '9'), $id2, 'id2 saved in $9' );
+ is( $biblio->subfield('612', 'a'), ' ', 'Kept an empty $a too' );
+
+ $fw2->auth_tag_to_report('112')->store;
+};
+
+subtest 'merge should not reorder too much' => sub {
+ plan tests => 2;
+
+ # Back to loose mode
+ t::lib::Mocks::mock_preference('AuthorityMergeMode', 'loose');
+
+ my $authmarc = MARC::Record->new;
+ $authmarc->append_fields( MARC::Field->new( '109', '', '', 'a' => 'aa', b => 'bb' ));
+ my $id = AddAuthority( $authmarc, undef, $authtype1 );
+ my $biblio = MARC::Record->new;
+ $biblio->append_fields(
+ MARC::Field->new( '109', '', '', 9 => $id, i => 'in front', a => 'aa', c => 'after controlled block' ), # this field shows the old situation when $9 is the first subfield
+ MARC::Field->new( '609', '', '', i => 'in front', a => 'aa', c => 'after controlled block', 9 => $id ), # here $9 is already the last one
+ );
+ my ( $biblionumber ) = C4::Biblio::AddBiblio( $biblio, '' );
+
+ # Merge 109 and 609 and check order of subfields
+ merge({ mergefrom => $id, MARCfrom => $authmarc, mergeto => $id, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
+ my $biblio2 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
+ my $subfields = [ map { $_->[0] } $biblio2->field('109')->subfields ];
+ is_deeply( $subfields, [ 'i', 'a', 'b', 'c', '9' ], 'Merge only moved $9' );
+ $subfields = [ map { $_->[0] } $biblio2->field('609')->subfields ];
+ is_deeply( $subfields, [ 'i', 'a', 'b', 'c', '9' ], 'Order kept' );
+};
+
+subtest 'Test how merge handles controlled indicators' => sub {
+ plan tests => 4;
+
+ # Note: See more detailed tests in t/Koha/Authority/ControlledIndicators.t
+
+ # Testing MARC21 because thesaurus code makes it more interesting
+ t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
+ t::lib::Mocks::mock_preference('AuthorityControlledIndicators', q|marc21,*,ind1:auth1,ind2:thesaurus|);
+
+ my $authmarc = MARC::Record->new;
+ $authmarc->append_fields(
+ MARC::Field->new( '008', (' 'x11).'r' ), # thesaurus code
+ MARC::Field->new( '109', '7', '', 'a' => 'a' ),
+ );
+ my $id = AddAuthority( $authmarc, undef, $authtype1 );
+ my $biblio = MARC::Record->new;
+ $biblio->append_fields(
+ MARC::Field->new( '609', '8', '4', a => 'a', 2 => '2', 9 => $id ),
+ );
+ my ( $biblionumber ) = C4::Biblio::AddBiblio( $biblio, '' );
+
+ # Merge and check indicators and $2
+ merge({ mergefrom => $id, MARCfrom => $authmarc, mergeto => $id, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
+ my $biblio2 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
+ is( $biblio2->field('609')->indicator(1), '7', 'Indicator1 OK' );
+ is( $biblio2->field('609')->indicator(2), '7', 'Indicator2 OK' );
+ is( $biblio2->subfield('609', '2'), 'aat', 'Subfield $2 OK' );
+
+ # Test $2 removal now
+ $authmarc->field('008')->update( (' 'x11).'a' ); # LOC, no $2 needed
+ AddAuthority( $authmarc, $id, $authtype1 ); # modify
+ merge({ mergefrom => $id, MARCfrom => $authmarc, mergeto => $id, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
+ $biblio2 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
+ is( $biblio2->subfield('609', '2'), undef, 'No subfield $2 left' );
};
sub set_mocks {