feat: lit/divi-design-snapshots und lit/divi-design-restore (Bereich presets)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Jan-Niclas Loosen
2026-10-16 17:52:20 +02:00
co-authored by Claude Sonnet 5.5
parent 2e8f723b01
commit 327f8d2d35
6 changed files with 322 additions and 0 deletions
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Design;
use Brain\Monkey\Functions;
use LIT\MCP\Integrations\Divi\Design\DesignChange;
use LIT\MCP\Integrations\Divi\Design\DesignSystemWriter;
use LIT\MCP\Integrations\Divi\Design\DiviDesignRestoreAbility;
use LIT\MCP\Integrations\Divi\Design\SnapshotStore;
use LIT\MCP\Tests\Support\FakeChangeLog;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
final class DiviDesignRestoreAbilityTest extends TestCase
{
public function testRestoresWithDryRunAndASnapshotOfTheCurrentState(): void
{
Functions\when('current_user_can')->justReturn(true);
$older = PresetBookTest::stored();
unset($older['module']['divi/button']['items']['bbbbbbbbbb']);
$writer = Mockery::mock(DesignSystemWriter::class);
$writer->allows('presets')->andReturn(PresetBookTest::stored());
$writer->allows('canUseVisualBuilder')->andReturn(true);
$writer->expects('savePresets')->with(Mockery::type('array'), true)->andReturn($older);
$snapshots = Mockery::mock(SnapshotStore::class);
$snapshots->allows('get')->with('presets', 'presets-20261016T100000Z-aaaaaa')->andReturn(['id' => 'presets-20261016T100000Z-aaaaaa', 'data' => $older,
'created' => 'x', 'user_id' => 1, 'ability' => 'lit/divi-presets-set', 'log_id' => 2, 'size' => 1]);
$snapshots->expects('save')->with('presets', Mockery::type('array'), 'lit/divi-design-restore')->andReturn('presets-3');
$snapshots->expects('attachLog');
$ability = new DiviDesignRestoreAbility($writer, new DesignChange($snapshots, new FakeChangeLog(8)), $snapshots);
$dry = self::assertToolError($ability->execute(['snapshot_id' => 'presets-20261016T100000Z-aaaaaa']), 'confirm_required');
self::assertSame('removed', $dry['diff'][0]['action']);
$result = $ability->execute(['snapshot_id' => 'presets-20261016T100000Z-aaaaaa', 'confirm' => true, 'design_rev' => $dry['design_rev']]);
self::assertSame('presets-3', $result['snapshot_id']);
}
public function testUnknownSnapshotAndOtherAreas(): void
{
Functions\when('current_user_can')->justReturn(true);
$writer = Mockery::mock(DesignSystemWriter::class);
$writer->allows('canUseVisualBuilder')->andReturn(true);
$snapshots = Mockery::mock(SnapshotStore::class);
$snapshots->allows('get')->andReturn(null);
$ability = new DiviDesignRestoreAbility($writer, new DesignChange($snapshots, FakeChangeLog::forbidden()), $snapshots);
self::assertToolError($ability->execute(['snapshot_id' => 'presets-20261016T100000Z-abcdef']), 'not_found');
self::assertToolError($ability->execute(['snapshot_id' => 'colors-20261016T100000Z-abcdef']), 'unsupported');
self::assertToolError($ability->execute(['snapshot_id' => 'kaputt']), 'validation_failed');
}
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Design;
use Brain\Monkey\Functions;
use LIT\MCP\Integrations\Divi\Design\DiviDesignSnapshotsAbility;
use LIT\MCP\Integrations\Divi\Design\SnapshotStore;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
final class DiviDesignSnapshotsAbilityTest extends TestCase
{
public function testListsTheSnapshotsOfAnArea(): void
{
Functions\when('current_user_can')->alias(static fn (string $cap): bool => $cap === 'edit_theme_options');
$store = Mockery::mock(SnapshotStore::class);
$store->expects('list')->with('presets')->andReturn([
['id' => 'presets-2', 'created' => '2026-10-16T10:00:00+00:00', 'user_id' => 1,
'ability' => 'lit/divi-presets-set', 'log_id' => 4, 'size' => 120],
]);
$ability = new DiviDesignSnapshotsAbility($store);
self::assertTrue($ability->permission([]));
$result = $ability->execute([]);
self::assertSame('presets', $result['area']);
self::assertSame('presets-2', $result['snapshots'][0]['id']);
self::assertTrue($ability->annotations()['readonly']);
}
public function testUnknownAreaAndRights(): void
{
Functions\when('current_user_can')->justReturn(false);
$ability = new DiviDesignSnapshotsAbility(Mockery::mock(SnapshotStore::class));
self::assertToolError($ability->permission([]), 'forbidden');
Functions\when('current_user_can')->justReturn(true);
self::assertToolError($ability->execute(['area' => 'seiten']), 'validation_failed');
}
}