SimilarSections compares inserted sections (not global instances) with at most 200 editable library section entries by structure fingerprint, read lazily once per request, only while library IDs may be named (P13). lit/library-insert is not asked. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
123 lines
4.0 KiB
PHP
123 lines
4.0 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\Library\SimilarSections;
|
|
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,
|
|
?SimilarSections $similar = 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,
|
|
$similar
|
|
);
|
|
}
|
|
}
|