diff --git a/core/lib/Drupal/Component/Diff/DiffOpOutputBuilder.php b/core/lib/Drupal/Component/Diff/DiffOpOutputBuilder.php index 72cc5805ae8a59d68ed31eafec0e937be507691c..41a850bb5dc1d5e32a7aec013827c142f2f74638 100644 --- a/core/lib/Drupal/Component/Diff/DiffOpOutputBuilder.php +++ b/core/lib/Drupal/Component/Diff/DiffOpOutputBuilder.php @@ -61,12 +61,8 @@ public function toOpsArray(array $diff): array { if (count($hunkTarget) === 0) { $ops[] = $this->hunkOp(Differ::REMOVED, $hunkSource, $hunkTarget); } - elseif (count($hunkSource) >= count($hunkTarget)) { - $ops[] = $this->hunkOp(self::CHANGED, $hunkSource, $hunkTarget); - } else { - $ops[] = $this->hunkOp(self::CHANGED, $hunkSource, array_slice($hunkTarget, 0, count($hunkSource))); - $ops[] = $this->hunkOp(Differ::ADDED, array_slice($hunkTarget, count($hunkSource)), []); + $ops[] = $this->hunkOp(self::CHANGED, $hunkSource, $hunkTarget); } $hunkMode = NULL; $hunkSource = []; diff --git a/core/tests/Drupal/Tests/Component/Diff/DiffOpOutputBuilderTest.php b/core/tests/Drupal/Tests/Component/Diff/DiffOpOutputBuilderTest.php index 725e178a4742c0bb59dcd534ede292ad1ab3a754..53ada909fec2e4ff45b159a390c53e893b9589a6 100644 --- a/core/tests/Drupal/Tests/Component/Diff/DiffOpOutputBuilderTest.php +++ b/core/tests/Drupal/Tests/Component/Diff/DiffOpOutputBuilderTest.php @@ -73,6 +73,16 @@ public function provideTestDiff(): array { ['aa', 'bb', 'cc', 'd'], ['a', 'c', 'd'], ], + 'copy-change-copy-change' => [ + [ + new DiffOpCopy(['a']), + new DiffOpChange(['bb'], ['b', 'c']), + new DiffOpCopy(['d']), + new DiffOpChange(['ee'], ['e']), + ], + ['a', 'bb', 'd', 'ee'], + ['a', 'b', 'c', 'd', 'e'], + ], ]; } @@ -111,12 +121,11 @@ public function testDiffInfiniteLoop(): void { $differ = new Differ($diffOpBuilder); $diff = $differ->diffToArray($from, $to); $diffOps = $diffOpBuilder->toOpsArray($diff); - $this->assertCount(5, $diffOps); + $this->assertCount(4, $diffOps); $this->assertEquals($diffOps[0], new DiffOpAdd([' - image.style.max_325x325'])); $this->assertEquals($diffOps[1], new DiffOpCopy([' - image.style.max_650x650'])); - $this->assertEquals($diffOps[2], new DiffOpChange([' - image.style.max_325x325'], ['_core:'])); - $this->assertEquals($diffOps[3], new DiffOpAdd([' default_config_hash: 3mjM9p-kQ8syzH7N8T0L9OnCJDSPvHAZoi3q6jcXJKM'])); - $this->assertEquals($diffOps[4], new DiffOpCopy(['fallback_image_style: max_325x325', ''])); + $this->assertEquals($diffOps[2], new DiffOpChange([' - image.style.max_325x325'], ['_core:', ' default_config_hash: 3mjM9p-kQ8syzH7N8T0L9OnCJDSPvHAZoi3q6jcXJKM'])); + $this->assertEquals($diffOps[3], new DiffOpCopy(['fallback_image_style: max_325x325', ''])); } } diff --git a/core/tests/Drupal/Tests/Component/Diff/Engine/DiffEngineTest.php b/core/tests/Drupal/Tests/Component/Diff/Engine/DiffEngineTest.php index 1020373e6ac69ad8f57c64f6025baa7061b2adc5..70265df1f6a557c0386dbdaed8af34ad766c3f2a 100644 --- a/core/tests/Drupal/Tests/Component/Diff/Engine/DiffEngineTest.php +++ b/core/tests/Drupal/Tests/Component/Diff/Engine/DiffEngineTest.php @@ -70,6 +70,24 @@ public function provideTestDiff() { ['a', 'b', 'd'], ['a'], ], + 'change-copy' => [ + [ + DiffOpChange::class, + DiffOpCopy::class, + ], + ['aa', 'bb', 'cc', 'd'], + ['a', 'c', 'd'], + ], + 'copy-change-copy-change' => [ + [ + DiffOpCopy::class, + DiffOpChange::class, + DiffOpCopy::class, + DiffOpChange::class, + ], + ['a', 'bb', 'd', 'ee'], + ['a', 'b', 'c', 'd', 'e'], + ], ]; }