You are here

function FieldValidationTest::testCardinalityConstraint in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/FieldValidationTest.php \Drupal\field\Tests\FieldValidationTest::testCardinalityConstraint()

Tests that the number of values is validated against the field cardinality.

File

core/modules/field/src/Tests/FieldValidationTest.php, line 50
Contains \Drupal\field\Tests\FieldValidationTest.

Class

FieldValidationTest
Tests field validation.

Namespace

Drupal\field\Tests

Code

function testCardinalityConstraint() {
  $cardinality = $this->fieldTestData->field_storage
    ->getCardinality();
  $entity = $this->entity;
  for ($delta = 0; $delta < $cardinality + 1; $delta++) {
    $entity->{$this->fieldTestData->field_name}[] = array(
      'value' => 1,
    );
  }

  // Validate the field.
  $violations = $entity->{$this->fieldTestData->field_name}
    ->validate();

  // Check that the expected constraint violations are reported.
  $this
    ->assertEqual(count($violations), 1);
  $this
    ->assertEqual($violations[0]
    ->getPropertyPath(), '');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('%name: this field cannot hold more than @count values.', array(
    '%name' => $this->fieldTestData->field
      ->getLabel(),
    '@count' => $cardinality,
  )));
}