Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entity_browser / tests / src / FunctionalJavascript / EntityReferenceWidgetTest.php
index c760cce7d762026455e5265eec7c264f6e4646da..1fe6d17ff86e4114be9f0e11f13be969c53d7c98 100644 (file)
@@ -24,8 +24,11 @@ class EntityReferenceWidgetTest extends EntityBrowserJavascriptTestBase {
 
     /** @var \Drupal\user\RoleInterface $role */
     $role = Role::load('authenticated');
-    $this->grantPermissions($role, ['access test_entity_browser_iframe_node_view entity browser pages']);
-    $this->grantPermissions($role, ['bypass node access']);
+    $this->grantPermissions($role, [
+      'access test_entity_browser_iframe_node_view entity browser pages',
+      'bypass node access',
+      'administer node form display',
+    ]);
 
   }
 
@@ -33,12 +36,12 @@ class EntityReferenceWidgetTest extends EntityBrowserJavascriptTestBase {
    * Tests Entity Reference widget.
    */
   public function testEntityReferenceWidget() {
-
-    $page = $this->getSession()->getPage();
+    $session = $this->getSession();
+    $page = $session->getPage();
     $assert_session = $this->assertSession();
 
     // Create an entity_reference field to test the widget.
-    FieldStorageConfig::create([
+    $field_storage = FieldStorageConfig::create([
       'field_name' => 'field_entity_reference1',
       'type' => 'entity_reference',
       'entity_type' => 'node',
@@ -46,15 +49,17 @@ class EntityReferenceWidgetTest extends EntityBrowserJavascriptTestBase {
       'settings' => [
         'target_type' => 'node',
       ],
-    ])->save();
+    ]);
+    $field_storage->save();
 
-    FieldConfig::create([
+    $field = FieldConfig::create([
       'field_name' => 'field_entity_reference1',
       'entity_type' => 'node',
       'bundle' => 'article',
       'label' => 'Referenced articles',
       'settings' => [],
-    ])->save();
+    ]);
+    $field->save();
 
     /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
     $form_display = $this->container->get('entity_type.manager')
@@ -68,6 +73,7 @@ class EntityReferenceWidgetTest extends EntityBrowserJavascriptTestBase {
         'open' => TRUE,
         'field_widget_edit' => TRUE,
         'field_widget_remove' => TRUE,
+        'field_widget_replace' => FALSE,
         'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
         'field_widget_display' => 'label',
         'field_widget_display_settings' => [],
@@ -83,11 +89,11 @@ class EntityReferenceWidgetTest extends EntityBrowserJavascriptTestBase {
 
     $this->drupalGet('/node/add/article');
     $page->fillField('title[0][value]', 'Referencing node 1');
-    $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_node_view');
+    $session->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_node_view');
     $this->waitForAjaxToFinish();
     $page->checkField('edit-entity-browser-select-node1');
     $page->pressButton('Select entities');
-    $this->getSession()->switchToIFrame();
+    $session->switchToIFrame();
     $this->waitForAjaxToFinish();
     $page->pressButton('Save');
 
@@ -110,6 +116,7 @@ class EntityReferenceWidgetTest extends EntityBrowserJavascriptTestBase {
         'open' => TRUE,
         'field_widget_edit' => FALSE,
         'field_widget_remove' => FALSE,
+        'field_widget_replace' => FALSE,
         'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
         'field_widget_display' => 'label',
         'field_widget_display_settings' => [],
@@ -127,31 +134,174 @@ class EntityReferenceWidgetTest extends EntityBrowserJavascriptTestBase {
         'open' => TRUE,
         'field_widget_edit' => TRUE,
         'field_widget_remove' => TRUE,
+        'field_widget_replace' => FALSE,
         'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
         'field_widget_display' => 'label',
         'field_widget_display_settings' => [],
       ],
     ])->save();
     $this->drupalGet('node/' . $nid . '/edit');
-    $assert_session->buttonExists('edit-field-entity-reference1-current-items-0-remove-button');
-    $assert_session->buttonExists('edit-field-entity-reference1-current-items-0-edit-button');
+    $remove_button = $assert_session->buttonExists('edit-field-entity-reference1-current-items-0-remove-button');
+    $this->assertEquals('Remove', $remove_button->getValue());
+    $this->assertTrue($remove_button->hasClass('remove-button'));
+    $edit_button = $assert_session->buttonExists('edit-field-entity-reference1-current-items-0-edit-button');
+    $this->assertEquals('Edit', $edit_button->getValue());
+    $this->assertTrue($edit_button->hasClass('edit-button'));
+    // Make sure the "Replace" button is not there.
+    $assert_session->buttonNotExists('edit-field-entity-reference1-current-items-0-replace-button');
 
     // Test the "Remove" button on the widget works.
     $page->pressButton('Remove');
     $this->waitForAjaxToFinish();
     $assert_session->pageTextNotContains('Target example node 1');
 
+    // Test the "Replace" button functionality.
+    $form_display->setComponent('field_entity_reference1', [
+      'type' => 'entity_browser_entity_reference',
+      'settings' => [
+        'entity_browser' => 'test_entity_browser_iframe_node_view',
+        'open' => TRUE,
+        'field_widget_edit' => TRUE,
+        'field_widget_remove' => TRUE,
+        'field_widget_replace' => TRUE,
+        'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
+        'field_widget_display' => 'label',
+        'field_widget_display_settings' => [],
+      ],
+    ])->save();
+    // In order to ensure the replace button opens the browser, it needs to be
+    // closed.
+    /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
+    $browser = $this->container->get('entity_type.manager')
+      ->getStorage('entity_browser')
+      ->load('test_entity_browser_iframe_node_view');
+    $browser->getDisplay()
+      ->setConfiguration([
+        'width' => 650,
+        'height' => 500,
+        'link_text' => 'Select entities',
+        'auto_open' => FALSE,
+      ]);
+    $browser->save();
+    // We'll need a third node to be able to make a new selection.
+    $target_node2 = Node::create([
+      'title' => 'Target example node 2',
+      'type' => 'article',
+    ]);
+    $target_node2->save();
+    $this->drupalGet('node/' . $nid . '/edit');
+    // If there is only one entity in the current selection the button should
+    // show up.
+    $replace_button = $assert_session->buttonExists('edit-field-entity-reference1-current-items-0-replace-button');
+    $this->assertEquals('Replace', $replace_button->getValue());
+    $this->assertTrue($replace_button->hasClass('replace-button'));
+    // Clicking on the button should empty the selection and automatically
+    // open the browser again.
+    $replace_button->click();
+    $this->waitForAjaxToFinish();
+    $session->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_node_view');
+    $this->waitForAjaxToFinish();
+    $page->checkField('edit-entity-browser-select-node3');
+    $page->pressButton('Select entities');
+    $session->wait(1000);
+    $session->switchToIFrame();
+    $this->waitForAjaxToFinish();
+    // Even in the AJAX-built markup for the newly selected element, the replace
+    // button should be there.
+    $assert_session->elementExists('css', 'input[data-drupal-selector="edit-field-entity-reference1-current-items-0-replace-button"]');
+    // Adding a new node to the selection, however, should make it disappear.
+    $open_iframe_link = $assert_session->elementExists('css', 'a[data-drupal-selector="edit-field-entity-reference1-entity-browser-entity-browser-link"]');
+    $open_iframe_link->click();
+    $this->waitForAjaxToFinish();
+    $session->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_node_view');
+    $this->waitForAjaxToFinish();
+    $page->checkField('edit-entity-browser-select-node1');
+    $page->pressButton('Select entities');
+    $session->wait(1000);
+    $session->switchToIFrame();
+    $this->waitForAjaxToFinish();
+    $assert_session->elementNotExists('css', 'input[data-drupal-selector="edit-field-entity-reference1-current-items-0-replace-button"]');
+    $page->pressButton('Save');
+    $assert_session->pageTextContains('Article Referencing node 1 has been updated.');
+
+    // Test the replace button again with different field cardinalities.
+    FieldStorageConfig::load('node.field_entity_reference1')->setCardinality(1)->save();
+    $this->drupalGet('/node/add/article');
+    $page->fillField('title[0][value]', 'Referencing node 2');
+    $open_iframe_link = $assert_session->elementExists('css', 'a[data-drupal-selector="edit-field-entity-reference1-entity-browser-entity-browser-link"]');
+    $open_iframe_link->click();
+    $this->waitForAjaxToFinish();
+    $session->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_node_view');
+    $this->waitForAjaxToFinish();
+    $page->checkField('edit-entity-browser-select-node1');
+    $page->pressButton('Select entities');
+    $session->wait(1000);
+    $session->switchToIFrame();
+    $this->waitForAjaxToFinish();
+    $assert_session->elementContains('css', '#edit-field-entity-reference1-wrapper', 'Target example node 1');
+    // All three buttons should be visible.
+    $assert_session->elementExists('css', 'input[data-drupal-selector="edit-field-entity-reference1-current-items-0-remove-button"]');
+    $assert_session->elementExists('css', 'input[data-drupal-selector="edit-field-entity-reference1-current-items-0-edit-button"]');
+    $replace_button = $assert_session->elementExists('css', 'input[data-drupal-selector="edit-field-entity-reference1-current-items-0-replace-button"]');
+    // Clicking on the button should empty the selection and automatically
+    // open the browser again.
+    $replace_button->click();
+    $this->waitForAjaxToFinish();
+    $session->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_node_view');
+    $this->waitForAjaxToFinish();
+    $page->checkField('edit-entity-browser-select-node2');
+    $page->pressButton('Select entities');
+    $session->wait(1000);
+    $session->switchToIFrame();
+    $this->waitForAjaxToFinish();
+    $assert_session->elementContains('css', '#edit-field-entity-reference1-wrapper', 'Referencing node 1');
+
+    // Do the same as above but now with cardinality 2.
+    FieldStorageConfig::load('node.field_entity_reference1')->setCardinality(2)->save();
+    $this->drupalGet('/node/add/article');
+    $page->fillField('title[0][value]', 'Referencing node 3');
+    $open_iframe_link = $assert_session->elementExists('css', 'a[data-drupal-selector="edit-field-entity-reference1-entity-browser-entity-browser-link"]');
+    $open_iframe_link->click();
+    $this->waitForAjaxToFinish();
+    $session->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_node_view');
+    $this->waitForAjaxToFinish();
+    $page->checkField('edit-entity-browser-select-node1');
+    $page->pressButton('Select entities');
+    $session->wait(1000);
+    $session->switchToIFrame();
+    $this->waitForAjaxToFinish();
+    $assert_session->elementContains('css', '#edit-field-entity-reference1-wrapper', 'Target example node 1');
+    // All three buttons should be visible.
+    $assert_session->elementExists('css', 'input[data-drupal-selector="edit-field-entity-reference1-current-items-0-remove-button"]');
+    $assert_session->elementExists('css', 'input[data-drupal-selector="edit-field-entity-reference1-current-items-0-edit-button"]');
+    $replace_button = $assert_session->elementExists('css', 'input[data-drupal-selector="edit-field-entity-reference1-current-items-0-replace-button"]');
+    // Clicking on the button should empty the selection and automatically
+    // open the browser again.
+    $replace_button->click();
+    $this->waitForAjaxToFinish();
+    $session->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_node_view');
+    $this->waitForAjaxToFinish();
+    $page->checkField('edit-entity-browser-select-node2');
+    $page->pressButton('Select entities');
+    $session->wait(1000);
+    $session->switchToIFrame();
+    $this->waitForAjaxToFinish();
+    $assert_session->elementContains('css', '#edit-field-entity-reference1-wrapper', 'Referencing node 1');
+
     // Verify that if the user cannot edit the entity, the "Edit" button does
     // not show up, even if configured to.
     /** @var \Drupal\user\RoleInterface $role */
     $role = Role::load('authenticated');
     $role->revokePermission('bypass node access')->trustData()->save();
     $this->drupalGet('node/add/article');
-    $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_node_view');
+    $open_iframe_link = $assert_session->elementExists('css', 'a[data-drupal-selector="edit-field-entity-reference1-entity-browser-entity-browser-link"]');
+    $open_iframe_link->click();
+    $this->waitForAjaxToFinish();
+    $session->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_node_view');
     $this->waitForAjaxToFinish();
     $page->checkField('edit-entity-browser-select-node1');
     $page->pressButton('Select entities');
-    $this->getSession()->switchToIFrame();
+    $session->switchToIFrame();
     $this->waitForAjaxToFinish();
     $assert_session->buttonNotExists('edit-field-entity-reference1-current-items-0-edit-button');