bf25a913fcefc0b918da7af385a52914c0a5c79d
[srvgit] / t / db_dependent / Authority / Merge.t
1 #!/usr/bin/perl
2
3 # Tests for C4::AuthoritiesMarc::merge
4
5 use Modern::Perl;
6
7 use Test::More tests => 10;
8
9 use Getopt::Long;
10 use MARC::Record;
11 use Test::MockModule;
12
13 use t::lib::Mocks;
14 use t::lib::TestBuilder;
15
16 use C4::Biblio;
17 use Koha::Authorities;
18 use Koha::Authority::ControlledIndicators;
19 use Koha::Authority::MergeRequests;
20 use Koha::Database;
21
22 BEGIN {
23         use_ok('C4::AuthoritiesMarc');
24 }
25
26 # Optionally change marc flavour
27 my $marcflavour;
28 GetOptions( 'flavour:s' => \$marcflavour );
29 t::lib::Mocks::mock_preference( 'marcflavour', $marcflavour ) if $marcflavour;
30
31 my $schema  = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
33
34 # Global variables, mocking and framework modifications
35 our @linkedrecords;
36 my $mocks = set_mocks();
37 our ( $authtype1, $authtype2 ) = modify_framework();
38
39 subtest 'Test postponed merge feature' => sub {
40     plan tests => 6;
41
42     # Set limit to zero, and call merge a few times
43     t::lib::Mocks::mock_preference('AuthorityMergeLimit', 0);
44     my $auth1 = t::lib::TestBuilder->new->build({ source => 'AuthHeader' });
45     my $cnt = Koha::Authority::MergeRequests->count;
46     merge({ mergefrom => '0' });
47     is( Koha::Authority::MergeRequests->count, $cnt, 'No merge request added as expected' );
48     merge({ mergefrom => $auth1->{authid} });
49     is( Koha::Authority::MergeRequests->count, $cnt, 'No merge request added since we have zero hits' );
50     @linkedrecords = ( 1, 2 ); # these biblionumbers do not matter
51     merge({ mergefrom => $auth1->{authid} });
52     is( Koha::Authority::MergeRequests->count, $cnt + 1, 'Merge request added as expected' );
53
54     # Set limit to two (starting with two records)
55     t::lib::Mocks::mock_preference('AuthorityMergeLimit', 2);
56     merge({ mergefrom => $auth1->{authid} });
57     is( Koha::Authority::MergeRequests->count, $cnt + 1, 'No merge request added as we do not exceed the limit' );
58     @linkedrecords = ( 1, 2, 3 ); # these biblionumbers do not matter
59     merge({ mergefrom => $auth1->{authid} });
60     is( Koha::Authority::MergeRequests->count, $cnt + 2, 'Merge request added as we do exceed the limit again' );
61     # Now override
62     merge({ mergefrom => $auth1->{authid}, override_limit => 1 });
63     is( Koha::Authority::MergeRequests->count, $cnt + 2, 'No merge request added as we did override' );
64
65     # Set merge limit high enough for the other subtests
66     t::lib::Mocks::mock_preference('AuthorityMergeLimit', 1000);
67 };
68
69 subtest 'Test merge A1 to A2 (within same authtype)' => sub {
70 # Tests originate from bug 11700
71     plan tests => 9;
72
73     # Start in loose mode, although it actually does not matter here
74     t::lib::Mocks::mock_preference('AuthorityMergeMode', 'loose');
75
76     # Add two authority records
77     my $auth1 = MARC::Record->new;
78     $auth1->append_fields( MARC::Field->new( '109', '0', '0', 'a' => 'George Orwell' ));
79     my $authid1 = AddAuthority( $auth1, undef, $authtype1 );
80     my $auth2 = MARC::Record->new;
81     $auth2->append_fields( MARC::Field->new( '109', '0', '0', 'a' => 'G. Orwell' ));
82     my $authid2 = AddAuthority( $auth2, undef, $authtype1 );
83
84     # Add two biblio records
85     my $biblio1 = MARC::Record->new;
86     $biblio1->append_fields( MARC::Field->new( '609', '0', '0', '9' => $authid1, 'a' => 'George Orwell' ));
87     my ( $biblionumber1 ) = AddBiblio($biblio1, '');
88     my $biblio2 = MARC::Record->new;
89     $biblio2->append_fields( MARC::Field->new( '609', '0', '0', '9' => $authid2, 'a' => 'G. Orwell' ));
90     my ( $biblionumber2 ) = AddBiblio($biblio2, '');
91
92     # Time to merge
93     @linkedrecords = ( $biblionumber1, $biblionumber2 );
94     my $rv = C4::AuthoritiesMarc::merge({ mergefrom => $authid2, MARCfrom => $auth2, mergeto => $authid1, MARCto => $auth1 });
95     is( $rv, 1, 'We expect one biblio record (out of two) to be updated' );
96
97     # Check the results
98     my $newbiblio1 = GetMarcBiblio({ biblionumber => $biblionumber1 });
99     compare_fields( $biblio1, $newbiblio1, {}, 'count' );
100     compare_fields( $biblio1, $newbiblio1, {}, 'order' );
101     is( $newbiblio1->subfield('609', '9'), $authid1, 'Check biblio1 609$9' );
102     is( $newbiblio1->subfield('609', 'a'), 'George Orwell',
103         'Check biblio1 609$a' );
104     my $newbiblio2 = GetMarcBiblio({ biblionumber => $biblionumber2 });
105     compare_fields( $biblio2, $newbiblio2, {}, 'count' );
106     compare_fields( $biblio2, $newbiblio2, {}, 'order' );
107     is( $newbiblio2->subfield('609', '9'), $authid1, 'Check biblio2 609$9' );
108     is( $newbiblio2->subfield('609', 'a'), 'George Orwell',
109         'Check biblio2 609$a' );
110 };
111
112 subtest 'Test merge A1 to modified A1, test strict mode' => sub {
113 # Tests originate from bug 11700
114     plan tests => 12;
115
116     # Simulate modifying an authority from auth1old to auth1new
117     my $auth1old = MARC::Record->new;
118     $auth1old->append_fields( MARC::Field->new( '109', '0', '0', 'a' => 'Bruce Wayne' ));
119     my $auth1new = $auth1old->clone;
120     $auth1new->field('109')->update( a => 'Batman' );
121     my $authid1 = AddAuthority( $auth1new, undef, $authtype1 );
122
123     # Add two biblio records
124     my $MARC1 = MARC::Record->new;
125     $MARC1->append_fields( MARC::Field->new( '109', '', '', 'a' => 'Bruce Wayne', 'b' => '2014', '9' => $authid1 ));
126     $MARC1->append_fields( MARC::Field->new( '245', '', '', 'a' => 'From the depths' ));
127     $MARC1->append_fields( MARC::Field->new( '609', '', '', 'a' => 'Bruce Lee', 'b' => 'Should be cleared too', '9' => $authid1 ));
128     $MARC1->append_fields( MARC::Field->new( '609', '', '', 'a' => 'Bruce Lee', 'c' => 'This is a duplicate to be removed in strict mode', '9' => $authid1 ));
129     my $MARC2 = MARC::Record->new;
130     $MARC2->append_fields( MARC::Field->new( '109', '', '', 'a' => 'Batman', '9' => $authid1 ));
131     $MARC2->append_fields( MARC::Field->new( '245', '', '', 'a' => 'All the way to heaven' ));
132     my ( $biblionumber1 ) = AddBiblio( $MARC1, '');
133     my ( $biblionumber2 ) = AddBiblio( $MARC2, '');
134
135     # Time to merge in loose mode first
136     @linkedrecords = ( $biblionumber1, $biblionumber2 );
137     t::lib::Mocks::mock_preference('AuthorityMergeMode', 'loose');
138     my $rv = C4::AuthoritiesMarc::merge({ mergefrom => $authid1, MARCfrom => $auth1old, mergeto => $authid1, MARCto => $auth1new });
139     is( $rv, 2, 'Both records are updated now' );
140
141     #Check the results
142     my $biblio1 = GetMarcBiblio({ biblionumber => $biblionumber1 });
143     compare_fields( $MARC1, $biblio1, {}, 'count' );
144     compare_fields( $MARC1, $biblio1, {}, 'order' );
145     is( $auth1new->field(109)->subfield('a'), $biblio1->field(109)->subfield('a'), 'Record1 values updated correctly' );
146     my $biblio2 = GetMarcBiblio({ biblionumber => $biblionumber2 });
147     compare_fields( $MARC2, $biblio2, {}, 'count' );
148     compare_fields( $MARC2, $biblio2, {}, 'order' );
149     is( $auth1new->field(109)->subfield('a'), $biblio2->field(109)->subfield('a'), 'Record2 values updated correctly' );
150     # This is only true in loose mode:
151     is( $biblio1->field(109)->subfield('b'), $MARC1->field(109)->subfield('b'), 'Subfield not overwritten in loose mode');
152
153     # Merge again in strict mode
154     t::lib::Mocks::mock_preference('AuthorityMergeMode', 'strict');
155     ModBiblio( $MARC1, $biblionumber1, '' );
156     @linkedrecords = ( $biblionumber1 );
157     $rv = C4::AuthoritiesMarc::merge({ mergefrom => $authid1, MARCfrom => $auth1old, mergeto => $authid1, MARCto => $auth1new });
158     $biblio1 = GetMarcBiblio({ biblionumber => $biblionumber1 });
159     is( $biblio1->field(109)->subfield('b'), undef, 'Subfield overwritten in strict mode' );
160     compare_fields( $MARC1, $biblio1, { 609 => 1 }, 'count' );
161     my @old609 = $MARC1->field('609');
162     my @new609 = $biblio1->field('609');
163     is( scalar @new609, @old609 - 1, 'strict mode should remove a duplicate 609' );
164     is( $biblio1->field(609)->subfields,
165         scalar($auth1new->field('109')->subfields) + 1,
166         'Check number of subfields in strict mode for the remaining 609' );
167         # Note: the +1 comes from the added subfield $9 in the biblio
168 };
169
170 subtest 'Test merge A1 to B1 (changing authtype)' => sub {
171 # Tests were aimed for bug 9988, moved to 17909 in adjusted form
172 # Would not encourage this type of merge, but we should test what we offer
173     plan tests => 13;
174
175     # Get back to loose mode now
176     t::lib::Mocks::mock_preference('AuthorityMergeMode', 'loose');
177
178     # create two auth recs of different type
179     my $auth1 = MARC::Record->new;
180     $auth1->append_fields( MARC::Field->new( '109', '0', '0', 'a' => 'George Orwell', b => 'bb' ));
181     my $authid1 = AddAuthority( $auth1, undef, $authtype1 );
182     my $auth2 = MARC::Record->new;
183     $auth2->append_fields( MARC::Field->new( '112', '0', '0', 'a' => 'Batman', c => 'cc' ));
184     my $authid2 = AddAuthority($auth1, undef, $authtype2 );
185
186     # create a biblio with one 109 and two 609s to be touched
187     # seems exceptional see bug 13760 comment10
188     my $marc = MARC::Record->new;
189     $marc->append_fields(
190         MARC::Field->new( '003', 'some_003' ),
191         MARC::Field->new( '109', '', '', a => 'G. Orwell', b => 'bb', d => 'd', 9 => $authid1 ),
192         MARC::Field->new( '245', '', '', a => 'My title' ),
193         MARC::Field->new( '609', '', '', a => 'Orwell', 9 => "$authid1" ),
194         MARC::Field->new( '609', '', '', a => 'Orwell', x => 'xx', 9 => "$authid1" ),
195         MARC::Field->new( '611', '', '', a => 'Added for testing order' ),
196         MARC::Field->new( '612', '', '', a => 'unrelated', 9 => 'other' ),
197     );
198     my ( $biblionumber ) = C4::Biblio::AddBiblio( $marc, '' );
199     my $oldbiblio = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
200
201     # Time to merge
202     @linkedrecords = ( $biblionumber );
203     my $retval = C4::AuthoritiesMarc::merge({ mergefrom => $authid1, MARCfrom => $auth1, mergeto => $authid2, MARCto => $auth2 });
204     is( $retval, 1, 'We touched only one biblio' );
205
206     # Get new marc record for compares
207     my $newbiblio = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
208     compare_fields( $oldbiblio, $newbiblio, {}, 'count' );
209     # Exclude 109/609 and 112/612 in comparing order
210     my $excl = { '109' => 1, '112' => 1, '609' => 1, '612' => 1 };
211     compare_fields( $oldbiblio, $newbiblio, $excl, 'order' );
212     # Check position of 612s in the new record
213     my $full_order = join q/,/, map { $_->tag } $newbiblio->fields;
214     is( $full_order =~ /611(,612){3}/, 1, 'Check position of all 612s' );
215
216     # Check some fields
217     is( $newbiblio->field('003')->data,
218         $oldbiblio->field('003')->data,
219         'Check contents of a control field not expected to be touched' );
220     is( $newbiblio->subfield( '245', 'a' ),
221         $oldbiblio->subfield( '245', 'a' ),
222         'Check contents of a data field not expected to be touched' );
223     is( $newbiblio->subfield( '112', 'a' ),
224         $auth2->subfield( '112', 'a' ), 'Check modified 112a' );
225     is( $newbiblio->subfield( '112', 'c' ),
226         $auth2->subfield( '112', 'c' ), 'Check new 112c' );
227
228     # Check 112b; this subfield was cleared when moving from 109 to 112
229     # Note that this fix only applies to the current loose mode only
230     is( $newbiblio->subfield( '112', 'b' ), undef,
231         'Merge respects a cleared subfield in loose mode' );
232
233     # Check the original 612
234     is( ( $newbiblio->field('612') )[0]->subfield( 'a' ),
235         $oldbiblio->subfield( '612', 'a' ), 'Check untouched 612a' );
236     # Check second 612
237     is( ( $newbiblio->field('612') )[1]->subfield( 'a' ),
238         $auth2->subfield( '112', 'a' ), 'Check second touched 612a' );
239     # Check second new 612ax (in LOOSE mode)
240     is( ( $newbiblio->field('612') )[2]->subfield( 'a' ),
241         $auth2->subfield( '112', 'a' ), 'Check touched 612a' );
242     is( ( $newbiblio->field('612') )[2]->subfield( 'x' ),
243         ( $oldbiblio->field('609') )[1]->subfield('x'),
244         'Check 612x' );
245 };
246
247 subtest 'Merging authorities should handle deletes (BZ 18070)' => sub {
248     plan tests => 2;
249
250     # Add authority and linked biblio, delete authority
251     my $auth1 = MARC::Record->new;
252     $auth1->append_fields( MARC::Field->new( '109', '', '', 'a' => 'DEL'));
253     my $authid1 = AddAuthority( $auth1, undef, $authtype1 );
254     my $bib1 = MARC::Record->new;
255     $bib1->append_fields(
256         MARC::Field->new( '245', '', '', a => 'test DEL' ),
257         MARC::Field->new( '609', '', '', a => 'DEL', 9 => "$authid1" ),
258     );
259     my ( $biblionumber ) = C4::Biblio::AddBiblio( $bib1, '' );
260     @linkedrecords = ( $biblionumber );
261     DelAuthority({ authid => $authid1 }); # this triggers a merge call
262
263     # See what happened in the biblio record
264     my $marc1 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
265     is( $marc1->field('609'), undef, 'Field 609 should be gone too' );
266
267     # Now we simulate the delete as done in the cron job
268     # First, restore auth1 and add 609 back in bib1
269     $auth1 = MARC::Record->new;
270     $auth1->append_fields( MARC::Field->new( '109', '', '', 'a' => 'DEL'));
271     $authid1 = AddAuthority( $auth1, undef, $authtype1 );
272     $marc1->append_fields(
273         MARC::Field->new( '609', '', '', a => 'DEL', 9 => "$authid1" ),
274     );
275     ModBiblio( $marc1, $biblionumber, '' );
276     # Instead of going through DelAuthority, we manually delete the auth
277     # record and call merge afterwards.
278     # This mimics deleting an authority and calling merge later in the
279     # merge cron job.
280     # We use the biblionumbers parameter here and unmock linked_biblionumbers.
281     C4::Context->dbh->do( "DELETE FROM auth_header WHERE authid=?", undef, $authid1 );
282     @linkedrecords = ();
283     $mocks->{auth_mod}->unmock_all;
284     merge({ mergefrom => $authid1, biblionumbers => [ $biblionumber ] });
285     # Final check
286     $marc1 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
287     is( $marc1->field('609'), undef, 'Merge removed the 609 again even after deleting the authority record' );
288 };
289
290 subtest "Test some specific postponed merge issues" => sub {
291     plan tests => 6;
292
293     my $authmarc = MARC::Record->new;
294     $authmarc->append_fields( MARC::Field->new( '109', '', '', 'a' => 'aa', b => 'bb' ));
295     my $oldauthmarc = MARC::Record->new;
296     $oldauthmarc->append_fields( MARC::Field->new( '112', '', '', c => 'cc' ));
297     my $id = AddAuthority( $authmarc, undef, $authtype1 );
298     my $biblio = MARC::Record->new;
299     $biblio->append_fields(
300         MARC::Field->new( '109', '', '', a => 'a1', 9 => $id ),
301         MARC::Field->new( '612', '', '', a => 'a2', c => 'cc', 9 => $id+1 ),
302         MARC::Field->new( '612', '', '', a => 'a3', 9 => $id+2 ),
303     );
304     my ( $biblionumber ) = C4::Biblio::AddBiblio( $biblio, '' );
305
306     # Merge A to B postponed, A is deleted (simulated by id+1)
307     # This proves the !authtypefrom condition in sub merge
308     # Additionally, we test clearing subfield
309     merge({ mergefrom => $id + 1, MARCfrom => $oldauthmarc, mergeto => $id, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
310     $biblio = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
311     is( $biblio->subfield('609', '9'), $id, '612 moved to 609' );
312     is( $biblio->subfield('609', 'c'), undef, '609c cleared correctly' );
313
314     # Merge A to B postponed, delete B immediately (hits B < hits A)
315     # This proves the !@record_to test in sub merge
316     merge({ mergefrom => $id + 2, mergeto => $id + 1, MARCto => undef, biblionumbers => [ $biblionumber ] });
317     $biblio = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
318     is( $biblio->field('612'), undef, 'Last 612 must be gone' );
319
320     # Show that we 'need' skip_merge; this example is far-fetched.
321     # We *prove* by contradiction.
322     # Suppose: Merge A to B postponed, and delete A would merge rightaway.
323     # (You would need some special race condition with merge.pl to do so.)
324     # The modify merge would be useless after that.
325     @linkedrecords = ( $biblionumber );
326     my $restored_mocks = set_mocks();
327     DelAuthority({ authid => $id, skip_merge => 1 }); # delete A
328     $restored_mocks->{auth_mod}->unmock_all;
329     $biblio = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
330     is( $biblio->subfield('109', '9'), $id, 'If the 109 is no longer present, another modify merge would not bring it back' );
331
332     # Bug 22437 now removes older postponed A->A merges in DelAuthority
333     $id = AddAuthority( $authmarc, undef, $authtype1 );
334     my $merge = Koha::Authority::MergeRequest->new({ authid => $id })->store;
335     DelAuthority({ authid => $id, skip_merge => 1 });
336     $merge->discard_changes;
337     is( $merge->in_storage, 0, 'Older merge should be removed' );
338     # And even if we don't pass skip_merge
339     $id = AddAuthority( $authmarc, undef, $authtype1 );
340     $merge = Koha::Authority::MergeRequest->new({ authid => $id })->store;
341     DelAuthority({ authid => $id });
342     $merge->discard_changes;
343     is( $merge->in_storage, 0, 'Older merge should be removed again' );
344 };
345
346 subtest "Graceful resolution of missing reporting tag" => sub {
347     plan tests => 2;
348
349     # Simulate merge with authority in Default fw without reporting tag
350     # We expect data loss in biblio, but we keep $a and the reference in $9
351     # in order to allow a future merge to restore data.
352
353     # Accomplish the above by clearing reporting tag in $authtype2
354     my $fw2 = Koha::Authority::Types->find( $authtype2 );
355     $fw2->auth_tag_to_report('')->store;
356
357     my $authmarc = MARC::Record->new;
358     $authmarc->append_fields( MARC::Field->new( '109', '', '', 'a' => 'aa', b => 'bb' ));
359     my $id1 = AddAuthority( $authmarc, undef, $authtype1 );
360     my $id2 = AddAuthority( $authmarc, undef, $authtype2 );
361
362     my $biblio = MARC::Record->new;
363     $biblio->append_fields(
364         MARC::Field->new( '609', '', '', a => 'aa', 9 => $id1 ),
365     );
366     my ( $biblionumber ) = C4::Biblio::AddBiblio( $biblio, '' );
367
368     # Merge
369     merge({ mergefrom => $id1, MARCfrom => $authmarc, mergeto => $id2, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
370     $biblio = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
371     is( $biblio->subfield('612', '9'), $id2, 'id2 saved in $9' );
372     is( $biblio->subfield('612', 'a'), ' ', 'Kept an empty $a too' );
373
374     $fw2->auth_tag_to_report('112')->store;
375 };
376
377 subtest 'merge should not reorder too much' => sub {
378     plan tests => 2;
379
380     # Back to loose mode
381     t::lib::Mocks::mock_preference('AuthorityMergeMode', 'loose');
382
383     my $authmarc = MARC::Record->new;
384     $authmarc->append_fields( MARC::Field->new( '109', '', '', 'a' => 'aa', b => 'bb' ));
385     my $id = AddAuthority( $authmarc, undef, $authtype1 );
386     my $biblio = MARC::Record->new;
387     $biblio->append_fields(
388         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
389         MARC::Field->new( '609', '', '', i => 'in front', a => 'aa', c => 'after controlled block', 9 => $id ), # here $9 is already the last one
390     );
391     my ( $biblionumber ) = C4::Biblio::AddBiblio( $biblio, '' );
392
393     # Merge 109 and 609 and check order of subfields
394     merge({ mergefrom => $id, MARCfrom => $authmarc, mergeto => $id, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
395     my $biblio2 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
396     my $subfields = [ map { $_->[0] } $biblio2->field('109')->subfields ];
397     is_deeply( $subfields, [ 'i', 'a', 'b', 'c', '9' ], 'Merge only moved $9' );
398     $subfields = [ map { $_->[0] } $biblio2->field('609')->subfields ];
399     is_deeply( $subfields, [ 'i', 'a', 'b', 'c', '9' ], 'Order kept' );
400 };
401
402 subtest 'Test how merge handles controlled indicators' => sub {
403     plan tests => 4;
404
405     # Note: See more detailed tests in t/Koha/Authority/ControlledIndicators.t
406
407     # Testing MARC21 because thesaurus code makes it more interesting
408     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
409     t::lib::Mocks::mock_preference('AuthorityControlledIndicators', q|marc21,*,ind1:auth1,ind2:thesaurus|);
410
411     my $authmarc = MARC::Record->new;
412     $authmarc->append_fields(
413         MARC::Field->new( '008', (' 'x11).'r' ), # thesaurus code
414         MARC::Field->new( '109', '7', '', 'a' => 'a' ),
415     );
416     my $id = AddAuthority( $authmarc, undef, $authtype1 );
417     my $biblio = MARC::Record->new;
418     $biblio->append_fields(
419         MARC::Field->new( '609', '8', '4', a => 'a', 2 => '2', 9 => $id ),
420     );
421     my ( $biblionumber ) = C4::Biblio::AddBiblio( $biblio, '' );
422
423     # Merge and check indicators and $2
424     merge({ mergefrom => $id, MARCfrom => $authmarc, mergeto => $id, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
425     my $biblio2 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
426     is( $biblio2->field('609')->indicator(1), '7', 'Indicator1 OK' );
427     is( $biblio2->field('609')->indicator(2), '7', 'Indicator2 OK' );
428     is( $biblio2->subfield('609', '2'), 'aat', 'Subfield $2 OK' );
429
430     # Test $2 removal now
431     $authmarc->field('008')->update( (' 'x11).'a' ); # LOC, no $2 needed
432     AddAuthority( $authmarc, $id, $authtype1 ); # modify
433     merge({ mergefrom => $id, MARCfrom => $authmarc, mergeto => $id, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
434     $biblio2 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
435     is( $biblio2->subfield('609', '2'), undef, 'No subfield $2 left' );
436 };
437
438 sub set_mocks {
439     # After we removed the Zebra code from merge, we only need to mock
440     # get_usage_count and linked_biblionumbers here.
441
442     my $mocks;
443     $mocks->{auth_mod} = Test::MockModule->new( 'Koha::Authorities' );
444     $mocks->{auth_mod}->mock( 'get_usage_count', sub {
445          return scalar @linkedrecords;
446     });
447     $mocks->{auth_mod}->mock( 'linked_biblionumbers', sub {
448          return @linkedrecords;
449     });
450     return $mocks;
451 }
452
453 sub modify_framework {
454     my $builder = t::lib::TestBuilder->new;
455
456     # create two auth types
457     my $authtype1 = $builder->build({
458         source => 'AuthType',
459         value  => {
460             auth_tag_to_report => '109',
461         },
462     });
463     my $authtype2 = $builder->build({
464         source => 'AuthType',
465         value  => {
466             auth_tag_to_report => '112',
467         },
468     });
469
470     # Link 109/609 to the first authtype
471     $builder->build({
472         source => 'MarcSubfieldStructure',
473         value  => {
474             tagfield => '109',
475             tagsubfield => 'a',
476             authtypecode => $authtype1->{authtypecode},
477             frameworkcode => '',
478         },
479     });
480     $builder->build({
481         source => 'MarcSubfieldStructure',
482         value  => {
483             tagfield => '609',
484             tagsubfield => 'a',
485             authtypecode => $authtype1->{authtypecode},
486             frameworkcode => '',
487         },
488     });
489
490     # Link 112/612 to the second authtype
491     $builder->build({
492         source => 'MarcSubfieldStructure',
493         value  => {
494             tagfield => '112',
495             tagsubfield => 'a',
496             authtypecode => $authtype2->{authtypecode},
497             frameworkcode => '',
498         },
499     });
500     $builder->build({
501         source => 'MarcSubfieldStructure',
502         value  => {
503             tagfield => '612',
504             tagsubfield => 'a',
505             authtypecode => $authtype2->{authtypecode},
506             frameworkcode => '',
507         },
508     });
509
510     return ( $authtype1->{authtypecode}, $authtype2->{authtypecode} );
511 }
512
513 sub compare_fields { # mode parameter: order or count
514     my ( $oldmarc, $newmarc, $exclude, $mode ) = @_;
515     $exclude //= {};
516     if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
517         # By default exclude field 100 from comparison in UNIMARC.
518         # Will have been added by ModBiblio in merge.
519         $exclude->{100} = 1;
520     }
521     my @oldfields = map { $exclude->{$_->tag} ? () : $_->tag } $oldmarc->fields;
522     my @newfields = map { $exclude->{$_->tag} ? () : $_->tag } $newmarc->fields;
523
524     if( $mode eq 'count' ) {
525         my $t;
526         is( scalar @newfields, $t = @oldfields, "Number of fields still equal to $t" );
527     } elsif( $mode eq 'order' ) {
528         is( ( join q/,/, @newfields ), ( join q/,/, @oldfields ), 'Order of fields unchanged' );
529     }
530 }
531
532 $schema->storage->txn_rollback;