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