3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
6 * Tests entity browser integration with paragraphs.
8 * @group entity_browser
10 class ParagraphsTest extends EntityBrowserJavascriptTestBase {
15 public static $modules = [
25 'entity_browser_test_paragraphs',
31 protected static $userPermissions = [
32 'access test_nodes entity browser pages',
33 'create paragraphs_test content',
34 'delete own paragraphs_test content',
36 'edit own article content',
37 'edit any paragraphs_test content',
41 * Tests a flow of adding/removing references with paragraphs.
43 public function testParagraphs() {
44 // Create an article that we'll reference later.
45 $node = $this->createNode(['type' => 'article', 'title' => 'Hello world']);
46 $node_nested = $this->createNode(['type' => 'article', 'title' => 'Nested node']);
47 $node_nested_second = $this->createNode(['type' => 'article', 'title' => 'Nested second node']);
48 $this->drupalGet('node/add/paragraphs_test');
50 // Fill in the title field.
51 $title = $this->assertSession()->fieldExists('Title');
52 $title->setValue('Paragraph test');
54 // Create a new paragraph referencing our article.
55 $this->getSession()->getPage()->pressButton('field_paragraph_content_embed_add_more');
56 $this->waitForAjaxToFinish();
57 $this->assertSession()->linkExists('Select entities');
58 $this->getSession()->getPage()->clickLink('Select entities');
59 $this->getSession()->switchToIFrame('entity_browser_iframe_test_nodes');
60 $this->getSession()->getPage()->checkField('entity_browser_select[node:' . $node->id() . ']');
61 $this->getSession()->getPage()->pressButton('Select entities');
63 // Switch back to the main page.
64 $this->getSession()->switchToIFrame();
65 $this->waitForAjaxToFinish();
67 // Create another paragraph referencing our article.
68 $this->getSession()->getPage()->pressButton('field_paragraph_content_embed_add_more');
69 $this->waitForAjaxToFinish();
70 $this->assertSession()->linkExists('Select entities');
71 $this->getSession()->getPage()->clickLink('Select entities');
72 $this->getSession()->switchToIFrame('entity_browser_iframe_test_nodes');
73 $this->getSession()->getPage()->checkField('entity_browser_select[node:' . $node->id() . ']');
74 $this->getSession()->getPage()->pressButton('Select entities');
76 // Switch back to the main page.
77 $this->getSession()->switchToIFrame();
78 $this->waitForAjaxToFinish();
80 // Ensure that two paragraphs now exist.
81 $selector_1 = '.field--name-field-paragraph tbody tr:nth-child(1) .paragraphs-subform';
82 $selector_2 = str_replace('1', '2', $selector_1);
83 $this->assertSession()->elementExists('css', $selector_1);
84 $this->assertSession()->elementExists('css', $selector_2);
86 // Click the remove button on the second paragraph's article reference,
87 // and ensure that only that instance of our article is removed.
88 $this->click('.field--name-field-paragraph tbody tr:nth-child(2) .paragraphs-subform [value="Remove"]');
89 $this->waitForAjaxToFinish();
90 $this->assertSession()->elementTextNotContains('css', $selector_2, 'Hello world');
91 $this->assertSession()->elementTextContains('css', $selector_1, 'Hello world');
93 // Testing nested paragraphs field.
94 $this->getSession()->getPage()->pressButton('field_paragraph_0_subform_field_paragraphs_nested_nested_paragraph_add_more');
95 $this->waitForAjaxToFinish();
96 $this->assertSession()->linkExists('Select entities');
97 $this->getSession()->getPage()->clickLink('Select entities');
98 $this->getSession()->switchToIFrame('entity_browser_iframe_test_nodes');
99 $this->getSession()->getPage()->checkField('entity_browser_select[node:' . $node_nested->id() . ']');
100 $this->getSession()->getPage()->pressButton('Select entities');
102 // Switch back to the main page.
103 $this->getSession()->switchToIFrame();
104 $this->waitForAjaxToFinish();
106 $this->getSession()->getPage()->pressButton('field_paragraph_0_subform_field_paragraphs_nested_nested_paragraph_add_more');
107 $this->waitForAjaxToFinish();
108 $this->assertSession()->linkExists('Select entities');
109 $this->getSession()->getPage()->clickLink('Select entities');
110 $this->getSession()->switchToIFrame('entity_browser_iframe_test_nodes');
111 $this->getSession()->getPage()->checkField('entity_browser_select[node:' . $node_nested_second->id() . ']');
112 $this->getSession()->getPage()->pressButton('Select entities');
114 // Switch back to the main page.
115 $this->getSession()->switchToIFrame();
116 $this->waitForAjaxToFinish();
118 // Test editing node in nested paragraph.
119 $this->assertSession()->elementTextContains('css', '.field--name-field-paragraphs-nested tbody tr:nth-child(1) .paragraphs-subform', 'Nested node');
120 $this->click('.field--name-field-paragraphs-nested tbody tr:nth-child(1) .paragraphs-subform [value="Edit"]');
121 $this->waitForAjaxToFinish();
122 $this->assertSession()->elementExists('css', '.ui-dialog .node-article-edit-form .form-item-title-0-value [value="Nested node"]');
123 $this->getSession()->getPage()->pressButton('Close');
126 $this->submitForm([], 'Save');
127 $this->assertSession()->linkExists('Hello world');
128 $this->assertSession()->elementTextContains('css', '.field--name-field-paragraphs-nested', 'Nested node');
129 $this->assertSession()->elementTextContains('css', '.field--name-field-paragraphs-nested', 'Nested second node');
131 $parent_node = $this->container->get('entity_type.manager')
133 ->loadByProperties(['title' => 'Paragraph test']);
134 $this->drupalGet('node/' . current($parent_node)->id() . '/edit');
136 // Test removing node in nested paragraph.
137 $this->click('.field--name-field-paragraphs-nested tbody tr:nth-child(1) .paragraphs-subform [value="Remove"]');
138 $this->waitForAjaxToFinish();
139 $this->assertSession()->elementTextNotContains('css', '.field--name-field-paragraphs-nested tbody tr:nth-child(1) .paragraphs-subform', 'Nested node');
140 $this->assertSession()->elementTextContains('css', '.field--name-field-paragraphs-nested tbody tr:nth-child(2) .paragraphs-subform', 'Nested second node');
143 $this->submitForm([], 'Save');
145 // Make sure the form submitted and a link to the articles are present.
146 $this->assertSession()->linkExists('Hello world');
147 $this->assertSession()->linkNotExists('Nested node');
148 $this->assertSession()->elementTextContains('css', '.field--name-field-paragraphs-nested', 'Nested second node');