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:
co-authored by
Claude Sonnet 5.5
parent
d092c05311
commit
7608be6558
@@ -11,8 +11,8 @@ defined('ABSPATH') || exit;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Restores a snapshot of the global colors (ruling V10): the snapshot replaces every stored color
|
* 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
|
* (set_global_colors($data, false), ruling V4) – colors created after the snapshot are removed. ColorDiff reports them
|
||||||
* removal, so the planner adds those rows ("removed") itself and warns that pages keep their references.
|
* as "removed" rows (listed last); the planner counts them in the summary and warns that pages keep their references.
|
||||||
*/
|
*/
|
||||||
final class ColorRestorePlanner implements RestorePlanner
|
final class ColorRestorePlanner implements RestorePlanner
|
||||||
{
|
{
|
||||||
@@ -48,25 +48,22 @@ final class ColorRestorePlanner implements RestorePlanner
|
|||||||
{
|
{
|
||||||
$before = ColorBook::fromDivi($current);
|
$before = ColorBook::fromDivi($current);
|
||||||
$after = ColorBook::fromDivi($snapshot);
|
$after = ColorBook::fromDivi($snapshot);
|
||||||
$kept = array_values(array_filter(
|
$diff = ColorDiff::between($before, $after);
|
||||||
ColorDiff::between($before, $after),
|
$removed = array_values(array_filter($diff, static fn (array $row): bool => $row['action'] === 'removed'));
|
||||||
static fn (array $row): bool => $after->entry($row['id']) !== null
|
$diff = array_merge(
|
||||||
));
|
array_values(array_filter($diff, static fn (array $row): bool => $row['action'] !== 'removed')),
|
||||||
$removed = [];
|
$removed
|
||||||
foreach (array_keys(array_diff_key($before->data(), $after->data())) as $id) {
|
);
|
||||||
$removed[] = ['id' => (string) $id, 'name' => $before->label((string) $id), 'action' => 'removed'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$summary = sprintf('Snapshot %s wiederherstellen – %s', $snapshotId, ColorDiff::summary($kept));
|
$summary = sprintf('Snapshot %s wiederherstellen – %s', $snapshotId, ColorDiff::summary($diff));
|
||||||
$warnings = [];
|
$warnings = [];
|
||||||
if ($removed !== []) {
|
if ($removed !== []) {
|
||||||
$summary .= sprintf(' Entfernt: %d.', count($removed));
|
|
||||||
$warnings[] = sprintf(
|
$warnings[] = sprintf(
|
||||||
self::REMOVED,
|
self::REMOVED,
|
||||||
implode(', ', array_map(static fn (array $row): string => '„' . $row['name'] . '“', $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)
|
array_map(static fn (array $row): array => [$row['id'], $row['action']], $plan->diff)
|
||||||
);
|
);
|
||||||
self::assertStringStartsWith('Snapshot ' . self::SNAPSHOT_ID . ' wiederherstellen – Farben: ', $plan->summary);
|
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(
|
self::assertSame(
|
||||||
['Farben, die es im Snapshot nicht gibt, entfallen („Akzent“); Seiten und Presets, die sie verwenden, '
|
['Farben, die es im Snapshot nicht gibt, entfallen („Akzent“); Seiten und Presets, die sie verwenden, '
|
||||||
. 'behalten den Verweis ohne Wert.'],
|
. '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
|
public function testTheSameColorsAreNoChange(): void
|
||||||
{
|
{
|
||||||
$plan = self::planner()->plan(self::snapshot(), self::snapshot(), self::SNAPSHOT_ID);
|
$plan = self::planner()->plan(self::snapshot(), self::snapshot(), self::SNAPSHOT_ID);
|
||||||
|
|||||||
Reference in New Issue
Block a user