after_save hook, re-read, change log and ToolResult (rev, log_id, warnings) were repeated in nine post abilities; they now share one helper. Behaviour and warning texts are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
92 lines
3.4 KiB
PHP
92 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\WordPress;
|
|
|
|
use Brain\Monkey\Actions;
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\Abilities\Rev;
|
|
use LIT\MCP\ChangeLog\ChangeLog;
|
|
use LIT\MCP\Integrations\WordPress\PostChangeResult;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
use RuntimeException;
|
|
use WP_Post;
|
|
|
|
final class PostChangeResultTest extends TestCase
|
|
{
|
|
private string $errorLog = '';
|
|
private string|false $previousLog = false;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->errorLog = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
|
$this->previousLog = ini_set('error_log', $this->errorLog);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
ini_set('error_log', (string) $this->previousLog);
|
|
unlink($this->errorLog);
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function testRunsTheHookLogsAndBuildsTheResult(): void
|
|
{
|
|
$post = new WP_Post(['ID' => 5, 'post_title' => 'Team']);
|
|
Actions\expectDone('lit_mcp_after_save')->once()->with(5, 'lit/content-update', ['post_id' => 5]);
|
|
Functions\when('get_post')->justReturn($post);
|
|
$log = Mockery::mock(ChangeLog::class);
|
|
$log->expects('record')->with('lit/content-update', 'page', '5', 'geändert', 811)->andReturn(9);
|
|
|
|
$change = new PostChangeResult('lit/content-update', $log, 5, 'die Änderung', ['vorher']);
|
|
$change->afterSave(['post_id' => 5]);
|
|
$updated = $change->reread();
|
|
$change->log('page', 'geändert', 811);
|
|
$result = $change->result('Fertig.', $updated)->toArray();
|
|
|
|
self::assertSame($post, $updated);
|
|
self::assertSame('Fertig.', $result['summary']);
|
|
self::assertSame(Rev::ofPost($post), $result['rev']);
|
|
self::assertSame(9, $result['log_id']);
|
|
self::assertSame(['vorher'], $result['warnings']);
|
|
}
|
|
|
|
public function testFailuresAfterTheWriteBecomeWarnings(): void
|
|
{
|
|
Actions\expectDone('lit_mcp_after_save')->once()->whenHappen(static function (): void {
|
|
throw new RuntimeException('listener kaputt');
|
|
});
|
|
Functions\when('get_post')->justReturn(null);
|
|
$log = Mockery::mock(ChangeLog::class);
|
|
$log->expects('record')->andThrow(new RuntimeException('log kaputt'));
|
|
$fallback = new WP_Post(['ID' => 5]);
|
|
|
|
$change = new PostChangeResult('lit/post-publish', $log, 5);
|
|
$change->afterSave([], 'Veröffentlichen');
|
|
$updated = $change->reread($fallback);
|
|
$change->log('page', 'x');
|
|
$result = $change->result('ok', $updated)->toArray();
|
|
|
|
self::assertSame($fallback, $updated);
|
|
self::assertNull($result['log_id']);
|
|
self::assertSame([
|
|
'Eine Erweiterung ist nach dem Veröffentlichen auf einen Fehler gestoßen; die Änderung ist gespeichert.',
|
|
'Änderungsprotokoll konnte nicht geschrieben werden; die Änderung ist gespeichert.',
|
|
], $result['warnings']);
|
|
self::assertStringContainsString('listener kaputt', (string) file_get_contents($this->errorLog));
|
|
}
|
|
|
|
public function testWithoutAReadablePostTheRevIsNull(): void
|
|
{
|
|
Functions\when('get_post')->justReturn(null);
|
|
|
|
$change = new PostChangeResult('lit/content-create', Mockery::mock(ChangeLog::class), 5, 'der Entwurf');
|
|
|
|
self::assertNull($change->reread());
|
|
self::assertNull($change->result('ok', null)->toArray()['rev']);
|
|
}
|
|
}
|