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:
@@ -76,7 +76,7 @@ final class ContentCreateAbilityTest extends TestCase
|
||||
$result = (new ContentCreateAbility($this->schema(), $writer, FakeChangeLog::forbidden()))
|
||||
->execute(['post_type' => 'lit_person', 'title' => 'Anna']);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
self::assertToolError($result, 'validation_failed');
|
||||
}
|
||||
|
||||
public function testCreatesADraftWritesExtrasLogsAndReturnsAToolResult(): void
|
||||
@@ -131,7 +131,7 @@ final class ContentCreateAbilityTest extends TestCase
|
||||
|
||||
self::assertTrue($failure->rolledBack);
|
||||
self::assertStringContainsString('Pods could not save the fields.', $failure->getMessage());
|
||||
self::assertSame('lit_write_failed', $failure->toError()->get_error_code());
|
||||
self::assertToolError($failure->toError(), 'write_failed');
|
||||
}
|
||||
|
||||
public function testMetaFieldsTheUserMayNotAddAreRefusedAndTheDraftIsRemoved(): void
|
||||
@@ -149,8 +149,8 @@ final class ContentCreateAbilityTest extends TestCase
|
||||
|
||||
$result = (new ContentCreateAbility($this->schema(), $writer, $log))->execute(self::INPUT);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
self::assertArrayHasKey('fields.city', $result->get_error_data()['fields']);
|
||||
self::assertToolError($result, 'validation_failed');
|
||||
self::assertArrayHasKey('fields.city', self::errorFields($result));
|
||||
}
|
||||
|
||||
public function testARefusedDraftThatCannotBeDeletedIsAPartialWrite(): void
|
||||
@@ -219,7 +219,7 @@ final class ContentCreateAbilityTest extends TestCase
|
||||
|
||||
self::assertFalse($failure->rolledBack);
|
||||
self::assertNull($failure->postId);
|
||||
self::assertSame('lit_partial_write', $failure->toError()->get_error_code());
|
||||
self::assertToolError($failure->toError(), 'partial_write');
|
||||
$log->assertPartials(['ability' => 'lit/content-create', 'object_type' => 'lit_person', 'object_id' => '']);
|
||||
}
|
||||
|
||||
@@ -261,6 +261,6 @@ final class ContentCreateAbilityTest extends TestCase
|
||||
self::assertTrue($ability->permission(['post_type' => 'page']));
|
||||
|
||||
Functions\when('current_user_can')->justReturn(false);
|
||||
self::assertSame('lit_forbidden', $ability->permission(['post_type' => 'page'])->get_error_code());
|
||||
self::assertToolError($ability->permission(['post_type' => 'page']), 'forbidden');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user