Koha/t/db_dependent/Authorities/Merge.t
Marcel de Rooy 8cde85451e Bug 17913: Fix the new field tag in merge when changing type
Originally aimed for 9988, adjusted for this report.

Old behavior was: pick the first tag. This is definitely wrong.
If you (would) merge 610 to 611, you don't want to get a 111.

This patch resolves the problem by determining the new tag in a small
helper routine _merge_newtag, and corrects the position of the new field
in the MARC record with append_fields_ordered. Too bad that MARC::Record
does not have such a function; it looks like insert_fields_ordered, but
it is different in case of multiple fields with the same tag.

Note: These two small helper functions are not tested separately, since they
should not be called outside of merge. They are implicitly tested by the
adjusted tests in Merge.t.

Note: In adding tests for this fix, I chose to simplify compare_field_count
(no need for the pass parameter), and replace the pass parameter of sub
compare_field_order by an exclude parameter, a hash of fields to exclude in
counting fields.

Test plan:
Run t/db_dependent/Authorities/Merge.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
2017-01-20 13:55:11 +00:00

304 lines
12 KiB
Perl
Executable file

#!/usr/bin/perl
# Tests for C4::AuthoritiesMarc::merge
use Modern::Perl;
use Test::More tests => 4;
use MARC::Record;
use Test::MockModule;
use Test::MockObject;
use t::lib::Mocks;
use t::lib::TestBuilder;
use C4::Biblio;
use Koha::Database;
BEGIN {
use_ok('C4::AuthoritiesMarc');
}
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
my $dbh = C4::Context->dbh;
# Some advanced mocking :)
my ( @zebrarecords, $index );
my $context_mod = Test::MockModule->new( 'C4::Context' );
my $search_mod = Test::MockModule->new( 'C4::Search' );
my $zoom_mod = Test::MockModule->new( 'ZOOM::Query::CCL2RPN', no_auto => 1 );
my $conn_obj = Test::MockObject->new;
my $zoom_obj = Test::MockObject->new;
my $zoom_record_obj = Test::MockObject->new;
set_mocks();
# Framework operations
my ( $authtype1, $authtype2 ) = modify_framework();
subtest 'Test merge A1 to A2 (within same authtype)' => sub {
# Tests originate from bug 11700
plan tests => 9;
# Add two authority records
my $auth1 = MARC::Record->new;
$auth1->append_fields( MARC::Field->new( '109', '0', '0', 'a' => 'George Orwell' ));
my $authid1 = AddAuthority( $auth1, undef, $authtype1 );
my $auth2 = MARC::Record->new;
$auth2->append_fields( MARC::Field->new( '109', '0', '0', 'a' => 'G. Orwell' ));
my $authid2 = AddAuthority( $auth2, undef, $authtype1 );
# Add two biblio records
my $biblio1 = MARC::Record->new;
$biblio1->append_fields( MARC::Field->new( '609', '0', '0', '9' => $authid1, 'a' => 'George Orwell' ));
my ( $biblionumber1 ) = AddBiblio($biblio1, '');
my $biblio2 = MARC::Record->new;
$biblio2->append_fields( MARC::Field->new( '609', '0', '0', '9' => $authid2, 'a' => 'G. Orwell' ));
my ( $biblionumber2 ) = AddBiblio($biblio2, '');
# Time to merge
@zebrarecords = ( $biblio1, $biblio2 );
$index = 0;
my $rv = C4::AuthoritiesMarc::merge( $authid2, $auth2, $authid1, $auth1 );
is( $rv, 1, 'We expect one biblio record (out of two) to be updated' );
# Check the results
my $newbiblio1 = GetMarcBiblio($biblionumber1);
$newbiblio1->delete_fields( $newbiblio1->field('100') ); # fix for UNIMARC
compare_field_count( $biblio1, $newbiblio1 );
compare_field_order( $biblio1, $newbiblio1 );
is( $newbiblio1->subfield('609', '9'), $authid1, 'Check biblio1 609$9' );
is( $newbiblio1->subfield('609', 'a'), 'George Orwell',
'Check biblio1 609$a' );
my $newbiblio2 = GetMarcBiblio($biblionumber2);
$newbiblio2->delete_fields( $newbiblio2->field('100') ); # fix for UNIMARC
compare_field_count( $biblio2, $newbiblio2 );
compare_field_order( $biblio2, $newbiblio2 );
is( $newbiblio2->subfield('609', '9'), $authid1, 'Check biblio2 609$9' );
is( $newbiblio2->subfield('609', 'a'), 'George Orwell',
'Check biblio2 609$a' );
};
subtest 'Test merge A1 to modified A1' => sub {
# Tests originate from bug 11700
plan tests => 9;
# Simulate modifying an authority from auth1old to auth1new
my $auth1old = MARC::Record->new;
$auth1old->append_fields( MARC::Field->new( '109', '0', '0', 'a' => 'Bruce Wayne' ));
my $auth1new = $auth1old->clone;
$auth1new->field('109')->update( a => 'Batman' );
my $authid1 = AddAuthority( $auth1new, undef, $authtype1 );
# Add two biblio records
my $MARC1 = MARC::Record->new;
$MARC1->append_fields( MARC::Field->new( '109', '', '', 'a' => 'Bruce Wayne', 'b' => '2014', '9' => $authid1 ));
$MARC1->append_fields( MARC::Field->new( '245', '', '', 'a' => 'From the depths' ));
my $MARC2 = MARC::Record->new;
$MARC2->append_fields( MARC::Field->new( '109', '', '', 'a' => 'Batman', '9' => $authid1 ));
$MARC2->append_fields( MARC::Field->new( '245', '', '', 'a' => 'All the way to heaven' ));
my ( $biblionumber1 ) = AddBiblio( $MARC1, '');
my ( $biblionumber2 ) = AddBiblio( $MARC2, '');
# Time to merge
@zebrarecords = ( $MARC1, $MARC2 );
$index = 0;
t::lib::Mocks::mock_preference('AuthorityMergeMode', 'loose');
my $rv = C4::AuthoritiesMarc::merge( $authid1, $auth1old, $authid1, $auth1new );
is( $rv, 2, 'Both records are updated now' );
#Check the results
my $biblio1 = GetMarcBiblio($biblionumber1);
$biblio1->delete_fields( $biblio1->field('100') ); # quick fix for UNIMARC
compare_field_count( $MARC1, $biblio1 );
compare_field_order( $MARC1, $biblio1 );
is( $auth1new->field(109)->subfield('a'), $biblio1->field(109)->subfield('a'), 'Record1 values updated correctly' );
my $biblio2 = GetMarcBiblio( $biblionumber2 );
$biblio2->delete_fields( $biblio2->field('100') ); # quick fix for UNIMARC
compare_field_count( $MARC2, $biblio2 );
compare_field_order( $MARC2, $biblio2 );
is( $auth1new->field(109)->subfield('a'), $biblio2->field(109)->subfield('a'), 'Record2 values updated correctly' );
# This is only true in loose mode:
is( $biblio1->field(109)->subfield('b'), $MARC1->field(109)->subfield('b'), 'Subfield not overwritten in loose mode');
# Merge again in strict mode
t::lib::Mocks::mock_preference('AuthorityMergeMode', 'strict');
ModBiblio( $MARC1, $biblionumber1, '' );
@zebrarecords = ( $MARC1 );
$index = 0;
$rv = C4::AuthoritiesMarc::merge( $authid1, $auth1old, $authid1, $auth1new );
$biblio1 = GetMarcBiblio($biblionumber1);
is( $biblio1->field(109)->subfield('b'), undef, 'Subfield overwritten in strict mode' );
t::lib::Mocks::mock_preference('AuthorityMergeMode', 'loose');
};
subtest 'Test merge A1 to B1 (changing authtype)' => sub {
# Tests were aimed for bug 9988, moved to 17909 in adjusted form
# Would not encourage this type of merge, but we should test what we offer
# The merge routine still needs the fixes on bug 17913
plan tests => 12;
# create two auth recs of different type
my $auth1 = MARC::Record->new;
$auth1->append_fields( MARC::Field->new( '109', '0', '0', 'a' => 'George Orwell', b => 'bb' ));
my $authid1 = AddAuthority( $auth1, undef, $authtype1 );
my $auth2 = MARC::Record->new;
$auth2->append_fields( MARC::Field->new( '112', '0', '0', 'a' => 'Batman', c => 'cc' ));
my $authid2 = AddAuthority($auth1, undef, $authtype2 );
# create a biblio with one 109 and two 609s to be touched
# seems exceptional see bug 13760 comment10
my $marc = MARC::Record->new;
$marc->append_fields(
MARC::Field->new( '003', 'some_003' ),
MARC::Field->new( '109', '', '', a => 'G. Orwell', b => 'bb', d => 'd', 9 => $authid1 ),
MARC::Field->new( '245', '', '', a => 'My title' ),
MARC::Field->new( '609', '', '', a => 'Orwell', 9 => "$authid1" ),
MARC::Field->new( '609', '', '', a => 'Orwell', x => 'xx', 9 => "$authid1" ),
MARC::Field->new( '611', '', '', a => 'Added for testing order' ),
MARC::Field->new( '612', '', '', a => 'unrelated', 9 => 'other' ),
);
my ( $biblionumber ) = C4::Biblio::AddBiblio( $marc, '' );
my $oldbiblio = C4::Biblio::GetMarcBiblio( $biblionumber );
$oldbiblio->delete_fields( $oldbiblio->field('100') ); # fix for UNIMARC
# Time to merge
@zebrarecords = ( $marc );
$index = 0;
my $retval = C4::AuthoritiesMarc::merge( $authid1, $auth1, $authid2, $auth2 );
is( $retval, 1, 'We touched only one biblio' );
# Get new marc record for compares
my $newbiblio = C4::Biblio::GetMarcBiblio( $biblionumber );
$newbiblio->delete_fields( $newbiblio->field('100') ); # fix for UNIMARC
compare_field_count( $oldbiblio, $newbiblio );
# Exclude 109/609 and 112/612 in comparing order
compare_field_order( $oldbiblio, $newbiblio,
{ '109' => 1, '112' => 1, '609' => 1, '612' => 1 },
);
# Check position of 612s in the new record
my $full_order = join q/,/, map { $_->tag } $newbiblio->fields;
is( $full_order =~ /611(,612){3}/, 1, 'Check position of all 612s' );
# Check some fields
is( $newbiblio->field('003')->data,
$oldbiblio->field('003')->data,
'Check contents of a control field not expected to be touched' );
is( $newbiblio->subfield( '245', 'a' ),
$oldbiblio->subfield( '245', 'a' ),
'Check contents of a data field not expected to be touched' );
is( $newbiblio->subfield( '112', 'a' ),
$auth2->subfield( '112', 'a' ), 'Check modified 112a' );
is( $newbiblio->subfield( '112', 'c' ),
$auth2->subfield( '112', 'c' ), 'Check new 112c' );
# Check the original 612
is( ( $newbiblio->field('612') )[0]->subfield( 'a' ),
$oldbiblio->subfield( '612', 'a' ), 'Check untouched 612a' );
# Check second 612
is( ( $newbiblio->field('612') )[1]->subfield( 'a' ),
$auth2->subfield( '112', 'a' ), 'Check second touched 612a' );
# Check second new 612ax (in LOOSE mode)
is( ( $newbiblio->field('612') )[2]->subfield( 'a' ),
$auth2->subfield( '112', 'a' ), 'Check touched 612a' );
is( ( $newbiblio->field('612') )[2]->subfield( 'x' ),
( $oldbiblio->field('609') )[1]->subfield('x'),
'Check 612x' );
};
sub set_mocks {
# Mock ZOOM objects: They do nothing actually
# Get new_record_from_zebra to return the records
$context_mod->mock( 'Zconn', sub { $conn_obj; } );
$search_mod->mock( 'new_record_from_zebra', sub {
return if $index >= @zebrarecords;
return $zebrarecords[ $index++ ];
});
$zoom_mod->mock( 'new', sub {} );
$conn_obj->mock( 'search', sub { $zoom_obj; } );
$zoom_obj->mock( 'destroy', sub {} );
$zoom_obj->mock( 'record', sub { $zoom_record_obj; } );
$zoom_obj->mock( 'search', sub {} );
$zoom_obj->mock( 'size', sub { @zebrarecords } );
$zoom_record_obj->mock( 'raw', sub {} );
}
sub modify_framework {
my $builder = t::lib::TestBuilder->new;
# create two auth types
my $authtype1 = $builder->build({
source => 'AuthType',
value => {
auth_tag_to_report => '109',
},
});
my $authtype2 = $builder->build({
source => 'AuthType',
value => {
auth_tag_to_report => '112',
},
});
# Link 109/609 to the first authtype
$builder->build({
source => 'MarcSubfieldStructure',
value => {
tagfield => '109',
tagsubfield => 'a',
authtypecode => $authtype1->{authtypecode},
frameworkcode => '',
},
});
$builder->build({
source => 'MarcSubfieldStructure',
value => {
tagfield => '609',
tagsubfield => 'a',
authtypecode => $authtype1->{authtypecode},
frameworkcode => '',
},
});
# Link 112/612 to the second authtype
$builder->build({
source => 'MarcSubfieldStructure',
value => {
tagfield => '112',
tagsubfield => 'a',
authtypecode => $authtype2->{authtypecode},
frameworkcode => '',
},
});
$builder->build({
source => 'MarcSubfieldStructure',
value => {
tagfield => '612',
tagsubfield => 'a',
authtypecode => $authtype2->{authtypecode},
frameworkcode => '',
},
});
return ( $authtype1->{authtypecode}, $authtype2->{authtypecode} );
}
sub compare_field_count {
my ( $oldmarc, $newmarc ) = @_;
my $t;
is( scalar $newmarc->fields, $t = $oldmarc->fields, "Number of fields still equal to $t" );
}
sub compare_field_order {
my ( $oldmarc, $newmarc, $exclude ) = @_;
$exclude //= {};
my @oldfields = map { $exclude->{$_->tag} ? () : $_->tag } $oldmarc->fields;
my @newfields = map { $exclude->{$_->tag} ? () : $_->tag } $newmarc->fields;
is( ( join q/,/, @newfields ), ( join q/,/, @oldfields ),
'Order of fields unchanged' );
}
$schema->storage->txn_rollback;