Updated to Drupal 8.5. Core Media not yet in use.
[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();
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     $page->selectFieldOption('fields[name][region]', 'content');
58     $assert_session->waitForElementVisible('css', '#edit-fields-name-settings-edit');
59     $page->pressButton('Save');
60     $this->drupalGet('media/' . $media->id());
61     // Verify the name is present, and its text matches what is expected.
62     $assert_session->elementExists('css', '.field--name-name');
63     $name_field = $page->find('css', '.field--name-name .field__item');
64     $this->assertEquals($media->label(), $name_field->getText());
65
66     // In the standard profile, there are some pre-cooked types. Make sure the
67     // elements configured on their displays are the expected ones.
68     $this->drupalGet('media/add/image');
69     $image_media_name = 'Fantastic image asset!';
70     $page->fillField('name[0][value]', $image_media_name);
71     $page->attachFileToField('files[field_media_image_0]', \Drupal::root() . '/core/modules/media/tests/fixtures/example_1.jpeg');
72     $result = $assert_session->waitForButton('Remove');
73     $this->assertNotEmpty($result);
74     $page->fillField('field_media_image[0][alt]', 'Image Alt Text 1');
75     $page->pressButton('Save');
76     $image_media_id = $this->container->get('entity.query')->get('media')
77       ->sort('mid', 'DESC')
78       ->execute();
79     $image_media_id = reset($image_media_id);
80
81     // Here we expect to see only the image, nothing else.
82     // Assert only one element in the content region.
83     $this->assertEquals(1, count($page->findAll('css', '.media--type-image > div')));
84     // Assert the image is present inside the media element.
85     $media_item = $assert_session->elementExists('css', '.media--type-image > div');
86     $assert_session->elementExists('css', 'img', $media_item);
87     // Assert that the image src is the original image and not an image style.
88     $media_image = $assert_session->elementExists('css', '.media--type-image img');
89     $expected_image_src = file_url_transform_relative(file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/example_1.jpeg')));
90     $this->assertEquals($expected_image_src, $media_image->getAttribute('src'));
91
92     $test_filename = $this->randomMachineName() . '.txt';
93     $test_filepath = 'public://' . $test_filename;
94     file_put_contents($test_filepath, $this->randomMachineName());
95     $this->drupalGet("media/add/file");
96     $page->fillField('name[0][value]', 'File media 1');
97     $page->attachFileToField("files[field_media_file_0]", \Drupal::service('file_system')->realpath($test_filepath));
98     $result = $assert_session->waitForButton('Remove');
99     $this->assertNotEmpty($result);
100     $page->pressButton('Save');
101
102     // Here we expect to see only the linked filename.
103     // Assert only one element in the content region.
104     $this->assertEquals(1, count($page->findAll('css', 'article.media--type-file > div')));
105     // Assert the file link is present, and its text matches the filename.
106     $assert_session->elementExists('css', 'article.media--type-file .field--name-field-media-file a');
107     $link = $page->find('css', 'article.media--type-file .field--name-field-media-file a');
108     $this->assertEquals($test_filename, $link->getText());
109
110     // Create a node type "page" to use as host entity.
111     $node_type = NodeType::create([
112       'type' => 'page',
113       'name' => 'Page',
114     ]);
115     $node_type->save();
116
117     // Reference the created media using an entity_refernce field and make sure
118     // the output is what we expect.
119     $storage = FieldStorageConfig::create([
120       'entity_type' => 'node',
121       'field_name' => 'field_related_media',
122       'type' => 'entity_reference',
123       'settings' => [
124         'target_type' => 'media',
125       ],
126     ]);
127     $storage->save();
128
129     FieldConfig::create([
130       'field_storage' => $storage,
131       'entity_type' => 'node',
132       'bundle' => $node_type->id(),
133       'label' => 'Related media',
134       'settings' => [
135         'handler_settings' => [
136           'target_bundles' => [
137             'image' => 'image',
138           ],
139         ],
140       ],
141     ])->save();
142
143     entity_get_display('node', $node_type->id(), 'default')
144       ->setComponent('field_related_media', [
145         'type' => 'entity_reference_entity_view',
146         'label' => 'hidden',
147         'settings' => [
148           'view_mode' => 'full',
149         ],
150       ])->save();
151
152     $node = Node::create([
153       'title' => 'Host node',
154       'type' => $node_type->id(),
155       'field_related_media' => [
156         'target_id' => $image_media_id,
157       ],
158     ]);
159     $node->save();
160
161     $this->drupalGet('/node/' . $node->id());
162     // Media field is there.
163     $assert_session->elementExists('css', '.field--name-field-related-media');
164     // Media name element is not there.
165     $assert_session->elementNotExists('css', '.field--name-name');
166     $assert_session->pageTextNotContains($image_media_name);
167     // Only one element is present inside the media container.
168     $this->assertEquals(1, count($page->findAll('css', '.field--name-field-related-media article.media--type-image > div')));
169     // Assert the image is present.
170     $assert_session->elementExists('css', '.field--name-field-related-media article.media--type-image img');
171   }
172
173 }