From 448299fd16d303f1bfe5d5baabace8fdff9852d7 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Mon, 6 Apr 2020 13:44:46 +0000
Subject: [PATCH] Bug 22437: Add test case to Merge.t
Content-Type: text/plain; charset=utf-8

The scenario outlined on Bugzilla is simulated and proves that we need
to remove a merge B->B when B is deleted during a merge.

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

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 t/db_dependent/Authority/Merge.t | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/Authority/Merge.t b/t/db_dependent/Authority/Merge.t
index 3634250cb4..838b87b31f 100755
--- a/t/db_dependent/Authority/Merge.t
+++ b/t/db_dependent/Authority/Merge.t
@@ -288,7 +288,7 @@ subtest 'Merging authorities should handle deletes (BZ 18070)' => sub {
 };
 
 subtest "Test some specific postponed merge issues" => sub {
-    plan tests => 4;
+    plan tests => 5;
 
     my $authmarc = MARC::Record->new;
     $authmarc->append_fields( MARC::Field->new( '109', '', '', 'a' => 'aa', b => 'bb' ));
@@ -328,6 +328,20 @@ subtest "Test some specific postponed merge issues" => sub {
     $restored_mocks->{auth_mod}->unmock_all;
     $biblio = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
     is( $biblio->subfield('109', '9'), $id, 'If the 109 is no longer present, another modify merge would not bring it back' );
+    # Bug 22437 however presented scenario C->B, B->A (B and A should have linkcount>limit)
+    # This results in: postponed merge B->B, run merge C->B, delete C, and postponed A->A, postponed B->A, delete B
+    # Showing what happens if merge.pl did not remove the B->B merge from the queue
+    t::lib::Mocks::mock_preference('AuthorityMergeLimit', 2);
+    @linkedrecords = ( 1, 2, 3 ); # these biblionumbers do not matter, count>=2
+    $id = AddAuthority( $authmarc, undef, $authtype1 ); # this is B
+    $biblio->delete_fields( $biblio->field('109') ); # cleanup
+    $biblio->append_fields( MARC::Field->new( '109', '', '', a => 'a', 9 => $id ) ); # a biblio connected to B
+    ModBiblio( $biblio, $biblionumber, '' );
+    DelAuthority({ authid => $id, skip_merge => 1 }); # delete B when doing B->A
+    merge({ mergefrom => $id, mergeto => $id, MARCto => undef, biblionumbers => [ $biblionumber ] }); # destructive B->B from cron
+    $biblio = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
+    my @fields = $biblio->field('109');
+    is( scalar @fields, 0, 'No field 109: the B,B merge was destructive, so needs to be removed.' );
 };
 
 subtest "Graceful resolution of missing reporting tag" => sub {
-- 
2.11.0