function ContentEntityFormFieldValidationFilteringTest::setUp

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/FunctionalTests/Entity/ContentEntityFormFieldValidationFilteringTest.php \Drupal\FunctionalTests\Entity\ContentEntityFormFieldValidationFilteringTest::setUp()
  2. 10 core/tests/Drupal/FunctionalTests/Entity/ContentEntityFormFieldValidationFilteringTest.php \Drupal\FunctionalTests\Entity\ContentEntityFormFieldValidationFilteringTest::setUp()
  3. 11.x core/tests/Drupal/FunctionalTests/Entity/ContentEntityFormFieldValidationFilteringTest.php \Drupal\FunctionalTests\Entity\ContentEntityFormFieldValidationFilteringTest::setUp()

Overrides BrowserTestBase::setUp

File

core/tests/Drupal/FunctionalTests/Entity/ContentEntityFormFieldValidationFilteringTest.php, line 61

Class

ContentEntityFormFieldValidationFilteringTest
Tests field validation filtering on content entity forms.

Namespace

Drupal\FunctionalTests\Entity

Code

protected function setUp() : void {
    parent::setUp();
    $web_user = $this->drupalCreateUser([
        'administer entity_test content',
    ]);
    $this->drupalLogin($web_user);
    // Create two fields of field type "test_field", one with single cardinality
    // and one with unlimited cardinality on the entity type "entity_test". It
    // is important to use this field type because its default widget has a
    // custom \Drupal\Core\Field\WidgetInterface::errorElement() implementation.
    $this->entityTypeId = 'entity_test';
    $this->fieldNameSingle = 'test_single';
    $this->fieldNameMultiple = 'test_multiple';
    $this->fieldNameFile = 'test_file';
    FieldStorageConfig::create([
        'field_name' => $this->fieldNameSingle,
        'entity_type' => $this->entityTypeId,
        'type' => 'test_field',
        'cardinality' => 1,
    ])
        ->save();
    FieldConfig::create([
        'entity_type' => $this->entityTypeId,
        'field_name' => $this->fieldNameSingle,
        'bundle' => $this->entityTypeId,
        'label' => 'Test single',
        'required' => TRUE,
        'translatable' => FALSE,
    ])
        ->save();
    FieldStorageConfig::create([
        'field_name' => $this->fieldNameMultiple,
        'entity_type' => $this->entityTypeId,
        'type' => 'test_field',
        'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ])
        ->save();
    FieldConfig::create([
        'entity_type' => $this->entityTypeId,
        'field_name' => $this->fieldNameMultiple,
        'bundle' => $this->entityTypeId,
        'label' => 'Test multiple',
        'translatable' => FALSE,
    ])
        ->save();
    // Also create a file field to test its '#limit_validation_errors'
    // implementation.
    FieldStorageConfig::create([
        'field_name' => $this->fieldNameFile,
        'entity_type' => $this->entityTypeId,
        'type' => 'file',
        'cardinality' => 1,
    ])
        ->save();
    FieldConfig::create([
        'entity_type' => $this->entityTypeId,
        'field_name' => $this->fieldNameFile,
        'bundle' => $this->entityTypeId,
        'label' => 'Test file',
        'translatable' => FALSE,
    ])
        ->save();
    $this->container
        ->get('entity_display.repository')
        ->getFormDisplay($this->entityTypeId, $this->entityTypeId, 'default')
        ->setComponent($this->fieldNameSingle, [
        'type' => 'test_field_widget',
    ])
        ->setComponent($this->fieldNameMultiple, [
        'type' => 'test_field_widget',
    ])
        ->setComponent($this->fieldNameFile, [
        'type' => 'file_generic',
    ])
        ->save();
}

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