fa39962c362db9dacc2baec5d89e016f5dbae83c
[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\Entity\MediaType;
9 use Drupal\media\MediaTypeInterface;
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   /**
19    * Modules to install.
20    *
21    * @var array
22    */
23   public static $modules = [
24     'media',
25     'media_test_source',
26     'image',
27     'user',
28     'field',
29     'system',
30     'file',
31   ];
32
33   /**
34    * The test media type.
35    *
36    * @var \Drupal\media\MediaTypeInterface
37    */
38   protected $testMediaType;
39
40   /**
41    * The test media type with constraints.
42    *
43    * @var \Drupal\media\MediaTypeInterface
44    */
45   protected $testConstraintsMediaType;
46
47   /**
48    * A user.
49    *
50    * @var \Drupal\user\UserInterface
51    */
52   protected $user;
53
54   /**
55    * {@inheritdoc}
56    */
57   protected function setUp() {
58     parent::setUp();
59
60     $this->installEntitySchema('user');
61     $this->installEntitySchema('file');
62     $this->installSchema('file', 'file_usage');
63     $this->installSchema('system', 'sequences');
64     $this->installEntitySchema('media');
65     $this->installConfig(['field', 'system', 'image', 'file', 'media']);
66
67     // Create a test media type.
68     $this->testMediaType = $this->createMediaType('test');
69     // Create a test media type with constraints.
70     $this->testConstraintsMediaType = $this->createMediaType('test_constraints');
71
72     $this->user = User::create([
73       'name' => 'username',
74       'status' => 1,
75     ]);
76     $this->user->save();
77     $this->container->get('current_user')->setAccount($this->user);
78   }
79
80   /**
81    * Create a media type for a source plugin.
82    *
83    * @param string $media_source_name
84    *   The name of the media source.
85    *
86    * @return \Drupal\media\MediaTypeInterface
87    *   A media type.
88    */
89   protected function createMediaType($media_source_name) {
90     $id = strtolower($this->randomMachineName());
91     $media_type = MediaType::create([
92       'id' => $id,
93       'label' => $id,
94       'source' => $media_source_name,
95       'new_revision' => FALSE,
96     ]);
97     $media_type->save();
98     $source_field = $media_type->getSource()->createSourceField($media_type);
99     // The media type form creates a source field if it does not exist yet. The
100     // same must be done in a kernel test, since it does not use that form.
101     // @see \Drupal\media\MediaTypeForm::save()
102     $source_field->getFieldStorageDefinition()->save();
103     // The source field storage has been created, now the field can be saved.
104     $source_field->save();
105     $media_type->set('source_configuration', [
106       'source_field' => $source_field->getName(),
107     ])->save();
108     return $media_type;
109   }
110
111   /**
112    * Helper to generate media entity.
113    *
114    * @param string $filename
115    *   String filename with extension.
116    * @param \Drupal\media\MediaTypeInterface $media_type
117    *   The the media type.
118    *
119    * @return \Drupal\media\Entity\Media
120    *   A media entity.
121    */
122   protected function generateMedia($filename, MediaTypeInterface $media_type) {
123     vfsStream::setup('drupal_root');
124     vfsStream::create([
125       'sites' => [
126         'default' => [
127           'files' => [
128             $filename => str_repeat('a', 3000),
129           ],
130         ],
131       ],
132     ]);
133
134     $file = File::create([
135       'uri' => 'vfs://drupal_root/sites/default/files/' . $filename,
136       'uid' => $this->user->id(),
137     ]);
138     $file->setPermanent();
139     $file->save();
140
141     return Media::create([
142       'bundle' => $media_type->id(),
143       'name' => 'Mr. Jones',
144       'field_media_file' => [
145         'target_id' => $file->id(),
146       ],
147     ]);
148   }
149
150 }