annotate core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php @ 19:fa3358dc1485 tip

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