Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / media / tests / src / FunctionalJavascript / MediaTypeCreationTest.php
1 <?php
2
3 namespace Drupal\Tests\media\FunctionalJavascript;
4
5 use Drupal\Component\Utility\Html;
6
7 /**
8  * Tests the media type creation.
9  *
10  * @group media
11  */
12 class MediaTypeCreationTest extends MediaJavascriptTestBase {
13
14   /**
15    * Tests the source field behavior on the add media type form.
16    */
17   public function testSourceChangeOnMediaTypeCreationForm() {
18     $session = $this->getSession();
19     $page = $session->getPage();
20     $assert_session = $this->assertSession();
21
22     $label = 'Type with Default Field';
23     $mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
24
25     $this->drupalGet('admin/structure/media/add');
26
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')
31     );
32
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"]')
37     );
38
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)")')
43     );
44
45     $page->pressButton('Save');
46
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');
51   }
52
53   /**
54    * Tests the media type creation form.
55    */
56   public function testMediaTypeCreationFormWithDefaultField() {
57     $session = $this->getSession();
58     $page = $session->getPage();
59     $assert_session = $this->assertSession();
60
61     $label = 'Type with Default Field';
62     $mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
63
64     $this->drupalGet('admin/structure/media/add');
65
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);
74
75     // Fill in a label to the media type.
76     $page->fillField('label', $label);
77     // Wait for machine name generation. Default: waitUntilVisible(), does not
78     // work properly.
79     $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
80
81     $page->pressButton('Save');
82
83     // Check whether the source field was correctly created.
84     $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields");
85
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)');
90
91     // Check that the source field is correctly assigned to media type.
92     $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}");
93
94     $assert_session->pageTextContains('Test source field is used to store the essential information about the media item.');
95
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.');
99
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");
103
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();
108
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));
120
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.');
129   }
130
131   /**
132    * Test creation of media type, reusing an existing source field.
133    */
134   public function testMediaTypeCreationReuseSourceField() {
135     $session = $this->getSession();
136     $page = $session->getPage();
137     $assert_session = $this->assertSession();
138
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');
148
149     $label = 'Type reusing Default Field';
150     $mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
151
152     $this->drupalGet('admin/structure/media/add');
153
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');
163
164     // Fill in a label to the media type.
165     $page->fillField('label', $label);
166
167     // Wait for machine name generation. Default: waitUntilVisible(), does not
168     // work properly.
169     $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
170
171     $page->pressButton('Save');
172
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');
179   }
180
181 }