*/ private array $specs = []; /** @var list */ private array $entryContents = []; /** @var list Page contents DiviWriter saved. */ private array $saved = []; private FakeChangeLog $log; protected function setUp(): void { parent::setUp(); $this->page = LibraryFixtures::page(DiviFixtures::markup('stage3/page--globale-instanzen.html')); LibraryFixtures::given([$this->page]); Functions\when('wp_revisions_enabled')->justReturn(true); $this->log = DiviWriterFixture::log(); } /** * @param list $deniedRoles * @param list $pageFailures */ private function ability( array $deniedRoles = [], bool $diviWritable = true, bool $unfiltered = false, array $pageFailures = [], ?DiviPageLoader $loader = null, ?DiviWriter $writer = null ): LibraryMakeGlobalAbility { $store = LibraryFixtures::store(); $store->allows('create')->andReturnUsing(function (LibraryItemSpec $spec): int { $this->specs[] = $spec; return 2300; }); $saver = Mockery::mock(DiviSaver::class); $saver->allows('saveNew')->andReturnUsing(function (int $id, string $content, SaveScope $scope): array { $this->entryContents[] = [$id, $content, $scope]; return []; }); $access = LibraryFixtures::access($deniedRoles, true, $store); return new LibraryMakeGlobalAbility( $access, $writer ?? DiviWriterFixture::writer( $this->saved, $pageFailures, $unfiltered, $loader ?? LibraryFixtures::loader($access) ), new NewLibraryEntry($access, $saver, DiviWriterFixture::modules()), $this->log, static fn (): bool => $diviWritable ); } /** @return array */ private function input(array $extra = []): array { return array_merge( ['post_id' => 7, 'tree_rev' => Rev::of((string) $this->page->post_content), 'id' => 's1', 'title' => 'Leistungen'], $extra ); } public function testReplacesTheNodeByAnInstanceOfTheNewGlobalEntry(): void { $result = $this->ability()->execute($this->input()); self::assertTrue($result['ok']); self::assertSame(2300, $result['library_id']); self::assertTrue($this->specs[0]->global); self::assertSame( str_replace("\r\n", "\n", DiviFixtures::markup('stage3/library--library-abschnitt.html')), $this->entryContents[0][1], 'the entry holds exactly the node, framed like the builder' ); self::assertCount(1, $this->saved); self::assertStringContainsString( '' . '' . '' ); LibraryFixtures::given([$this->page]); $result = $this->ability()->execute($this->input(['id' => 's0'])); self::assertToolError($result, 'validation_failed'); self::assertStringContainsString('Code-Modul', implode(' ', self::errorFields($result))); self::assertSame([], $this->specs); self::assertSame([], $this->saved); } /** All or nothing: the page save failing removes the new entry again. */ public function testFailedPageSaveDeletesTheNewEntry(): void { Functions\expect('wp_delete_post')->once()->with(2300, true)->andReturn(new WP_Post(['ID' => 2300])); try { $this->ability([], true, false, [WriteFailed::rolledBack('Die Divi-Seite konnte nicht gespeichert werden.')]) ->execute($this->input()); self::fail('no WriteFailed'); } catch (WriteFailed $e) { self::assertTrue($e->rolledBack); } self::assertCount(1, $this->specs); } public function testRefusesNodesThatAreOrHoldGlobalModules(): void { self::assertToolError($this->ability()->execute($this->input(['id' => 'g0'])), 'validation_failed'); self::assertToolError($this->ability()->execute($this->input(['id' => 's0'])), 'validation_failed'); self::assertSame([], $this->specs); } public function testNeedsSaveGlobalAndInsertRightsAndTheDiviGate(): void { self::assertToolError($this->ability(['edit_global_library'])->execute($this->input()), 'forbidden'); self::assertToolError($this->ability(['add_library'])->execute($this->input()), 'forbidden'); self::assertToolError($this->ability([], false)->execute($this->input()), 'unsupported'); self::assertToolError($this->ability()->execute($this->input(['tree_rev' => 'alt'])), 'rev_mismatch'); self::assertSame([], $this->specs); self::assertFalse($this->ability()->isDirect()); } public function testLogsTheNewEntry(): void { $this->ability()->execute($this->input()); self::assertSame('et_pb_layout', $this->log->records[0]['object_type']); self::assertSame('2300', $this->log->records[0]['object_id']); self::assertStringContainsString('global', $this->log->records[0]['summary']); } /** * Ruling Q7: a Divi 4 shortcode in a text value renders another entry (or, casting to 0, the newest * layout – the entry being created); such a node never becomes a global entry. */ public function testRefusesAShortcodeReferenceInsideAValue(): void { $quote = chr(92) . '"'; foreach (['1058', 'abc'] as $ref) { $this->page = LibraryFixtures::page( '' . '' . '' ); LibraryFixtures::given([$this->page]); $result = $this->ability([], true, true)->execute($this->input(['id' => 's0'])); self::assertToolError($result, 'validation_failed'); self::assertStringContainsString('global_module', implode(' ', self::errorFields($result))); } self::assertSame([], $this->specs); self::assertSame([], $this->saved); } public function testLibraryEntriesAndThemeBuilderLayoutsAreNoSource(): void { $item = LibraryFixtures::item(1055); LibraryFixtures::given([$item]); $fromLibrary = $this->ability()->execute( ['post_id' => 1055, 'tree_rev' => Rev::of((string) $item->post_content), 'id' => 's0', 'title' => 'x'] ); self::assertToolError($fromLibrary, 'validation_failed'); self::assertArrayHasKey('post_id', self::errorFields($fromLibrary)); $body = ThemeBuilderFixtures::layout(ThemeBuilderFixtures::BODY); LibraryFixtures::given([$body]); Functions\when('admin_url')->alias(static fn (string $path): string => 'https://kunde.test/wp-admin/' . $path); $loader = new DiviPageLoader( DiviFixtures::schema(), new Parser(), LibraryFixtures::access(), ThemeBuilderFixtures::access([], true, ThemeBuilderFixtures::store([ThemeBuilderFixtures::template()])) ); $fromLayout = $this->ability([], true, false, [], $loader)->execute( ['post_id' => 1062, 'tree_rev' => Rev::of((string) $body->post_content), 'id' => 's0', 'title' => 'x'] ); self::assertToolError($fromLayout, 'validation_failed'); self::assertStringContainsString('Theme-Builder', $fromLayout->get_error_message()); self::assertSame([], $this->specs); self::assertSame([], $this->saved); } /** A page write that may have been saved keeps the entry – the page may already show it. */ public function testAPartialPageWriteKeepsTheEntry(): void { Functions\expect('wp_delete_post')->never(); $failures = [WriteFailed::partial('Die Divi-Seite konnte nicht gespeichert werden.', postId: 7)]; $design = DiviWriterFixture::designReader(); $writer = new DiviWriter( LibraryFixtures::loader(), DiviWriterFixture::saver($this->saved, $failures), FakeChangeLog::any(), $design, DiviWriterFixture::composition($design) ); try { $this->ability([], true, false, [], null, $writer)->execute($this->input()); self::fail('no WriteFailed'); } catch (WriteFailed $e) { self::assertFalse($e->rolledBack); self::assertSame(2300, $e->details['library_id']); } } }