function FieldCrudTest::doFieldPropertyConstraintsTests

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::doFieldPropertyConstraintsTests()
  2. 10 core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::doFieldPropertyConstraintsTests()
  3. 11.x core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::doFieldPropertyConstraintsTests()

Tests configurable field validation.

See also

field_test_entity_bundle_field_info_alter()

1 call to FieldCrudTest::doFieldPropertyConstraintsTests()
FieldCrudTest::testFieldPropertyConstraints in core/modules/field/tests/src/Kernel/FieldCrudTest.php
Tests setting and adding property constraints to a configurable field.

File

core/modules/field/tests/src/Kernel/FieldCrudTest.php, line 166

Class

FieldCrudTest
Create field entities by attaching fields to entities.

Namespace

Drupal\Tests\field\Kernel

Code

protected function doFieldPropertyConstraintsTests() {
    $field_name = $this->fieldStorage
        ->getName();
    // Check that a valid value (not -2 and between 0 and 32) doesn't trigger
    // any violation.
    $entity = EntityTest::create();
    $entity->set($field_name, 1);
    $violations = $entity->validate();
    $this->assertCount(0, $violations, 'No violations found when in-range value passed.');
    // Check that a value that is specifically restricted triggers both
    // violations.
    $entity->set($field_name, -2);
    $violations = $entity->validate();
    $this->assertCount(2, $violations, 'Two violations found when using a null and outside the range value.');
    $this->assertEquals($field_name . '.0.value', $violations[0]->getPropertyPath());
    $this->assertEquals(t('%name does not accept the value @value.', [
        '%name' => $field_name,
        '@value' => -2,
    ]), $violations[0]->getMessage());
    $this->assertEquals($field_name . '.0.value', $violations[1]->getPropertyPath());
    $this->assertEquals(t('This value should be %limit or more.', [
        '%limit' => 0,
    ]), $violations[1]->getMessage());
    // Check that a value that is not specifically restricted but outside the
    // range triggers the expected violation.
    $entity->set($field_name, 33);
    $violations = $entity->validate();
    $this->assertCount(1, $violations, 'Violations found when using value outside the range.');
    $this->assertEquals($field_name . '.0.value', $violations[0]->getPropertyPath());
    $this->assertEquals(t('This value should be %limit or less.', [
        '%limit' => 32,
    ]), $violations[0]->getMessage());
}

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