function MediaKernelTestBase::generateMedia

Same name in other branches
  1. 9 core/modules/media/tests/src/Kernel/MediaKernelTestBase.php \Drupal\Tests\media\Kernel\MediaKernelTestBase::generateMedia()
  2. 10 core/modules/media/tests/src/Kernel/MediaKernelTestBase.php \Drupal\Tests\media\Kernel\MediaKernelTestBase::generateMedia()
  3. 11.x 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(),
        ],
    ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.