- Header: h1 + logo in a wrapping flex row, hr.wp-header-end before the tabs, aria-current on the active tab; own CSS/JS enqueued only on this screen. - Verbindung: ordered "So verbindest du Claude" steps, copy button (core clipboard.js + wp.a11y.speak), Site-Health-style status list, German details. - Zugriff: roles as form-table fieldset, connections with site date format, empty-state row, confirm on revoke; ConnectionUsage records last use via WP_Application_Passwords::record_application_password_usage (once a day). - Tools: plain-language legend, counter as inline notice, groups as core postboxes (postbox.js toggles, per-user closed state, summary when closed), fixed column widths with new "Art" column, submit above and below. - Protokoll: site-timezone times with UTC tooltip, result icons, filters with labels in the list table nav, retention link to Limits. - Limits: short labels with units and descriptions; snapshot count hidden but preserved. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
266 lines
11 KiB
PHP
266 lines
11 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace LIT\MCP\Tests\Unit\Admin;
|
||
|
||
use Brain\Monkey\Functions;
|
||
use LIT\MCP\Admin\LogListTable;
|
||
use LIT\MCP\ChangeLog\ChangeLog;
|
||
use LIT\MCP\Tests\Unit\TestCase;
|
||
use Mockery;
|
||
use WP_Post;
|
||
use WP_Term;
|
||
|
||
final class LogListTableTest extends TestCase
|
||
{
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
Functions\stubEscapeFunctions();
|
||
Functions\stubTranslationFunctions();
|
||
// Harmless defaults so single_row()'s full column pass doesn't blow up in tests
|
||
// that only care about a subset of columns; overridden per test where needed.
|
||
Functions\when('get_userdata')->justReturn(false);
|
||
Functions\when('post_type_exists')->justReturn(false);
|
||
Functions\when('get_post')->justReturn(null);
|
||
Functions\when('get_term')->justReturn(null);
|
||
Functions\when('current_user_can')->justReturn(false);
|
||
Functions\when('admin_url')->alias(static fn (string $path = ''): string => 'https://kunde.de/wp-admin/' . $path);
|
||
}
|
||
|
||
protected function tearDown(): void
|
||
{
|
||
unset($_GET['paged']);
|
||
parent::tearDown();
|
||
}
|
||
|
||
public function testPrepareItemsQueriesTheCurrentPageAndSetsPagination(): void
|
||
{
|
||
$_GET['paged'] = '3';
|
||
$log = Mockery::mock(ChangeLog::class);
|
||
$log->expects('query')
|
||
->with(['ability' => 'lit/content-update', 'user_id' => 0], 50, 100)
|
||
->andReturn(['rows' => [['id' => '1']], 'total' => 120]);
|
||
|
||
$table = new LogListTable($log, ['ability' => 'lit/content-update', 'user_id' => 0]);
|
||
$table->prepare_items();
|
||
|
||
self::assertSame([['id' => '1']], $table->items);
|
||
self::assertSame(['total_items' => 120, 'per_page' => 50], $table->pagination_args());
|
||
}
|
||
|
||
public function testColumnDefaultEscapesUntrustedClientAndSummaryText(): void
|
||
{
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
$item = ['client' => 'Claude <script>alert(1)</script>', 'summary' => '<b>böse</b> Zusammenfassung'];
|
||
|
||
self::assertStringNotContainsString('<script>', $this->columnDefault($table, $item, 'client'));
|
||
self::assertStringContainsString('<script>', $this->columnDefault($table, $item, 'client'));
|
||
self::assertStringNotContainsString('<b>', $this->columnDefault($table, $item, 'summary'));
|
||
}
|
||
|
||
public function testColumnDefaultShowsUserDisplayNameOrFallback(): void
|
||
{
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
Functions\when('get_userdata')->justReturn((object) ['display_name' => 'Jan <b>']);
|
||
|
||
$withUser = $this->columnDefault($table, ['user_id' => 3], 'user');
|
||
self::assertStringContainsString('Jan <b>', $withUser);
|
||
}
|
||
|
||
public function testColumnDefaultUserFallsBackToIdWhenUserIsGone(): void
|
||
{
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
Functions\when('get_userdata')->justReturn(false);
|
||
|
||
self::assertSame('#7', $this->columnDefault($table, ['user_id' => 7], 'user'));
|
||
}
|
||
|
||
public function testColumnDefaultLinksTheObjectForAKnownPostType(): void
|
||
{
|
||
Functions\when('post_type_exists')->justReturn(true);
|
||
Functions\when('get_edit_post_link')->justReturn('https://kunde.de/wp-admin/post.php?post=12&action=edit');
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
$html = $this->columnDefault($table, ['object_type' => 'page', 'object_id' => '12'], 'object');
|
||
|
||
self::assertStringContainsString('href="https://kunde.de/wp-admin/post.php?post=12&action=edit"', $html);
|
||
self::assertStringContainsString('page 12', $html);
|
||
}
|
||
|
||
public function testColumnDefaultShowsPlainLabelWhenGetEditPostLinkRefusesIt(): void
|
||
{
|
||
Functions\when('post_type_exists')->justReturn(true);
|
||
Functions\when('get_edit_post_link')->justReturn(null);
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
$html = $this->columnDefault($table, ['object_type' => 'page', 'object_id' => '12'], 'object');
|
||
|
||
self::assertSame('page 12', $html);
|
||
}
|
||
|
||
public function testColumnDefaultLinksAnAttachmentToTheMediaEditScreen(): void
|
||
{
|
||
Functions\when('post_type_exists')->justReturn(true);
|
||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 5]));
|
||
Functions\when('admin_url')->alias(static fn (string $path = ''): string => 'https://kunde.de/wp-admin/' . $path);
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
$html = $this->columnDefault($table, ['object_type' => 'attachment', 'object_id' => '5'], 'object');
|
||
|
||
self::assertStringContainsString('href="https://kunde.de/wp-admin/upload.php?item=5"', $html);
|
||
}
|
||
|
||
public function testColumnDefaultShowsPlainLabelForADeletedAttachment(): void
|
||
{
|
||
Functions\when('get_post')->justReturn(null);
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
self::assertSame('attachment 5', $this->columnDefault($table, ['object_type' => 'attachment', 'object_id' => '5'], 'object'));
|
||
}
|
||
|
||
public function testColumnDefaultLinksANavMenuToTheMenusScreen(): void
|
||
{
|
||
Functions\when('get_term')->justReturn(new WP_Term(['term_id' => 3, 'taxonomy' => 'nav_menu']));
|
||
Functions\when('admin_url')->alias(static fn (string $path = ''): string => 'https://kunde.de/wp-admin/' . $path);
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
$html = $this->columnDefault($table, ['object_type' => 'nav_menu', 'object_id' => '3'], 'object');
|
||
|
||
self::assertStringContainsString('href="https://kunde.de/wp-admin/nav-menus.php?menu=3"', $html);
|
||
}
|
||
|
||
/** @dataProvider unlinkableObjectTypes */
|
||
public function testColumnDefaultNeverLinksSettingsOrMenuLocations(string $type, string $id): void
|
||
{
|
||
Functions\when('post_type_exists')->justReturn(false);
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
self::assertSame(trim($type . ' ' . $id), $this->columnDefault($table, ['object_type' => $type, 'object_id' => $id], 'object'));
|
||
}
|
||
|
||
/** @return list<list<string>> */
|
||
public static function unlinkableObjectTypes(): array
|
||
{
|
||
return [
|
||
['option', 'blogname,blogdescription'],
|
||
['menu_location', 'primary'],
|
||
['unknown', '42'],
|
||
];
|
||
}
|
||
|
||
public function testRevisionColumnShowsADashWithoutARevision(): void
|
||
{
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
self::assertSame('–', $this->columnDefault($table, ['revision_id' => 0], 'revision'));
|
||
}
|
||
|
||
public function testRevisionColumnLinksOnlyWhenTheUserCanEditTheParentPost(): void
|
||
{
|
||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 811, 'post_type' => 'revision', 'post_parent' => 12]));
|
||
Functions\when('current_user_can')->justReturn(true);
|
||
Functions\when('admin_url')->alias(static fn (string $path = ''): string => 'https://kunde.de/wp-admin/' . $path);
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
$html = $this->columnDefault($table, ['revision_id' => 811], 'revision');
|
||
|
||
self::assertStringContainsString('href="https://kunde.de/wp-admin/revision.php?revision=811"', $html);
|
||
}
|
||
|
||
public function testRevisionColumnHidesTheLinkWithoutEditPermission(): void
|
||
{
|
||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 811, 'post_type' => 'revision', 'post_parent' => 12]));
|
||
Functions\when('current_user_can')->justReturn(false);
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
self::assertSame('–', $this->columnDefault($table, ['revision_id' => 811], 'revision'));
|
||
}
|
||
|
||
public function testSingleRowHighlightsPartialResults(): void
|
||
{
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
ob_start();
|
||
$table->single_row(['result' => 'partial']);
|
||
$partial = (string) ob_get_clean();
|
||
|
||
ob_start();
|
||
$table->single_row(['result' => 'ok']);
|
||
$ok = (string) ob_get_clean();
|
||
|
||
self::assertStringContainsString('<tr class="lit-mcp-partial">', $partial);
|
||
self::assertSame('<tr>', substr($ok, 0, 4));
|
||
}
|
||
|
||
public function testTimeColumnShowsSiteTimeInWordPressFormatWithUtcAsTitle(): void
|
||
{
|
||
Functions\when('get_option')->alias(static fn (string $key) => $key === 'date_format' ? 'd.m.Y' : 'H:i');
|
||
Functions\when('wp_date')->alias(static fn (string $format, int $ts): string => $format . '@' . $ts);
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
self::assertSame('Zeit', $table->get_columns()['created_at']);
|
||
$html = $this->column($table, ['created_at' => '2026-09-26 10:39:13'], 'created_at');
|
||
|
||
self::assertStringContainsString('d.m.Y H:i@' . gmmktime(10, 39, 13, 9, 26, 2026), $html);
|
||
self::assertStringContainsString('title="UTC: 2026-09-26 10:39:13"', $html);
|
||
self::assertSame('–', $this->column($table, ['created_at' => 'kaputt'], 'created_at'));
|
||
}
|
||
|
||
/** @dataProvider results */
|
||
public function testResultColumnShowsIconAndWord(string $result, string $icon, string $word): void
|
||
{
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0]);
|
||
|
||
$html = $this->column($table, ['result' => $result], 'result');
|
||
|
||
self::assertStringContainsString('dashicons-' . $icon, $html);
|
||
self::assertStringContainsString($word, $html);
|
||
}
|
||
|
||
/** @return list<list<string>> */
|
||
public static function results(): array
|
||
{
|
||
return [
|
||
['ok', 'yes-alt', 'erfolgreich'],
|
||
['partial', 'warning', 'teilweise'],
|
||
['error<b>', 'dismiss', 'error<b>'],
|
||
];
|
||
}
|
||
|
||
public function testToolColumnIsMutedTextAndTopNavRendersTheGivenFilters(): void
|
||
{
|
||
$table = new LogListTable(Mockery::mock(ChangeLog::class), ['ability' => '', 'user_id' => 0], static function (): void {
|
||
echo '<select name="ability"></select>';
|
||
});
|
||
|
||
self::assertSame(
|
||
'<span class="lit-mcp-tool-name">lit/x<</span>',
|
||
$this->column($table, ['ability' => 'lit/x<'], 'ability')
|
||
);
|
||
|
||
ob_start();
|
||
$table->display();
|
||
self::assertStringContainsString('<div class="alignleft actions"><select name="ability"></select></div>', (string) ob_get_clean());
|
||
}
|
||
|
||
/** @param array<string, mixed> $item */
|
||
private function column(LogListTable $table, array $item, string $column): string
|
||
{
|
||
$method = 'column_' . $column;
|
||
if (method_exists($table, $method)) {
|
||
return (string) (new \ReflectionMethod($table, $method))->invoke($table, $item);
|
||
}
|
||
|
||
return $this->columnDefault($table, $item, $column);
|
||
}
|
||
|
||
/** @param array<string, mixed> $item */
|
||
private function columnDefault(LogListTable $table, array $item, string $column): string
|
||
{
|
||
return (string) (new \ReflectionMethod($table, 'column_default'))->invoke($table, $item, $column);
|
||
}
|
||
}
|