tests/Support/AssertsToolErrors (used by TestCase): assertToolError($result, code)
checks the WP_Error code lit_<code> and the code of the client JSON and returns the
payload; clientError() decodes the payload; errorFields() returns the fields and
asserts they exist. Replaces the private clientError() decoders of
DiviWriteAbilitiesTest/DiviReplaceTextAbilityTest, 216 single-line
assertSame('lit_…', …->get_error_code()) assertions, 24 get_error_data()['fields']
digs and two json_decode(message) reads. ErrorsTest keeps json_decode: it tests
the format itself. Tests 1404 -> 1409 (AssertsToolErrorsTest); no test has fewer
assertions.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
220 lines
8.8 KiB
PHP
220 lines
8.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\Media;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\ChangeLog\ChangeLog;
|
|
use LIT\MCP\Integrations\Media\MediaDeleteAbility;
|
|
use LIT\MCP\Integrations\Media\MediaUsageFinder;
|
|
use LIT\MCP\Tests\Support\FakeChangeLog;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
use RuntimeException;
|
|
use WP_Post;
|
|
|
|
final class MediaDeleteAbilityTest extends TestCase
|
|
{
|
|
private const USAGE = [['post_id' => 5, 'title' => 'Team', 'post_type' => 'page', 'where' => 'featured', 'certainty' => 'sicher']];
|
|
private const POSSIBLE = [['post_id' => 7, 'title' => 'Blog', 'post_type' => 'post', 'where' => 'content', 'certainty' => 'möglicherweise']];
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 33, 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_title' => 'Foto']));
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
Functions\when('get_post_meta')->justReturn('2026/09/foto.jpg');
|
|
}
|
|
|
|
private function ability(array $usages, ChangeLog $log, bool $trash = false): MediaDeleteAbility
|
|
{
|
|
$finder = Mockery::mock(MediaUsageFinder::class);
|
|
$finder->allows('usages')->with(33)->andReturn($usages);
|
|
|
|
return new MediaDeleteAbility($finder, $log, static fn (): bool => $trash);
|
|
}
|
|
|
|
public function testConfirmationIsRequiredAndMentionsUsages(): void
|
|
{
|
|
Functions\expect('wp_delete_attachment')->never();
|
|
|
|
$result = $this->ability(self::USAGE, FakeChangeLog::forbidden())->execute(['id' => 33]);
|
|
|
|
self::assertToolError($result, 'confirm_required');
|
|
self::assertStringContainsString('endgültig', $result->get_error_message());
|
|
self::assertStringContainsString('1 Stelle', $result->get_error_message());
|
|
self::assertSame(self::USAGE, $result->get_error_data()['usages']);
|
|
}
|
|
|
|
public function testConfirmationMentionsTheTrashWhenMediaTrashIsOn(): void
|
|
{
|
|
$result = $this->ability([], FakeChangeLog::forbidden(), true)->execute(['id' => 33, 'confirm' => false]);
|
|
|
|
self::assertToolError($result, 'confirm_required');
|
|
self::assertStringContainsString('Papierkorb', $result->get_error_message());
|
|
}
|
|
|
|
public function testUsedMediaNeedsForce(): void
|
|
{
|
|
Functions\expect('wp_delete_attachment')->never();
|
|
|
|
$result = $this->ability(self::USAGE, FakeChangeLog::forbidden())->execute(['id' => 33, 'confirm' => true]);
|
|
|
|
self::assertToolError($result, 'in_use');
|
|
self::assertSame(self::USAGE, $result->get_error_data()['usages']);
|
|
}
|
|
|
|
public function testPossibleUsageAlsoNeedsForce(): void
|
|
{
|
|
Functions\expect('wp_delete_attachment')->never();
|
|
|
|
$result = $this->ability(self::POSSIBLE, FakeChangeLog::forbidden())->execute(['id' => 33, 'confirm' => true]);
|
|
|
|
self::assertToolError($result, 'in_use');
|
|
}
|
|
|
|
public function testForceDeletesUsedMedia(): void
|
|
{
|
|
Functions\expect('wp_delete_attachment')->once()->with(33, false)->andReturn(new WP_Post(['ID' => 33]));
|
|
$log = new FakeChangeLog(50);
|
|
|
|
$result = $this->ability(self::USAGE, $log)->execute(['id' => 33, 'confirm' => true, 'force' => true]);
|
|
|
|
self::assertSame(['id' => '33', 'type' => 'attachment', 'action' => 'deleted', 'label' => 'Foto'], $result['changed'][0]);
|
|
self::assertSame(50, $result['log_id']);
|
|
self::assertSame(self::USAGE, $result['usages']);
|
|
$log->assertRecorded([
|
|
'ability' => 'lit/media-delete',
|
|
'object_type' => 'attachment',
|
|
'object_id' => '33',
|
|
'summary' => 'Medium "Foto" gelöscht',
|
|
]);
|
|
}
|
|
|
|
public function testUnusedMediaGoesToTheTrashWhenMediaTrashIsOn(): void
|
|
{
|
|
Functions\expect('wp_delete_attachment')->once()->with(33, false)->andReturn(new WP_Post(['ID' => 33]));
|
|
$log = new FakeChangeLog(51);
|
|
|
|
$result = $this->ability([], $log, true)->execute(['id' => 33, 'confirm' => true]);
|
|
|
|
self::assertSame('trashed', $result['changed'][0]['action']);
|
|
$log->assertRecorded([
|
|
'ability' => 'lit/media-delete',
|
|
'object_type' => 'attachment',
|
|
'object_id' => '33',
|
|
'summary' => 'Medium "Foto" in den Papierkorb gelegt',
|
|
]);
|
|
}
|
|
|
|
public function testRefusedDeleteWithTheAttachmentIntactIsAWriteFailure(): void
|
|
{
|
|
Functions\when('get_post_meta')->justReturn('2026/09/foto.jpg');
|
|
Functions\expect('wp_delete_attachment')->once()->andReturn(false);
|
|
|
|
$result = $this->ability([], FakeChangeLog::forbidden())->execute(['id' => 33, 'confirm' => true]);
|
|
|
|
self::assertToolError($result, 'write_failed');
|
|
}
|
|
|
|
public function testRefusedDeleteThatRemovedDataIsAPartialWrite(): void
|
|
{
|
|
$meta = '2026/09/foto.jpg';
|
|
Functions\when('get_post_meta')->alias(static function () use (&$meta): string {
|
|
return $meta;
|
|
});
|
|
Functions\expect('wp_delete_attachment')->once()->andReturnUsing(static function () use (&$meta) {
|
|
$meta = '';
|
|
|
|
return null;
|
|
});
|
|
$log = FakeChangeLog::partialsOnly();
|
|
|
|
$result = $this->ability([], $log)->execute(['id' => 33, 'confirm' => true]);
|
|
|
|
self::assertToolError($result, 'partial_write');
|
|
self::assertSame(33, $result->get_error_data()['attachment_id']);
|
|
$log->assertPartials(['ability' => 'lit/media-delete', 'object_type' => 'attachment', 'object_id' => '33']);
|
|
}
|
|
|
|
public function testADatabaseErrorInTheUsageCheckRefusesWithoutForce(): void
|
|
{
|
|
Functions\when('get_post_meta')->justReturn('2026/09/foto.jpg');
|
|
Functions\when('get_option')->justReturn(false);
|
|
Functions\when('get_theme_mod')->justReturn(false);
|
|
Functions\expect('wp_delete_attachment')->never();
|
|
/** @var \Mockery\MockInterface&\wpdb $db */
|
|
$db = Mockery::mock(\wpdb::class)->makePartial();
|
|
$db->allows('get_results')->andReturn(null);
|
|
$ability = new MediaDeleteAbility(
|
|
new MediaUsageFinder(static fn (): array => [], $db),
|
|
FakeChangeLog::forbidden(),
|
|
static fn (): bool => false
|
|
);
|
|
|
|
$file = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
|
$previous = ini_set('error_log', $file);
|
|
try {
|
|
$result = $ability->execute(['id' => 33, 'confirm' => true]);
|
|
} finally {
|
|
ini_set('error_log', (string) $previous);
|
|
unlink($file);
|
|
}
|
|
|
|
self::assertToolError($result, 'in_use');
|
|
self::assertSame(MediaUsageFinder::FAILED_TITLE, $result->get_error_data()['usages'][0]['title']);
|
|
}
|
|
|
|
public function testChangeLogFailureIsOnlyAWarning(): void
|
|
{
|
|
Functions\expect('wp_delete_attachment')->once()->andReturn(new WP_Post(['ID' => 33]));
|
|
$log = new FakeChangeLog(1, new RuntimeException('db down'));
|
|
|
|
$file = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
|
$previous = ini_set('error_log', $file);
|
|
try {
|
|
$result = $this->ability([], $log)->execute(['id' => 33, 'confirm' => true]);
|
|
} finally {
|
|
ini_set('error_log', (string) $previous);
|
|
unlink($file);
|
|
}
|
|
|
|
self::assertSame('deleted', $result['changed'][0]['action']);
|
|
self::assertNull($result['log_id']);
|
|
self::assertCount(1, $result['warnings']);
|
|
$log->assertRecorded([]);
|
|
}
|
|
|
|
public function testTrashedAttachmentIsRefused(): void
|
|
{
|
|
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 33, 'post_type' => 'attachment', 'post_status' => 'trash']));
|
|
Functions\expect('wp_delete_attachment')->never();
|
|
|
|
$ability = $this->ability([], FakeChangeLog::forbidden());
|
|
|
|
self::assertToolError($ability->permission(['id' => 33]), 'validation_failed');
|
|
self::assertToolError($ability->execute(['id' => 33, 'confirm' => true]), 'validation_failed');
|
|
}
|
|
|
|
public function testChecksDeletePostOnTheAttachment(): void
|
|
{
|
|
Functions\when('current_user_can')->alias(static fn (string $cap, int $id): bool => ! ($cap === 'delete_post' && $id === 33));
|
|
|
|
$result = $this->ability([], FakeChangeLog::forbidden())->permission(['id' => 33]);
|
|
|
|
self::assertToolError($result, 'forbidden');
|
|
}
|
|
|
|
public function testIsDestructiveAndDescribesWhatTheUsageCheckMisses(): void
|
|
{
|
|
$ability = $this->ability([], FakeChangeLog::forbidden());
|
|
|
|
self::assertSame(['readonly' => false, 'destructive' => true, 'idempotent' => false], $ability->annotations());
|
|
self::assertFalse($ability->isDirect());
|
|
foreach (['endgültig', 'Term-Meta', 'Benutzer-Meta', 'Pods', 'ACF', 'kommagetrennt', 'Optionen anderer Plugins'] as $needle) {
|
|
self::assertStringContainsString($needle, $ability->description());
|
|
}
|
|
}
|
|
}
|