Files
WP-LIT-MCP/tests/Support/DiviWriterFixture.php
T
janniclas.loosenandClaude Opus 5.5 b1dd35abd8 feat: Divi tools edit Theme Builder layouts; visible layouts need confirm
TB layouts load through ThemeBuilderAccess (P12), live layouts (attached +
enabled, Q8) need a dry run and confirm (Q9) and get the shared restore point
of VisibleEdits, extracted from GlobalEdits (Q2: backup when revisions are
off, P9 refusals, P14 naming). divi/post-content only in body layouts (F22),
also for nested stored nodes. SaveScope::ThemeBuilderLayout sets Divi flags
and clears site CSS + TB cache.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 15:45:07 +02:00

120 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Support;
use LIT\MCP\Integrations\Divi\Design\DesignSystemReader;
use LIT\MCP\Integrations\Divi\Library\GlobalEdits;
use LIT\MCP\Integrations\Divi\Pages\DiviComposition;
use LIT\MCP\Integrations\Divi\Pages\DiviPageLoader;
use LIT\MCP\Integrations\Divi\Pages\DiviSaver;
use LIT\MCP\Integrations\Divi\Pages\DiviWriter;
use LIT\MCP\Integrations\Divi\Pages\DynamicContentMap;
use LIT\MCP\Integrations\Divi\Schema\DiviModules;
use LIT\MCP\Integrations\Divi\ThemeBuilder\ThemeBuilderEdits;
use Mockery;
use Throwable;
use WP_Post;
/**
* The Divi write stack of the page tests: DiviModules and DesignSystemReader serve the sample
* catalog (builder 5.2.1) and design system, DiviComposition uses ComposeFixture's sanitizer and
* media, and the saver records what it saves instead of touching the database.
*/
final class DiviWriterFixture
{
public static function modules(): DiviModules
{
$modules = Mockery::mock(DiviModules::class);
$modules->allows('catalog')->andReturn(DiviFixtures::catalog());
$modules->allows('builderVersion')->andReturn('5.2.1');
return $modules;
}
public static function designReader(): DesignSystemReader
{
$design = Mockery::mock(DesignSystemReader::class);
$design->allows('read')->andReturn(DiviFixtures::designSystem());
return $design;
}
/**
* $design is the reader the writer gets too (one instance, like the live wiring); without
* $registry the composition uses DynamicContentMap's default (VariableRef's built-in map in
* unit tests, where Divi's classes are missing).
*/
public static function composition(
DesignSystemReader $design,
bool $unfiltered = false,
?DynamicContentMap $registry = null
): DiviComposition {
return new DiviComposition(
self::modules(),
$design,
ComposeFixture::sanitizer($unfiltered),
ComposeFixture::media(),
$registry ?? new DynamicContentMap()
);
}
/**
* A DiviSaver whose save() appends the content to $saved, puts it on the post and reports
* revision 811. Each call first takes the next entry of $failures and throws it, if any.
*
* @param list<string> $saved
* @param list<Throwable> $failures
*/
public static function saver(array &$saved, array &$failures = []): DiviSaver
{
$saver = Mockery::mock(DiviSaver::class);
$saver->allows('save')->andReturnUsing(static function (WP_Post $post, string $content) use (&$saved, &$failures): array {
$failure = array_shift($failures);
if ($failure !== null) {
throw $failure;
}
$saved[] = $content;
$post->post_content = $content;
return ['revision_id' => 811, 'warnings' => []];
});
return $saver;
}
/** A ChangeLog that accepts records (log ids from 4711). */
public static function log(): FakeChangeLog
{
return new FakeChangeLog(4711);
}
/**
* The writer over the page of DiviFixtures::givenPost(), saving into $saved (see saver()).
*
* @param list<string> $saved
* @param list<Throwable> $failures
*/
public static function writer(
array &$saved,
array &$failures = [],
bool $unfiltered = false,
?DiviPageLoader $loader = null,
?GlobalEdits $globals = null,
?ThemeBuilderEdits $themeBuilder = null
): DiviWriter {
$design = self::designReader();
return new DiviWriter(
$loader ?? new DiviPageLoader(DiviFixtures::schema()),
self::saver($saved, $failures),
self::log(),
$design,
self::composition($design, $unfiltered),
$globals,
$themeBuilder
);
}
}