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 $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)); } }