View source
<?php
namespace Drupal\Tests\paragraphs_features\FunctionalJavascript;
class ParagraphsFeaturesDeleteConfirmationTest extends ParagraphsFeaturesJavascriptTestBase {
public function testDeleteConfirmation() {
$content_type = 'test_delete_confirm';
$this
->createTestConfiguration($content_type, 1);
$this
->setupParagraphSettings($content_type);
$this
->enableDeleteConfirmation($content_type);
$this
->drupalGet("node/add/{$content_type}");
$session = $this
->getSession();
$page = $session
->getPage();
$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")]');
$page
->find('xpath', '//button[contains(@class, "paragraphs-dropdown-toggle")]')
->click();
$page
->find('xpath', '//button[contains(@class, "paragraphs-features__delete-confirm")]')
->click();
$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.');
$page
->find('xpath', '//button[contains(@class, "paragraphs-features__delete-confirmation__cancel-button")]')
->click();
$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.');
$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();
$this
->assertSession()
->elementNotExists('xpath', '//div[@id="field-paragraphs-0-item-wrapper"]');
$this
->setupNestedParagraphSettings();
$this
->drupalGet("node/add/{$content_type}");
$session = $this
->getSession();
$page = $session
->getPage();
$page
->find('xpath', '//input[@data-drupal-selector="field-paragraphs-test-nested-add-more"]')
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$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.');
$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.');
$this
->disableDeleteConfirmation($content_type);
$this
->drupalGet("node/add/{$content_type}");
$session = $this
->getSession();
$page = $session
->getPage();
$this
->assertSession()
->elementNotExists('xpath', '//button[@class="paragraphs-features__delete-confirm"]');
$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();
$this
->assertSession()
->elementNotExists('xpath', '//div[@id="field-paragraphs-0-item-wrapper"]');
}
public function testCkEditorAfterCancel() {
$content_type = 'test_delete_confirm';
$unique_comparison_text = 'Far, far below the deepest delvings of the dwarves, the world is gnawed by nameless things.';
$this
->createTestConfiguration($content_type, 1);
$this
->setupParagraphSettings($content_type);
$this
->createEditor();
$this
->enableDeleteConfirmation($content_type);
$this
->drupalGet("node/add/{$content_type}");
$session = $this
->getSession();
$driver = $session
->getDriver();
$page = $session
->getPage();
$ck_editor_id_0 = $this
->getCkEditorId(0);
$driver
->executeScript("CKEDITOR.instances['{$ck_editor_id_0}'].setData('{$unique_comparison_text}');");
static::assertEquals("<p>{$unique_comparison_text}</p>", $driver
->evaluateScript("jQuery('#' + CKEDITOR.instances['{$ck_editor_id_0}'].id + '_contents').find('iframe').contents().find('body').html();"));
$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__cancel-button")]')
->click();
static::assertEquals("<p>{$unique_comparison_text}</p>", $driver
->evaluateScript("jQuery('#' + CKEDITOR.instances['{$ck_editor_id_0}'].id + '_contents').find('iframe').contents().find('body').html();"));
static::assertTrue($driver
->evaluateScript("typeof CKEDITOR.instances['{$ck_editor_id_0}'].document.getWindow().\$ !== 'undefined';"));
}
protected function setupParagraphSettings($content_type) {
$currentUrl = $this
->getSession()
->getCurrentUrl();
$this
->config('core.entity_form_display.node.' . $content_type . '.default')
->set('content.field_paragraphs.settings.default_paragraph_type', 'test_1')
->set('content.field_paragraphs.settings.add_mode', 'button')
->set('content.field_paragraphs.third_party_settings.paragraphs_features.show_drag_and_drop', FALSE)
->save();
$this
->drupalGet($currentUrl);
}
protected function setupNestedParagraphSettings() {
$currentUrl = $this
->getSession()
->getCurrentUrl();
$this
->config('core.entity_form_display.paragraph.test_nested.default')
->set('content.field_paragraphs.settings.default_paragraph_type', 'test_1')
->set('content.field_paragraphs.settings.edit_mode', 'open')
->save();
$this
->drupalGet($currentUrl);
}
protected function enableDeleteConfirmation($content_type) {
$currentUrl = $this
->getSession()
->getCurrentUrl();
$this
->toggleDeleteConfirmation($content_type, 'check');
$this
->drupalGet($currentUrl);
}
protected function disableDeleteConfirmation($content_type) {
$currentUrl = $this
->getSession()
->getCurrentUrl();
$this
->toggleDeleteConfirmation($content_type, 'uncheck');
$this
->drupalGet($currentUrl);
}
protected function toggleDeleteConfirmation($content_type, $op = 'check') {
$this
->drupalGet("admin/structure/types/manage/{$content_type}/form-display");
$session = $this
->getSession();
$page = $session
->getPage();
$page
->pressButton('field_paragraphs_settings_edit');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$action = "{$op}Field";
$page
->{$action}('fields[field_paragraphs][settings_edit_form][third_party_settings][paragraphs_features][delete_confirmation]');
$this
->drupalPostForm(NULL, [], 'Update');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->drupalPostForm(NULL, [], $this
->t('Save'));
}
}