3 namespace Drupal\Tests\media\Kernel;
5 use Drupal\file\Entity\File;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\media\Entity\Media;
8 use Drupal\media\MediaTypeInterface;
9 use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
10 use Drupal\user\Entity\User;
11 use org\bovigo\vfs\vfsStream;
14 * Base class for Media kernel tests.
16 abstract class MediaKernelTestBase extends KernelTestBase {
18 use MediaTypeCreationTrait;
25 public static $modules = [
36 * The test media type.
38 * @var \Drupal\media\MediaTypeInterface
40 protected $testMediaType;
43 * The test media type with constraints.
45 * @var \Drupal\media\MediaTypeInterface
47 protected $testConstraintsMediaType;
52 * @var \Drupal\user\UserInterface
59 protected function setUp() {
62 $this->installEntitySchema('user');
63 $this->installEntitySchema('file');
64 $this->installSchema('file', 'file_usage');
65 $this->installSchema('system', 'sequences');
66 $this->installEntitySchema('media');
67 $this->installConfig(['field', 'system', 'image', 'file', 'media']);
69 // Create a test media type.
70 $this->testMediaType = $this->createMediaType('test');
71 // Create a test media type with constraints.
72 $this->testConstraintsMediaType = $this->createMediaType('test_constraints');
74 $this->user = User::create([
79 $this->container->get('current_user')->setAccount($this->user);
83 * Helper to generate a media item.
85 * @param string $filename
86 * String filename with extension.
87 * @param \Drupal\media\MediaTypeInterface $media_type
90 * @return \Drupal\media\Entity\Media
93 protected function generateMedia($filename, MediaTypeInterface $media_type) {
94 vfsStream::setup('drupal_root');
99 $filename => str_repeat('a', 3000),
105 $file = File::create([
106 'uri' => 'vfs://drupal_root/sites/default/files/' . $filename,
107 'uid' => $this->user->id(),
109 $file->setPermanent();
112 return Media::create([
113 'bundle' => $media_type->id(),
114 'name' => 'Mr. Jones',
115 'field_media_file' => [
116 'target_id' => $file->id(),