From 91837e93795f72baee64def040db2e9fed56dcf9 Mon Sep 17 00:00:00 2001 From: webchick Date: Fri, 7 Mar 2014 13:31:41 -0800 Subject: [PATCH] Issue #2142999 by stefan.r, fago: Test for EntityTypeConstraintValidator. --- .../EntityTypeConstraintValidatorTest.php | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 core/modules/system/lib/Drupal/system/Tests/Entity/EntityTypeConstraintValidatorTest.php diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTypeConstraintValidatorTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTypeConstraintValidatorTest.php new file mode 100644 index 000000000000..6428ef084892 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTypeConstraintValidatorTest.php @@ -0,0 +1,73 @@ + 'Entity type constraint', + 'description' => 'Tests validation constraints for EntityTypeConstraintValidator.', + 'group' => 'Validation', + ); + } + + public function setUp() { + parent::setUp(); + $this->typedData = $this->container->get('typed_data_manager'); + } + + /** + * Tests the EntityTypeConstraintValidator. + */ + public function testValidation() { + // Create a typed data definition with an EntityType constraint. + $entity_type = 'node'; + $definition = DataDefinition::create('entity_reference') + ->setConstraints(array( + 'EntityType' => $entity_type, + ) + ); + + // Test the validation. + $node = $this->container->get('entity.manager')->getStorageController('node')->create(array('type' => 'page')); + $typed_data = $this->typedData->create($definition, $node); + $violations = $typed_data->validate(); + $this->assertEqual($violations->count(), 0, 'Validation passed for correct value.'); + + // Test the validation when an invalid value (in this case a user entity) + // is passed. + $account = $this->createUser(); + + $typed_data = $this->typedData->create($definition, $account); + $violations = $typed_data->validate(); + $this->assertEqual($violations->count(), 1, 'Validation failed for incorrect value.'); + + // Make sure the information provided by a violation is correct. + $violation = $violations[0]; + $this->assertEqual($violation->getMessage(), t('The entity must be of type %type.', array('%type' => $entity_type)), 'The message for invalid value is correct.'); + $this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.'); + $this->assertEqual($violation->getInvalidValue(), $account, 'The invalid value is set correctly in the violation.'); + } +}