annotate core/modules/media/tests/src/FunctionalJavascript/MediaStandardProfileTest.php @ 5:12f9dff5fda9 tip

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