test: AssertsToolErrors helper for tool error assertions (T3)

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>
This commit is contained in:
2026-09-26 17:55:35 +02:00
co-authored by Claude Opus 5.5
parent 72eeaa46ad
commit 60290c85a5
50 changed files with 337 additions and 256 deletions
@@ -13,17 +13,17 @@ final class MediaGuardTest extends TestCase
{
public function testInvalidIdIsAValidationError(): void
{
self::assertSame('lit_validation_failed', MediaGuard::attachment('abc')->get_error_code());
self::assertSame('lit_validation_failed', MediaGuard::attachment(0)->get_error_code());
self::assertToolError(MediaGuard::attachment('abc'), 'validation_failed');
self::assertToolError(MediaGuard::attachment(0), 'validation_failed');
}
public function testMissingOrNonAttachmentIsNotFound(): void
{
Functions\when('get_post')->justReturn(null);
self::assertSame('lit_not_found', MediaGuard::attachment(33)->get_error_code());
self::assertToolError(MediaGuard::attachment(33), 'not_found');
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 33, 'post_type' => 'page']));
self::assertSame('lit_not_found', MediaGuard::attachment(33)->get_error_code());
self::assertToolError(MediaGuard::attachment(33), 'not_found');
}
public function testChecksTheRequestedCapabilityOnTheAttachment(): void
@@ -32,7 +32,7 @@ final class MediaGuardTest extends TestCase
Functions\when('get_post')->justReturn($attachment);
Functions\expect('current_user_can')->once()->with('delete_post', 33)->andReturn(false);
self::assertSame('lit_forbidden', MediaGuard::attachment('33', 'delete_post')->get_error_code());
self::assertToolError(MediaGuard::attachment('33', 'delete_post'), 'forbidden');
}
public function testRefusesTrashedAttachmentsUnlessAllowed(): void
@@ -42,7 +42,7 @@ final class MediaGuardTest extends TestCase
Functions\when('current_user_can')->justReturn(true);
$error = MediaGuard::attachment(33);
self::assertSame('lit_validation_failed', $error->get_error_code());
self::assertToolError($error, 'validation_failed');
self::assertStringContainsString('Papierkorb', $error->get_error_message());
self::assertSame($attachment, MediaGuard::attachment(33, 'read_post', true));
}