You are here

protected function DownloadTest::setUp in Media Download 1.0.x

Same name and namespace in other branches
  1. 1.2.x tests/src/Functional/DownloadTest.php \Drupal\Tests\media_download\Functional\DownloadTest::setUp()
  2. 1.1.x tests/src/Functional/DownloadTest.php \Drupal\Tests\media_download\Functional\DownloadTest::setUp()

Overrides BrowserTestBase::setUp

File

tests/src/Functional/DownloadTest.php, line 52

Class

DownloadTest
Tests that the download functionality works as expected.

Namespace

Drupal\Tests\media_download\Functional

Code

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

  // Ensure that no user can view media by default.
  foreach (\Drupal::entityTypeManager()
    ->getStorage('user_role')
    ->loadMultiple() as $role) {
    $role
      ->revokePermission('view media');
    $role
      ->save();
  }
  $this->owner = $this
    ->drupalCreateUser([
    'administer media',
  ]);
  $file_storage = \Drupal::entityTypeManager()
    ->getStorage('file');
  $file_name = $this
    ->randomMachineName();
  file_put_contents("public://{$file_name}.txt", "{$file_name}.txt");
  $file = $file_storage
    ->create([
    'uid' => [
      [
        'target_id' => $this->owner
          ->id(),
      ],
    ],
    'uri' => "public://{$file_name}.txt",
  ]);
  $file
    ->save();
  $media_storage = \Drupal::entityTypeManager()
    ->getStorage('media');
  $media_name = $this
    ->randomMachineName();
  $this->media = $media_storage
    ->create([
    'bundle' => [
      [
        'target_id' => 'document',
      ],
    ],
    'field_media_document' => [
      [
        'entity' => $file,
      ],
    ],
    'name' => [
      [
        'value' => $media_name,
      ],
    ],
    'uid' => [
      [
        'entity' => $this->owner,
      ],
    ],
    'path' => [
      [
        'alias' => "/{$media_name}",
      ],
    ],
  ]);
  $this->media
    ->save();
}