protected function EntityValidationTest::checkValidation

Executes the validation test set for a defined entity type.

Parameters

string $entity_type: The entity type to run the tests with.

1 call to EntityValidationTest::checkValidation()
EntityValidationTest::testValidation in drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php
Tests validating test entity types.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php, line 100
Contains \Drupal\system\Tests\Entity\EntityValidationTest.

Class

EntityValidationTest
Tests Entity API base functionality.

Namespace

Drupal\system\Tests\Entity

Code

protected function checkValidation($entity_type) {
  $entity = $this
    ->createTestEntity($entity_type);
  $violations = $entity
    ->validate();
  $this
    ->assertEqual($violations
    ->count(), 0, 'Validation passes.');

  // Test triggering a fail for each of the constraints specified.
  $test_entity = clone $entity;
  $test_entity->uuid->value = $this
    ->randomString(129);
  $violations = $test_entity
    ->validate();
  $this
    ->assertEqual($violations
    ->count(), 1, 'Validation failed.');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('This value is too long. It should have %limit characters or less.', array(
    '%limit' => '128',
  )));
  $test_entity = clone $entity;
  $test_entity->langcode->value = $this
    ->randomString(13);
  $violations = $test_entity
    ->validate();
  $this
    ->assertEqual($violations
    ->count(), 1, 'Validation failed.');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('This value is too long. It should have %limit characters or less.', array(
    '%limit' => '12',
  )));
  $test_entity = clone $entity;
  $test_entity->type->value = NULL;
  $violations = $test_entity
    ->validate();
  $this
    ->assertEqual($violations
    ->count(), 1, 'Validation failed.');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('This value should not be null.'));
  $test_entity = clone $entity;
  $test_entity->name->value = $this
    ->randomString(33);
  $violations = $test_entity
    ->validate();
  $this
    ->assertEqual($violations
    ->count(), 1, 'Validation failed.');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('This value is too long. It should have %limit characters or less.', array(
    '%limit' => '32',
  )));

  // Make sure the information provided by a violation is correct.
  $violation = $violations[0];
  $this
    ->assertEqual($violation
    ->getRoot(), $test_entity, 'Violation root is entity.');
  $this
    ->assertEqual($violation
    ->getPropertyPath(), 'name.0.value', 'Violation property path is correct.');
  $this
    ->assertEqual($violation
    ->getInvalidValue(), $test_entity->name->value, 'Violation contains invalid value.');
}