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:
@@ -71,12 +71,6 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
return new DiviDuplicateNodeAbility($this->writer);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> The structured error the client sees (message JSON, spec §8.2). */
|
||||
private static function clientError(WP_Error $error): array
|
||||
{
|
||||
return (array) json_decode($error->get_error_message(), true);
|
||||
}
|
||||
|
||||
public function testInsertComposesAndSavesWithTreeRevAndPostRev(): void
|
||||
{
|
||||
$result = $this->insert()->execute([
|
||||
@@ -106,7 +100,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
public function testStaleTreeRevAndInvalidNodesWriteNothing(): void
|
||||
{
|
||||
$stale = $this->insert()->execute(['post_id' => 7, 'tree_rev' => '000000000000', 'nodes' => [['type' => 'section']]]);
|
||||
self::assertSame('lit_rev_mismatch', $stale->get_error_code());
|
||||
self::assertToolError($stale, 'rev_mismatch');
|
||||
self::assertSame('rev_mismatch', self::clientError($stale)['code'], 'the client sees the code without lit_');
|
||||
self::assertStringContainsString('lit-divi-outline', $stale->get_error_data()['hint']);
|
||||
self::assertSame($stale->get_error_code(), $this->insert()->permission(['post_id' => 7, 'tree_rev' => 'x'])->get_error_code());
|
||||
@@ -120,7 +114,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
['type' => 'button', 'url' => 'javascript:alert(1)', 'text' => 'x', 'nope' => 1],
|
||||
],
|
||||
]);
|
||||
self::assertSame('lit_invalid_attr_path', $invalid->get_error_code());
|
||||
self::assertToolError($invalid, 'invalid_attr_path');
|
||||
$client = self::clientError($invalid);
|
||||
self::assertSame('invalid_attr_path', $client['code']);
|
||||
self::assertArrayHasKey('nodes[0].attrs.title.fnt', $client['fields']);
|
||||
@@ -128,7 +122,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
self::assertStringContainsString('lit-divi-catalog', $client['hint']);
|
||||
|
||||
$wrongPlace = $this->insert()->execute(['post_id' => 7, 'tree_rev' => $this->treeRev(), 'nodes' => [['type' => 'button']]]);
|
||||
self::assertSame('lit_validation_failed', $wrongPlace->get_error_code());
|
||||
self::assertToolError($wrongPlace, 'validation_failed');
|
||||
|
||||
$badPosition = $this->insert()->execute([
|
||||
'post_id' => 7,
|
||||
@@ -137,7 +131,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
'after' => 's0.r0.c1.m1',
|
||||
'nodes' => [['type' => 'text']],
|
||||
]);
|
||||
self::assertSame('lit_validation_failed', $badPosition->get_error_code());
|
||||
self::assertToolError($badPosition, 'validation_failed');
|
||||
self::assertArrayHasKey('position', self::clientError($badPosition)['fields']);
|
||||
|
||||
$unknown = $this->insert()->execute([
|
||||
@@ -146,7 +140,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
'parent' => 's9',
|
||||
'nodes' => [['type' => 'section']],
|
||||
]);
|
||||
self::assertSame('lit_validation_failed', $unknown->get_error_code());
|
||||
self::assertToolError($unknown, 'validation_failed');
|
||||
self::assertSame([], $this->saved);
|
||||
self::assertSame($this->original, $this->post->post_content);
|
||||
}
|
||||
@@ -177,7 +171,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
['id' => 's0.r0.c1.m0', 'set' => ['level' => 'h9']],
|
||||
['id' => 's0.r0.c1.m9', 'label' => 'x'],
|
||||
]]);
|
||||
self::assertSame('lit_validation_failed', $failed->get_error_code());
|
||||
self::assertToolError($failed, 'validation_failed');
|
||||
$fields = self::clientError($failed)['fields'];
|
||||
self::assertArrayHasKey('changes[1].set.level', $fields);
|
||||
self::assertArrayHasKey('changes[2].id', $fields, 'every error of the call in one response (S39)');
|
||||
@@ -220,7 +214,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
'changes' => [['id' => 's0.r0.c1.m2', 'label' => 'x']],
|
||||
]);
|
||||
|
||||
self::assertSame('lit_rev_mismatch', $result->get_error_code());
|
||||
self::assertToolError($result, 'rev_mismatch');
|
||||
self::assertSame([], $this->saved);
|
||||
}
|
||||
|
||||
@@ -251,14 +245,14 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
$intoSection = $this->move()->execute([
|
||||
'post_id' => 7, 'tree_rev' => $treeRev, 'id' => 's0.c0.m0', 'parent' => 's1',
|
||||
]);
|
||||
self::assertSame('lit_validation_failed', $intoSection->get_error_code());
|
||||
self::assertToolError($intoSection, 'validation_failed');
|
||||
self::assertStringContainsString('Zeilen (row)', self::clientError($intoSection)['fields']['position']);
|
||||
|
||||
// s0.c1 is a specialty column (specialtyColumns set, holds a row-inner): refuses every child.
|
||||
$intoSpecialtyColumn = $this->move()->execute([
|
||||
'post_id' => 7, 'tree_rev' => $treeRev, 'id' => 's0.c0.m0', 'parent' => 's0.c1',
|
||||
]);
|
||||
self::assertSame('lit_validation_failed', $intoSpecialtyColumn->get_error_code());
|
||||
self::assertToolError($intoSpecialtyColumn, 'validation_failed');
|
||||
self::assertStringContainsString('Spezialspalten', self::clientError($intoSpecialtyColumn)['fields']['position']);
|
||||
|
||||
self::assertSame([], $this->saved);
|
||||
@@ -271,7 +265,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
'post_id' => 7, 'tree_rev' => $this->treeRev(), 'id' => 's0', 'parent' => 's0.r0.c1',
|
||||
]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
self::assertToolError($result, 'validation_failed');
|
||||
self::assertStringContainsString(
|
||||
'nicht in sich selbst verschoben',
|
||||
self::clientError($result)['fields']['position']
|
||||
@@ -285,7 +279,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
'post_id' => 7, 'tree_rev' => '000000000000', 'id' => 's0.r0.c1.m2', 'parent' => 's1.r0.c0',
|
||||
]);
|
||||
|
||||
self::assertSame('lit_rev_mismatch', $result->get_error_code());
|
||||
self::assertToolError($result, 'rev_mismatch');
|
||||
self::assertSame('rev_mismatch', self::clientError($result)['code'], 'the client sees the code without lit_');
|
||||
self::assertSame([], $this->saved);
|
||||
}
|
||||
@@ -349,7 +343,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
'post_id' => 7, 'tree_rev' => $this->treeRev(), 'ids' => ['s9'], 'confirm' => true,
|
||||
]);
|
||||
|
||||
self::assertSame('lit_not_found', $result->get_error_code());
|
||||
self::assertToolError($result, 'not_found');
|
||||
self::assertSame('not_found', self::clientError($result)['code']);
|
||||
self::assertSame([], $this->saved);
|
||||
}
|
||||
@@ -371,10 +365,10 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
|
||||
$delete = $this->delete();
|
||||
$ask = $delete->execute(['post_id' => 7, 'tree_rev' => Rev::of($this->saved[0]), 'ids' => ['s2', 's2.r0']]);
|
||||
self::assertSame('lit_confirm_required', $ask->get_error_code());
|
||||
self::assertToolError($ask, 'confirm_required');
|
||||
self::assertSame(
|
||||
'1 Divi-Knoten würden samt Inhalt entfernt: s2 section "Leistungen (Kopie)" (1 Kind-Knoten).',
|
||||
json_decode($ask->get_error_message(), true)['message']
|
||||
self::clientError($ask)['message']
|
||||
);
|
||||
|
||||
$deleted = $delete->execute(['post_id' => 7, 'tree_rev' => Rev::of($this->saved[0]), 'ids' => ['s2'], 'confirm' => true]);
|
||||
@@ -490,7 +484,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
foreach ([$this->move(), $this->delete(), $this->duplicate()] as $ability) {
|
||||
$result = $ability->permission(['post_id' => 7, 'tree_rev' => $this->treeRev()]);
|
||||
self::assertInstanceOf(WP_Error::class, $result, $ability->name());
|
||||
self::assertSame('lit_forbidden', $result->get_error_code(), $ability->name());
|
||||
self::assertToolError($result, 'forbidden', $ability->name());
|
||||
}
|
||||
self::assertSame([], $this->saved);
|
||||
}
|
||||
@@ -505,7 +499,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
'changes' => [['id' => 's1.g1', 'label' => 'x']],
|
||||
]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
self::assertToolError($result, 'validation_failed');
|
||||
self::assertStringContainsString('nur lesbar', $result->get_error_message());
|
||||
self::assertSame([], $this->saved);
|
||||
}
|
||||
@@ -518,7 +512,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
['id' => 's0.r0.c1.m2', 'set' => ['url' => '/zwei']],
|
||||
]]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
self::assertToolError($result, 'validation_failed');
|
||||
self::assertStringContainsString('changes[0]', self::clientError($result)['fields']['changes[1].id']);
|
||||
self::assertSame([], $this->saved);
|
||||
}
|
||||
@@ -532,7 +526,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
'attrs' => ['button.innerContent' => ['desktop' => ['linkUrl' => '/neu']]],
|
||||
]]]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
self::assertToolError($result, 'validation_failed');
|
||||
self::assertArrayHasKey('changes[0].attrs.button.innerContent', self::clientError($result)['fields']);
|
||||
self::assertSame([], $this->saved);
|
||||
}
|
||||
@@ -551,7 +545,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
]],
|
||||
]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
self::assertToolError($result, 'validation_failed');
|
||||
self::assertArrayHasKey('nodes[0].attrs.button.innerContent', self::clientError($result)['fields']);
|
||||
self::assertSame([], $this->saved);
|
||||
}
|
||||
@@ -564,7 +558,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
$broken = $this->update()->execute(['post_id' => 7, 'tree_rev' => $this->treeRev(), 'changes' => [
|
||||
['id' => 's0.r0.c0', 'attrs' => $sizing('12_24')],
|
||||
]]);
|
||||
self::assertSame('lit_validation_failed', $broken->get_error_code());
|
||||
self::assertToolError($broken, 'validation_failed');
|
||||
self::assertArrayHasKey('changes[0].attrs.module.decoration.sizing', self::clientError($broken)['fields']);
|
||||
self::assertSame([], $this->saved);
|
||||
|
||||
@@ -636,9 +630,9 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
public function testUpdateKeepsExactRowsStrict(): void
|
||||
{
|
||||
$broken = $this->updateOn($this->original, [['id' => 's0.r0.c0', 'attrs' => self::sizing(null)]]);
|
||||
self::assertSame('lit_validation_failed', $broken->get_error_code());
|
||||
self::assertToolError($broken, 'validation_failed');
|
||||
$broken = $this->updateOn($this->original, [['id' => 's0.r0.c0', 'attrs' => self::sizing('10_24')]]);
|
||||
self::assertSame('lit_validation_failed', $broken->get_error_code());
|
||||
self::assertToolError($broken, 'validation_failed');
|
||||
self::assertSame([], $this->saved);
|
||||
}
|
||||
|
||||
@@ -650,7 +644,7 @@ final class DiviWriteAbilitiesTest extends TestCase
|
||||
'attrs' => ['module.decoration.attributes' => ['desktop' => ['attributes' => [['name' => 'onclick', 'value' => 'x']]]]],
|
||||
]]]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
self::assertToolError($result, 'validation_failed');
|
||||
$client = self::clientError($result);
|
||||
self::assertSame('validation_failed', $client['code']);
|
||||
self::assertStringContainsString('nur im Divi-Builder', $client['fields']['changes[0].attrs.module.decoration.attributes']);
|
||||
|
||||
Reference in New Issue
Block a user