function NoFieldItemsExistWithHigherCardinalityTest::testValidation
Tests validation error and message when cardinality is set too low.
File
-
core/
modules/ field/ tests/ src/ Kernel/ Plugin/ Validation/ Constraint/ NoFieldItemsExistWithHigherCardinalityTest.php, line 28
Class
- NoFieldItemsExistWithHigherCardinalityTest
- Tests NoFieldItemsExistWithHigherCardinality validation.
Namespace
Drupal\Tests\field\Kernel\Plugin\Validation\ConstraintCode
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.