You are here

protected function MediaKernelTestBase::generateMedia in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media/tests/src/Kernel/MediaKernelTestBase.php \Drupal\Tests\media\Kernel\MediaKernelTestBase::generateMedia()

Helper to generate a media item.

Parameters

string $filename: String filename with extension.

\Drupal\media\MediaTypeInterface $media_type: The media type.

Return value

\Drupal\media\Entity\Media A media item.

2 calls to MediaKernelTestBase::generateMedia()
MediaSourceFileTest::testFileDeletion in core/modules/media/tests/src/Kernel/MediaSourceFileTest.php
Tests a media file can be deleted.
MediaSourceFileTest::testFileExtensionConstraint in core/modules/media/tests/src/Kernel/MediaSourceFileTest.php
Tests the file extension constraint.

File

core/modules/media/tests/src/Kernel/MediaKernelTestBase.php, line 93

Class

MediaKernelTestBase
Base class for Media kernel tests.

Namespace

Drupal\Tests\media\Kernel

Code

protected function generateMedia($filename, MediaTypeInterface $media_type) {
  vfsStream::setup('drupal_root');
  vfsStream::create([
    'sites' => [
      'default' => [
        'files' => [
          $filename => str_repeat('a', 3000),
        ],
      ],
    ],
  ]);
  $file = File::create([
    'uri' => 'vfs://drupal_root/sites/default/files/' . $filename,
    'uid' => $this->user
      ->id(),
  ]);
  $file
    ->setPermanent();
  $file
    ->save();
  return Media::create([
    'bundle' => $media_type
      ->id(),
    'name' => 'Mr. Jones',
    'field_media_file' => [
      'target_id' => $file
        ->id(),
    ],
  ]);
}