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:
@@ -23,16 +23,16 @@ final class PostGuardTest extends TestCase
|
||||
|
||||
public function testRejectsInvalidIds(): void
|
||||
{
|
||||
self::assertSame('lit_validation_failed', PostGuard::editable('abc', $this->schema())->get_error_code());
|
||||
self::assertSame('lit_validation_failed', PostGuard::editable(-3, $this->schema())->get_error_code());
|
||||
self::assertToolError(PostGuard::editable('abc', $this->schema()), 'validation_failed');
|
||||
self::assertToolError(PostGuard::editable(-3, $this->schema()), 'validation_failed');
|
||||
}
|
||||
|
||||
public function testMissingOrUnmanagedPostsAreNotFound(): void
|
||||
{
|
||||
Functions\when('get_post')->alias(static fn (int $id) => $id === 8 ? new WP_Post(['ID' => 8, 'post_type' => 'et_pb_layout']) : null);
|
||||
|
||||
self::assertSame('lit_not_found', PostGuard::editable(5, $this->schema())->get_error_code());
|
||||
self::assertSame('lit_not_found', PostGuard::editable(8, $this->schema())->get_error_code());
|
||||
self::assertToolError(PostGuard::editable(5, $this->schema()), 'not_found');
|
||||
self::assertToolError(PostGuard::editable(8, $this->schema()), 'not_found');
|
||||
}
|
||||
|
||||
public function testRequiresEditRightsAndReturnsThePost(): void
|
||||
@@ -44,7 +44,7 @@ final class PostGuardTest extends TestCase
|
||||
self::assertSame($post, PostGuard::editable('5', $this->schema()));
|
||||
|
||||
Functions\when('current_user_can')->justReturn(false);
|
||||
self::assertSame('lit_forbidden', PostGuard::editable(5, $this->schema())->get_error_code());
|
||||
self::assertToolError(PostGuard::editable(5, $this->schema()), 'forbidden');
|
||||
}
|
||||
|
||||
public function testTrashedPostsAreRefusedUnlessAllowed(): void
|
||||
@@ -54,8 +54,8 @@ final class PostGuardTest extends TestCase
|
||||
Functions\when('current_user_can')->justReturn(true);
|
||||
|
||||
$error = PostGuard::editable(5, $this->schema(), 'copy_id');
|
||||
self::assertSame('lit_validation_failed', $error->get_error_code());
|
||||
self::assertArrayHasKey('copy_id', $error->get_error_data()['fields']);
|
||||
self::assertToolError($error, 'validation_failed');
|
||||
self::assertArrayHasKey('copy_id', self::errorFields($error));
|
||||
self::assertSame($post, PostGuard::editable(5, $this->schema(), 'post_id', true));
|
||||
}
|
||||
|
||||
@@ -107,8 +107,8 @@ final class PostGuardTest extends TestCase
|
||||
|
||||
$error = PostGuard::statusStable($post, 'original_id');
|
||||
|
||||
self::assertSame('lit_validation_failed', $error->get_error_code());
|
||||
self::assertArrayHasKey('original_id', $error->get_error_data()['fields']);
|
||||
self::assertToolError($error, 'validation_failed');
|
||||
self::assertArrayHasKey('original_id', self::errorFields($error));
|
||||
self::assertStringContainsString('lit-post-publish', $error->get_error_data()['hint']);
|
||||
self::assertStringContainsString('lit/post-schedule', $error->get_error_data()['hint']);
|
||||
self::assertNull(PostGuard::statusStable(new WP_Post(['post_status' => 'publish'])));
|
||||
@@ -141,12 +141,12 @@ final class PostGuardTest extends TestCase
|
||||
Functions\when('get_post_type_object')->justReturn($object);
|
||||
Functions\when('current_user_can')->alias(static fn (string $cap): bool => $cap === 'edit_pages');
|
||||
|
||||
self::assertSame('lit_not_found', PostGuard::postType('et_pb_layout', $this->schema(), 'create_posts')->get_error_code());
|
||||
self::assertSame('lit_not_found', PostGuard::postType(null, $this->schema(), 'create_posts')->get_error_code());
|
||||
self::assertToolError(PostGuard::postType('et_pb_layout', $this->schema(), 'create_posts'), 'not_found');
|
||||
self::assertToolError(PostGuard::postType(null, $this->schema(), 'create_posts'), 'not_found');
|
||||
self::assertSame($object, PostGuard::postType('page', $this->schema(), 'create_posts'));
|
||||
|
||||
Functions\when('current_user_can')->justReturn(false);
|
||||
self::assertSame('lit_forbidden', PostGuard::postType('page', $this->schema(), 'create_posts')->get_error_code());
|
||||
self::assertToolError(PostGuard::postType('page', $this->schema(), 'create_posts'), 'forbidden');
|
||||
}
|
||||
|
||||
public function testAssertNoMissedScheduleRefusesAMissedScheduleWithTheCallersMessage(): void
|
||||
|
||||
Reference in New Issue
Block a user