You are here

abstract class MediaKernelTestBase in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media/tests/src/Kernel/MediaKernelTestBase.php \Drupal\Tests\media\Kernel\MediaKernelTestBase
  2. 9 core/modules/media/tests/src/Kernel/MediaKernelTestBase.php \Drupal\Tests\media\Kernel\MediaKernelTestBase

Base class for Media kernel tests.

Hierarchy

Expanded class hierarchy of MediaKernelTestBase

File

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

Namespace

Drupal\Tests\media\Kernel
View source
abstract class MediaKernelTestBase extends KernelTestBase {
  use MediaTypeCreationTrait;

  /**
   * Modules to install.
   *
   * @var array
   */
  protected static $modules = [
    'media',
    'media_test_source',
    'image',
    'user',
    'field',
    'system',
    'file',
  ];

  /**
   * The test media type.
   *
   * @var \Drupal\media\MediaTypeInterface
   */
  protected $testMediaType;

  /**
   * The test media type with constraints.
   *
   * @var \Drupal\media\MediaTypeInterface
   */
  protected $testConstraintsMediaType;

  /**
   * A user.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $user;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this
      ->installEntitySchema('user');
    $this
      ->installEntitySchema('file');
    $this
      ->installSchema('file', 'file_usage');
    $this
      ->installSchema('system', 'sequences');
    $this
      ->installEntitySchema('media');
    $this
      ->installConfig([
      'field',
      'system',
      'image',
      'file',
      'media',
    ]);

    // Create a test media type.
    $this->testMediaType = $this
      ->createMediaType('test');

    // Create a test media type with constraints.
    $this->testConstraintsMediaType = $this
      ->createMediaType('test_constraints');
    $this->user = User::create([
      'name' => 'username',
      'status' => 1,
    ]);
    $this->user
      ->save();
    $this->container
      ->get('current_user')
      ->setAccount($this->user);
  }

  /**
   * Helper to generate a media item.
   *
   * @param string $filename
   *   String filename with extension.
   * @param \Drupal\media\MediaTypeInterface $media_type
   *   The media type.
   *
   * @return \Drupal\media\Entity\Media
   *   A media item.
   */
  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(),
      ],
    ]);
  }

}

Members