feat: change log table with retention cron and limits option
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\ChangeLog;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Server\RequestContext;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
|
||||
final class ChangeLogTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
RequestContext::reset();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testRecordStoresReferencesAndReturnsTheId(): void
|
||||
{
|
||||
Functions\when('current_time')->justReturn('2026-09-25 08:00:00');
|
||||
Functions\when('get_current_user_id')->justReturn(3);
|
||||
RequestContext::reset(true, 'Claude');
|
||||
$db = Mockery::mock(\wpdb::class)->makePartial();
|
||||
$db->expects('insert')
|
||||
->with('wp_lit_mcp_log', Mockery::on(static function (array $row): bool {
|
||||
return $row['created_at'] === '2026-09-25 08:00:00'
|
||||
&& $row['user_id'] === 3
|
||||
&& $row['client'] === 'Claude'
|
||||
&& $row['ability'] === 'lit/content-update'
|
||||
&& $row['object_type'] === 'page'
|
||||
&& $row['object_id'] === '12'
|
||||
&& $row['summary'] === 'Seite geändert'
|
||||
&& $row['revision_id'] === 811
|
||||
&& $row['snapshot_id'] === null
|
||||
&& $row['result'] === 'ok';
|
||||
}), Mockery::type('array'))
|
||||
->andReturnUsing(static function () use ($db): int {
|
||||
$db->insert_id = 4711;
|
||||
return 1;
|
||||
});
|
||||
|
||||
$id = (new ChangeLog($db))->record('lit/content-update', 'page', '12', 'Seite geändert', 811);
|
||||
|
||||
self::assertSame(4711, $id);
|
||||
}
|
||||
|
||||
public function testRecordReturnsNullWhenTheInsertFails(): void
|
||||
{
|
||||
Functions\when('current_time')->justReturn('2026-09-25 08:00:00');
|
||||
Functions\when('get_current_user_id')->justReturn(3);
|
||||
$db = Mockery::mock(\wpdb::class)->makePartial();
|
||||
$db->allows('insert')->andReturn(false);
|
||||
$log = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
||||
$previous = ini_set('error_log', $log);
|
||||
|
||||
try {
|
||||
$id = (new ChangeLog($db))->record('lit/content-update', 'page', '12', 'x');
|
||||
} finally {
|
||||
ini_set('error_log', (string) $previous);
|
||||
}
|
||||
|
||||
self::assertNull($id);
|
||||
self::assertStringContainsString('change log insert failed', (string) file_get_contents($log));
|
||||
unlink($log);
|
||||
}
|
||||
|
||||
public function testPurgeDeletesRowsOlderThanTheRetention(): void
|
||||
{
|
||||
$db = Mockery::mock(\wpdb::class)->makePartial();
|
||||
$db->expects('query')
|
||||
->with(Mockery::on(static fn (string $sql): bool => str_starts_with(
|
||||
$sql,
|
||||
"DELETE FROM wp_lit_mcp_log WHERE created_at < '"
|
||||
)))
|
||||
->andReturn(3);
|
||||
|
||||
self::assertSame(3, (new ChangeLog($db))->purgeOlderThan(90));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user