You are here

protected function FileItemTest::setUp in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Kernel/FileItemTest.php \Drupal\Tests\file\Kernel\FileItemTest::setUp()

Set the default field storage backend for fields created during tests.

Overrides FieldKernelTestBase::setUp

File

core/modules/file/tests/src/Kernel/FileItemTest.php, line 43

Class

FileItemTest
Tests using entity fields of the file field type.

Namespace

Drupal\Tests\file\Kernel

Code

protected function setUp() {
  parent::setUp();
  $this
    ->installEntitySchema('user');
  $this
    ->installConfig([
    'user',
  ]);

  // Give anonymous users permission to access content, so they can view and
  // download public files.
  $anonymous_role = Role::load(Role::ANONYMOUS_ID);
  $anonymous_role
    ->grantPermission('access content');
  $anonymous_role
    ->save();
  $this
    ->installEntitySchema('file');
  $this
    ->installSchema('file', [
    'file_usage',
  ]);
  FieldStorageConfig::create([
    'field_name' => 'file_test',
    'entity_type' => 'entity_test',
    'type' => 'file',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ])
    ->save();
  $this->directory = $this
    ->getRandomGenerator()
    ->name(8);
  FieldConfig::create([
    'entity_type' => 'entity_test',
    'field_name' => 'file_test',
    'bundle' => 'entity_test',
    'settings' => [
      'file_directory' => $this->directory,
    ],
  ])
    ->save();
  file_put_contents('public://example.txt', $this
    ->randomMachineName());
  $this->file = File::create([
    'uri' => 'public://example.txt',
  ]);
  $this->file
    ->save();
}