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
@@ -230,7 +230,7 @@ final class PostClonerTest extends TestCase
self::fail('WriteFailed expected');
} catch (WriteFailed $e) {
self::assertTrue($e->rolledBack);
self::assertSame('lit_write_failed', $e->toError()->get_error_code());
self::assertToolError($e->toError(), 'write_failed');
}
}
@@ -71,7 +71,7 @@ final class PostDuplicateAbilityTest extends TestCase
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute(['post_id' => 5]);
self::assertSame('lit_forbidden', $result->get_error_code());
self::assertToolError($result, 'forbidden');
}
public function testATrashedOriginalIsRefused(): void
@@ -82,7 +82,7 @@ final class PostDuplicateAbilityTest extends TestCase
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute(['post_id' => 5]);
self::assertSame('lit_validation_failed', $result->get_error_code());
self::assertToolError($result, 'validation_failed');
}
public function testIsIndirectAndNotIdempotent(): void
@@ -95,7 +95,7 @@ final class PostPublishAbilityTest extends TestCase
self::assertFalse($e->rolledBack);
self::assertSame(5, $e->postId);
self::assertStringContainsString('save_post-Hook kaputt', $e->getMessage());
self::assertSame('lit_partial_write', $e->toError()->get_error_code());
self::assertToolError($e->toError(), 'partial_write');
}
$log->assertPartials(['ability' => 'lit/post-publish', 'object_type' => 'page', 'object_id' => '5']);
}
@@ -107,7 +107,7 @@ final class PostPublishAbilityTest extends TestCase
$result = $this->ability(FakeChangeLog::forbidden())->execute(['post_id' => 5]);
self::assertSame('lit_validation_failed', $result->get_error_code());
self::assertToolError($result, 'validation_failed');
}
public function testPermissionNeedsPublishRights(): void
@@ -115,6 +115,6 @@ final class PostPublishAbilityTest extends TestCase
$this->stubPost(['post_status' => 'draft']);
Functions\when('current_user_can')->alias(static fn (string $cap): bool => $cap !== 'publish_post');
self::assertSame('lit_forbidden', $this->ability(FakeChangeLog::forbidden())->permission(['post_id' => 5])->get_error_code());
self::assertToolError($this->ability(FakeChangeLog::forbidden())->permission(['post_id' => 5]), 'forbidden');
}
}
@@ -68,7 +68,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$result = $this->ability($cloner, FakeChangeLog::forbidden())
->execute(['original_id' => 5, 'copy_id' => 77, 'rev' => Rev::ofPost($this->original)]);
self::assertSame('lit_confirm_required', $result->get_error_code());
self::assertToolError($result, 'confirm_required');
}
public function testCopyMustStemFromTheOriginal(): void
@@ -79,7 +79,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
self::assertSame('lit_validation_failed', $result->get_error_code());
self::assertToolError($result, 'validation_failed');
}
/** @return list<array{0: string, 1: string}> */
@@ -103,7 +103,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
self::assertSame('lit_validation_failed', $result->get_error_code());
self::assertToolError($result, 'validation_failed');
}
public function testAMissedScheduleOfTheOriginalIsRefused(): void
@@ -116,8 +116,8 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
self::assertSame('lit_validation_failed', $result->get_error_code());
self::assertArrayHasKey('original_id', $result->get_error_data()['fields']);
self::assertToolError($result, 'validation_failed');
self::assertArrayHasKey('original_id', self::errorFields($result));
}
public function testAPendingCopyIsAccepted(): void
@@ -142,7 +142,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$result = $this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input(['rev' => 'veraltet']));
self::assertSame('lit_rev_mismatch', $result->get_error_code());
self::assertToolError($result, 'rev_mismatch');
}
public function testDisabledTrashIsRefusedBeforeAnyChange(): void
@@ -152,7 +152,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$result = $this->ability($cloner, FakeChangeLog::forbidden(), false)->execute($this->input());
self::assertSame('lit_unsupported', $result->get_error_code());
self::assertToolError($result, 'unsupported');
}
public function testRevisionThenReplaceThenTrashTheCopy(): void
@@ -232,7 +232,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
self::fail('WriteFailed expected');
} catch (WriteFailed $e) {
self::assertTrue($e->rolledBack);
self::assertSame('lit_write_failed', $e->toError()->get_error_code());
self::assertToolError($e->toError(), 'write_failed');
self::assertSame(88, $e->toError()->get_error_data()['backup_id']);
$message = $e->toError()->get_error_message();
self::assertStringNotContainsString('Nichts wurde gespeichert', $message);
@@ -259,7 +259,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
self::assertSame(5, $e->postId);
self::assertSame(900, $e->revisionId);
$error = $e->toError();
self::assertSame('lit_partial_write', $error->get_error_code());
self::assertToolError($error, 'partial_write');
self::assertSame(88, $error->get_error_data()['backup_id']);
self::assertStringContainsString('Sicherung', $error->get_error_message());
self::assertStringContainsString('88', $error->get_error_message());
@@ -280,8 +280,8 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$cloner->expects('replace')->never();
$ability = $this->ability($cloner, FakeChangeLog::forbidden());
self::assertSame('lit_forbidden', $ability->permission($this->input())->get_error_code());
self::assertSame('lit_forbidden', $ability->execute($this->input())->get_error_code());
self::assertToolError($ability->permission($this->input()), 'forbidden');
self::assertToolError($ability->execute($this->input()), 'forbidden');
}
public function testAFailedBackupAbortsBeforeTheOriginalIsTouched(): void
@@ -298,7 +298,7 @@ final class PostReplaceWithCopyAbilityTest extends TestCase
$this->ability($cloner, FakeChangeLog::forbidden())->execute($this->input());
self::fail('WriteFailed expected');
} catch (WriteFailed $e) {
self::assertSame('lit_write_failed', $e->toError()->get_error_code());
self::assertToolError($e->toError(), 'write_failed');
}
}
@@ -92,7 +92,7 @@ final class PostScheduleAbilityTest extends TestCase
self::assertFalse($e->rolledBack);
self::assertSame(5, $e->postId);
self::assertStringContainsString('save_post-Hook kaputt', $e->getMessage());
self::assertSame('lit_partial_write', $e->toError()->get_error_code());
self::assertToolError($e->toError(), 'partial_write');
}
$log->assertPartials(['ability' => 'lit/post-schedule', 'object_type' => 'post', 'object_id' => '5']);
}
@@ -104,7 +104,7 @@ final class PostScheduleAbilityTest extends TestCase
$result = $this->ability(FakeChangeLog::forbidden())->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
self::assertSame('lit_validation_failed', $result->get_error_code());
self::assertToolError($result, 'validation_failed');
}
public function testPastAndImpossibleDatesAreRejected(): void
@@ -115,9 +115,9 @@ final class PostScheduleAbilityTest extends TestCase
$past = $ability->execute(['post_id' => 5, 'date' => '2020-01-01 10:00']);
$impossible = $ability->execute(['post_id' => 5, 'date' => '2099-13-45 10:00']);
self::assertSame('lit_validation_failed', $past->get_error_code());
self::assertToolError($past, 'validation_failed');
self::assertStringContainsString('Zukunft', $past->get_error_message());
self::assertSame('lit_validation_failed', $impossible->get_error_code());
self::assertToolError($impossible, 'validation_failed');
}
public function testADateWithinTheNextMinuteIsRejectedBecauseWordPressWouldPublishAtOnce(): void
@@ -131,6 +131,6 @@ final class PostScheduleAbilityTest extends TestCase
$result = $this->ability(FakeChangeLog::forbidden())
->execute(['post_id' => 5, 'date' => $soon->format('Y-m-d H:i')]);
self::assertSame('lit_validation_failed', $result->get_error_code());
self::assertToolError($result, 'validation_failed');
}
}
@@ -44,7 +44,7 @@ final class PostTrashAbilityTest extends TestCase
$result = $this->ability(FakeChangeLog::forbidden())->execute(['post_id' => 5]);
self::assertSame('lit_confirm_required', $result->get_error_code());
self::assertToolError($result, 'confirm_required');
}
public function testMovesToTrashNeverDeletes(): void
@@ -76,7 +76,7 @@ final class PostTrashAbilityTest extends TestCase
$result = $ability->execute(['post_id' => 5, 'confirm' => true]);
self::assertSame('lit_unsupported', $result->get_error_code());
self::assertToolError($result, 'unsupported');
}
public function testAHookThrowingDuringTheWriteIsAPartialWrite(): void
@@ -93,7 +93,7 @@ final class PostTrashAbilityTest extends TestCase
self::assertFalse($e->rolledBack);
self::assertSame(5, $e->postId);
self::assertStringContainsString('save_post-Hook kaputt', $e->getMessage());
self::assertSame('lit_partial_write', $e->toError()->get_error_code());
self::assertToolError($e->toError(), 'partial_write');
}
$log->assertPartials(['ability' => 'lit/post-trash', 'object_type' => 'page', 'object_id' => '5']);
}
@@ -52,7 +52,7 @@ final class PostUnpublishAbilityTest extends TestCase
self::assertFalse($e->rolledBack);
self::assertSame(5, $e->postId);
self::assertStringContainsString('save_post-Hook kaputt', $e->getMessage());
self::assertSame('lit_partial_write', $e->toError()->get_error_code());
self::assertToolError($e->toError(), 'partial_write');
}
$log->assertPartials(['ability' => 'lit/post-unpublish', 'object_type' => 'page', 'object_id' => '5']);
}
@@ -63,7 +63,7 @@ final class PostUnpublishAbilityTest extends TestCase
$result = $this->ability(FakeChangeLog::forbidden(), 'trash')->execute(['post_id' => 5]);
self::assertSame('lit_validation_failed', $result->get_error_code());
self::assertToolError($result, 'validation_failed');
}
public function testDraftIsANoOp(): void