DataValidationTestTrait::testDataValidationScalar() currently expects both the scalar and maxLength validation errors:
$expected ??= [
'scalar' => 'The provided value must be scalar',
'maxLength' => 'The provided value must be at most `50` characters long',
];
This is undesirable when the purpose of the test is specifically to verify the scalar validation. CakePHP may also apply a maxLength validation rule to the same field, causing testDataValidationScalar() to assert an unrelated validation error.
The method should only check the scalar validation by default and should not be affected by other validation rules configured for the field.
We currently have a way to specify custom $expected values to work around this behavior. This option should remain available.
Possible solutions
-
One option is to investigate whether the desired behavior can be achieved through the $options parameter passed to CakePHP's newEntity() method. This would allow the existing testDataValidation() implementation to remain unchanged while controlling which validation rules are applied during entity creation.
-
Another option is to introduce a separate testDataValidation method that checks only a specific validation error key rather than comparing the complete error array. For example:
$expected ??= [
'The provided value must be scalar',
];
$entity = $table->newEntity($dataSet, $options);
$errors = $entity->getError($fieldName);
static::assertArrayHasKey('scalar', $errors);
static::assertEquals($expected, $errors['scalar']);
This approach would allow the test to verify the scalar validation independently of additional validation errors such as maxLength.
The preferred solution should be evaluated based on which approach best fits the existing API and testing patterns of the plugin.
DataValidationTestTrait::testDataValidationScalar()currently expects both the scalar and maxLength validation errors:This is undesirable when the purpose of the test is specifically to verify the scalar validation. CakePHP may also apply a maxLength validation rule to the same field, causing
testDataValidationScalar()to assert an unrelated validation error.The method should only check the scalar validation by default and should not be affected by other validation rules configured for the field.
We currently have a way to specify custom
$expectedvalues to work around this behavior. This option should remain available.Possible solutions
One option is to investigate whether the desired behavior can be achieved through the
$optionsparameter passed to CakePHP's newEntity() method. This would allow the existingtestDataValidation()implementation to remain unchanged while controlling which validation rules are applied during entity creation.Another option is to introduce a separate testDataValidation method that checks only a specific validation error key rather than comparing the complete error array. For example:
This approach would allow the test to verify the scalar validation independently of additional validation errors such as maxLength.
The preferred solution should be evaluated based on which approach best fits the existing API and testing patterns of the plugin.