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