feat: batch media metadata updates and delete with usage check
lit/media-update validates the whole batch first (edit_post, lengths, optional modified lock) and restores already written items when a write fails midway. lit/media-delete requires confirm, refuses used media without force and reports refused deletes as write_failed. 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,158 @@
|
||||
<?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\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);
|
||||
}
|
||||
|
||||
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, Mockery::mock(ChangeLog::class))->execute(['id' => 33]);
|
||||
|
||||
self::assertSame('lit_confirm_required', $result->get_error_code());
|
||||
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([], Mockery::mock(ChangeLog::class), true)->execute(['id' => 33, 'confirm' => false]);
|
||||
|
||||
self::assertSame('lit_confirm_required', $result->get_error_code());
|
||||
self::assertStringContainsString('Papierkorb', $result->get_error_message());
|
||||
}
|
||||
|
||||
public function testUsedMediaNeedsForce(): void
|
||||
{
|
||||
Functions\expect('wp_delete_attachment')->never();
|
||||
|
||||
$result = $this->ability(self::USAGE, Mockery::mock(ChangeLog::class))->execute(['id' => 33, 'confirm' => true]);
|
||||
|
||||
self::assertSame('lit_in_use', $result->get_error_code());
|
||||
self::assertSame(self::USAGE, $result->get_error_data()['usages']);
|
||||
}
|
||||
|
||||
public function testPossibleUsageAlsoNeedsForce(): void
|
||||
{
|
||||
Functions\expect('wp_delete_attachment')->never();
|
||||
|
||||
$result = $this->ability(self::POSSIBLE, Mockery::mock(ChangeLog::class))->execute(['id' => 33, 'confirm' => true]);
|
||||
|
||||
self::assertSame('lit_in_use', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testForceDeletesUsedMedia(): void
|
||||
{
|
||||
Functions\expect('wp_delete_attachment')->once()->with(33, false)->andReturn(new WP_Post(['ID' => 33]));
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/media-delete', 'attachment', '33', 'Medium "Foto" gelöscht')->andReturn(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']);
|
||||
}
|
||||
|
||||
public function testUnusedMediaGoesToTheTrashWhenMediaTrashIsOn(): void
|
||||
{
|
||||
Functions\expect('wp_delete_attachment')->once()->with(33, false)->andReturn(new WP_Post(['ID' => 33]));
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/media-delete', 'attachment', '33', 'Medium "Foto" in den Papierkorb gelegt')->andReturn(51);
|
||||
|
||||
$result = $this->ability([], $log, true)->execute(['id' => 33, 'confirm' => true]);
|
||||
|
||||
self::assertSame('trashed', $result['changed'][0]['action']);
|
||||
}
|
||||
|
||||
public function testRefusedDeleteIsAWriteFailure(): void
|
||||
{
|
||||
Functions\expect('wp_delete_attachment')->once()->andReturn(false);
|
||||
|
||||
$result = $this->ability([], Mockery::mock(ChangeLog::class))->execute(['id' => 33, 'confirm' => true]);
|
||||
|
||||
self::assertSame('lit_write_failed', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testChangeLogFailureIsOnlyAWarning(): void
|
||||
{
|
||||
Functions\expect('wp_delete_attachment')->once()->andReturn(new WP_Post(['ID' => 33]));
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->andThrow(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']);
|
||||
}
|
||||
|
||||
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([], Mockery::mock(ChangeLog::class));
|
||||
|
||||
self::assertSame('lit_validation_failed', $ability->permission(['id' => 33])->get_error_code());
|
||||
self::assertSame('lit_validation_failed', $ability->execute(['id' => 33, 'confirm' => true])->get_error_code());
|
||||
}
|
||||
|
||||
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([], Mockery::mock(ChangeLog::class))->permission(['id' => 33]);
|
||||
|
||||
self::assertSame('lit_forbidden', $result->get_error_code());
|
||||
}
|
||||
|
||||
public function testIsDestructiveAndDescribesWhatTheUsageCheckMisses(): void
|
||||
{
|
||||
$ability = $this->ability([], Mockery::mock(ChangeLog::class));
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user