3 namespace Drupal\Tests\media\FunctionalJavascript;
5 use Drupal\Component\Utility\Html;
8 * Tests the media type creation.
12 class MediaTypeCreationTest extends MediaJavascriptTestBase {
15 * Tests the source field behavior on the add media type form.
17 public function testSourceChangeOnMediaTypeCreationForm() {
18 $session = $this->getSession();
19 $page = $session->getPage();
20 $assert_session = $this->assertSession();
22 $label = 'Type with Default Field';
23 $mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
25 $this->drupalGet('admin/structure/media/add');
27 // Fill in a label to the media type.
28 $page->fillField('label', $label);
29 $this->assertNotEmpty(
30 $assert_session->waitForElementVisible('css', '.machine-name-value')
33 // Select the media source used by our media type.
34 $assert_session->selectExists('Media source')->selectOption('test_different_displays');
35 $this->assertNotEmpty(
36 $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]')
39 // Change the media source.
40 $assert_session->selectExists('Media source')->selectOption('test');
41 $this->assertNotEmpty(
42 $assert_session->waitForElement('css', 'fieldset[data-drupal-selector="edit-source-configuration"] .fieldset-wrapper .placeholder:contains("Text (plain)")')
45 $page->pressButton('Save');
47 // Check that source can not be changed anymore.
48 $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}");
49 $assert_session->pageTextContains('The media source cannot be changed after the media type is created');
50 $assert_session->fieldDisabled('Media source');
54 * Tests the media type creation form.
56 public function testMediaTypeCreationFormWithDefaultField() {
57 $session = $this->getSession();
58 $page = $session->getPage();
59 $assert_session = $this->assertSession();
61 $label = 'Type with Default Field';
62 $mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
64 $this->drupalGet('admin/structure/media/add');
66 // Select the media source used by our media type. Do this before setting
67 // the label or machine name in order to guard against the regression in
68 // https://www.drupal.org/project/drupal/issues/2557299.
69 $assert_session->fieldExists('Media source');
70 $assert_session->optionExists('Media source', 'test');
71 $page->selectFieldOption('Media source', 'test');
72 $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
73 $this->assertNotEmpty($result);
75 // Fill in a label to the media type.
76 $page->fillField('label', $label);
77 // Wait for machine name generation. Default: waitUntilVisible(), does not
79 $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
81 $page->pressButton('Save');
83 // Check whether the source field was correctly created.
84 $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields");
86 // Check 2nd column of first data row, to be machine name for field name.
87 $assert_session->elementContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[2]', 'field_media_test');
88 // Check 3rd column of first data row, to be correct field type.
89 $assert_session->elementTextContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[3]', 'Text (plain)');
91 // Check that the source field is correctly assigned to media type.
92 $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}");
94 $assert_session->pageTextContains('Test source field is used to store the essential information about the media item.');
96 // Check that the plugin cannot be changed after it is set on type creation.
97 $assert_session->fieldDisabled('Media source');
98 $assert_session->pageTextContains('The media source cannot be changed after the media type is created.');
100 // Open up the media add form and verify that the source field is right
101 // after the name, and before the vertical tabs.
102 $this->drupalGet("/media/add/$mediaTypeMachineName");
104 // Get the form element, and its HTML representation.
105 $form_selector = '#media-' . Html::cleanCssIdentifier($mediaTypeMachineName) . '-add-form';
106 $form = $assert_session->elementExists('css', $form_selector);
107 $form_html = $form->getOuterHtml();
109 // The name field should come before the source field, which should itself
110 // come before the vertical tabs.
111 $name_field = $assert_session->fieldExists('Name', $form)->getOuterHtml();
112 $test_source_field = $assert_session->fieldExists('Test source', $form)->getOuterHtml();
113 $vertical_tabs = $assert_session->elementExists('css', '.vertical-tabs', $form)->getOuterHtml();
114 $date_field = $assert_session->fieldExists('Date', $form)->getOuterHtml();
115 $published_checkbox = $assert_session->fieldExists('Published', $form)->getOuterHtml();
116 $this->assertTrue(strpos($form_html, $test_source_field) > strpos($form_html, $name_field));
117 $this->assertTrue(strpos($form_html, $vertical_tabs) > strpos($form_html, $test_source_field));
118 // The "Published" checkbox should be the last element.
119 $this->assertTrue(strpos($form_html, $published_checkbox) > strpos($form_html, $date_field));
121 // Check that a new type with the same machine name cannot be created.
122 $this->drupalGet('admin/structure/media/add');
123 $page->fillField('label', $label);
124 $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
125 $page->selectFieldOption('Media source', 'test');
126 $assert_session->assertWaitOnAjaxRequest();
127 $page->pressButton('Save');
128 $assert_session->pageTextContains('The machine-readable name is already in use. It must be unique.');
132 * Test creation of media type, reusing an existing source field.
134 public function testMediaTypeCreationReuseSourceField() {
135 $session = $this->getSession();
136 $page = $session->getPage();
137 $assert_session = $this->assertSession();
139 // Create a new media type, which should create a new field we can reuse.
140 $this->drupalGet('/admin/structure/media/add');
141 // Choose the source plugin before setting the label and machine name.
142 $page->selectFieldOption('Media source', 'test');
143 $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
144 $this->assertNotEmpty($result);
145 $page->fillField('label', 'Pastafazoul');
146 $session->wait(5000, "jQuery('.machine-name-value').text() === 'pastafazoul'");
147 $page->pressButton('Save');
149 $label = 'Type reusing Default Field';
150 $mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
152 $this->drupalGet('admin/structure/media/add');
154 // Select the media source used by our media type. Do this before setting
155 // the label and machine name.
156 $assert_session->fieldExists('Media source');
157 $assert_session->optionExists('Media source', 'test');
158 $page->selectFieldOption('Media source', 'test');
159 $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
160 $this->assertNotEmpty($result);
161 // Select the existing field for re-use.
162 $page->selectFieldOption('source_configuration[source_field]', 'field_media_test');
164 // Fill in a label to the media type.
165 $page->fillField('label', $label);
167 // Wait for machine name generation. Default: waitUntilVisible(), does not
169 $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
171 $page->pressButton('Save');
173 // Check that no new fields were created.
174 $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields");
175 // The reused field should be present...
176 $assert_session->pageTextContains('field_media_test');
177 // ...not a new, unique one.
178 $assert_session->pageTextNotContains('field_media_test_1');