View source
<?php
namespace Drupal\Tests\paragraphs\Functional;
use Drupal\Tests\paragraphs\Functional\WidgetStable\ParagraphsTestBase;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\node\Entity\Node;
class ParagraphsWidgetButtonsTest extends ParagraphsTestBase {
public static $modules = [
'paragraphs_test',
'node',
'paragraphs',
'field',
'field_ui',
'block',
];
public function testAutocollapse() {
$this
->addParagraphedContentType('paragraphed_test');
$permissions = [
'administer content types',
'administer node fields',
'administer paragraphs types',
'administer node form display',
'administer paragraph fields',
'administer paragraph form display',
'create paragraphed_test content',
'edit any paragraphed_test content',
];
$this
->loginAsAdmin($permissions, TRUE);
$this
->addParagraphsType('text_paragraph');
$this
->addFieldtoParagraphType('text_paragraph', 'field_text', 'text_long');
$this
->addParagraphsType('another_paragraph');
$this
->drupalGet('admin/structure/types/manage/paragraphed_test/form-display');
$option = $this
->assertSession()
->optionExists('fields[field_paragraphs][type]', 'paragraphs');
$this
->assertTrue($option
->isSelected());
$this
->assertSession()
->pageTextNotContains('Autocollapse: None');
$this
->assertSession()
->pageTextContains('Edit mode: Open');
$this
->drupalGet('node/add/paragraphed_test');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_text_paragraph_add_more')
->press();
$this
->getSession()
->getPage()
->findButton('field_paragraphs_text_paragraph_add_more')
->press();
$edit = [
'title[0][value]' => 'Autocollapse test node',
'field_paragraphs[0][subform][field_text][0][value]' => 'Fist paragraph',
'field_paragraphs[1][subform][field_text][0][value]' => 'Second paragraph',
];
$this
->submitForm($edit, 'Save');
$node = $this
->drupalGetNodeByTitle('Autocollapse test node');
$settings = [
'edit_mode' => 'open',
'closed_mode' => 'summary',
'autocollapse' => 'none',
];
$this
->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_0_collapse')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'closed');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_0_edit')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_collapse_all')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'closed');
$this
->checkParagraphInMode('field_paragraphs_1', 'closed');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_0_edit')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$this
->checkParagraphInMode('field_paragraphs_1', 'closed');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_1_edit')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_edit_all')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_0_collapse')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'closed');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_0_edit')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$settings = [
'edit_mode' => 'closed',
'closed_mode' => 'summary',
'autocollapse' => 'all',
];
$this
->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->checkParagraphInMode('field_paragraphs_0', 'closed');
$this
->checkParagraphInMode('field_paragraphs_1', 'closed');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_0_edit')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$this
->checkParagraphInMode('field_paragraphs_1', 'closed');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_1_edit')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'closed');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_edit_all')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_0_collapse')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'closed');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_0_edit')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_collapse_all')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'closed');
$this
->checkParagraphInMode('field_paragraphs_1', 'closed');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_0_edit')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$this
->checkParagraphInMode('field_paragraphs_1', 'closed');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_1_edit')
->press();
$this
->checkParagraphInMode('field_paragraphs_0', 'closed');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_text_paragraph_add_more')
->press();
$this
->getSession()
->getPage()
->fillField('field_paragraphs[2][subform][field_text][0][value]', 'Third paragraph');
$this
->checkParagraphInMode('field_paragraphs_0', 'closed');
$this
->checkParagraphInMode('field_paragraphs_1', 'closed');
$this
->checkParagraphInMode('field_paragraphs_2', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_2_duplicate')
->press();
$this
->getSession()
->getPage()
->fillField('field_paragraphs[3][subform][field_text][0][value]', 'Fourth paragraph');
$this
->checkParagraphInMode('field_paragraphs_0', 'closed');
$this
->checkParagraphInMode('field_paragraphs_1', 'closed');
$this
->checkParagraphInMode('field_paragraphs_2', 'closed');
$this
->checkParagraphInMode('field_paragraphs_3', 'edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_3_remove')
->press();
$this
->checkParagraphInMode('field_paragraphs_3', 'removed');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_2_edit')
->press();
$this
->checkParagraphInMode('field_paragraphs_3', 'removed');
}
public function testClosedExtendNestedEditMode() {
$this
->addParagraphedContentType('paragraphed_test');
$permissions = [
'administer content types',
'administer node fields',
'administer paragraphs types',
'administer node form display',
'administer paragraph fields',
'administer paragraph form display',
'create paragraphed_test content',
'edit any paragraphed_test content',
];
$this
->loginAsAdmin($permissions, TRUE);
$this
->addParagraphsType('container_paragraph');
$this
->addParagraphsField('container_paragraph', 'field_paragraphs', 'paragraph', 'paragraphs');
$settings = [
'edit_mode' => 'closed',
'closed_mode' => 'summary',
];
$this
->setParagraphsWidgetSettings('container_paragraph', 'field_paragraphs', $settings, 'paragraphs', 'paragraph');
$this
->addParagraphsType('text_paragraph');
$this
->addFieldtoParagraphType('text_paragraph', 'field_text', 'text_long');
$settings = [
'edit_mode' => 'closed_expand_nested',
'closed_mode' => 'summary',
];
$this
->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings);
$this
->drupalGet('admin/structure/types/manage/paragraphed_test/form-display');
$option = $this
->assertSession()
->optionExists('fields[field_paragraphs][type]', 'paragraphs');
$this
->assertTrue($option
->isSelected());
$this
->assertSession()
->pageTextContains('Edit mode: Closed, show nested');
$this
->drupalGet('admin/structure/paragraphs_type/container_paragraph/form-display');
$option = $this
->assertSession()
->optionExists('fields[field_paragraphs][type]', 'paragraphs');
$this
->assertTrue($option
->isSelected());
$this
->assertSession()
->pageTextContains('Edit mode: Closed');
$text_paragraph_1 = Paragraph::create([
'type' => 'text_paragraph',
'field_text' => [
'value' => 'Test text 1',
'format' => 'plain_text',
],
]);
$text_paragraph_1
->save();
$paragraph_1 = Paragraph::create([
'type' => 'container_paragraph',
'field_paragraphs' => [
$text_paragraph_1,
],
]);
$paragraph_1
->save();
$text_paragraph_2 = Paragraph::create([
'type' => 'text_paragraph',
'field_text' => [
'value' => 'Test text 2',
'format' => 'plain_text',
],
]);
$text_paragraph_2
->save();
$paragraph_2 = Paragraph::create([
'type' => 'container_paragraph',
'field_paragraphs' => [
$text_paragraph_2,
$paragraph_1,
],
]);
$paragraph_2
->save();
$text_paragraph_3 = Paragraph::create([
'type' => 'text_paragraph',
'field_text' => [
'value' => 'Test text 3',
'format' => 'plain_text',
],
]);
$text_paragraph_3
->save();
$node = Node::create([
'type' => 'paragraphed_test',
'title' => 'Paragraphs Test',
'field_paragraphs' => [
$paragraph_2,
$text_paragraph_3,
],
]);
$node
->save();
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$this
->checkParagraphInMode('field_paragraphs_1', 'closed');
$this
->checkParagraphInMode('field_paragraphs_0_subform_field_paragraphs_0', 'closed');
$this
->checkParagraphInMode('field_paragraphs_0_subform_field_paragraphs_1', 'closed');
$settings = [
'edit_mode' => 'closed_expand_nested',
];
$this
->setParagraphsWidgetSettings('container_paragraph', 'field_paragraphs', $settings, 'paragraphs', 'paragraph');
$this
->drupalGet('admin/structure/paragraphs_type/container_paragraph/form-display');
$this
->assertSession()
->pageTextContains('Edit mode: Closed, show nested');
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->checkParagraphInMode('field_paragraphs_0_subform_field_paragraphs_1', 'edit');
}
public function testClosedModeThreshold() {
$this
->addParagraphedContentType('paragraphed_test');
$permissions = [
'administer content types',
'administer node fields',
'administer paragraphs types',
'administer node form display',
'administer paragraph fields',
'administer paragraph form display',
'create paragraphed_test content',
'edit any paragraphed_test content',
];
$this
->loginAsAdmin($permissions, TRUE);
$this
->addParagraphsType('text_paragraph');
$this
->addFieldtoParagraphType('text_paragraph', 'field_text', 'text_long');
$this
->addParagraphsType('container_paragraph');
$this
->addParagraphsField('container_paragraph', 'field_paragraphs', 'paragraph', 'paragraphs');
$settings = [
'edit_mode' => 'closed',
'closed_mode' => 'summary',
];
$this
->setParagraphsWidgetSettings('container_paragraph', 'field_paragraphs', $settings, 'paragraphs', 'paragraph');
$settings = [
'edit_mode' => 'closed',
'closed_mode' => 'summary',
];
$this
->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings);
$this
->assertSession()
->pageTextNotContains('Closed mode threshold: 1');
$text_paragraph_1 = Paragraph::create([
'type' => 'text_paragraph',
'field_text' => [
'value' => 'Test text 1',
'format' => 'plain_text',
],
]);
$text_paragraph_1
->save();
$node = Node::create([
'type' => 'paragraphed_test',
'title' => 'Paragraphs Test',
'field_paragraphs' => [
$text_paragraph_1,
],
]);
$node
->save();
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->checkParagraphInMode('field_paragraphs_0', 'closed');
$settings = [
'closed_mode_threshold' => 2,
];
$this
->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings);
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$settings = [
'edit_mode' => 'closed_expand_nested',
];
$this
->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings);
$text_paragraph_2 = Paragraph::create([
'type' => 'text_paragraph',
'field_text' => [
'value' => 'Test text 2',
'format' => 'plain_text',
],
]);
$text_paragraph_2
->save();
$paragraph_1 = Paragraph::create([
'type' => 'container_paragraph',
'field_paragraphs' => [
$text_paragraph_2,
],
]);
$paragraph_1
->save();
$node
->set('field_paragraphs', [
$text_paragraph_1,
$paragraph_1,
]);
$node
->save();
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->checkParagraphInMode('field_paragraphs_0', 'closed');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
$settings = [
'closed_mode_threshold' => 3,
];
$this
->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings);
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->checkParagraphInMode('field_paragraphs_0', 'edit');
$this
->checkParagraphInMode('field_paragraphs_1', 'edit');
}
public function testAddModeSelect() {
$this
->loginAsAdmin();
$this
->addParagraphedContentType('paragraphed_test', 'paragraphs');
$this
->addParagraphsType('test_paragraph');
$this
->addParagraphsType('text');
$this
->addFieldtoParagraphType('text', 'field_text_demo', 'text');
$settings = [
'add_mode' => 'select',
'edit_mode' => 'closed',
'closed_mode' => 'summary',
];
$this
->setParagraphsWidgetSettings('paragraphed_test', 'paragraphs', $settings, 'paragraphs');
$this
->drupalGet('node/add/paragraphed_test');
$this
->assertSession()
->selectExists('paragraphs[add_more][add_more_select]');
$edit = [
'settings[handler_settings][negate]' => 0,
'settings[handler_settings][target_bundles_drag_drop][text][enabled]' => 1,
];
$this
->drupalGet('admin/structure/types/manage/paragraphed_test/fields/node.paragraphed_test.paragraphs');
$this
->submitForm($edit, 'Save settings');
$this
->drupalGet('node/add/paragraphed_test');
$this
->assertSession()
->fieldNotExists('paragraphs[add_more][add_more_select]');
$this
->getSession()
->getPage()
->findButton('paragraphs_add_more')
->press();
$edit = [
'title[0][value]' => 'Demo text title',
'paragraphs[0][subform][field_text_demo][0][value]' => 'Demo text for the detail page',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Demo text for the detail page');
}
public function checkParagraphInMode($button_prefix, $mode) {
switch ($mode) {
case 'edit':
$this
->assertSession()
->buttonNotExists($button_prefix . '_edit');
$this
->assertSession()
->buttonExists($button_prefix . '_collapse');
break;
case 'closed':
$this
->assertSession()
->buttonExists($button_prefix . '_edit');
$this
->assertSession()
->buttonNotExists($button_prefix . '_collapse');
break;
case 'removed':
$this
->assertSession()
->buttonNotExists($button_prefix . '_edit');
$this
->assertSession()
->buttonNotExists($button_prefix . '_collapse');
break;
default:
throw new \InvalidArgumentException('This function does not support "' . $mode . '" as an argument for "$mode" parameter');
}
}
}