You are here

protected function EntityReferenceFileUploadTest::setUp in Drupal 8

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

Overrides BrowserTestBase::setUp

File

core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFileUploadTest.php, line 48

Class

EntityReferenceFileUploadTest
Tests an autocomplete widget with file upload.

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();
  $this->nodeId = $this
    ->drupalCreateNode([
    'type' => $referenced
      ->id(),
  ])
    ->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',
    'required' => TRUE,
    'bundle' => $referencing
      ->id(),
    'settings' => [
      'handler' => 'default',
      'handler_settings' => [
        // Reference a single vocabulary.
        'target_bundles' => [
          $referenced
            ->id(),
        ],
      ],
    ],
  ])
    ->save();

  // Create a file field.
  $file_field_name = 'file_field';
  $field_storage = FieldStorageConfig::create([
    'field_name' => $file_field_name,
    'entity_type' => 'node',
    'type' => 'file',
  ]);
  $field_storage
    ->save();
  FieldConfig::create([
    'entity_type' => 'node',
    'field_storage' => $field_storage,
    'bundle' => $referencing
      ->id(),
    'label' => $this
      ->randomMachineName() . '_label',
  ])
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $display_repository
    ->getViewDisplay('node', $referencing
    ->id())
    ->setComponent('test_field')
    ->setComponent($file_field_name)
    ->save();
  $display_repository
    ->getFormDisplay('node', $referencing
    ->id())
    ->setComponent('test_field', [
    'type' => 'entity_reference_autocomplete',
  ])
    ->setComponent($file_field_name, [
    'type' => 'file_generic',
  ])
    ->save();
}