class NoFieldItemsExistWithHigherCardinalityTest

Same name in this branch
  1. main core/modules/field/tests/src/Unit/Plugin/Validation/Constraint/NoFieldItemsExistWithHigherCardinalityTest.php \Drupal\Tests\field\Unit\Plugin\Validation\Constraint\NoFieldItemsExistWithHigherCardinalityTest

Tests NoFieldItemsExistWithHigherCardinality validation.

Attributes

#[CoversClass(NoFieldItemsExistWithHigherCardinalityValidator::class)] #[Group('field')] #[RunTestsInSeparateProcesses]

Hierarchy

Expanded class hierarchy of NoFieldItemsExistWithHigherCardinalityTest

File

core/modules/field/tests/src/Kernel/Plugin/Validation/Constraint/NoFieldItemsExistWithHigherCardinalityTest.php, line 20

Namespace

Drupal\Tests\field\Kernel\Plugin\Validation\Constraint
View source
class NoFieldItemsExistWithHigherCardinalityTest extends FieldKernelTestBase {
  
  /**
   * Tests validation error and message when cardinality is set too low.
   */
  public function testValidation() : void {
    // Create a field with a cardinality of 2 to show that we are counting
    // entities and not rows in a table.
    /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
    $field_storage = FieldStorageConfig::create([
      'field_name' => 'field_int',
      'entity_type' => 'entity_test',
      'type' => 'integer',
      'cardinality' => 2,
    ]);
    $field_storage->save();
    $field_config = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => 'entity_test',
    ]);
    $field_config->save();
    $entity = EntityTest::create();
    $entity->field_int[] = mt_rand(1, 99);
    $entity->field_int[] = mt_rand(1, 99);
    $entity->name[] = $this->randomMachineName();
    $entity->save();
    $field_storage->set('cardinality', 1);
    $this->expectException(SchemaIncompleteException::class);
    $this->expectExceptionMessage('Schema errors for field.storage.entity_test.field_int with the following errors: 0 [cardinality] The field 'field_int' of entity type 'entity_test' has more entries (2) than the cardinality (1) allows');
    $field_storage->save();
  }

}

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