-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
MediaTrait.php
256 lines (222 loc) · 6.82 KB
/
MediaTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Drupal\media\Entity\Media;
use Drupal\media\MediaInterface;
/**
* Trait MediaTrait.
*
* Trait to handle media entities.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
trait MediaTrait {
/**
* Array of created media entities.
*
* @var array<int,\Drupal\media\MediaInterface>
*/
protected $mediaEntities = [];
/**
* Remove any created media items.
*
* @AfterScenario
*/
public function mediaClean(AfterScenarioScope $scope): void {
// Allow to skip this by adding a tag.
if ($scope->getScenario()->hasTag('behat-steps-skip:' . __FUNCTION__)) {
return;
}
foreach ($this->mediaEntities as $media) {
$media->delete();
}
$this->mediaEntities = [];
}
/**
* Remove media type.
*
* @code
* @Given no "video" media type
* @endcode
*
* @Given no :type media type
*/
public function mediaRemoveType(string $type): void {
$type_entity = \Drupal::entityTypeManager()->getStorage('media_type')->load($type);
if ($type_entity) {
$type_entity->delete();
}
}
/**
* Creates media of a given type.
*
* @code
* Given "video" media:
* | name | field1 | field2 | field3 |
* | My media | file.jpg | value | value |
* | ... | ... | ... | ... |
* @endcode
*
* @Given :type media:
*/
public function mediaCreate(string $type, TableNode $nodesTable): void {
foreach ($nodesTable->getHash() as $nodeHash) {
$node = (object) $nodeHash;
$node->bundle = $type;
$this->mediaCreateSingle($node);
}
}
/**
* Remove media defined by provided properties.
*
* @code
* Given no "image" media:
* | name |
* | Media item |
* | Another media item |
* @endcode
*
* @Given /^no ([a-zA-z0-9_-]+) media:$/
*/
public function mediaDelete(string $type, TableNode $table): void {
foreach ($table->getHash() as $node_hash) {
$ids = $this->mediaLoadMultiple($type, $node_hash);
$controller = \Drupal::entityTypeManager()->getStorage('media');
$entities = $controller->loadMultiple($ids);
$controller->delete($entities);
}
}
/**
* Navigate to edit media with specified type and name.
*
* @code
* When I edit "document" media "Test document"
* @endcode
*
* @When I edit :type media :name
*/
public function mediaEditWithName(string $type, string $name): void {
$mids = $this->mediaLoadMultiple($type, [
'name' => $name,
]);
if (empty($mids)) {
throw new \RuntimeException(sprintf('Unable to find %s media "%s"', $type, $name));
}
$mid = current($mids);
$path = $this->locatePath('/media/' . $mid) . '/edit';
print $path;
$this->getSession()->visit($path);
}
/**
* Create a single media item.
*
* @param \StdClass $stub
* The media item properties.
*
* @return \Drupal\media\MediaInterface
* The created media item.
*/
protected function mediaCreateSingle(\StdClass $stub): MediaInterface {
$this->parseEntityFields('media', $stub);
$saved = $this->mediaCreateEntity($stub);
$this->mediaEntities[] = $saved;
return $saved;
}
/**
* Create media entity.
*
* @param \StdClass $stub
* The media entity properties.
*
* @return \Drupal\media\MediaInterface
* The created media entity.
*/
protected function mediaCreateEntity(\StdClass $stub): MediaInterface {
// Throw an exception if the media type is missing or does not exist.
if (!property_exists($stub, 'bundle') || $stub->bundle === NULL || !$stub->bundle) {
throw new \Exception("Cannot create media because it is missing the required property 'bundle'.");
}
$bundles = \Drupal::getContainer()->get('entity_type.bundle.info')->getBundleInfo('media');
if (!in_array($stub->bundle, array_keys($bundles))) {
throw new \Exception(sprintf("Cannot create media because provided bundle '%s' does not exist.", $stub->bundle));
}
$this->mediaExpandEntityFieldsFixtures($stub);
$this->mediaExpandEntityFields('media', $stub);
$entity = Media::create((array) $stub);
$entity->save();
return $entity;
}
/**
* Expand parsed fields into expected field values based on field type.
*
* This is a re-use of the functionality provided by DrupalExtension.
*
* @param string $entity_type
* The entity type.
* @param \StdClass $stub
* The entity stub.
*/
protected function mediaExpandEntityFields(string $entity_type, \StdClass $stub): void {
$core = $this->getDriver()->getCore();
$class = new \ReflectionClass($core::class);
$method = $class->getMethod('expandEntityFields');
$method->setAccessible(TRUE);
$method->invokeArgs($core, func_get_args());
}
/**
* Expand entity fields with fixture values.
*
* @param \StdClass $stub
* The entity stub.
*/
protected function mediaExpandEntityFieldsFixtures(\StdClass $stub): void {
if (!empty($this->getMinkParameter('files_path'))) {
$fixture_path = rtrim((string) realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
if (empty($fixture_path) || !is_dir($fixture_path)) {
throw new \RuntimeException('Fixture files path is not set or does not exist. Check that the "files_path" parameter is set for Mink.');
}
$fields = get_object_vars($stub);
$field_types = $this->getDrupal()->getDriver()->getCore()->getEntityFieldTypes('media', array_keys($fields));
foreach ($fields as $name => $value) {
if (!str_contains($name, 'field_')) {
continue;
}
if (!empty($field_types[$name]) && $field_types[$name] == 'image') {
if (is_array($value)) {
if (!empty($value[0]) && is_file($fixture_path . $value[0])) {
$stub->{$name}[0] = $fixture_path . $value[0];
}
}
elseif (is_file($fixture_path . $value)) {
$stub->{$name} = $fixture_path . $value;
}
}
}
}
/**
* Load multiple media entities with specified type and conditions.
*
* @param string $type
* The node type.
* @param array<string, mixed> $conditions
* Conditions keyed by field names.
*
* @return array<int, string>
* Array of node ids.
*/
protected function mediaLoadMultiple(string $type, array $conditions = []): array {
$query = \Drupal::entityQuery('media')
->accessCheck(FALSE)
->condition('bundle', $type);
foreach ($conditions as $k => $v) {
$and = $query->andConditionGroup();
$and->condition($k, $v);
$query->condition($and);
}
return $query->execute();
}
}