981da61d59abe21dcc6c71776fdb5adc48d265d2
[yaffs-website] / web / core / modules / media / tests / src / FunctionalJavascript / MediaUiJavascriptTest.php
1 <?php
2
3 namespace Drupal\Tests\media\FunctionalJavascript;
4
5 use Drupal\field\FieldConfigInterface;
6 use Drupal\media\Entity\Media;
7 use Drupal\media\Entity\MediaType;
8 use Drupal\media\MediaSourceInterface;
9
10 /**
11  * Ensures that media UI works correctly.
12  *
13  * @group media
14  */
15 class MediaUiJavascriptTest extends MediaJavascriptTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = [
23     'block',
24     'media_test_source',
25   ];
26
27   /**
28    * The test media type.
29    *
30    * @var \Drupal\media\MediaTypeInterface
31    */
32   protected $testMediaType;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39     $this->drupalPlaceBlock('local_actions_block');
40     $this->drupalPlaceBlock('local_tasks_block');
41
42     // We need to test without any default configuration in place.
43     // @TODO: Remove this as part of https://www.drupal.org/node/2883813.
44     MediaType::load('file')->delete();
45     MediaType::load('image')->delete();
46   }
47
48   /**
49    * Tests a media type administration.
50    */
51   public function testMediaTypes() {
52     $session = $this->getSession();
53     $page = $session->getPage();
54     $assert_session = $this->assertSession();
55
56     $this->drupalGet('admin/structure/media');
57     $assert_session->pageTextContains('No media types available. Add media type.');
58     $assert_session->linkExists('Add media type');
59
60     // Test the creation of a media type using the UI.
61     $name = $this->randomMachineName();
62     $description = $this->randomMachineName();
63     $this->drupalGet('admin/structure/media/add');
64     $page->fillField('label', $name);
65     $machine_name = strtolower($name);
66     $this->assertJsCondition("jQuery('.machine-name-value').html() == '$machine_name'");
67     $page->selectFieldOption('source', 'test');
68     $this->assertJsCondition("jQuery('.form-item-source-configuration-test-config-value').length > 0;");
69     $page->fillField('description', $description);
70     $page->pressButton('Save');
71     $assert_session->pageTextContains('The media type ' . $name . ' has been added.');
72     $this->drupalGet('admin/structure/media');
73     $assert_session->pageTextContains($name);
74     $assert_session->pageTextContains($description);
75
76     // We need to clear the statically cached field definitions to account for
77     // fields that have been created by API calls in this test, since they exist
78     // in a separate memory space from the web server.
79     $this->container->get('entity_field.manager')->clearCachedFieldDefinitions();
80     // Assert that the field and field storage were created.
81     $media_type = MediaType::load($machine_name);
82     $source = $media_type->getSource();
83     /** @var \Drupal\field\FieldConfigInterface $source_field */
84     $source_field = $source->getSourceFieldDefinition($media_type);
85     $this->assertInstanceOf(FieldConfigInterface::class, $source_field, 'Source field exists.');
86     $this->assertFalse($source_field->isNew(), 'Source field was saved.');
87     /** @var \Drupal\field\FieldStorageConfigInterface $storage */
88     $storage = $source_field->getFieldStorageDefinition();
89     $this->assertFalse($storage->isNew(), 'Source field storage definition was saved.');
90     $this->assertFalse($storage->isLocked(), 'Source field storage definition was not locked.');
91
92     /** @var \Drupal\media\MediaTypeInterface $media_type_storage */
93     $media_type_storage = $this->container->get('entity_type.manager')->getStorage('media_type');
94     $this->testMediaType = $media_type_storage->load(strtolower($name));
95
96     // Check if all action links exist.
97     $assert_session->linkByHrefExists('admin/structure/media/add');
98     $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id());
99     $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id() . '/fields');
100     $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id() . '/form-display');
101     $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id() . '/display');
102
103     // Assert that fields have expected values before editing.
104     $page->clickLink('Edit');
105     $assert_session->fieldValueEquals('label', $name);
106     $assert_session->fieldValueEquals('description', $description);
107     $assert_session->fieldValueEquals('source', 'test');
108     $assert_session->fieldValueEquals('label', $name);
109     $assert_session->checkboxNotChecked('edit-options-new-revision');
110     $assert_session->checkboxChecked('edit-options-status');
111     $assert_session->checkboxNotChecked('edit-options-queue-thumbnail-downloads');
112     $assert_session->pageTextContains('Create new revision');
113     $assert_session->pageTextContains('Automatically create new revisions. Users with the "Administer media" permission will be able to override this option.');
114     $assert_session->pageTextContains('Download thumbnails via a queue.');
115     $assert_session->pageTextContains('Media will be automatically published when created.');
116     $assert_session->pageTextContains('Media sources can provide metadata fields such as title, caption, size information, credits, etc. Media can automatically save this metadata information to entity fields, which can be configured below. Information will only be mapped if the entity field is empty.');
117
118     // Try to change media type and check if new configuration sub-form appears.
119     $page->selectFieldOption('source', 'test');
120     $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
121     $this->assertNotEmpty($result);
122     $assert_session->fieldExists('Test config value');
123     $assert_session->fieldValueEquals('Test config value', 'This is default value.');
124     $assert_session->fieldExists('Attribute 1');
125     $assert_session->fieldExists('Attribute 2');
126
127     // Test if the edit machine name is not editable.
128     $assert_session->fieldDisabled('Machine-readable name');
129
130     // Edit and save media type form fields with new values.
131     $new_name = $this->randomMachineName();
132     $new_description = $this->randomMachineName();
133     $page->fillField('label', $new_name);
134     $page->fillField('description', $new_description);
135     $page->selectFieldOption('source', 'test');
136     $page->fillField('Test config value', 'This is new config value.');
137     $page->selectFieldOption('field_map[attribute_1]', 'name');
138     $page->checkField('options[new_revision]');
139     $page->uncheckField('options[status]');
140     $page->checkField('options[queue_thumbnail_downloads]');
141     $page->pressButton('Save');
142     $assert_session->pageTextContains("The media type $new_name has been updated.");
143
144     // Test if edit worked and if new field values have been saved as expected.
145     $this->drupalGet('admin/structure/media/manage/' . $this->testMediaType->id());
146     $assert_session->fieldValueEquals('label', $new_name);
147     $assert_session->fieldValueEquals('description', $new_description);
148     $assert_session->fieldValueEquals('source', 'test');
149     $assert_session->checkboxChecked('options[new_revision]');
150     $assert_session->checkboxNotChecked('options[status]');
151     $assert_session->checkboxChecked('options[queue_thumbnail_downloads]');
152     $assert_session->fieldValueEquals('Test config value', 'This is new config value.');
153     $assert_session->fieldValueEquals('Attribute 1', 'name');
154     $assert_session->fieldValueEquals('Attribute 2', MediaSourceInterface::METADATA_FIELD_EMPTY);
155
156     /** @var \Drupal\media\MediaTypeInterface $loaded_media_type */
157     $loaded_media_type = $this->container->get('entity_type.manager')
158       ->getStorage('media_type')
159       ->load($this->testMediaType->id());
160     $this->assertEquals($loaded_media_type->id(), $this->testMediaType->id());
161     $this->assertEquals($loaded_media_type->label(), $new_name);
162     $this->assertEquals($loaded_media_type->getDescription(), $new_description);
163     $this->assertEquals($loaded_media_type->getSource()->getPluginId(), 'test');
164     $this->assertEquals($loaded_media_type->getSource()->getConfiguration()['test_config_value'], 'This is new config value.');
165     $this->assertTrue($loaded_media_type->shouldCreateNewRevision());
166     $this->assertTrue($loaded_media_type->thumbnailDownloadsAreQueued());
167     $this->assertFalse($loaded_media_type->getStatus());
168     $this->assertEquals($loaded_media_type->getFieldMap(), ['attribute_1' => 'name']);
169
170     // We need to clear the statically cached field definitions to account for
171     // fields that have been created by API calls in this test, since they exist
172     // in a separate memory space from the web server.
173     $this->container->get('entity_field.manager')->clearCachedFieldDefinitions();
174
175     // Test that a media item being created with default status to "FALSE",
176     // will be created unpublished.
177     /** @var \Drupal\media\MediaInterface $unpublished_media */
178     $unpublished_media = Media::create(['name' => 'unpublished test media', 'bundle' => $loaded_media_type->id()]);
179     $this->assertFalse($unpublished_media->isPublished());
180     $unpublished_media->delete();
181
182     // Tests media type delete form.
183     $page->clickLink('Delete');
184     $assert_session->addressEquals('admin/structure/media/manage/' . $this->testMediaType->id() . '/delete');
185     $page->pressButton('Delete');
186     $assert_session->addressEquals('admin/structure/media');
187     $assert_session->pageTextContains('The media type ' . $new_name . ' has been deleted.');
188
189     // Test that the system for preventing the deletion of media types works
190     // (they cannot be deleted if there is media content of that type/bundle).
191     $media_type2 = $this->createMediaType();
192     $label2 = $media_type2->label();
193     $media = Media::create(['name' => 'lorem ipsum', 'bundle' => $media_type2->id()]);
194     $media->save();
195     $this->drupalGet('admin/structure/media/manage/' . $media_type2->id());
196     $page->clickLink('Delete');
197     $assert_session->addressEquals('admin/structure/media/manage/' . $media_type2->id() . '/delete');
198     $assert_session->buttonNotExists('edit-submit');
199     $assert_session->pageTextContains("$label2 is used by 1 media item on your site. You can not remove this media type until you have removed all of the $label2 media items.");
200   }
201
202 }