8a55c5045872632a68a5d6acd824d17b9e74731e
[yaffs-website] / web / core / modules / media / tests / src / FunctionalJavascript / MediaDisplayTest.php
1 <?php
2
3 namespace Drupal\Tests\media\FunctionalJavascript;
4
5 use Drupal\Core\Config\FileStorage;
6 use Drupal\Core\Config\InstallStorage;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\media\Entity\Media;
10 use Drupal\node\Entity\Node;
11 use Drupal\node\Entity\NodeType;
12
13 /**
14  * Basic display tests for Media.
15  *
16  * @group media
17  */
18 class MediaDisplayTest extends MediaJavascriptTestBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function setUp() {
24     parent::setUp();
25
26     // Install the optional configs from the standard profile.
27     $extension_path = drupal_get_path('profile', 'standard');
28     $optional_install_path = $extension_path . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
29     $storage = new FileStorage($optional_install_path);
30     $this->container->get('config.installer')->installOptionalConfig($storage, '');
31     // Reset all the static caches and list caches.
32     $this->container->get('config.factory')->reset();
33   }
34
35   /**
36    * Test basic media display.
37    */
38   public function testMediaDisplay() {
39     $assert_session = $this->assertSession();
40     $page = $this->getSession()->getPage();
41
42     $media_type = $this->createMediaType('test');
43
44     // Create a media item.
45     $media = Media::create([
46       'bundle' => $media_type->id(),
47       'name' => 'Fantastic!',
48     ]);
49     $media->save();
50
51     $this->drupalGet('media/' . $media->id());
52     // Verify the "name" field is really not present.
53     $assert_session->elementNotExists('css', '.field--name-name');
54
55     // Enable the field on the display and verify it becomes visible on the UI.
56     $this->drupalGet("/admin/structure/media/manage/{$media_type->id()}/display");
57     $assert_session->buttonExists('Show row weights')->press();
58     $this->assertSession()->waitForElementVisible('css', '[name="fields[name][region]"]');
59     $page->selectFieldOption('fields[name][region]', 'content');
60     $assert_session->waitForElementVisible('css', '#edit-fields-name-settings-edit');
61     $page->pressButton('Save');
62     $this->drupalGet('media/' . $media->id());
63     // Verify the name is present, and its text matches what is expected.
64     $assert_session->elementExists('css', '.field--name-name');
65     $name_field = $page->find('css', '.field--name-name .field__item');
66     $this->assertSame($media->label(), $name_field->getText());
67
68     // In the standard profile, there are some pre-cooked types. Make sure the
69     // elements configured on their displays are the expected ones.
70     $this->drupalGet('media/add/image');
71     $image_media_name = 'Fantastic image asset!';
72     $page->fillField('name[0][value]', $image_media_name);
73     $page->attachFileToField('files[field_media_image_0]', $this->root . '/core/modules/media/tests/fixtures/example_1.jpeg');
74     $result = $assert_session->waitForButton('Remove');
75     $this->assertNotEmpty($result);
76     $page->fillField('field_media_image[0][alt]', 'Image Alt Text 1');
77     $page->pressButton('Save');
78     $image_media_id = $this->container
79       ->get('entity_type.manager')
80       ->getStorage('media')
81       ->getQuery()
82       ->sort('mid', 'DESC')
83       ->execute();
84     $image_media_id = reset($image_media_id);
85
86     // Go to the media entity view.
87     $this->drupalGet('/media/' . $image_media_id);
88
89     // Here we expect to see only the image, nothing else.
90     // Assert only one element in the content region.
91     $this->assertSame(1, count($page->findAll('css', '.media--type-image > div')));
92     // Assert the image is present inside the media element.
93     $media_item = $assert_session->elementExists('css', '.media--type-image > div');
94     $assert_session->elementExists('css', 'img', $media_item);
95     // Assert that the image src is the original image and not an image style.
96     $media_image = $assert_session->elementExists('css', '.media--type-image img');
97     $expected_image_src = file_url_transform_relative(file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/example_1.jpeg')));
98     $this->assertSame($expected_image_src, $media_image->getAttribute('src'));
99
100     $test_filename = $this->randomMachineName() . '.txt';
101     $test_filepath = 'public://' . $test_filename;
102     file_put_contents($test_filepath, $this->randomMachineName());
103     $this->drupalGet("media/add/file");
104     $page->fillField('name[0][value]', 'File media 1');
105     $page->attachFileToField("files[field_media_file_0]", \Drupal::service('file_system')->realpath($test_filepath));
106     $result = $assert_session->waitForButton('Remove');
107     $this->assertNotEmpty($result);
108     $page->pressButton('Save');
109
110     // Go to the media entity view.
111     $this->drupalGet($this->assertLinkToCreatedMedia());
112
113     // Here we expect to see only the linked filename.
114     // Assert only one element in the content region.
115     $this->assertSame(1, count($page->findAll('css', 'article.media--type-file > div')));
116     // Assert the file link is present, and its text matches the filename.
117     $assert_session->elementExists('css', 'article.media--type-file .field--name-field-media-file a');
118     $link = $page->find('css', 'article.media--type-file .field--name-field-media-file a');
119     $this->assertSame($test_filename, $link->getText());
120
121     // Create a node type "page" to use as host entity.
122     $node_type = NodeType::create([
123       'type' => 'page',
124       'name' => 'Page',
125     ]);
126     $node_type->save();
127
128     // Reference the created media using an entity_reference field and make sure
129     // the output is what we expect.
130     $storage = FieldStorageConfig::create([
131       'entity_type' => 'node',
132       'field_name' => 'field_related_media',
133       'type' => 'entity_reference',
134       'settings' => [
135         'target_type' => 'media',
136       ],
137     ]);
138     $storage->save();
139
140     FieldConfig::create([
141       'field_storage' => $storage,
142       'entity_type' => 'node',
143       'bundle' => $node_type->id(),
144       'label' => 'Related media',
145       'settings' => [
146         'handler_settings' => [
147           'target_bundles' => [
148             'image' => 'image',
149           ],
150         ],
151       ],
152     ])->save();
153
154     entity_get_display('node', $node_type->id(), 'default')
155       ->setComponent('field_related_media', [
156         'type' => 'entity_reference_entity_view',
157         'label' => 'hidden',
158         'settings' => [
159           'view_mode' => 'full',
160         ],
161       ])->save();
162
163     $node = Node::create([
164       'title' => 'Host node',
165       'type' => $node_type->id(),
166       'field_related_media' => [
167         'target_id' => $image_media_id,
168       ],
169     ]);
170     $node->save();
171
172     $this->drupalGet('/node/' . $node->id());
173     // Media field is there.
174     $assert_session->elementExists('css', '.field--name-field-related-media');
175     // Media name element is not there.
176     $assert_session->elementNotExists('css', '.field--name-name');
177     $assert_session->pageTextNotContains($image_media_name);
178     // Only one element is present inside the media container.
179     $this->assertSame(1, count($page->findAll('css', '.field--name-field-related-media article.media--type-image > div')));
180     // Assert the image is present.
181     $assert_session->elementExists('css', '.field--name-field-related-media article.media--type-image img');
182   }
183
184 }