diff --git a/core/modules/system/lib/Drupal/system/Tests/Validation/ComplexDataConstraintValidatorTest.php b/core/modules/system/lib/Drupal/system/Tests/Validation/ComplexDataConstraintValidatorTest.php index 05ef958..c11ff04 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Validation/ComplexDataConstraintValidatorTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Validation/ComplexDataConstraintValidatorTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Validation; +use Drupal\Core\TypedData\DataDefinition; use Drupal\simpletest\DrupalUnitTestBase; /** @@ -23,7 +24,7 @@ class ComplexDataConstraintValidatorTest extends DrupalUnitTestBase { public static function getInfo() { return array( - 'name' => 'Tests ComplexData validation constraint', + 'name' => 'Complex data constraint', 'description' => 'Tests ComplexData validation constraint with both valid and invalid values for a key', 'group' => 'Validation', ); @@ -31,28 +32,22 @@ public static function getInfo() { public function setUp() { parent::setUp(); - $this->typedData = $this->container->get('typed_data'); } /** - * Tests the ComplexData validation constraint validator by creating a - * typedData map definition with a ComplexData constraint containing one - * AllowedValues constraint, and then by tring to create a typedData object - * with both an allowed and a dissalowed value. + * Tests the ComplexData validation constraint validator. + * + * For testing a map including a constraint on one of its keys is defined. */ public function testValidation() { - // Create a definition that specifies some ComplexData. - $definition = array( - 'type' => 'map', - 'constraints' => array( - 'ComplexData' => array( - 'key' => array( - 'AllowedValues' => array(1,2,3) - ), + // Create a definition that specifies some ComplexData constraint. + $definition = DataDefinition::create('map') + ->addConstraint('ComplexData', array( + 'key' => array( + 'AllowedValues' => array(1, 2, 3) ), - ), - ); + )); // Test the validation. $typed_data = $this->typedData->create($definition, array('key' => 1)); @@ -69,6 +64,23 @@ public function testValidation() { $this->assertEqual($violation->getMessage(), t('The value you selected is not a valid choice.'), 'The message for invalid value is correct.'); $this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.'); $this->assertEqual($violation->getInvalidValue(), 4, 'The invalid value is set correctly in the violation.'); + + // Test using the constraint with a map without the specified key. This + // should be ignored as long as there is no NotNull or NotBlank constraint. + $typed_data = $this->typedData->create($definition, array('foo' => 'bar')); + $violations = $typed_data->validate(); + $this->assertEqual($violations->count(), 0, 'Not existing key is ignored.'); + + $definition = DataDefinition::create('map') + ->addConstraint('ComplexData', array( + 'key' => array( + 'NotNull' => array() + ), + )); + + $typed_data = $this->typedData->create($definition, array('foo' => 'bar')); + $violations = $typed_data->validate(); + $this->assertEqual($violations->count(), 1, 'Key is required.'); } }