From 7608be6558758de7b3a3f758056dfed6e68459a4 Mon Sep 17 00:00:00 2001 From: Jan-Niclas Loosen Date: Mon, 19 Oct 2026 14:40:36 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20ColorRestorePlanner=20baut=20den=20?= =?UTF-8?q?Diff=20einmal=20aus=20ColorDiff;=20die=20Zusammenfassung=20z?= =?UTF-8?q?=C3=A4hlt=20Entfernungen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- .../Divi/Design/ColorRestorePlanner.php | 23 ++++++++----------- .../Divi/Design/ColorRestorePlannerTest.php | 13 ++++++++++- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Integrations/Divi/Design/ColorRestorePlanner.php b/src/Integrations/Divi/Design/ColorRestorePlanner.php index 6b9a2ed..4207064 100644 --- a/src/Integrations/Divi/Design/ColorRestorePlanner.php +++ b/src/Integrations/Divi/Design/ColorRestorePlanner.php @@ -11,8 +11,8 @@ defined('ABSPATH') || exit; /** * Restores a snapshot of the global colors (ruling V10): the snapshot replaces every stored color - * (set_global_colors($data, false), ruling V4) – colors created after the snapshot are removed. ColorDiff knows no - * removal, so the planner adds those rows ("removed") itself and warns that pages keep their references. + * (set_global_colors($data, false), ruling V4) – colors created after the snapshot are removed. ColorDiff reports them + * as "removed" rows (listed last); the planner counts them in the summary and warns that pages keep their references. */ final class ColorRestorePlanner implements RestorePlanner { @@ -48,25 +48,22 @@ final class ColorRestorePlanner implements RestorePlanner { $before = ColorBook::fromDivi($current); $after = ColorBook::fromDivi($snapshot); - $kept = array_values(array_filter( - ColorDiff::between($before, $after), - static fn (array $row): bool => $after->entry($row['id']) !== null - )); - $removed = []; - foreach (array_keys(array_diff_key($before->data(), $after->data())) as $id) { - $removed[] = ['id' => (string) $id, 'name' => $before->label((string) $id), 'action' => 'removed']; - } + $diff = ColorDiff::between($before, $after); + $removed = array_values(array_filter($diff, static fn (array $row): bool => $row['action'] === 'removed')); + $diff = array_merge( + array_values(array_filter($diff, static fn (array $row): bool => $row['action'] !== 'removed')), + $removed + ); - $summary = sprintf('Snapshot %s wiederherstellen – %s', $snapshotId, ColorDiff::summary($kept)); + $summary = sprintf('Snapshot %s wiederherstellen – %s', $snapshotId, ColorDiff::summary($diff)); $warnings = []; if ($removed !== []) { - $summary .= sprintf(' Entfernt: %d.', count($removed)); $warnings[] = sprintf( self::REMOVED, implode(', ', array_map(static fn (array $row): string => '„' . $row['name'] . '“', $removed)) ); } - return new DesignPlan($after->data(), array_merge($kept, $removed), $summary, null, $warnings, true); + return new DesignPlan($after->data(), $diff, $summary, null, $warnings, true); } } diff --git a/tests/Unit/Integrations/Divi/Design/ColorRestorePlannerTest.php b/tests/Unit/Integrations/Divi/Design/ColorRestorePlannerTest.php index c4713eb..c554cc4 100644 --- a/tests/Unit/Integrations/Divi/Design/ColorRestorePlannerTest.php +++ b/tests/Unit/Integrations/Divi/Design/ColorRestorePlannerTest.php @@ -77,7 +77,7 @@ final class ColorRestorePlannerTest extends TestCase array_map(static fn (array $row): array => [$row['id'], $row['action']], $plan->diff) ); self::assertStringStartsWith('Snapshot ' . self::SNAPSHOT_ID . ' wiederherstellen – Farben: ', $plan->summary); - self::assertStringEndsWith(' Entfernt: 1.', $plan->summary); + self::assertStringEndsWith('Farben: 1 geändert, 1 gelöscht.', $plan->summary); self::assertSame( ['Farben, die es im Snapshot nicht gibt, entfallen („Akzent“); Seiten und Presets, die sie verwenden, ' . 'behalten den Verweis ohne Wert.'], @@ -85,6 +85,17 @@ final class ColorRestorePlannerTest extends TestCase ); } + public function testOnlyRemovalsAreCountedInTheSummary(): void + { + $current = self::snapshot() + ['gcid-akzent0001' => self::current()['gcid-akzent0001']]; + $plan = self::planner()->plan($current, self::snapshot(), self::SNAPSHOT_ID); + + self::assertInstanceOf(DesignPlan::class, $plan); + self::assertSame(['removed'], array_column($plan->diff, 'action')); + self::assertStringEndsWith('Farben: 1 gelöscht.', $plan->summary); + self::assertStringNotContainsString('keine Änderung', $plan->summary); + } + public function testTheSameColorsAreNoChange(): void { $plan = self::planner()->plan(self::snapshot(), self::snapshot(), self::SNAPSHOT_ID);