function EntityValidationTest::checkValidation

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::checkValidation()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\EntityValidationTest::checkValidation()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php \Drupal\KernelTests\Core\Entity\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 core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php
Tests validating test entity types.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php, line 128

Class

EntityValidationTest
Tests the Entity Validation API.

Namespace

Drupal\KernelTests\Core\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->id->value = -1;
    $violations = $test_entity->validate();
    $this->assertEqual($violations->count(), 1, 'Validation failed.');
    $this->assertEqual($violations[0]->getMessage(), t('%name: The integer must be larger or equal to %min.', [
        '%name' => 'ID',
        '%min' => 0,
    ]));
    $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('%name: may not be longer than @max characters.', [
        '%name' => 'UUID',
        '@max' => 128,
    ]));
    $test_entity = clone $entity;
    $langcode_key = $this->entityTypeManager
        ->getDefinition($entity_type)
        ->getKey('langcode');
    $test_entity->{$langcode_key}->value = $this->randomString(13);
    $violations = $test_entity->validate();
    // This should fail on AllowedValues and Length constraints.
    $this->assertEqual($violations->count(), 2, 'Validation failed.');
    $this->assertEqual($violations[0]->getMessage(), t('This value is too long. It should have %limit characters or less.', [
        '%limit' => '12',
    ]));
    $this->assertEqual($violations[1]->getMessage(), t('The value you selected is not a valid choice.'));
    $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('%name: may not be longer than @max characters.', [
        '%name' => 'Name',
        '@max' => 32,
    ]));
    // Make sure the information provided by a violation is correct.
    $violation = $violations[0];
    $this->assertEqual($violation->getRoot()
        ->getValue(), $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.');
    $test_entity = clone $entity;
    $test_entity->set('user_id', 9999);
    $violations = $test_entity->validate();
    $this->assertEqual($violations->count(), 1, 'Validation failed.');
    $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) does not exist.', [
        '%type' => 'user',
        '%id' => 9999,
    ]));
    $test_entity = clone $entity;
    $test_entity->field_test_text->format = $this->randomString(33);
    $violations = $test_entity->validate();
    $this->assertEqual($violations->count(), 1, 'Validation failed.');
    $this->assertEqual($violations[0]->getMessage(), t('The value you selected is not a valid choice.'));
    // Make sure the information provided by a violation is correct.
    $violation = $violations[0];
    $this->assertEqual($violation->getRoot()
        ->getValue(), $test_entity, 'Violation root is entity.');
    $this->assertEqual($violation->getPropertyPath(), 'field_test_text.0.format', 'Violation property path is correct.');
    $this->assertEqual($violation->getInvalidValue(), $test_entity->field_test_text->format, 'Violation contains invalid value.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.