View source
<?php
namespace Drupal\Tests\paragraphs_features\FunctionalJavascript;
class ParagraphsFeaturesSplitTextTest extends ParagraphsFeaturesJavascriptTestBase {
protected function createNewTextParagraph($index, $text) {
$session = $this
->getSession();
$page = $session
->getPage();
$driver = $session
->getDriver();
$page
->find('xpath', '(//*[contains(@class, "paragraph-type-add-modal-button")])[1]')
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->find('xpath', '//*[contains(@class, "paragraphs-add-dialog") and contains(@class, "ui-dialog-content")]//*[contains(@name, "test_1")]')
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
sleep(1);
$ck_editor_id = $this
->getCkEditorId($index);
$driver
->executeScript("CKEDITOR.instances['{$ck_editor_id}'].insertHtml('{$text}');");
return $ck_editor_id;
}
protected function clickParagraphSplitButton($ck_editor_index) {
$this
->getSession()
->executeScript("jQuery('.cke_button__splittext:nth({$ck_editor_index})').trigger('click');");
$this
->assertSession()
->assertWaitOnAjaxRequest();
}
public function testSplitTextFeature() {
$content_type = 'test_split_text';
$this
->createTestConfiguration($content_type, 1);
$this
->createEditor();
$this
->drupalGet("admin/structure/types/manage/{$content_type}/form-display");
$session = $this
->getSession();
$page = $session
->getPage();
$driver = $session
->getDriver();
$page
->pressButton('field_paragraphs_settings_edit');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$is_option_visible = $session
->evaluateScript("jQuery('.paragraphs-features__split-text__option:visible').length === 0");
$this
->assertEquals(TRUE, $is_option_visible, 'By default "split text" option should not be visible.');
$page
->selectFieldOption('fields[field_paragraphs][settings_edit_form][settings][add_mode]', 'modal');
$session
->executeScript("jQuery('[name=\"fields[field_paragraphs][settings_edit_form][settings][add_mode]\"]').trigger('change');");
$this
->assertSession()
->assertWaitOnAjaxRequest();
$is_option_visible = $session
->evaluateScript("jQuery('.paragraphs-features__split-text__option:visible').length === 1");
$this
->assertEquals(TRUE, $is_option_visible, 'After modal add mode is selected, "split text" option should be available.');
$page
->checkField('fields[field_paragraphs][settings_edit_form][third_party_settings][paragraphs_features][split_text]');
$is_checked = $session
->evaluateScript("jQuery('.paragraphs-features__split-text__option').is(':checked')");
$this
->assertEquals(TRUE, $is_checked, 'Checkbox should be checked.');
$this
->drupalPostForm(NULL, [], 'Update');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->drupalPostForm(NULL, [], $this
->t('Save'));
$paragraph_content_0 = '<p>Content that will be in the first paragraph after the split.</p>';
$paragraph_content_1 = '<p>Content that will be in the second paragraph after the split.</p>';
$this
->drupalGet("node/add/{$content_type}");
$ck_editor_id = $this
->createNewTextParagraph(0, $paragraph_content_0 . $paragraph_content_1);
$driver
->executeScript("var selection = CKEDITOR.instances['{$ck_editor_id}'].getSelection(); selection.selectElement(selection.root.getChild(1)); var ranges = selection.getRanges(); ranges[0].setEndBefore(ranges[0].getBoundaryNodes().endNode); selection.selectRanges(ranges);");
$this
->clickParagraphSplitButton(0);
$ck_editor_id_0 = $this
->getCkEditorId(0);
$ck_editor_id_1 = $this
->getCkEditorId(1);
static::assertEquals($paragraph_content_0 . PHP_EOL, $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_0}'].getData();"));
static::assertEquals($paragraph_content_1 . PHP_EOL, $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_1}'].getData();"));
$paragraph_content = '<p>Content will be split inside the word spl-it.</p>';
$this
->drupalGet("node/add/{$content_type}");
$ck_editor_id = $this
->createNewTextParagraph(0, $paragraph_content);
$driver
->executeScript("var selection = CKEDITOR.instances['{$ck_editor_id}'].getSelection(); var element = selection.getStartElement(); selection.selectElement(element); var ranges = selection.getRanges(); ranges[0].setStart(element.getFirst(), element.getHtml().indexOf('split') + 3); ranges[0].setEnd(element.getFirst(), element.getHtml().indexOf('split') + 3); selection.selectRanges(ranges);");
$this
->clickParagraphSplitButton(0);
$ck_editor_id_0 = $this
->getCkEditorId(0);
$ck_editor_id_1 = $this
->getCkEditorId(1);
static::assertEquals('<p>Content will be spl</p>' . PHP_EOL, $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_0}'].getData();"));
static::assertEquals('<p>it inside the word spl-it.</p>' . PHP_EOL, $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_1}'].getData();"));
$this
->drupalGet("node/add/{$content_type}");
$this
->createNewTextParagraph(0, '');
$driver
->executeScript("jQuery('[name=\"field_paragraphs_0_remove\"]').trigger('mousedown');");
$this
->assertSession()
->assertWaitOnAjaxRequest();
$ck_editor_id = $this
->createNewTextParagraph(1, $paragraph_content_0 . $paragraph_content_1);
$driver
->executeScript("var selection = CKEDITOR.instances['{$ck_editor_id}'].getSelection(); selection.selectElement(selection.root.getChild(1)); var ranges = selection.getRanges(); ranges[0].setEndBefore(ranges[0].getBoundaryNodes().endNode); selection.selectRanges(ranges);");
$this
->clickParagraphSplitButton(0);
$ck_editor_id_0 = $this
->getCkEditorId(1);
$ck_editor_id_1 = $this
->getCkEditorId(2);
static::assertEquals($paragraph_content_0 . PHP_EOL, $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_0}'].getData();"));
static::assertEquals($paragraph_content_1 . PHP_EOL, $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_1}'].getData();"));
$this
->drupalGet("node/add/{$content_type}");
$ck_editor_id = $this
->createNewTextParagraph(0, $paragraph_content_0 . $paragraph_content_1);
$driver
->executeScript("var selection = CKEDITOR.instances['{$ck_editor_id}'].getSelection(); selection.selectElement(selection.root.getChild(1)); var ranges = selection.getRanges(); ranges[0].setEndBefore(ranges[0].getBoundaryNodes().endNode); selection.selectRanges(ranges);");
$this
->clickParagraphSplitButton(0);
$paragraph_content_0_new = '<p>Content that will be placed into the first paragraph after split.</p>';
$paragraph_content_1_new = '<p>Content that will be placed into the second paragraph after split.</p>';
$ck_editor_id_0 = $this
->getCkEditorId(0);
$ck_editor_id_1 = $this
->getCkEditorId(1);
$driver
->executeScript("CKEDITOR.instances[\"{$ck_editor_id_0}\"].setData(\"{$paragraph_content_0_new}\");");
$driver
->executeScript("CKEDITOR.instances[\"{$ck_editor_id_1}\"].setData(\"{$paragraph_content_1_new}\");");
$this
->createNewTextParagraph(2, '');
$ck_editor_id_0 = $this
->getCkEditorId(0);
$ck_editor_id_1 = $this
->getCkEditorId(1);
$ck_editor_id_2 = $this
->getCkEditorId(2);
$ck_editor_content_0 = $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_0}'].getData();");
$ck_editor_content_1 = $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_1}'].getData();");
$ck_editor_content_2 = $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_2}'].getData();");
static::assertEquals($paragraph_content_0_new . PHP_EOL, $ck_editor_content_0);
static::assertEquals($paragraph_content_1_new . PHP_EOL, $ck_editor_content_1);
static::assertEquals('', $ck_editor_content_2);
$text = '<p>Text start</p><ol><li>line 1</li><li>line 2 with some <strong>bold text</strong> and back to normal</li><li>line 3</li></ol><p>Text end after indexed list</p>';
$this
->drupalGet("node/add/{$content_type}");
$ck_editor_id = $this
->createNewTextParagraph(0, $text);
$driver
->executeScript("var selection = CKEDITOR.instances['{$ck_editor_id}'].getSelection(); selection.selectElement(selection.document.findOne('strong').getChild(0)); var ranges = selection.getRanges(); var startNode = ranges[0].getBoundaryNodes().startNode; ranges[0].setStart(startNode, 4); ranges[0].setEnd(startNode, 4); selection.selectRanges(ranges);");
$this
->clickParagraphSplitButton(0);
$ck_editor_id_0 = $this
->getCkEditorId(0);
$ck_editor_id_1 = $this
->getCkEditorId(1);
$ck_editor_content_0 = $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_0}'].getData();");
$ck_editor_content_1 = $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_1}'].getData();");
$expected_content_0 = '<p>Text start</p>' . PHP_EOL . PHP_EOL . '<ol>' . PHP_EOL . "\t" . '<li>line 1</li>' . PHP_EOL . "\t" . '<li>line 2 with some <strong>bold</strong></li>' . PHP_EOL . '</ol>' . PHP_EOL;
$expected_content_1 = '<ol>' . PHP_EOL . "\t" . '<li><strong>text</strong> and back to normal</li>' . PHP_EOL . "\t" . '<li>line 3</li>' . PHP_EOL . '</ol>' . PHP_EOL . PHP_EOL . '<p>Text end after indexed list</p>' . PHP_EOL;
static::assertEquals($expected_content_0, $ck_editor_content_0);
static::assertEquals($expected_content_1, $ck_editor_content_1);
$this
->addParagraphsType("test_3_text_fields");
$this
->addFieldtoParagraphType('test_3_text_fields', 'text_3_1', 'text_long');
$this
->addFieldtoParagraphType('test_3_text_fields', 'text_3_2', 'text_long');
$this
->addFieldtoParagraphType('test_3_text_fields', 'text_3_3', 'text_long');
$this
->drupalGet("node/add/{$content_type}");
$page
->find('xpath', '(//*[contains(@class, "paragraph-type-add-modal-button")])[1]')
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->find('xpath', '//*[contains(@class, "paragraphs-add-dialog") and contains(@class, "ui-dialog-content")]//*[contains(@name, "test_3_text_fields")]')
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$paragraph_content_0_text_0 = '<p>Content that will be in the first text field.</p>';
$paragraph_content_0_text_1 = $paragraph_content_0 . $paragraph_content_1;
$paragraph_content_0_text_2 = '<p>Content that will be in the last text field.</p>';
$ck_editor_id_0 = $page
->find('xpath', '(//*[@data-drupal-selector="edit-field-paragraphs-0"]//textarea)[1]')
->getAttribute('id');
$ck_editor_id_1 = $page
->find('xpath', '(//*[@data-drupal-selector="edit-field-paragraphs-0"]//textarea)[2]')
->getAttribute('id');
$ck_editor_id_2 = $page
->find('xpath', '(//*[@data-drupal-selector="edit-field-paragraphs-0"]//textarea)[3]')
->getAttribute('id');
$driver
->executeScript("CKEDITOR.instances['{$ck_editor_id_0}'].insertHtml('{$paragraph_content_0_text_0}');");
$driver
->executeScript("CKEDITOR.instances['{$ck_editor_id_1}'].insertHtml('{$paragraph_content_0_text_1}');");
$driver
->executeScript("CKEDITOR.instances['{$ck_editor_id_2}'].insertHtml('{$paragraph_content_0_text_2}');");
$driver
->executeScript("var selection = CKEDITOR.instances['{$ck_editor_id_1}'].getSelection(); selection.selectElement(selection.root.getChild(1)); var ranges = selection.getRanges(); ranges[0].setEndBefore(ranges[0].getBoundaryNodes().endNode); selection.selectRanges(ranges);");
$this
->clickParagraphSplitButton(1);
$ck_editor_id_para_0_text_0 = $page
->find('xpath', '(//*[@data-drupal-selector="edit-field-paragraphs-0"]//textarea)[1]')
->getAttribute('id');
$ck_editor_id_para_0_text_1 = $page
->find('xpath', '(//*[@data-drupal-selector="edit-field-paragraphs-0"]//textarea)[2]')
->getAttribute('id');
$ck_editor_id_para_0_text_2 = $page
->find('xpath', '(//*[@data-drupal-selector="edit-field-paragraphs-0"]//textarea)[3]')
->getAttribute('id');
$ck_editor_id_para_1_text_0 = $page
->find('xpath', '(//*[@data-drupal-selector="edit-field-paragraphs-1"]//textarea)[1]')
->getAttribute('id');
$ck_editor_id_para_1_text_1 = $page
->find('xpath', '(//*[@data-drupal-selector="edit-field-paragraphs-1"]//textarea)[2]')
->getAttribute('id');
$ck_editor_id_para_1_text_2 = $page
->find('xpath', '(//*[@data-drupal-selector="edit-field-paragraphs-1"]//textarea)[3]')
->getAttribute('id');
static::assertEquals($paragraph_content_0_text_0 . PHP_EOL, $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_para_0_text_0}'].getData();"));
static::assertEquals($paragraph_content_0 . PHP_EOL, $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_para_0_text_1}'].getData();"));
static::assertEquals($paragraph_content_0_text_2 . PHP_EOL, $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_para_0_text_2}'].getData();"));
static::assertEquals('', $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_para_1_text_0}'].getData();"));
static::assertEquals($paragraph_content_1 . PHP_EOL, $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_para_1_text_1}'].getData();"));
static::assertEquals('', $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_para_1_text_2}'].getData();"));
$this
->drupalGet("admin/structure/types/manage/{$content_type}/form-display");
$page
->pressButton('field_paragraphs_settings_edit');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->selectFieldOption('fields[field_paragraphs][settings_edit_form][settings][edit_mode]', 'closed');
$session
->executeScript("jQuery('[name=\"fields[field_paragraphs][settings_edit_form][settings][edit_mode]\"]').trigger('change');");
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->selectFieldOption('fields[field_paragraphs][settings_edit_form][settings][autocollapse]', 'all');
$session
->executeScript("jQuery('[name=\"fields[field_paragraphs][settings_edit_form][settings][autocollapse]\"]').trigger('change');");
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->drupalPostForm(NULL, [], 'Update');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->drupalPostForm(NULL, [], $this
->t('Save'));
$paragraph_content_0 = '<p>Content that will be in the first paragraph after the split.</p>';
$paragraph_content_1 = '<p>Content that will be in the second paragraph after the split.</p>';
$this
->drupalGet("node/add/{$content_type}");
$ck_editor_id = $this
->createNewTextParagraph(0, $paragraph_content_0 . $paragraph_content_1);
$driver
->executeScript("var selection = CKEDITOR.instances['{$ck_editor_id}'].getSelection(); selection.selectElement(selection.root.getChild(1)); var ranges = selection.getRanges(); ranges[0].setEndBefore(ranges[0].getBoundaryNodes().endNode); selection.selectRanges(ranges);");
$this
->clickParagraphSplitButton(0);
$ck_editor_id_1 = $this
->getCkEditorId(1);
static::assertEquals($paragraph_content_1 . PHP_EOL, $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_1}'].getData();"));
$this
->scrollElementInView('[name=field_paragraphs_0_edit]');
$page
->pressButton('field_paragraphs_0_edit');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$ck_editor_id_0 = $this
->getCkEditorId(0);
static::assertEquals($paragraph_content_0 . PHP_EOL, $driver
->evaluateScript("CKEDITOR.instances['{$ck_editor_id_0}'].getData();"));
}
}