You are here

public function ParagraphsFeaturesDeleteConfirmationTest::testDeleteConfirmation in Paragraphs Features 8

Same name and namespace in other branches
  1. 2.x tests/src/FunctionalJavascript/ParagraphsFeaturesDeleteConfirmationTest.php \Drupal\Tests\paragraphs_features\FunctionalJavascript\ParagraphsFeaturesDeleteConfirmationTest::testDeleteConfirmation()

Test display of delete confirmation.

File

tests/src/FunctionalJavascript/ParagraphsFeaturesDeleteConfirmationTest.php, line 15

Class

ParagraphsFeaturesDeleteConfirmationTest
Test display delete confirmation.

Namespace

Drupal\Tests\paragraphs_features\FunctionalJavascript

Code

public function testDeleteConfirmation() {

  // Create content type with paragraphs field.
  $content_type = 'test_delete_confirm';

  // Create nested paragraph with addition of one text test paragraph.
  $this
    ->createTestConfiguration($content_type, 1);
  $this
    ->setupParagraphSettings($content_type);

  // 1) Enable setting to show confirmation on remove action.
  $this
    ->enableDeleteConfirmation($content_type);
  $this
    ->drupalGet("node/add/{$content_type}");
  $session = $this
    ->getSession();
  $page = $session
    ->getPage();

  // 1a) Check that custom remove button is present and the original remove
  // action is hidden.
  $is_element_hidden = $session
    ->evaluateScript("jQuery('input[data-drupal-selector=\"field-paragraphs-0-remove\"]').parent('.visually-hidden').length === 1");
  $this
    ->assertEquals(TRUE, $is_element_hidden, 'Original remove button should be hidden.');
  $this
    ->assertSession()
    ->elementExists('xpath', '//button[contains(@class, "paragraphs-features__delete-confirm")]');

  // 1b) Trigger delete confirmation message.
  $page
    ->find('xpath', '//button[contains(@class, "paragraphs-dropdown-toggle")]')
    ->click();
  $page
    ->find('xpath', '//button[contains(@class, "paragraphs-features__delete-confirm")]')
    ->click();

  // Message and buttons are shown, paragraphs inner form elements should be
  // hidden.
  $this
    ->assertSession()
    ->elementExists('css', 'div.paragraphs-features__delete-confirmation');
  $this
    ->assertSession()
    ->elementExists('css', 'button.paragraphs-features__delete-confirmation__remove-button');
  $this
    ->assertSession()
    ->elementExists('css', 'button.paragraphs-features__delete-confirmation__cancel-button');
  $are_elements_hidden = $session
    ->evaluateScript("jQuery('div[data-drupal-selector=\"edit-field-paragraphs-0\"]').hasClass('visually-hidden')");
  $this
    ->assertEquals(TRUE, $are_elements_hidden, 'Inner form elements should be hidden.');

  // 1c) Cancel remove paragraph.
  $page
    ->find('xpath', '//button[contains(@class, "paragraphs-features__delete-confirmation__cancel-button")]')
    ->click();

  // Confirmation message is removed, paragraphs inner form elements should be
  // shown.
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//div[@class="paragraphs-features__delete-confirmation"]');
  $are_elements_hidden = $session
    ->evaluateScript("jQuery('div[data-drupal-selector=\"edit-field-paragraphs-0\"]').hasClass('visually-hidden')");
  $this
    ->assertEquals(FALSE, $are_elements_hidden, 'Inner form elements should be visible.');

  // 1d) Trigger delete confirmation message, remove paragraph.
  $page
    ->find('xpath', '//button[contains(@class, "paragraphs-dropdown-toggle")]')
    ->click();
  $page
    ->find('xpath', '//button[contains(@class, "paragraphs-features__delete-confirm")]')
    ->click();
  $page
    ->find('xpath', '//button[contains(@class, "paragraphs-features__delete-confirmation__remove-button")]')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Paragraph is gone.
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//div[@id="field-paragraphs-0-item-wrapper"]');

  // 2) Setup nested.
  $this
    ->setupNestedParagraphSettings();
  $this
    ->drupalGet("node/add/{$content_type}");
  $session = $this
    ->getSession();
  $page = $session
    ->getPage();

  // 2a) Add nested paragraph.
  $page
    ->find('xpath', '//input[@data-drupal-selector="field-paragraphs-test-nested-add-more"]')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // 2b) Trigger confirmation.
  $session
    ->evaluateScript("jQuery('div[data-drupal-selector=\"edit-field-paragraphs-1-subform-field-paragraphs-0-top\"]').find('.paragraphs-features__delete-confirm').trigger('mousedown')");
  $is_element_visible = $session
    ->evaluateScript("jQuery('div[id^=\"field-paragraphs-1-subform-field-paragraphs-0-item-wrapper\"]').find('.paragraphs-features__delete-confirmation').length === 1");
  $this
    ->assertEquals(TRUE, $is_element_visible, 'Confirmation form should be visible in subform.');

  // 2b) Make sure correct paragraph form is removed.
  $session
    ->evaluateScript("jQuery('div[id^=\"field-paragraphs-1-subform-field-paragraphs-0-item-wrapper\"]').find('.paragraphs-features__delete-confirmation__remove-button').trigger('mousedown')");
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $is_element_gone = $session
    ->evaluateScript("jQuery('div[id^=\"field-paragraphs-1-subform-field-paragraphs-0-item-wrapper\"]').length === 0");
  $this
    ->assertEquals(TRUE, $is_element_gone, 'Nested paragraph subform should be gone.');

  // 3) Disable setting to show confirmation on remove action.
  $this
    ->disableDeleteConfirmation($content_type);
  $this
    ->drupalGet("node/add/{$content_type}");
  $session = $this
    ->getSession();
  $page = $session
    ->getPage();

  // 3a) Custom remove button is not present.
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//button[@class="paragraphs-features__delete-confirm"]');

  // 3b) Instant removal.
  $page
    ->find('xpath', '//button[contains(@class, "paragraphs-dropdown-toggle")]')
    ->click();
  $page
    ->find('xpath', '//input[@data-drupal-selector="field-paragraphs-0-remove"]')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Paragraph is gone.
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//div[@id="field-paragraphs-0-item-wrapper"]');
}