feat: write results warn about sections built like a library section (spec §5.5)

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>
This commit is contained in:
2026-09-27 21:16:16 +02:00
co-authored by Claude Opus 5.5
parent f82964f617
commit 4f70886006
6 changed files with 260 additions and 6 deletions
@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Library;
use LIT\MCP\Integrations\Divi\BlockTree\Block;
use LIT\MCP\Integrations\Divi\BlockTree\LibraryContent;
use LIT\MCP\Integrations\Divi\BlockTree\Parser;
use LIT\MCP\Integrations\Divi\Library\SimilarSections;
use LIT\MCP\Integrations\Divi\Pages\DiviChange;
use LIT\MCP\Tests\Support\DiviFixtures;
use LIT\MCP\Tests\Support\LibraryFixtures;
use LIT\MCP\Tests\Unit\TestCase;
final class SimilarSectionsTest extends TestCase
{
private static function sectionLike1055(): Block
{
$root = LibraryContent::roots(LibraryContent::load(
new Parser(),
DiviFixtures::markup('stage3/library--library-abschnitt.html')
))[0]->copy();
$blurb = $root->children()[0]->children()[0]->children()[0];
$attrs = $blurb->attrs();
$attrs['title']['innerContent']['desktop']['value'] = 'Neuer Titel';
$blurb->setAttrs($attrs);
return $root;
}
/** @param list<string> $deniedRoles */
private static function similar(array $deniedRoles = []): SimilarSections
{
LibraryFixtures::given([LibraryFixtures::item(1055), LibraryFixtures::item(1058)]);
$store = LibraryFixtures::store();
$store->allows('find')->andReturn([LibraryFixtures::item(1055), LibraryFixtures::item(1058)]);
return new SimilarSections(LibraryFixtures::access($deniedRoles, true, $store));
}
public function testWarnsForAnInsertedSectionBuiltLikeALibrarySection(): void
{
$change = (new DiviChange('1 Divi-Knoten eingefügt'))->record(self::sectionLike1055(), 'inserted');
$warnings = self::similar()->warnings($change);
self::assertCount(1, $warnings);
self::assertStringContainsString('„[Fixture] 1055“ (1055)', $warnings[0]);
self::assertStringContainsString('lit-library-insert', $warnings[0]);
self::assertStringContainsString('lit/library-find-similar', $warnings[0]);
}
public function testOnlyInsertedSectionsAndOnlyWithLibraryAccess(): void
{
$moved = (new DiviChange('verschoben'))->record(self::sectionLike1055(), 'moved');
$inserted = (new DiviChange('eingefügt'))->record(self::sectionLike1055(), 'inserted');
self::assertSame([], self::similar()->warnings($moved));
self::assertSame([], self::similar(['divi_library'])->warnings($inserted));
}
/** The library is read once per request, at most MAX_ITEMS section entries, only those the user may edit. */
public function testReadsTheLibraryOnceBoundedAndOnlyEditableEntries(): void
{
LibraryFixtures::given([LibraryFixtures::item(1055)]);
\Brain\Monkey\Functions\when('current_user_can')->alias(
static fn (string $cap, mixed ...$args): bool => ! ($cap === 'edit_post' && ($args[0] ?? 0) === 1055)
);
$store = LibraryFixtures::store();
$store->expects('find')->once()->with(['layout_type' => 'section'], SimilarSections::MAX_ITEMS - 1, 0)
->andReturn([LibraryFixtures::item(1055)]);
$similar = new SimilarSections(LibraryFixtures::access([], true, $store));
$change = (new DiviChange('eingefügt'))->record(self::sectionLike1055(), 'inserted');
self::assertSame([], $similar->warnings($change), 'entry 1055 is not editable');
self::assertSame([], $similar->warnings($change));
}
/** No section inserted: the library is not read at all (cheap on every write). */
public function testWithoutAnInsertedSectionTheLibraryIsNotRead(): void
{
LibraryFixtures::given([]);
$store = LibraryFixtures::store();
$store->expects('find')->never();
$similar = new SimilarSections(LibraryFixtures::access([], true, $store));
$row = self::sectionLike1055()->children()[0];
self::assertSame([], $similar->warnings((new DiviChange('eingefügt'))->record($row, 'inserted')));
self::assertSame([], $similar->warnings((new DiviChange('geändert'))->record(self::sectionLike1055(), 'updated')));
}
/** A global instance is already linked to the library – no warning, even for a global section entry. */
public function testGlobalInstancesDoNotWarn(): void
{
$instance = \LIT\MCP\Integrations\Divi\BlockTree\GlobalInstance::of(self::sectionLike1055(), 1055);
$change = (new DiviChange('eingefügt'))->record($instance, 'inserted');
self::assertSame([], self::similar()->warnings($change));
}
}