Chris@14: container->get('config.installer')->installOptionalConfig($storage, ''); Chris@14: // Reset all the static caches and list caches. Chris@14: $this->container->get('config.factory')->reset(); Chris@18: Chris@18: // This test is going to test the display, so we need the standalone URL. Chris@18: \Drupal::configFactory() Chris@18: ->getEditable('media.settings') Chris@18: ->set('standalone_url', TRUE) Chris@18: ->save(TRUE); Chris@18: Chris@18: $this->container->get('router.builder')->rebuild(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Test basic media display. Chris@14: */ Chris@14: public function testMediaDisplay() { Chris@14: $assert_session = $this->assertSession(); Chris@14: $page = $this->getSession()->getPage(); Chris@14: Chris@17: $media_type = $this->createMediaType('test'); Chris@14: Chris@14: // Create a media item. Chris@14: $media = Media::create([ Chris@14: 'bundle' => $media_type->id(), Chris@14: 'name' => 'Fantastic!', Chris@14: ]); Chris@14: $media->save(); Chris@14: Chris@14: $this->drupalGet('media/' . $media->id()); Chris@14: // Verify the "name" field is really not present. Chris@14: $assert_session->elementNotExists('css', '.field--name-name'); Chris@14: Chris@14: // Enable the field on the display and verify it becomes visible on the UI. Chris@14: $this->drupalGet("/admin/structure/media/manage/{$media_type->id()}/display"); Chris@17: $assert_session->buttonExists('Show row weights')->press(); Chris@17: $this->assertSession()->waitForElementVisible('css', '[name="fields[name][region]"]'); Chris@14: $page->selectFieldOption('fields[name][region]', 'content'); Chris@14: $assert_session->waitForElementVisible('css', '#edit-fields-name-settings-edit'); Chris@14: $page->pressButton('Save'); Chris@14: $this->drupalGet('media/' . $media->id()); Chris@14: // Verify the name is present, and its text matches what is expected. Chris@14: $assert_session->elementExists('css', '.field--name-name'); Chris@14: $name_field = $page->find('css', '.field--name-name .field__item'); Chris@17: $this->assertSame($media->label(), $name_field->getText()); Chris@14: Chris@14: // In the standard profile, there are some pre-cooked types. Make sure the Chris@14: // elements configured on their displays are the expected ones. Chris@14: $this->drupalGet('media/add/image'); Chris@18: $image_media_name = 'example_1.jpeg'; Chris@18: $page->attachFileToField('files[field_media_image_0]', $this->root . '/core/modules/media/tests/fixtures/' . $image_media_name); Chris@14: $result = $assert_session->waitForButton('Remove'); Chris@14: $this->assertNotEmpty($result); Chris@14: $page->fillField('field_media_image[0][alt]', 'Image Alt Text 1'); Chris@14: $page->pressButton('Save'); Chris@17: $image_media_id = $this->container Chris@17: ->get('entity_type.manager') Chris@17: ->getStorage('media') Chris@17: ->getQuery() Chris@14: ->sort('mid', 'DESC') Chris@14: ->execute(); Chris@14: $image_media_id = reset($image_media_id); Chris@14: Chris@17: // Go to the media entity view. Chris@17: $this->drupalGet('/media/' . $image_media_id); Chris@17: Chris@18: // Check if the default media name is generated as expected. Chris@18: $assert_session->elementTextContains('css', 'h1', $image_media_name); Chris@14: // Here we expect to see only the image, nothing else. Chris@14: // Assert only one element in the content region. Chris@17: $this->assertSame(1, count($page->findAll('css', '.media--type-image > div'))); Chris@14: // Assert the image is present inside the media element. Chris@14: $media_item = $assert_session->elementExists('css', '.media--type-image > div'); Chris@14: $assert_session->elementExists('css', 'img', $media_item); Chris@14: // Assert that the image src is the original image and not an image style. Chris@14: $media_image = $assert_session->elementExists('css', '.media--type-image img'); Chris@14: $expected_image_src = file_url_transform_relative(file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/example_1.jpeg'))); Chris@17: $this->assertSame($expected_image_src, $media_image->getAttribute('src')); Chris@14: Chris@14: $test_filename = $this->randomMachineName() . '.txt'; Chris@14: $test_filepath = 'public://' . $test_filename; Chris@14: file_put_contents($test_filepath, $this->randomMachineName()); Chris@14: $this->drupalGet("media/add/file"); Chris@14: $page->attachFileToField("files[field_media_file_0]", \Drupal::service('file_system')->realpath($test_filepath)); Chris@14: $result = $assert_session->waitForButton('Remove'); Chris@14: $this->assertNotEmpty($result); Chris@14: $page->pressButton('Save'); Chris@14: Chris@17: // Go to the media entity view. Chris@17: $this->drupalGet($this->assertLinkToCreatedMedia()); Chris@17: Chris@18: // Check if the default media name is generated as expected. Chris@18: $assert_session->elementTextContains('css', 'h1', $test_filename); Chris@14: // Here we expect to see only the linked filename. Chris@14: // Assert only one element in the content region. Chris@17: $this->assertSame(1, count($page->findAll('css', 'article.media--type-file > div'))); Chris@14: // Assert the file link is present, and its text matches the filename. Chris@14: $assert_session->elementExists('css', 'article.media--type-file .field--name-field-media-file a'); Chris@14: $link = $page->find('css', 'article.media--type-file .field--name-field-media-file a'); Chris@17: $this->assertSame($test_filename, $link->getText()); Chris@14: Chris@14: // Create a node type "page" to use as host entity. Chris@14: $node_type = NodeType::create([ Chris@14: 'type' => 'page', Chris@14: 'name' => 'Page', Chris@14: ]); Chris@14: $node_type->save(); Chris@14: Chris@17: // Reference the created media using an entity_reference field and make sure Chris@14: // the output is what we expect. Chris@14: $storage = FieldStorageConfig::create([ Chris@14: 'entity_type' => 'node', Chris@14: 'field_name' => 'field_related_media', Chris@14: 'type' => 'entity_reference', Chris@14: 'settings' => [ Chris@14: 'target_type' => 'media', Chris@14: ], Chris@14: ]); Chris@14: $storage->save(); Chris@14: Chris@14: FieldConfig::create([ Chris@14: 'field_storage' => $storage, Chris@14: 'entity_type' => 'node', Chris@14: 'bundle' => $node_type->id(), Chris@14: 'label' => 'Related media', Chris@14: 'settings' => [ Chris@14: 'handler_settings' => [ Chris@14: 'target_bundles' => [ Chris@14: 'image' => 'image', Chris@14: ], Chris@14: ], Chris@14: ], Chris@14: ])->save(); Chris@14: Chris@14: entity_get_display('node', $node_type->id(), 'default') Chris@14: ->setComponent('field_related_media', [ Chris@14: 'type' => 'entity_reference_entity_view', Chris@14: 'label' => 'hidden', Chris@14: 'settings' => [ Chris@14: 'view_mode' => 'full', Chris@14: ], Chris@14: ])->save(); Chris@14: Chris@14: $node = Node::create([ Chris@14: 'title' => 'Host node', Chris@14: 'type' => $node_type->id(), Chris@14: 'field_related_media' => [ Chris@14: 'target_id' => $image_media_id, Chris@14: ], Chris@14: ]); Chris@14: $node->save(); Chris@14: Chris@14: $this->drupalGet('/node/' . $node->id()); Chris@14: // Media field is there. Chris@14: $assert_session->elementExists('css', '.field--name-field-related-media'); Chris@14: // Media name element is not there. Chris@14: $assert_session->elementNotExists('css', '.field--name-name'); Chris@14: $assert_session->pageTextNotContains($image_media_name); Chris@14: // Only one element is present inside the media container. Chris@17: $this->assertSame(1, count($page->findAll('css', '.field--name-field-related-media article.media--type-image > div'))); Chris@14: // Assert the image is present. Chris@14: $assert_session->elementExists('css', '.field--name-field-related-media article.media--type-image img'); Chris@14: } Chris@14: Chris@14: }