You are here

protected function EntityReferenceAutoCreateTest::setUp in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceAutoCreateTest::setUp()

Overrides BrowserTestBase::setUp

File

core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php, line 43

Class

EntityReferenceAutoCreateTest
Tests creating new entity (e.g. taxonomy-term) from an autocomplete widget.

Namespace

Drupal\Tests\field\Functional\EntityReference

Code

protected function setUp() {
  parent::setUp();

  // Create "referencing" and "referenced" node types.
  $referencing = $this
    ->drupalCreateContentType();
  $this->referencingType = $referencing
    ->id();
  $referenced = $this
    ->drupalCreateContentType();
  $this->referencedType = $referenced
    ->id();
  FieldStorageConfig::create([
    'field_name' => 'test_field',
    'entity_type' => 'node',
    'translatable' => FALSE,
    'entity_types' => [],
    'settings' => [
      'target_type' => 'node',
    ],
    'type' => 'entity_reference',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ])
    ->save();
  FieldConfig::create([
    'label' => 'Entity reference field',
    'field_name' => 'test_field',
    'entity_type' => 'node',
    'bundle' => $referencing
      ->id(),
    'settings' => [
      'handler' => 'default',
      'handler_settings' => [
        // Reference a single vocabulary.
        'target_bundles' => [
          $referenced
            ->id(),
        ],
        // Enable auto-create.
        'auto_create' => TRUE,
      ],
    ],
  ])
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $display_repository
    ->getViewDisplay('node', $referencing
    ->id())
    ->setComponent('test_field')
    ->save();
  $display_repository
    ->getFormDisplay('node', $referencing
    ->id(), 'default')
    ->setComponent('test_field', [
    'type' => 'entity_reference_autocomplete',
  ])
    ->save();
  $account = $this
    ->drupalCreateUser([
    'access content',
    "create {$this->referencingType} content",
  ]);
  $this
    ->drupalLogin($account);
}