Files

123 lines
4.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Design;
use LIT\MCP\Integrations\Divi\Design\PageRewriteReport;
use PHPUnit\Framework\TestCase;
final class PageRewriteReportTest extends TestCase
{
/**
* A PageRewriter::preview() result: $pages listed pages (IDs from 12) with one module each.
*
* @param list<int> $blocked
* @return array<string, mixed>
*/
public static function preview(int $pages, array $blocked = []): array
{
$listed = [];
for ($i = 0; $i < $pages; ++$i) {
$listed[] = [
'post_id' => 12 + $i,
'title' => 'Seite ' . ($i + 1),
'type' => 'page',
'status' => 'publish',
'nodes' => [['id' => 's0.r0.c0.m0', 'module' => 'divi/button', 'label' => 'Button']],
'warnings' => [],
];
}
return [
'pages' => $listed,
'pages_count' => $pages,
'nodes_count' => $pages,
'skipped' => [],
'blocked_ids' => $blocked,
'hidden_count' => 0,
];
}
/**
* A PageRewriter::apply() result: $saved pages (IDs from 12) with two modules each.
*
* @param list<array{post_id: int, reason: string}> $skipped
* @return array<string, mixed>
*/
public static function applied(int $saved, int $remaining, array $skipped = [], int $hidden = 0): array
{
$rows = [];
for ($i = 0; $i < $saved; ++$i) {
$rows[] = ['post_id' => 12 + $i, 'revision_id' => 900 + $i, 'nodes' => 2];
}
return [
'saved' => $rows,
'remaining' => $remaining,
'warnings' => [],
'skipped' => $skipped,
'hidden_count' => $hidden,
];
}
public function testEmptyMeansNothingToWriteNothingBlockedNothingHidden(): void
{
self::assertTrue(PageRewriteReport::isEmpty(self::preview(0)));
self::assertFalse(PageRewriteReport::isEmpty(self::preview(1)));
self::assertFalse(PageRewriteReport::isEmpty(self::preview(0, [7])));
self::assertFalse(PageRewriteReport::isEmpty(['hidden_count' => 1] + self::preview(0)));
}
public function testDryRunMessageNamesCountsTheLimitAndBlockedPages(): void
{
$message = PageRewriteReport::dryRunMessage(self::preview(2), 'Zuweisung an divi/button');
self::assertStringStartsWith('Zuweisung an divi/button – betroffen (Seiten: 2, Module: 2).', $message);
self::assertStringContainsString('Nichts wurde gespeichert (Probelauf).', $message);
self::assertStringContainsString('confirm: true', $message);
self::assertStringContainsString('höchstens 50 Seiten', $message);
self::assertStringNotContainsString('blocked_ids', $message);
self::assertStringContainsString('blocked_ids', PageRewriteReport::dryRunMessage(self::preview(1, [7]), 'X'));
}
public function testSavedSummaryAsksForAnotherCallWhilePagesRemain(): void
{
self::assertSame(
'X – gespeichert (Seiten: 2, Module: 4). Noch 3 Seiten offen: mit denselben Angaben erneut mit '
. 'confirm: true aufrufen.',
PageRewriteReport::savedSummary(self::applied(2, 3), 'X')
);
self::assertSame(
'X – gespeichert (Seiten: 1, Module: 2). Alle Fundstellen sind erledigt.',
PageRewriteReport::savedSummary(self::applied(1, 0), 'X')
);
self::assertSame(
'X – gespeichert (Seiten: 0, Module: 0). Übersprungen: 1 (siehe skipped).',
PageRewriteReport::savedSummary(self::applied(0, 0, [['post_id' => 5, 'reason' => 'global']]), 'X')
);
}
public function testNothingToDoIsAToolResultWithTheSkippedPages(): void
{
$preview = ['skipped' => [['post_id' => 5, 'reason' => 'global']]] + self::preview(0);
$result = PageRewriteReport::nothingToDo('Nichts zu tun.', $preview, ['Hinweis']);
self::assertTrue($result['ok']);
self::assertSame('Nichts zu tun.', $result['summary']);
self::assertSame(['Hinweis'], $result['warnings']);
self::assertSame([[], 0], [$result['saved'], $result['remaining']]);
self::assertSame(5, $result['skipped'][0]['post_id']);
}
public function testOutputSchemaRequiresTheRewriteKeys(): void
{
$schema = PageRewriteReport::outputSchema(['diff' => ['type' => 'array']]);
self::assertSame(['saved', 'remaining', 'skipped'], array_slice($schema['required'], -3));
self::assertSame('integer', $schema['properties']['remaining']['type']);
self::assertSame(['type' => 'array'], $schema['properties']['diff']);
}
}