View source
<?php
namespace Drupal\Tests\paragraphs\Functional\WidgetStable;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
class ParagraphsDragAndDropModeTest extends ParagraphsTestBase {
public static $modules = [
'node',
'paragraphs',
'field',
];
public function setUp() : void {
parent::setUp();
$this
->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
$this
->addParagraphsType('paragraphs_container');
$this
->addParagraphsType('text');
$this
->addFieldtoParagraphType('text', 'field_text', 'text');
$this
->addParagraphsField('paragraphs_container', 'paragraphs_container_paragraphs', 'paragraph');
$component = [
'type' => 'paragraphs',
'region' => 'content',
'settings' => [
'edit_mode' => 'closed',
'form_display_mode' => 'default',
],
];
EntityFormDisplay::load('paragraph.paragraphs_container.default')
->setComponent('paragraphs_container_paragraphs', $component)
->save();
EntityFormDisplay::load('node.paragraphed_test.default')
->setComponent('field_paragraphs', $component)
->save();
$admin = $this
->drupalCreateUser([
'create paragraphed_test content',
'edit any paragraphed_test content',
]);
$this
->drupalLogin($admin);
\Drupal::state()
->set('paragraphs_test_dragdrop_force_show', TRUE);
}
public function testChangeParagraphParentWeight() {
$text_paragraph_1 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 1',
'format' => 'plain_text',
],
]);
$text_paragraph_1
->save();
$text_paragraph_2 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 2.',
'format' => 'plain_text',
],
]);
$text_paragraph_2
->save();
$paragraph = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$text_paragraph_1,
$text_paragraph_2,
],
]);
$paragraph
->save();
$node = Node::create([
'type' => 'paragraphed_test',
'title' => 'Paragraphs Test',
'field_paragraphs' => [
$paragraph,
],
]);
$node
->save();
$text_paragraph_2 = Paragraph::load($text_paragraph_2
->id());
$this
->assertEquals($text_paragraph_2
->get('parent_id')->value, $paragraph
->id());
$this
->assertEquals($text_paragraph_2
->get('parent_type')->value, 'paragraph');
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->submitForm([], 'Drag & drop');
$assert_session = $this
->assertSession();
$assert_session
->hiddenFieldValueEquals('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]', 'field_paragraphs][0][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldValueEquals('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_path]', 'field_paragraphs][0][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
->setValue('field_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_path]')
->setValue('field_paragraphs][1][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][_weight]')
->setValue(1);
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_weight]')
->setValue(0);
$this
->submitForm([], 'Complete drag & drop');
$this
->submitForm([], 'Save');
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('paragraph')
->resetCache();
$node = Node::load($node
->id());
$this
->assertEquals(count($node
->get('field_paragraphs')), 2);
$this
->assertEquals($node
->get('field_paragraphs')
->get(0)->target_id, $text_paragraph_1
->id());
$text_paragraph_1 = $node
->get('field_paragraphs')
->get(0)->entity;
$this
->assertEquals($text_paragraph_1
->get('parent_id')->value, $node
->id());
$this
->assertEquals($text_paragraph_1
->get('parent_type')->value, 'node');
$this
->assertEquals($node
->get('field_paragraphs')
->get(1)->target_id, $paragraph
->id());
$paragraph = $node
->get('field_paragraphs')
->get(1)->entity;
$this
->assertEquals($paragraph
->get('parent_id')->value, $node
->id());
$this
->assertEquals($paragraph
->get('parent_type')->value, 'node');
$this
->assertEquals(count($paragraph
->get('paragraphs_container_paragraphs')), 1);
$this
->assertEquals($paragraph
->get('paragraphs_container_paragraphs')->target_id, $text_paragraph_2
->id());
$text_paragraph_2 = $paragraph
->get('paragraphs_container_paragraphs')->entity;
$this
->assertEquals($text_paragraph_2
->get('parent_id')->value, $paragraph
->id());
$this
->assertEquals($text_paragraph_2
->get('parent_type')->value, 'paragraph');
$library_discovery = \Drupal::service('library.discovery');
$library = $library_discovery
->getLibraryByName('paragraphs', 'paragraphs-dragdrop');
if (!$library) {
\Drupal::state()
->set('paragraphs_test_dragdrop_force_show', FALSE);
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->assertSession()
->buttonNotExists('Drag & drop');
}
}
public function testChangeParagraphContainerMove() {
$text_paragraph_1 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 1',
'format' => 'plain_text',
],
]);
$text_paragraph_1
->save();
$paragraph = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$text_paragraph_1,
],
]);
$paragraph
->save();
$paragraph_1 = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [],
]);
$paragraph_1
->save();
$node = Node::create([
'type' => 'paragraphed_test',
'title' => 'Paragraphs Test',
'field_paragraphs' => [
$paragraph,
$paragraph_1,
],
]);
$node
->save();
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->submitForm([], 'Drag & drop');
$this
->assertSession()
->elementTextContains('css', '.paragraphs-dragdrop-wrapper li:nth-of-type(1)', 'Test text 1');
$this
->assertSession()
->elementTextNotContains('css', '.paragraphs-dragdrop-wrapper li:nth-of-type(2)', 'Test text 1');
$this
->assertSession()
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
->setValue('field_paragraphs][1][paragraphs_container_paragraphs');
$this
->submitForm([], 'Complete drag & drop');
$this
->assertSession()
->elementTextNotContains('css', '.field--name-field-paragraphs tbody tr:nth-of-type(1) .paragraph-summary', 'Test text 1');
$this
->assertSession()
->elementTextContains('css', '.field--name-field-paragraphs tbody tr:nth-of-type(2) .paragraph-summary', 'Test text 1');
$this
->submitForm([], 'Drag & drop');
$this
->assertSession()
->elementTextNotContains('css', '.paragraphs-dragdrop-wrapper li:nth-of-type(1)', 'Test text 1');
$this
->assertSession()
->elementTextContains('css', '.paragraphs-dragdrop-wrapper li:nth-of-type(2)', 'Test text 1');
$this
->submitForm([], 'Complete drag & drop');
$this
->submitForm([], 'Save');
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('paragraph')
->resetCache();
$node = Node::load($node
->id());
$this
->assertEquals(count($node
->get('field_paragraphs')), 2);
$this
->assertEquals($node
->get('field_paragraphs')
->get(0)->target_id, $paragraph
->id());
$this
->assertEquals($node
->get('field_paragraphs')
->get(1)->target_id, $paragraph_1
->id());
$paragraph = $node
->get('field_paragraphs')
->get(0)->entity;
$this
->assertEquals(count($paragraph
->get('paragraphs_container_paragraphs')), 0);
$paragraph_1 = $node
->get('field_paragraphs')
->get(1)->entity;
$this
->assertEquals(count($paragraph_1
->get('paragraphs_container_paragraphs')), 1);
$this
->assertEquals($paragraph_1
->get('paragraphs_container_paragraphs')
->get(0)->target_id, $text_paragraph_1
->id());
$text_paragraph_1 = $paragraph_1
->get('paragraphs_container_paragraphs')->entity;
$this
->assertEquals($text_paragraph_1
->get('parent_id')->value, $paragraph_1
->id());
$this
->assertEquals($text_paragraph_1
->get('parent_type')->value, 'paragraph');
}
public function testMultipleChangesParagraphs() {
$text_paragraph_1 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 1',
'format' => 'plain_text',
],
]);
$text_paragraph_1
->save();
$text_paragraph_2 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 2.',
'format' => 'plain_text',
],
]);
$text_paragraph_2
->save();
$paragraph_1 = Paragraph::create([
'title' => 'Test Paragraph 1',
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$text_paragraph_1,
$text_paragraph_2,
],
]);
$paragraph_1
->save();
$text_paragraph_3 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 3.',
'format' => 'plain_text',
],
]);
$text_paragraph_3
->save();
$paragraph_2 = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$text_paragraph_3,
],
]);
$paragraph_2
->save();
$paragraph_3 = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$paragraph_2,
],
]);
$paragraph_3
->save();
$paragraph_4 = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [],
]);
$paragraph_4
->save();
$node = Node::create([
'type' => 'paragraphed_test',
'title' => 'Paragraphs Test',
'field_paragraphs' => [
$paragraph_1,
$paragraph_3,
$paragraph_4,
],
]);
$node
->save();
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_2_edit')
->press();
$this
->getSession()
->getPage()
->findButton('field_paragraphs_2_subform_paragraphs_container_paragraphs_text_add_more')
->press();
$edit = [
'field_paragraphs[2][subform][paragraphs_container_paragraphs][0][subform][field_text][0][value]' => 'new paragraph',
];
$this
->submitForm($edit, 'Drag & drop');
$assert_session = $this
->assertSession();
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][1][dragdrop][paragraphs_container_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
->setValue('field_paragraphs][0][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
->setValue('field_paragraphs][1][paragraphs_container_paragraphs][0][paragraphs_container_paragraphs][0][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][1][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
->setValue('field_paragraphs][1][paragraphs_container_paragraphs][0][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][1][_path]')
->setValue('field_paragraphs][1][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][2][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
->setValue('field_paragraphs][1][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][1][_weight]')
->setValue(0);
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][2][dragdrop][paragraphs_container_paragraphs][list][0][_weight]')
->setValue(1);
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][2][_weight]')
->setValue(1);
$this
->submitForm([], 'Save');
\Drupal::entityTypeManager()
->getStorage('paragraph')
->resetCache();
$text_paragraph_1 = Paragraph::load($text_paragraph_1
->id());
$this
->assertEquals($text_paragraph_1
->get('parent_id')->value, $paragraph_2
->id());
$this
->assertEquals($text_paragraph_1
->get('parent_type')->value, 'paragraph');
$text_paragraph_3 = Paragraph::load($text_paragraph_3
->id());
$this
->assertEquals($text_paragraph_3
->get('parent_id')->value, $paragraph_1
->id());
$this
->assertEquals($text_paragraph_3
->get('parent_type')->value, 'paragraph');
$paragraph_3 = Paragraph::load($paragraph_3
->id());
$this
->assertEquals($paragraph_3
->get('parent_id')->value, $paragraph_4
->id());
$this
->assertEquals($paragraph_3
->get('parent_type')->value, 'paragraph');
}
public function testChangeParagraphContainerMultipleFields() {
$this
->addParagraphsField('paragraphed_test', 'field_paragraphs_second', 'node');
$text_paragraph_1 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 1',
'format' => 'plain_text',
],
]);
$text_paragraph_1
->save();
$text_paragraph_2 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 2',
'format' => 'plain_text',
],
]);
$text_paragraph_2
->save();
$paragraph = Paragraph::create([
'title' => 'Test Paragraph',
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$text_paragraph_1,
],
]);
$paragraph
->save();
$paragraph_1 = Paragraph::create([
'title' => 'Test Paragraph 1',
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [],
]);
$paragraph_1
->save();
$paragraph_second = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$text_paragraph_2,
],
]);
$paragraph_second
->save();
$node = Node::create([
'type' => 'paragraphed_test',
'title' => 'Paragraphs Test',
'field_paragraphs' => [
$paragraph,
$paragraph_1,
],
'field_paragraphs_second' => [
$paragraph_second,
],
]);
$node
->save();
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->submitForm([], 'Drag & drop');
$this
->assertSession()
->buttonExists('field_paragraphs_second_0_subform_paragraphs_container_paragraphs_0_edit');
$this
->assertSession()
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
->setValue('field_paragraphs][1][paragraphs_container_paragraphs');
$this
->submitForm([], 'Save');
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('paragraph')
->resetCache();
$node = Node::load($node
->id());
$this
->assertEquals(count($node
->get('field_paragraphs')), 2);
$this
->assertEquals($node
->get('field_paragraphs')
->get(0)->target_id, $paragraph
->id());
$this
->assertEquals($node
->get('field_paragraphs')
->get(1)->target_id, $paragraph_1
->id());
$paragraph = $node
->get('field_paragraphs')
->get(0)->entity;
$this
->assertEquals(count($paragraph
->get('paragraphs_container_paragraphs')), 0);
$paragraph_1 = $node
->get('field_paragraphs')
->get(1)->entity;
$this
->assertEquals(count($paragraph_1
->get('paragraphs_container_paragraphs')), 1);
$this
->assertEquals($paragraph_1
->get('paragraphs_container_paragraphs')
->get(0)->target_id, $text_paragraph_1
->id());
$text_paragraph_1 = $paragraph_1
->get('paragraphs_container_paragraphs')->entity;
$this
->assertEquals($text_paragraph_1
->get('parent_id')->value, $paragraph_1
->id());
$this
->assertEquals($text_paragraph_1
->get('parent_type')->value, 'paragraph');
$this
->assertEquals(count($node
->get('field_paragraphs_second')), 1);
$this
->assertEquals($node
->get('field_paragraphs_second')
->get(0)->target_id, $paragraph_second
->id());
$paragraph_second = $node
->get('field_paragraphs_second')
->get(0)->entity;
$this
->assertEquals(count($paragraph_second
->get('paragraphs_container_paragraphs')), 1);
$this
->assertEquals($paragraph_second
->get('paragraphs_container_paragraphs')
->get(0)->target_id, $text_paragraph_2
->id());
}
public function testChangeParagraphMoveBeforeReorder() {
$text_paragraph_1 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 1',
'format' => 'plain_text',
],
]);
$text_paragraph_1
->save();
$text_paragraph_2 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 2',
'format' => 'plain_text',
],
]);
$text_paragraph_2
->save();
$paragraph = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$text_paragraph_1,
],
]);
$paragraph
->save();
$paragraph_1 = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [],
]);
$paragraph_1
->save();
$node = Node::create([
'type' => 'paragraphed_test',
'title' => 'Paragraphs Test',
'field_paragraphs' => [
$paragraph,
$text_paragraph_2,
$paragraph_1,
],
]);
$node
->save();
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$edit = [
'field_paragraphs[1][_weight]' => 2,
'field_paragraphs[2][_weight]' => 1,
];
$this
->submitForm($edit, 'Drag & drop');
$this
->assertSession()
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
->setValue('field_paragraphs][1][paragraphs_container_paragraphs');
$this
->submitForm([], 'Complete drag & drop');
$this
->submitForm([], 'Save');
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('paragraph')
->resetCache();
$node = Node::load($node
->id());
$this
->assertEquals(count($node
->get('field_paragraphs')), 3);
$this
->assertEquals($node
->get('field_paragraphs')
->get(0)->target_id, $paragraph
->id());
$this
->assertEquals($node
->get('field_paragraphs')
->get(1)->target_id, $paragraph_1
->id());
$this
->assertEquals($node
->get('field_paragraphs')
->get(2)->target_id, $text_paragraph_2
->id());
$paragraph = $node
->get('field_paragraphs')
->get(0)->entity;
$this
->assertEquals(count($paragraph
->get('paragraphs_container_paragraphs')), 0);
$paragraph_1 = $node
->get('field_paragraphs')
->get(1)->entity;
$this
->assertEquals(count($paragraph_1
->get('paragraphs_container_paragraphs')), 1);
$this
->assertEquals($paragraph_1
->get('paragraphs_container_paragraphs')
->get(0)->target_id, $text_paragraph_1
->id());
$text_paragraph_1 = $paragraph_1
->get('paragraphs_container_paragraphs')->entity;
$this
->assertEquals($text_paragraph_1
->get('parent_id')->value, $paragraph_1
->id());
$this
->assertEquals($text_paragraph_1
->get('parent_type')->value, 'paragraph');
}
public function testChangeParagraphMoveAfterDelete() {
$text_paragraph_1 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 1',
'format' => 'plain_text',
],
]);
$text_paragraph_1
->save();
$text_paragraph_2 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 2',
'format' => 'plain_text',
],
]);
$text_paragraph_2
->save();
$paragraph = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$text_paragraph_1,
],
]);
$paragraph
->save();
$paragraph_1 = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [],
]);
$paragraph_1
->save();
$node = Node::create([
'type' => 'paragraphed_test',
'title' => 'Paragraphs Test',
'field_paragraphs' => [
$paragraph,
$text_paragraph_2,
$paragraph_1,
],
]);
$node
->save();
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->getSession()
->getPage()
->pressButton('field_paragraphs_0_remove');
$this
->submitForm([], 'Drag & drop');
$assert_session = $this
->assertSession();
$assert_session
->pageTextNotContains('Test text 1');
$assert_session
->pageTextContains('Test text 2');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][_path]')
->setValue('field_paragraphs][0][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][1][_weight]')
->setValue(0);
$this
->submitForm([], 'Complete drag & drop');
$this
->submitForm([], 'Save');
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('paragraph')
->resetCache();
$node = Node::load($node
->id());
$this
->assertEquals(count($node
->get('field_paragraphs')), 1);
$this
->assertEquals($node
->get('field_paragraphs')
->get(0)->target_id, $paragraph_1
->id());
$paragraph_1 = $node
->get('field_paragraphs')
->get(0)->entity;
$this
->assertEquals(count($paragraph_1
->get('paragraphs_container_paragraphs')), 1);
$this
->assertEquals($paragraph_1
->get('paragraphs_container_paragraphs')
->get(0)->target_id, $text_paragraph_2
->id());
$text_paragraph_2 = $paragraph_1
->get('paragraphs_container_paragraphs')->entity;
$this
->assertEquals($text_paragraph_2
->get('parent_id')->value, $paragraph_1
->id());
$this
->assertEquals($text_paragraph_2
->get('parent_type')->value, 'paragraph');
}
public function testChangeParagraphMoveAllFromTopLevelContainer() {
$text_paragraph_1 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 1',
'format' => 'plain_text',
],
]);
$text_paragraph_1
->save();
$text_paragraph_2 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 2.',
'format' => 'plain_text',
],
]);
$text_paragraph_2
->save();
$paragraph = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$text_paragraph_1,
$text_paragraph_2,
],
]);
$paragraph
->save();
$node = Node::create([
'type' => 'paragraphed_test',
'title' => 'Paragraphs Test',
'field_paragraphs' => [
$paragraph,
],
]);
$node
->save();
$text_paragraph_2 = Paragraph::load($text_paragraph_2
->id());
$this
->assertEquals($text_paragraph_2
->get('parent_id')->value, $paragraph
->id());
$this
->assertEquals($text_paragraph_2
->get('parent_type')->value, 'paragraph');
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->submitForm([], 'Drag & drop');
$assert_session = $this
->assertSession();
$assert_session
->hiddenFieldValueEquals('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]', 'field_paragraphs][0][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldValueEquals('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_path]', 'field_paragraphs][0][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
->setValue('field_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_path]')
->setValue('field_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_weight]')
->setValue(0);
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_weight]')
->setValue(1);
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][_weight]')
->setValue(2);
$this
->submitForm([], 'Complete drag & drop');
$this
->submitForm([], 'Save');
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('paragraph')
->resetCache();
$node = Node::load($node
->id());
$this
->assertEquals(3, count($node
->get('field_paragraphs')));
$this
->assertEquals($node
->get('field_paragraphs')
->get(0)->target_id, $text_paragraph_1
->id());
$text_paragraph_1 = $node
->get('field_paragraphs')
->get(0)->entity;
$this
->assertEquals('node', $text_paragraph_1
->get('parent_type')->value);
$this
->assertEquals($node
->id(), $text_paragraph_1
->get('parent_id')->value);
$this
->assertEquals($node
->get('field_paragraphs')
->get(1)->target_id, $text_paragraph_2
->id());
$text_paragraph_2 = $node
->get('field_paragraphs')
->get(1)->entity;
$this
->assertEquals('node', $text_paragraph_2
->get('parent_type')->value);
$this
->assertEquals($node
->id(), $text_paragraph_2
->get('parent_id')->value);
$this
->assertEquals($node
->get('field_paragraphs')
->get(2)->target_id, $paragraph
->id());
$paragraph = $node
->get('field_paragraphs')
->get(2)->entity;
$this
->assertEquals('node', $paragraph
->get('parent_type')->value);
$this
->assertEquals($node
->id(), $paragraph
->get('parent_id')->value);
$this
->assertEquals(0, count($paragraph
->get('paragraphs_container_paragraphs')));
}
public function testChangeParagraphMoveAllFromNestedContainer() {
$text_paragraph_1 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 1',
'format' => 'plain_text',
],
]);
$text_paragraph_1
->save();
$text_paragraph_2 = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 2.',
'format' => 'plain_text',
],
]);
$text_paragraph_2
->save();
$nested_container = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$text_paragraph_1,
$text_paragraph_2,
],
]);
$nested_container
->save();
$container = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$nested_container,
],
]);
$container
->save();
$node = Node::create([
'type' => 'paragraphed_test',
'title' => 'Paragraphs Test',
'field_paragraphs' => [
$container,
],
]);
$node
->save();
$text_paragraph_2 = Paragraph::load($text_paragraph_2
->id());
$this
->assertEquals($text_paragraph_2
->get('parent_id')->value, $nested_container
->id());
$this
->assertEquals($text_paragraph_2
->get('parent_type')->value, 'paragraph');
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->submitForm([], 'Drag & drop');
$assert_session = $this
->assertSession();
$assert_session
->hiddenFieldValueEquals('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]', 'field_paragraphs][0][paragraphs_container_paragraphs][0][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldValueEquals('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_path]', 'field_paragraphs][0][paragraphs_container_paragraphs][0][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_path]')
->setValue('field_paragraphs][0][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_path]')
->setValue('field_paragraphs][0][paragraphs_container_paragraphs');
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_weight]')
->setValue(0);
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][_weight]')
->setValue(1);
$assert_session
->hiddenFieldExists('field_paragraphs[dragdrop][field_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][0][dragdrop][paragraphs_container_paragraphs][list][1][_weight]')
->setValue(2);
$this
->submitForm([], 'Complete drag & drop');
$this
->submitForm([], 'Save');
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('paragraph')
->resetCache();
$node = Node::load($node
->id());
$this
->assertEquals(1, count($node
->get('field_paragraphs')));
$this
->assertEquals($container
->id(), $node
->get('field_paragraphs')
->get(0)->target_id);
$container = $node
->get('field_paragraphs')
->get(0)->entity;
$this
->assertEquals('node', $container
->get('parent_type')->value);
$this
->assertEquals($node
->id(), $container
->get('parent_id')->value);
$this
->assertEquals(3, count($container
->get('paragraphs_container_paragraphs')));
$this
->assertEquals($text_paragraph_1
->id(), $container
->get('paragraphs_container_paragraphs')
->get(0)->target_id);
$text_paragraph_1 = $container
->get('paragraphs_container_paragraphs')
->get(0)->entity;
$this
->assertEquals('paragraph', $text_paragraph_1
->get('parent_type')->value);
$this
->assertEquals($container
->id(), $text_paragraph_1
->get('parent_id')->value);
$this
->assertEquals($nested_container
->id(), $container
->get('paragraphs_container_paragraphs')
->get(1)->target_id);
$nested_container = $container
->get('paragraphs_container_paragraphs')
->get(1)->entity;
$this
->assertEquals('paragraph', $nested_container
->get('parent_type')->value);
$this
->assertEquals($container
->id(), $nested_container
->get('parent_id')->value);
$this
->assertEquals(count($nested_container
->get('paragraphs_container_paragraphs')), 0);
$this
->assertEquals($text_paragraph_2
->id(), $container
->get('paragraphs_container_paragraphs')
->get(2)->target_id);
$text_paragraph_2 = $container
->get('paragraphs_container_paragraphs')
->get(2)->entity;
$this
->assertEquals('paragraph', $text_paragraph_2
->get('parent_type')->value);
$this
->assertEquals($container
->id(), $text_paragraph_2
->get('parent_id')->value);
}
public function testEmptyNodeTitle() {
$this
->drupalGet('/node/add/paragraphed_test');
$this
->getSession()
->getPage()
->pressButton('Add text');
$this
->submitForm([], 'Drag & drop');
$this
->submitForm([], 'Complete drag & drop');
$this
->assertSession()
->fieldExists('field_paragraphs[0][subform][field_text][0][value]');
$this
->assertSession()
->pageTextNotContains('Title field is required.');
}
}