1c6a879eeaae2eee939c0722839802162f14d960
[yaffs-website] / web / core / modules / media / tests / src / Kernel / MediaKernelTestBase.php
1 <?php
2
3 namespace Drupal\Tests\media\Kernel;
4
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;
12
13 /**
14  * Base class for Media kernel tests.
15  */
16 abstract class MediaKernelTestBase extends KernelTestBase {
17
18   use MediaTypeCreationTrait;
19
20   /**
21    * Modules to install.
22    *
23    * @var array
24    */
25   public static $modules = [
26     'media',
27     'media_test_source',
28     'image',
29     'user',
30     'field',
31     'system',
32     'file',
33   ];
34
35   /**
36    * The test media type.
37    *
38    * @var \Drupal\media\MediaTypeInterface
39    */
40   protected $testMediaType;
41
42   /**
43    * The test media type with constraints.
44    *
45    * @var \Drupal\media\MediaTypeInterface
46    */
47   protected $testConstraintsMediaType;
48
49   /**
50    * A user.
51    *
52    * @var \Drupal\user\UserInterface
53    */
54   protected $user;
55
56   /**
57    * {@inheritdoc}
58    */
59   protected function setUp() {
60     parent::setUp();
61
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']);
68
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');
73
74     $this->user = User::create([
75       'name' => 'username',
76       'status' => 1,
77     ]);
78     $this->user->save();
79     $this->container->get('current_user')->setAccount($this->user);
80   }
81
82   /**
83    * Helper to generate a media item.
84    *
85    * @param string $filename
86    *   String filename with extension.
87    * @param \Drupal\media\MediaTypeInterface $media_type
88    *   The the media type.
89    *
90    * @return \Drupal\media\Entity\Media
91    *   A media item.
92    */
93   protected function generateMedia($filename, MediaTypeInterface $media_type) {
94     vfsStream::setup('drupal_root');
95     vfsStream::create([
96       'sites' => [
97         'default' => [
98           'files' => [
99             $filename => str_repeat('a', 3000),
100           ],
101         ],
102       ],
103     ]);
104
105     $file = File::create([
106       'uri' => 'vfs://drupal_root/sites/default/files/' . $filename,
107       'uid' => $this->user->id(),
108     ]);
109     $file->setPermanent();
110     $file->save();
111
112     return Media::create([
113       'bundle' => $media_type->id(),
114       'name' => 'Mr. Jones',
115       'field_media_file' => [
116         'target_id' => $file->id(),
117       ],
118     ]);
119   }
120
121 }