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
index efbe28849b19b308e28ee736f4f22e4145cc103f..42e3511f315b9d1b8a94f43e89663ecd4d932540 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace Drupal\Tests\media\FunctionalJavascript;
 
+use Drupal\Component\Utility\Html;
+
 /**
  * Tests the media type creation.
  *
@@ -10,9 +12,9 @@ namespace Drupal\Tests\media\FunctionalJavascript;
 class MediaTypeCreationTest extends MediaJavascriptTestBase {
 
   /**
-   * Tests the media type creation form.
+   * Tests the source field behavior on the add media type form.
    */
-  public function testMediaTypeCreationFormWithDefaultField() {
+  public function testSourceChangeOnMediaTypeCreationForm() {
     $session = $this->getSession();
     $page = $session->getPage();
     $assert_session = $this->assertSession();
@@ -24,17 +26,58 @@ class MediaTypeCreationTest extends MediaJavascriptTestBase {
 
     // Fill in a label to the media type.
     $page->fillField('label', $label);
-    // Wait for machine name generation. Default: waitUntilVisible(), does not
-    // work properly.
-    $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
+    $this->assertNotEmpty(
+      $assert_session->waitForElementVisible('css', '.machine-name-value')
+    );
 
     // Select the media source used by our media type.
+    $assert_session->selectExists('Media source')->selectOption('test_different_displays');
+    $this->assertNotEmpty(
+      $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]')
+    );
+
+    // Change the media source.
+    $assert_session->selectExists('Media source')->selectOption('test');
+    $this->assertNotEmpty(
+      $assert_session->waitForElement('css', 'fieldset[data-drupal-selector="edit-source-configuration"] .fieldset-wrapper .placeholder:contains("Text (plain)")')
+    );
+
+    $page->pressButton('Save');
+
+    // Check that source can not be changed anymore.
+    $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}");
+    $assert_session->pageTextContains('The media source cannot be changed after the media type is created');
+    $assert_session->fieldDisabled('Media source');
+  }
+
+  /**
+   * Tests the media type creation form.
+   */
+  public function testMediaTypeCreationFormWithDefaultField() {
+    $session = $this->getSession();
+    $page = $session->getPage();
+    $assert_session = $this->assertSession();
+
+    $label = 'Type with Default Field';
+    $mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
+
+    $this->drupalGet('admin/structure/media/add');
+
+    // Select the media source used by our media type. Do this before setting
+    // the label or machine name in order to guard against the regression in
+    // https://www.drupal.org/project/drupal/issues/2557299.
     $assert_session->fieldExists('Media source');
     $assert_session->optionExists('Media source', 'test');
     $page->selectFieldOption('Media source', 'test');
     $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
     $this->assertNotEmpty($result);
 
+    // Fill in a label to the media type.
+    $page->fillField('label', $label);
+    // Wait for machine name generation. Default: waitUntilVisible(), does not
+    // work properly.
+    $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
+
     $page->pressButton('Save');
 
     // Check whether the source field was correctly created.
@@ -49,6 +92,40 @@ class MediaTypeCreationTest extends MediaJavascriptTestBase {
     $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}");
 
     $assert_session->pageTextContains('Test source field is used to store the essential information about the media item.');
+
+    // Check that the plugin cannot be changed after it is set on type creation.
+    $assert_session->fieldDisabled('Media source');
+    $assert_session->pageTextContains('The media source cannot be changed after the media type is created.');
+
+    // Open up the media add form and verify that the source field is right
+    // after the name, and before the vertical tabs.
+    $this->drupalGet("/media/add/$mediaTypeMachineName");
+
+    // Get the form element, and its HTML representation.
+    $form_selector = '#media-' . Html::cleanCssIdentifier($mediaTypeMachineName) . '-add-form';
+    $form = $assert_session->elementExists('css', $form_selector);
+    $form_html = $form->getOuterHtml();
+
+    // The name field should come before the source field, which should itself
+    // come before the vertical tabs.
+    $name_field = $assert_session->fieldExists('Name', $form)->getOuterHtml();
+    $test_source_field = $assert_session->fieldExists('Test source', $form)->getOuterHtml();
+    $vertical_tabs = $assert_session->elementExists('css', '.vertical-tabs', $form)->getOuterHtml();
+    $date_field = $assert_session->fieldExists('Date', $form)->getOuterHtml();
+    $published_checkbox = $assert_session->fieldExists('Published', $form)->getOuterHtml();
+    $this->assertTrue(strpos($form_html, $test_source_field) > strpos($form_html, $name_field));
+    $this->assertTrue(strpos($form_html, $vertical_tabs) > strpos($form_html, $test_source_field));
+    // The "Published" checkbox should be the last element.
+    $this->assertTrue(strpos($form_html, $published_checkbox) > strpos($form_html, $date_field));
+
+    // Check that a new type with the same machine name cannot be created.
+    $this->drupalGet('admin/structure/media/add');
+    $page->fillField('label', $label);
+    $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
+    $page->selectFieldOption('Media source', 'test');
+    $assert_session->assertWaitOnAjaxRequest();
+    $page->pressButton('Save');
+    $assert_session->pageTextContains('The machine-readable name is already in use. It must be unique.');
   }
 
   /**
@@ -61,11 +138,12 @@ class MediaTypeCreationTest extends MediaJavascriptTestBase {
 
     // Create a new media type, which should create a new field we can reuse.
     $this->drupalGet('/admin/structure/media/add');
-    $page->fillField('label', 'Pastafazoul');
-    $session->wait(5000, "jQuery('.machine-name-value').text() === 'pastafazoul'");
+    // Choose the source plugin before setting the label and machine name.
     $page->selectFieldOption('Media source', 'test');
     $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
     $this->assertNotEmpty($result);
+    $page->fillField('label', 'Pastafazoul');
+    $session->wait(5000, "jQuery('.machine-name-value').text() === 'pastafazoul'");
     $page->pressButton('Save');
 
     $label = 'Type reusing Default Field';
@@ -73,14 +151,8 @@ class MediaTypeCreationTest extends MediaJavascriptTestBase {
 
     $this->drupalGet('admin/structure/media/add');
 
-    // Fill in a label to the media type.
-    $page->fillField('label', $label);
-
-    // Wait for machine name generation. Default: waitUntilVisible(), does not
-    // work properly.
-    $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
-
-    // Select the media source used by our media type.
+    // Select the media source used by our media type. Do this before setting
+    // the label and machine name.
     $assert_session->fieldExists('Media source');
     $assert_session->optionExists('Media source', 'test');
     $page->selectFieldOption('Media source', 'test');
@@ -88,6 +160,14 @@ class MediaTypeCreationTest extends MediaJavascriptTestBase {
     $this->assertNotEmpty($result);
     // Select the existing field for re-use.
     $page->selectFieldOption('source_configuration[source_field]', 'field_media_test');
+
+    // Fill in a label to the media type.
+    $page->fillField('label', $label);
+
+    // Wait for machine name generation. Default: waitUntilVisible(), does not
+    // work properly.
+    $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
+
     $page->pressButton('Save');
 
     // Check that no new fields were created.