de9efab82d8ebc7610199e9a9bffae3ac809e552
[koha-ffzg.git] / t / db_dependent / Authorities / Merge.t
1 #!/usr/bin/perl
2
3 # Tests for C4::AuthoritiesMarc::merge
4
5 use Modern::Perl;
6
7 use Test::More tests => 3;
8
9 use MARC::Record;
10 use Test::MockModule;
11 use Test::MockObject;
12
13 use C4::Biblio;
14 use Koha::Database;
15
16 BEGIN {
17         use_ok('C4::AuthoritiesMarc');
18 }
19
20 my $schema  = Koha::Database->new->schema;
21 $schema->storage->txn_begin;
22 my $dbh = C4::Context->dbh;
23
24 # Some advanced mocking :)
25 my ( @zebrarecords, $index );
26 my $auth_mod = Test::MockModule->new( 'C4::AuthoritiesMarc' );
27 my $context_mod = Test::MockModule->new( 'C4::Context' );
28 my $search_mod = Test::MockModule->new( 'C4::Search' );
29 my $zoom_mod = Test::MockModule->new( 'ZOOM::Query::CCL2RPN', no_auto => 1 );
30 my $conn_obj = Test::MockObject->new;
31 my $zoom_obj = Test::MockObject->new;
32 my $zoom_record_obj = Test::MockObject->new;
33 set_mocks();
34
35 subtest 'Test merge A1 to A2 (withing same authtype)' => sub {
36 # Tests originate from bug 11700
37     plan tests => 5;
38
39     # Create authority type TEST_PERSO
40     $dbh->do("INSERT INTO auth_types(authtypecode, authtypetext, auth_tag_to_report, summary) VALUES('TEST_PERSO', 'Personal Name', '109', 'Personal Names');");
41     $dbh->do("INSERT INTO auth_tag_structure (authtypecode, tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value) VALUES('TEST_PERSO', '109', 'HEADING--PERSONAL NAME', 'HEADING--PERSONAL NAME', 0, 0, NULL)");
42     $dbh->do("INSERT INTO auth_subfield_structure (authtypecode, tagfield, tagsubfield, liblibrarian, libopac, repeatable, mandatory, tab, authorised_value, value_builder, seealso, isurl, hidden, linkid, kohafield, frameworkcode) VALUES ('TEST_PERSO', '109', 'a', 'Personal name', 'Personal name', 0, 0, 1, NULL, NULL, '', 0, 0, '', '', '')");
43
44     my $auth1 = new MARC::Record;
45     $auth1->append_fields(new MARC::Field('109', '0', '0', 'a' => 'George Orwell'));
46     my $authid1 = AddAuthority($auth1, undef, 'TEST_PERSO');
47     my $auth2 = new MARC::Record;
48     $auth2->append_fields(new MARC::Field('109', '0', '0', 'a' => 'G. Orwell'));
49     my $authid2 = AddAuthority($auth2, undef, 'TEST_PERSO');
50
51     $dbh->do("INSERT IGNORE INTO marc_subfield_structure(tagfield, tagsubfield, liblibrarian, libopac, repeatable, mandatory, kohafield, tab, authorised_value, authtypecode, value_builder, isurl, hidden, frameworkcode, seealso, link, defaultvalue) VALUES('609', 'a', 'Personal name', 'Personal name', 0, 0, '', 6, '', 'TEST_PERSO', '', NULL, 0, '', '', '', NULL)");
52     $dbh->do("UPDATE marc_subfield_structure SET authtypecode = 'TEST_PERSO' WHERE tagfield='609' AND tagsubfield='a' AND frameworkcode='';");
53     my $tagfields = $dbh->selectcol_arrayref("select distinct tagfield from marc_subfield_structure where authtypecode='TEST_PERSO'");
54     my $biblio1 = new MARC::Record;
55     $biblio1->append_fields(
56         new MARC::Field('609', '0', '0', '9' => $authid1, 'a' => 'George Orwell')
57     );
58     my ( $biblionumber1 ) = AddBiblio($biblio1, '');
59     my $biblio2 = new MARC::Record;
60     $biblio2->append_fields(
61         new MARC::Field('609', '0', '0', '9' => $authid2, 'a' => 'G. Orwell')
62     );
63     my ( $biblionumber2 ) = AddBiblio($biblio2, '');
64
65     @zebrarecords = ( $biblio1, $biblio2 );
66     $index = 0;
67     my $rv = C4::AuthoritiesMarc::merge( $authid2, $auth2, $authid1, $auth1 );
68     is( $rv, 1, 'We expect one biblio record (out of two) to be updated' );
69
70     $biblio1 = GetMarcBiblio($biblionumber1);
71     is($biblio1->subfield('609', '9'), $authid1, 'Check biblio1 609$9' );
72     is($biblio1->subfield('609', 'a'), 'George Orwell',
73         'Check biblio1 609$a' );
74     $biblio2 = GetMarcBiblio($biblionumber2);
75     is($biblio2->subfield('609', '9'), $authid1, 'Check biblio2 609$9' );
76     is($biblio2->subfield('609', 'a'), 'George Orwell',
77         'Check biblio2 609$a' );
78 };
79
80 subtest 'Test merge A1 to modified A1' => sub {
81 # Tests originate from bug 11700
82     plan tests => 4;
83
84     $dbh->do("INSERT IGNORE INTO marc_subfield_structure(tagfield, tagsubfield, liblibrarian, libopac, repeatable, mandatory, kohafield, tab, authorised_value, authtypecode, value_builder, isurl, hidden, frameworkcode, seealso, link, defaultvalue) VALUES('109', 'a', 'Personal name', 'Personal name', 0, 0, '', 6, '', 'TEST_PERSO', '', NULL, 0, '', '', '', NULL)");
85     $dbh->do("UPDATE marc_subfield_structure SET authtypecode = 'TEST_PERSO' WHERE tagfield='109' AND tagsubfield='a' AND frameworkcode='';");
86
87     my $auth1old = MARC::Record->new;
88     $auth1old->append_fields( MARC::Field->new( '109', '0', '0', 'a' => 'Bruce Wayne' ));
89     my $auth1new = $auth1old->clone;
90     $auth1new->field('109')->update( a => 'Batman' );
91     my $authid1 = AddAuthority( $auth1new, undef, 'TEST_PERSO' );
92
93     my $MARC1 = MARC::Record->new();
94     $MARC1->append_fields( MARC::Field->new( '245', '', '', 'a' => 'From the depths' ));
95     $MARC1->append_fields( MARC::Field->new( '109', '', '', 'a' => 'Bruce Wayne', 'b' => '2014', '9' => $authid1 ));
96     my $MARC2 = MARC::Record->new();
97     $MARC2->append_fields( MARC::Field->new( '245', '', '', 'a' => 'All the way to heaven' ));
98     $MARC2->append_fields( MARC::Field->new( '109', '', '', 'a' => 'Batman', '9' => $authid1 ));
99     my ( $biblionumber1 ) = AddBiblio( $MARC1, '');
100     my ( $biblionumber2 ) = AddBiblio( $MARC2, '');
101
102     @zebrarecords = ( $MARC1, $MARC2 );
103     $index = 0;
104
105     my $rv = C4::AuthoritiesMarc::merge( $authid1, $auth1old, $authid1, $auth1new );
106     is( $rv, 2, 'Both records are updated now' );
107
108     my $biblio1 = GetMarcBiblio($biblionumber1);
109     my $biblio2 = GetMarcBiblio($biblionumber1);
110
111     my $auth_field = $auth1new->field(109)->subfield('a');
112     is( $auth_field, $biblio1->field(109)->subfield('a'), 'Record1 values updated correctly' );
113     is( $auth_field, $biblio2->field(109)->subfield('a'), 'Record2 values updated correctly' );
114
115     # TODO Following test will change when we improve merge
116     # Will depend on a preference
117     is( $biblio1->field(109)->subfield('b'), $MARC1->field(109)->subfield('b'), 'Record not overwritten while merging');
118 };
119
120 sub set_mocks {
121     # Mock ZOOM objects: They do nothing actually
122     # Get new_record_from_zebra to return the records
123
124     $context_mod->mock( 'Zconn', sub { $conn_obj; } );
125     $search_mod->mock( 'new_record_from_zebra', sub {
126          return if $index >= @zebrarecords;
127          return $zebrarecords[ $index++ ];
128     });
129     $zoom_mod->mock( 'new', sub {} );
130
131     $conn_obj->mock( 'search', sub { $zoom_obj; } );
132     $zoom_obj->mock( 'destroy', sub {} );
133     $zoom_obj->mock( 'record', sub { $zoom_record_obj; } );
134     $zoom_obj->mock( 'search', sub {} );
135     $zoom_obj->mock( 'size', sub { @zebrarecords } );
136     $zoom_record_obj->mock( 'raw', sub {} );
137 }
138
139 $schema->storage->txn_rollback;