Bug 20434: Update UNIMARC framework - authorised values
[koha.git] / t / db_dependent / Authority / Merge.t
blob3634250cb42016c08cf57f9d116fad06877a43f9
1 #!/usr/bin/perl
3 # Tests for C4::AuthoritiesMarc::merge
5 use Modern::Perl;
7 use Test::More tests => 10;
9 use Getopt::Long;
10 use MARC::Record;
11 use Test::MockModule;
13 use t::lib::Mocks;
14 use t::lib::TestBuilder;
16 use C4::Biblio;
17 use Koha::Authorities;
18 use Koha::Authority::ControlledIndicators;
19 use Koha::Authority::MergeRequests;
20 use Koha::Database;
22 BEGIN {
23 use_ok('C4::AuthoritiesMarc');
26 # Optionally change marc flavour
27 my $marcflavour;
28 GetOptions( 'flavour:s' => \$marcflavour );
29 t::lib::Mocks::mock_preference( 'marcflavour', $marcflavour ) if $marcflavour;
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
34 # Global variables, mocking and framework modifications
35 our @linkedrecords;
36 my $mocks = set_mocks();
37 our ( $authtype1, $authtype2 ) = modify_framework();
39 subtest 'Test postponed merge feature' => sub {
40 plan tests => 6;
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' );
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' );
65 # Set merge limit high enough for the other subtests
66 t::lib::Mocks::mock_preference('AuthorityMergeLimit', 1000);
69 subtest 'Test merge A1 to A2 (within same authtype)' => sub {
70 # Tests originate from bug 11700
71 plan tests => 9;
73 # Start in loose mode, although it actually does not matter here
74 t::lib::Mocks::mock_preference('AuthorityMergeMode', 'loose');
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 );
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, '');
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' );
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' );
112 subtest 'Test merge A1 to modified A1, test strict mode' => sub {
113 # Tests originate from bug 11700
114 plan tests => 12;
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 );
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, '');
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' );
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');
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
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;
175 # Get back to loose mode now
176 t::lib::Mocks::mock_preference('AuthorityMergeMode', 'loose');
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 );
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' ),
198 my ( $biblionumber ) = C4::Biblio::AddBiblio( $marc, '' );
199 my $oldbiblio = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
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' );
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' );
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' );
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' );
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' );
247 subtest 'Merging authorities should handle deletes (BZ 18070)' => sub {
248 plan tests => 2;
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" ),
259 my ( $biblionumber ) = C4::Biblio::AddBiblio( $bib1, '' );
260 @linkedrecords = ( $biblionumber );
261 DelAuthority({ authid => $authid1 }); # this triggers a merge call
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' );
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" ),
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' );
290 subtest "Test some specific postponed merge issues" => sub {
291 plan tests => 4;
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 ),
304 my ( $biblionumber ) = C4::Biblio::AddBiblio( $biblio, '' );
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' );
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' );
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' );
333 subtest "Graceful resolution of missing reporting tag" => sub {
334 plan tests => 2;
336 # Simulate merge with authority in Default fw without reporting tag
337 # We expect data loss in biblio, but we keep $a and the reference in $9
338 # in order to allow a future merge to restore data.
340 # Accomplish the above by clearing reporting tag in $authtype2
341 my $fw2 = Koha::Authority::Types->find( $authtype2 );
342 $fw2->auth_tag_to_report('')->store;
344 my $authmarc = MARC::Record->new;
345 $authmarc->append_fields( MARC::Field->new( '109', '', '', 'a' => 'aa', b => 'bb' ));
346 my $id1 = AddAuthority( $authmarc, undef, $authtype1 );
347 my $id2 = AddAuthority( $authmarc, undef, $authtype2 );
349 my $biblio = MARC::Record->new;
350 $biblio->append_fields(
351 MARC::Field->new( '609', '', '', a => 'aa', 9 => $id1 ),
353 my ( $biblionumber ) = C4::Biblio::AddBiblio( $biblio, '' );
355 # Merge
356 merge({ mergefrom => $id1, MARCfrom => $authmarc, mergeto => $id2, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
357 $biblio = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
358 is( $biblio->subfield('612', '9'), $id2, 'id2 saved in $9' );
359 is( $biblio->subfield('612', 'a'), ' ', 'Kept an empty $a too' );
361 $fw2->auth_tag_to_report('112')->store;
364 subtest 'merge should not reorder too much' => sub {
365 plan tests => 2;
367 # Back to loose mode
368 t::lib::Mocks::mock_preference('AuthorityMergeMode', 'loose');
370 my $authmarc = MARC::Record->new;
371 $authmarc->append_fields( MARC::Field->new( '109', '', '', 'a' => 'aa', b => 'bb' ));
372 my $id = AddAuthority( $authmarc, undef, $authtype1 );
373 my $biblio = MARC::Record->new;
374 $biblio->append_fields(
375 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
376 MARC::Field->new( '609', '', '', i => 'in front', a => 'aa', c => 'after controlled block', 9 => $id ), # here $9 is already the last one
378 my ( $biblionumber ) = C4::Biblio::AddBiblio( $biblio, '' );
380 # Merge 109 and 609 and check order of subfields
381 merge({ mergefrom => $id, MARCfrom => $authmarc, mergeto => $id, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
382 my $biblio2 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
383 my $subfields = [ map { $_->[0] } $biblio2->field('109')->subfields ];
384 is_deeply( $subfields, [ 'i', 'a', 'b', 'c', '9' ], 'Merge only moved $9' );
385 $subfields = [ map { $_->[0] } $biblio2->field('609')->subfields ];
386 is_deeply( $subfields, [ 'i', 'a', 'b', 'c', '9' ], 'Order kept' );
389 subtest 'Test how merge handles controlled indicators' => sub {
390 plan tests => 4;
392 # Note: See more detailed tests in t/Koha/Authority/ControlledIndicators.t
394 # Testing MARC21 because thesaurus code makes it more interesting
395 t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
396 t::lib::Mocks::mock_preference('AuthorityControlledIndicators', q|marc21,*,ind1:auth1,ind2:thesaurus|);
398 my $authmarc = MARC::Record->new;
399 $authmarc->append_fields(
400 MARC::Field->new( '008', (' 'x11).'r' ), # thesaurus code
401 MARC::Field->new( '109', '7', '', 'a' => 'a' ),
403 my $id = AddAuthority( $authmarc, undef, $authtype1 );
404 my $biblio = MARC::Record->new;
405 $biblio->append_fields(
406 MARC::Field->new( '609', '8', '4', a => 'a', 2 => '2', 9 => $id ),
408 my ( $biblionumber ) = C4::Biblio::AddBiblio( $biblio, '' );
410 # Merge and check indicators and $2
411 merge({ mergefrom => $id, MARCfrom => $authmarc, mergeto => $id, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
412 my $biblio2 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
413 is( $biblio2->field('609')->indicator(1), '7', 'Indicator1 OK' );
414 is( $biblio2->field('609')->indicator(2), '7', 'Indicator2 OK' );
415 is( $biblio2->subfield('609', '2'), 'aat', 'Subfield $2 OK' );
417 # Test $2 removal now
418 $authmarc->field('008')->update( (' 'x11).'a' ); # LOC, no $2 needed
419 AddAuthority( $authmarc, $id, $authtype1 ); # modify
420 merge({ mergefrom => $id, MARCfrom => $authmarc, mergeto => $id, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
421 $biblio2 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
422 is( $biblio2->subfield('609', '2'), undef, 'No subfield $2 left' );
425 sub set_mocks {
426 # After we removed the Zebra code from merge, we only need to mock
427 # get_usage_count and linked_biblionumbers here.
429 my $mocks;
430 $mocks->{auth_mod} = Test::MockModule->new( 'Koha::Authorities' );
431 $mocks->{auth_mod}->mock( 'get_usage_count', sub {
432 return scalar @linkedrecords;
434 $mocks->{auth_mod}->mock( 'linked_biblionumbers', sub {
435 return @linkedrecords;
437 return $mocks;
440 sub modify_framework {
441 my $builder = t::lib::TestBuilder->new;
443 # create two auth types
444 my $authtype1 = $builder->build({
445 source => 'AuthType',
446 value => {
447 auth_tag_to_report => '109',
450 my $authtype2 = $builder->build({
451 source => 'AuthType',
452 value => {
453 auth_tag_to_report => '112',
457 # Link 109/609 to the first authtype
458 $builder->build({
459 source => 'MarcSubfieldStructure',
460 value => {
461 tagfield => '109',
462 tagsubfield => 'a',
463 authtypecode => $authtype1->{authtypecode},
464 frameworkcode => '',
467 $builder->build({
468 source => 'MarcSubfieldStructure',
469 value => {
470 tagfield => '609',
471 tagsubfield => 'a',
472 authtypecode => $authtype1->{authtypecode},
473 frameworkcode => '',
477 # Link 112/612 to the second authtype
478 $builder->build({
479 source => 'MarcSubfieldStructure',
480 value => {
481 tagfield => '112',
482 tagsubfield => 'a',
483 authtypecode => $authtype2->{authtypecode},
484 frameworkcode => '',
487 $builder->build({
488 source => 'MarcSubfieldStructure',
489 value => {
490 tagfield => '612',
491 tagsubfield => 'a',
492 authtypecode => $authtype2->{authtypecode},
493 frameworkcode => '',
497 return ( $authtype1->{authtypecode}, $authtype2->{authtypecode} );
500 sub compare_fields { # mode parameter: order or count
501 my ( $oldmarc, $newmarc, $exclude, $mode ) = @_;
502 $exclude //= {};
503 if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
504 # By default exclude field 100 from comparison in UNIMARC.
505 # Will have been added by ModBiblio in merge.
506 $exclude->{100} = 1;
508 my @oldfields = map { $exclude->{$_->tag} ? () : $_->tag } $oldmarc->fields;
509 my @newfields = map { $exclude->{$_->tag} ? () : $_->tag } $newmarc->fields;
511 if( $mode eq 'count' ) {
512 my $t;
513 is( scalar @newfields, $t = @oldfields, "Number of fields still equal to $t" );
514 } elsif( $mode eq 'order' ) {
515 is( ( join q/,/, @newfields ), ( join q/,/, @oldfields ), 'Order of fields unchanged' );
519 $schema->storage->txn_rollback;