comparison core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php
2
3 namespace Drupal\Tests\media\FunctionalJavascript;
4
5 use Drupal\Core\Entity\Entity\EntityViewDisplay;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\media_test_oembed\Controller\ResourceController;
9 use Drupal\node\Entity\Node;
10 use Drupal\Tests\media\Traits\OEmbedTestTrait;
11
12 /**
13 * Basic tests for Media configuration in the standard profile.
14 *
15 * @group media
16 */
17 class MediaStandardProfileTest extends MediaJavascriptTestBase {
18
19 use OEmbedTestTrait;
20
21 /**
22 * {@inheritdoc}
23 */
24 protected $profile = 'standard';
25
26 /**
27 * {@inheritdoc}
28 */
29 protected function setUp() {
30 parent::setUp();
31 $this->lockHttpClientToFixtures();
32 }
33
34 /**
35 * Tests all media sources in one method.
36 *
37 * This prevents installing the standard profile for every test case and
38 * increases the performance of this test.
39 */
40 public function testMediaSources() {
41 $storage = FieldStorageConfig::create([
42 'entity_type' => 'node',
43 'field_name' => 'field_related_media',
44 'type' => 'entity_reference',
45 'settings' => [
46 'target_type' => 'media',
47 ],
48 ]);
49 $storage->save();
50
51 FieldConfig::create([
52 'field_storage' => $storage,
53 'entity_type' => 'node',
54 'bundle' => 'article',
55 'label' => 'Related media',
56 'settings' => [
57 'handler_settings' => [
58 'target_bundles' => [
59 'image' => 'image',
60 'video' => 'video',
61 'remote_video' => 'remote_video',
62 'audio' => 'audio',
63 'file' => 'file',
64 ],
65 ],
66 ],
67 ])->save();
68
69 $display = EntityViewDisplay::load('node.article.default');
70 $display->setComponent('field_related_media', [
71 'type' => 'entity_reference_entity_view',
72 'settings' => [
73 'view_mode' => 'full',
74 ],
75 ])->save();
76
77 $this->audioTest();
78 $this->fileTest();
79 $this->imageTest();
80 $this->remoteVideoTest();
81 $this->videoTest();
82 }
83
84 /**
85 * Test the standard profile configuration for media type 'audio'.
86 */
87 protected function audioTest() {
88 $assert_session = $this->assertSession();
89 $page = $this->getSession()->getPage();
90 $source_field_id = 'field_media_audio_file';
91
92 // Create 2 test files.
93 $test_filename = $this->randomMachineName() . '.mp3';
94 $test_filepath = 'public://' . $test_filename;
95 $test_filename_updated = $this->randomMachineName() . '.mp3';
96 $test_filepath_updated = 'public://' . $test_filename_updated;
97 file_put_contents($test_filepath, str_repeat('t', 10));
98 file_put_contents($test_filepath_updated, str_repeat('u', 10));
99
100 // Check if the name field is properly hidden on the media form.
101 $this->drupalGet('media/add/audio');
102 $assert_session->fieldNotExists('name');
103
104 // Check if the source field is available.
105 $assert_session->fieldExists("files[{$source_field_id}_0]");
106
107 // Create a media item.
108 $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath));
109 $result = $assert_session->waitForButton('Remove');
110 $this->assertNotEmpty($result);
111 $page->pressButton('Save');
112 $audio_media_id = $this->container
113 ->get('entity_type.manager')
114 ->getStorage('media')
115 ->getQuery()
116 ->sort('mid', 'DESC')
117 ->execute();
118 $audio_media_id = reset($audio_media_id);
119
120 // Reference the created media using an entity_reference field and make sure
121 // the output is what we expect.
122 $node = Node::create([
123 'title' => 'Host node',
124 'type' => 'article',
125 'field_related_media' => [
126 'target_id' => $audio_media_id,
127 ],
128 ]);
129 $node->save();
130
131 $this->drupalGet('/node/' . $node->id());
132
133 // Check if the default media name is generated as expected.
134 $media = \Drupal::entityTypeManager()->getStorage('media')->loadUnchanged($audio_media_id);
135 $this->assertSame($test_filename, $media->label());
136
137 // Here we expect to see only the linked filename. Assert only one element
138 // in the content region.
139 $assert_session->elementsCount('css', 'article.media--type-audio > *', 1);
140
141 // Assert the audio file is present inside the media element and that its
142 // src attribute matches the audio file.
143 $audio_element = $assert_session->elementExists('css', 'article.media--type-audio .field--name-field-media-audio-file audio > source');
144 $expected_audio_src = file_url_transform_relative(file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/' . $test_filename)));
145 $this->assertSame($expected_audio_src, $audio_element->getAttribute('src'));
146
147 // Assert the media name is updated through the field mapping when changing
148 // the source field.
149 $this->drupalGet('media/' . $audio_media_id . '/edit');
150 $page->pressButton('Remove');
151 $result = $assert_session->waitForField("files[{$source_field_id}_0]");
152 $this->assertNotEmpty($result);
153 $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath_updated));
154 $result = $assert_session->waitForButton('Remove');
155 $this->assertNotEmpty($result);
156 $page->pressButton('Save');
157
158 $this->drupalGet('/node/' . $node->id());
159
160 // Check if the default media name is updated as expected.
161 $media = \Drupal::entityTypeManager()->getStorage('media')->loadUnchanged($audio_media_id);
162 $this->assertSame($test_filename_updated, $media->label());
163
164 // Again we expect to see only the linked filename. Assert only one element
165 // in the content region.
166 $assert_session->elementsCount('css', 'article.media--type-audio > *', 1);
167
168 // Assert the audio file is present inside the media element and that its
169 // src attribute matches the updated audio file.
170 $audio_element = $assert_session->elementExists('css', 'article.media--type-audio .field--name-field-media-audio-file audio > source');
171 $expected_audio_src = file_url_transform_relative(file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/' . $test_filename_updated)));
172 $this->assertSame($expected_audio_src, $audio_element->getAttribute('src'));
173 }
174
175 /**
176 * Test the standard profile configuration for media type 'image'.
177 */
178 protected function imageTest() {
179 $assert_session = $this->assertSession();
180 $page = $this->getSession()->getPage();
181 $source_field_id = 'field_media_image';
182
183 // Check if the name field is properly hidden on the media form.
184 $this->drupalGet('media/add/image');
185 $assert_session->fieldNotExists('name');
186
187 // Check if the source field is available.
188 $assert_session->fieldExists("files[{$source_field_id}_0]");
189
190 // Create a media item.
191 $image_media_name = 'example_1.jpeg';
192 $page->attachFileToField("files[{$source_field_id}_0]", $this->root . '/core/modules/media/tests/fixtures/' . $image_media_name);
193 $result = $assert_session->waitForButton('Remove');
194 $this->assertNotEmpty($result);
195 $page->fillField("{$source_field_id}[0][alt]", 'Image Alt Text 1');
196 $page->pressButton('Save');
197 $image_media_id = $this->container
198 ->get('entity_type.manager')
199 ->getStorage('media')
200 ->getQuery()
201 ->sort('mid', 'DESC')
202 ->execute();
203 $image_media_id = reset($image_media_id);
204
205 // Reference the created media using an entity_reference field and make sure
206 // the output is what we expect.
207 $node = Node::create([
208 'title' => 'Host node',
209 'type' => 'article',
210 'field_related_media' => [
211 'target_id' => $image_media_id,
212 ],
213 ]);
214 $node->save();
215
216 $this->drupalGet('/node/' . $node->id());
217
218 // Check if the default media name is generated as expected.
219 $media = \Drupal::entityTypeManager()->getStorage('media')->loadUnchanged($image_media_id);
220 $this->assertSame($image_media_name, $media->label());
221
222 // Here we expect to see only the image, nothing else. Assert only one
223 // element in the content region.
224 $assert_session->elementsCount('css', 'article.media--type-image > *', 1);
225
226 // Assert the image element is present inside the media element and that its
227 // src attribute matches the image.
228 $image_element = $assert_session->elementExists('css', 'article.media--type-image img');
229 $expected_image_src = file_url_transform_relative(file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/' . $image_media_name)));
230 $this->assertSame($expected_image_src, $image_element->getAttribute('src'));
231
232 // Assert the media name is updated through the field mapping when changing
233 // the source field.
234 $this->drupalGet('media/' . $image_media_id . '/edit');
235 $page->pressButton('Remove');
236 $result = $assert_session->waitForField("files[{$source_field_id}_0]");
237 $this->assertNotEmpty($result);
238 $image_media_name_updated = 'example_2.jpeg';
239 $page->attachFileToField("files[{$source_field_id}_0]", $this->root . '/core/modules/media/tests/fixtures/' . $image_media_name_updated);
240 $result = $assert_session->waitForButton('Remove');
241 $this->assertNotEmpty($result);
242 $page->fillField("{$source_field_id}[0][alt]", 'Image Alt Text 2');
243 $page->pressButton('Save');
244
245 $this->drupalGet('/node/' . $node->id());
246
247 // Check if the default media name is updated as expected.
248 $media = \Drupal::entityTypeManager()->getStorage('media')->loadUnchanged($image_media_id);
249 $this->assertSame($image_media_name_updated, $media->label());
250
251 // Again we expect to see only the image, nothing else. Assert only one
252 // element in the content region.
253 $assert_session->elementsCount('css', 'article.media--type-image > *', 1);
254
255 // Assert the image element is present inside the media element and that its
256 // src attribute matches the updated image.
257 $image_element = $assert_session->elementExists('css', 'article.media--type-image img');
258 $expected_image_src = file_url_transform_relative(file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/' . $image_media_name_updated)));
259 $this->assertSame($expected_image_src, $image_element->getAttribute('src'));
260 }
261
262 /**
263 * Test the standard profile configuration for media type 'file'.
264 */
265 protected function fileTest() {
266 $assert_session = $this->assertSession();
267 $page = $this->getSession()->getPage();
268 $source_field_id = 'field_media_file';
269
270 // Create 2 test files.
271 $test_filename = $this->randomMachineName() . '.txt';
272 $test_filepath = 'public://' . $test_filename;
273 $test_filename_updated = $this->randomMachineName() . '.txt';
274 $test_filepath_updated = 'public://' . $test_filename_updated;
275 file_put_contents($test_filepath, $this->randomMachineName());
276 file_put_contents($test_filepath_updated, $this->randomMachineName());
277
278 // Check if the name field is properly hidden on the media form.
279 $this->drupalGet('media/add/file');
280 $assert_session->fieldNotExists('name');
281
282 // Check if the source field is available.
283 $assert_session->fieldExists("files[{$source_field_id}_0]");
284
285 // Create a media item.
286 $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath));
287 $result = $assert_session->waitForButton('Remove');
288 $this->assertNotEmpty($result);
289 $page->pressButton('Save');
290 $file_media_id = $this->container
291 ->get('entity_type.manager')
292 ->getStorage('media')
293 ->getQuery()
294 ->sort('mid', 'DESC')
295 ->execute();
296 $file_media_id = reset($file_media_id);
297
298 // Reference the created media using an entity_reference field and make sure
299 // the output is what we expect.
300 $node = Node::create([
301 'title' => 'Host node',
302 'type' => 'article',
303 'field_related_media' => [
304 'target_id' => $file_media_id,
305 ],
306 ]);
307 $node->save();
308
309 $this->drupalGet('/node/' . $node->id());
310
311 // Check if the default media name is generated as expected.
312 $media = \Drupal::entityTypeManager()->getStorage('media')->loadUnchanged($file_media_id);
313 $this->assertSame($test_filename, $media->label());
314
315 // Here we expect to see only the linked filename. Assert only one element
316 // in the content region.
317 $assert_session->elementsCount('css', 'article.media--type-file > *', 1);
318
319 // Assert the file link is present in the media element and its text matches
320 // the filename.
321 $link_element = $assert_session->elementExists('css', 'article.media--type-file .field--name-field-media-file a');
322 $this->assertSame($test_filename, $link_element->getText());
323
324 // Assert the media name is updated through the field mapping when changing
325 // the source field.
326 $this->drupalGet('media/' . $file_media_id . '/edit');
327 $page->pressButton('Remove');
328 $result = $assert_session->waitForField("files[{$source_field_id}_0]");
329 $this->assertNotEmpty($result);
330 $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath_updated));
331 $result = $assert_session->waitForButton('Remove');
332 $this->assertNotEmpty($result);
333 $page->pressButton('Save');
334
335 $this->drupalGet('/node/' . $node->id());
336
337 // Check if the default media name is updated as expected.
338 $media = \Drupal::entityTypeManager()->getStorage('media')->loadUnchanged($file_media_id);
339 $this->assertSame($test_filename_updated, $media->label());
340
341 // Again we expect to see only the linked filename. Assert only one element
342 // in the content region.
343 $assert_session->elementsCount('css', 'article.media--type-file > *', 1);
344
345 // Assert the file link is present in the media element and its text matches
346 // the updated filename.
347 $link_element = $assert_session->elementExists('css', 'article.media--type-file .field--name-field-media-file a');
348 $this->assertSame($test_filename_updated, $link_element->getText());
349 }
350
351 /**
352 * Test the standard profile configuration for media type 'remote_video'.
353 */
354 protected function remoteVideoTest() {
355 $assert_session = $this->assertSession();
356 $page = $this->getSession()->getPage();
357 $source_field_id = 'field_media_oembed_video';
358
359 // Set video fixtures.
360 $video_title = 'Drupal Rap Video - Schipulcon09';
361 $video_url = 'https://vimeo.com/7073899';
362 ResourceController::setResourceUrl($video_url, $this->getFixturesDirectory() . '/video_vimeo.json');
363 $video_title_updated = "Everyday I'm Drupalin' Drupal Rap (Rick Ross - Hustlin)";
364 $video_url_updated = 'https://www.youtube.com/watch?v=PWjcqE3QKBg';
365 ResourceController::setResourceUrl($video_url_updated, $this->getFixturesDirectory() . '/video_youtube.json');
366
367 // Check if the name field is properly hidden on the media form.
368 $this->drupalGet('media/add/remote_video');
369 $assert_session->fieldNotExists('name');
370
371 // Check if the source field is available.
372 $assert_session->fieldExists("{$source_field_id}[0][value]");
373
374 // Create a media item.
375 $page->fillField("{$source_field_id}[0][value]", $video_url);
376 $page->pressButton('Save');
377 $remote_video_media_id = $this->container
378 ->get('entity_type.manager')
379 ->getStorage('media')
380 ->getQuery()
381 ->sort('mid', 'DESC')
382 ->execute();
383 $remote_video_media_id = reset($remote_video_media_id);
384
385 // Reference the created media using an entity_reference field and make sure
386 // the output is what we expect.
387 $node = Node::create([
388 'title' => 'Host node',
389 'type' => 'article',
390 'field_related_media' => [
391 'target_id' => $remote_video_media_id,
392 ],
393 ]);
394 $node->save();
395
396 $this->drupalGet('/node/' . $node->id());
397
398 // Check if the default media name is generated as expected.
399 $media = \Drupal::entityTypeManager()->getStorage('media')->loadUnchanged($remote_video_media_id);
400 $this->assertSame($video_title, $media->label());
401
402 // Here we expect to see only the video iframe. Assert only one element in
403 // the content region.
404 $assert_session->elementsCount('css', 'article.media--type-remote-video > *', 1);
405
406 // Assert the iframe is present in the media element and its src attribute
407 // matches the URL and query parameters.
408 $iframe_url = $assert_session->elementExists('css', 'article.media--type-remote-video .field--name-field-media-oembed-video iframe')->getAttribute('src');
409 $iframe_url = parse_url($iframe_url);
410 $this->assertStringEndsWith('/media/oembed', $iframe_url['path']);
411 $this->assertNotEmpty($iframe_url['query']);
412 $query = [];
413 parse_str($iframe_url['query'], $query);
414 $this->assertSame($video_url, $query['url']);
415 $this->assertNotEmpty($query['hash']);
416
417 // Assert the media name is updated through the field mapping when changing
418 // the source field.
419 $this->drupalGet('media/' . $remote_video_media_id . '/edit');
420 $page->fillField("{$source_field_id}[0][value]", $video_url_updated);
421 $page->pressButton('Save');
422
423 $this->drupalGet('/node/' . $node->id());
424
425 // Check if the default media name is updated as expected.
426 $media = \Drupal::entityTypeManager()->getStorage('media')->loadUnchanged($remote_video_media_id);
427 $this->assertSame($video_title_updated, $media->label());
428
429 // Again we expect to see only the video iframe. Assert only one element in
430 // the content region.
431 $assert_session->elementsCount('css', 'article.media--type-remote-video > *', 1);
432
433 // Assert the iframe is present in the media element and its src attribute
434 // matches the updated URL and query parameters.
435 $iframe_url = $assert_session->elementExists('css', 'article.media--type-remote-video .field--name-field-media-oembed-video iframe')->getAttribute('src');
436 $iframe_url = parse_url($iframe_url);
437 $this->assertStringEndsWith('/media/oembed', $iframe_url['path']);
438 $this->assertNotEmpty($iframe_url['query']);
439 $query = [];
440 parse_str($iframe_url['query'], $query);
441 $this->assertSame($video_url_updated, $query['url']);
442 $this->assertNotEmpty($query['hash']);
443 }
444
445 /**
446 * Test the standard profile configuration for media type 'video'.
447 */
448 protected function videoTest() {
449 $assert_session = $this->assertSession();
450 $page = $this->getSession()->getPage();
451 $source_field_id = 'field_media_video_file';
452
453 // Create 2 test files.
454 $test_filename = $this->randomMachineName() . '.mp4';
455 $test_filepath = 'public://' . $test_filename;
456 $test_filename_updated = $this->randomMachineName() . '.mp4';
457 $test_filepath_updated = 'public://' . $test_filename_updated;
458 file_put_contents($test_filepath, str_repeat('t', 10));
459 file_put_contents($test_filepath_updated, str_repeat('u', 10));
460
461 // Check if the name field is properly hidden on the media form.
462 $this->drupalGet('media/add/video');
463 $assert_session->fieldNotExists('name');
464
465 // Check if the source field is available.
466 $assert_session->fieldExists("files[{$source_field_id}_0]");
467
468 // Create a media item.
469 $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath));
470 $result = $assert_session->waitForButton('Remove');
471 $this->assertNotEmpty($result);
472 $page->pressButton('Save');
473 $video_media_id = $this->container
474 ->get('entity_type.manager')
475 ->getStorage('media')
476 ->getQuery()
477 ->sort('mid', 'DESC')
478 ->execute();
479 $video_media_id = reset($video_media_id);
480
481 // Reference the created media using an entity_reference field and make sure
482 // the output is what we expect.
483 $node = Node::create([
484 'title' => 'Host node',
485 'type' => 'article',
486 'field_related_media' => [
487 'target_id' => $video_media_id,
488 ],
489 ]);
490 $node->save();
491
492 $this->drupalGet('/node/' . $node->id());
493
494 // Check if the default media name is generated as expected.
495 $media = \Drupal::entityTypeManager()->getStorage('media')->loadUnchanged($video_media_id);
496 $this->assertSame($test_filename, $media->label());
497
498 // Here we expect to see only the linked filename. Assert only one element
499 // in the content region.
500 $assert_session->elementsCount('css', 'article.media--type-video > *', 1);
501
502 // Assert the video element is present inside the media element and that its
503 // src attribute matches the video file.
504 $video_element = $assert_session->elementExists('css', 'article.media--type-video .field--name-field-media-video-file video > source');
505 $expected_video_src = file_url_transform_relative(file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/' . $test_filename)));
506 $this->assertSame($expected_video_src, $video_element->getAttribute('src'));
507
508 // Assert the media name is updated through the field mapping when changing
509 // the source field.
510 $this->drupalGet('media/' . $video_media_id . '/edit');
511 $page->pressButton('Remove');
512 $result = $assert_session->waitForField("files[{$source_field_id}_0]");
513 $this->assertNotEmpty($result);
514 $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath_updated));
515 $result = $assert_session->waitForButton('Remove');
516 $this->assertNotEmpty($result);
517 $page->pressButton('Save');
518
519 $this->drupalGet('/node/' . $node->id());
520
521 // Check if the default media name is updated as expected.
522 $media = \Drupal::entityTypeManager()->getStorage('media')->loadUnchanged($video_media_id);
523 $this->assertSame($test_filename_updated, $media->label());
524
525 // Again we expect to see only the linked filename. Assert only one element
526 // in the content region.
527 $assert_session->elementsCount('css', 'article.media--type-video > *', 1);
528
529 // Assert the video element is present inside the media element and that its
530 // src attribute matches the updated video file.
531 $video_element = $assert_session->elementExists('css', 'article.media--type-video .field--name-field-media-video-file video > source');
532 $expected_video_src = file_url_transform_relative(file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/' . $test_filename_updated)));
533 $this->assertSame($expected_video_src, $video_element->getAttribute('src'));
534 }
535
536 }