Version 1
[yaffs-website] / web / modules / contrib / entity_browser / tests / src / FunctionalJavascript / ParagraphsTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
4
5 /**
6  * Tests entity browser integration with paragraphs.
7  *
8  * @group entity_browser
9  */
10 class ParagraphsTest extends EntityBrowserJavascriptTestBase {
11
12   /**
13    * {@inheritdoc}
14    */
15   public static $modules = [
16     'ctools',
17     'views',
18     'block',
19     'node',
20     'file',
21     'image',
22     'field_ui',
23     'views_ui',
24     'system',
25     'entity_browser_test_paragraphs',
26   ];
27
28   /**
29    * {@inheritdoc}
30    */
31   protected static $userPermissions = [
32     'access test_nodes entity browser pages',
33     'create paragraphs_test content',
34     'delete own paragraphs_test content',
35     'access content',
36     'edit own article content',
37     'edit any paragraphs_test content',
38   ];
39
40   /**
41    * Tests a flow of adding/removing references with paragraphs.
42    */
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');
49
50     // Fill in the title field.
51     $title = $this->assertSession()->fieldExists('Title');
52     $title->setValue('Paragraph test');
53
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');
62
63     // Switch back to the main page.
64     $this->getSession()->switchToIFrame();
65     $this->waitForAjaxToFinish();
66
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');
75
76     // Switch back to the main page.
77     $this->getSession()->switchToIFrame();
78     $this->waitForAjaxToFinish();
79
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);
85
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');
92
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');
101
102     // Switch back to the main page.
103     $this->getSession()->switchToIFrame();
104     $this->waitForAjaxToFinish();
105
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');
113
114     // Switch back to the main page.
115     $this->getSession()->switchToIFrame();
116     $this->waitForAjaxToFinish();
117
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');
124
125     // Submit the form.
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');
130
131     $parent_node = $this->container->get('entity_type.manager')
132       ->getStorage('node')
133       ->loadByProperties(['title' => 'Paragraph test']);
134     $this->drupalGet('node/' . current($parent_node)->id() . '/edit');
135
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');
141
142     // Submit the form.
143     $this->submitForm([], 'Save');
144
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');
149   }
150
151 }