refactor: ColorRestorePlanner baut den Diff einmal aus ColorDiff; die Zusammenfassung zählt Entfernungen

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Jan-Niclas Loosen
2026-10-19 14:40:36 +02:00
co-authored by Claude Sonnet 5.5
parent d092c05311
commit 7608be6558
2 changed files with 22 additions and 14 deletions
@@ -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);
}
}
@@ -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);