adfdb81551790c200aaf82353e0731cc43d7c9a3
[yaffs-website] / web / modules / contrib / paragraphs / tests / src / FunctionalJavascript / ParagraphsExperimentalEditPerspectivesUiTest.php
1 <?php
2
3 namespace Drupal\Tests\paragraphs\FunctionalJavascript;
4
5 use Drupal\Core\Entity\Entity\EntityFormDisplay;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
9
10 /**
11  * Test paragraphs user interface.
12  *
13  * @group paragraphs
14  */
15 class ParagraphsExperimentalEditPerspectivesUiTest extends JavascriptTestBase {
16
17   use LoginAdminTrait;
18   use ParagraphsTestBaseTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = [
26     'node',
27     'paragraphs_test',
28     'paragraphs',
29     'field',
30     'field_ui',
31     'block',
32     'link',
33     'text',
34   ];
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::setUp();
41   }
42
43   /**
44    * Test paragraphs user interface.
45    */
46   public function testEditPerspectives() {
47
48     $this->loginAsAdmin([
49       'access content overview',
50       'edit behavior plugin settings'
51     ]);
52
53     $page = $this->getSession()->getPage();
54     $this->drupalGet('admin/structure/paragraphs_type/add');
55     $edit = [
56       'label' => 'TestPlugin',
57       'id' => 'testplugin',
58       'behavior_plugins[test_text_color][enabled]' => TRUE,
59     ];
60     $this->drupalPostForm(NULL, $edit, t('Save and manage fields'));
61     $this->drupalGet('admin/structure/types/add');
62     $edit = [
63       'name' => 'TestContent',
64       'type' => 'testcontent',
65     ];
66     $this->drupalPostForm(NULL, $edit, t('Save and manage fields'));
67     $this->drupalGet('admin/structure/types/manage/testcontent/fields/add-field');
68     $edit = [
69       'new_storage_type' => 'field_ui:entity_reference_revisions:paragraph',
70       'label' => 'testparagraphfield',
71       'field_name' => 'testparagraphfield',
72     ];
73     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
74     $edit = [
75       'settings[target_type]' => 'paragraph',
76     ];
77     $this->drupalPostForm(NULL, $edit, t('Save field settings'));
78     $edit = [
79       'settings[handler_settings][target_bundles_drag_drop][testplugin][enabled]' => TRUE,
80     ];
81     $this->drupalPostForm(NULL, $edit, t('Save settings'));
82     $this->drupalGet('admin/structure/types/manage/testcontent/form-display');
83     $page->selectFieldOption('fields[field_testparagraphfield][type]', 'paragraphs');
84     $this->assertSession()->assertWaitOnAjaxRequest();
85     $this->drupalPostForm(NULL, [], t('Save'));
86     $this->drupalGet('node/add/testcontent');
87     $this->clickLink('Behavior');
88     $style_selector = $page->find('css', '.form-item-field-testparagraphfield-0-behavior-plugins-test-text-color-text-color');
89     $this->assertTrue($style_selector->isVisible());
90     $this->clickLink('Content');
91     $this->assertFalse($style_selector->isVisible());
92   }
93
94   /**
95    * Test if tabs are visible with no behavior elements.
96    */
97   public function testTabsVisibility() {
98     $this->loginAsAdmin([
99       'access content overview',
100     ]);
101
102     $page = $this->getSession()->getPage();
103     $this->drupalGet('admin/structure/paragraphs_type/add');
104     $edit = [
105       'label' => 'TestPlugin',
106       'id' => 'testplugin',
107     ];
108     $this->drupalPostForm(NULL, $edit, t('Save and manage fields'));
109     $this->drupalGet('admin/structure/types/add');
110     $edit = [
111       'name' => 'TestContent',
112       'type' => 'testcontent',
113     ];
114     $this->drupalPostForm(NULL, $edit, t('Save and manage fields'));
115     $this->drupalGet('admin/structure/types/manage/testcontent/fields/add-field');
116     $edit = [
117       'new_storage_type' => 'field_ui:entity_reference_revisions:paragraph',
118       'label' => 'testparagraphfield',
119       'field_name' => 'testparagraphfield',
120     ];
121     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
122     $edit = [
123       'settings[target_type]' => 'paragraph',
124     ];
125     $this->drupalPostForm(NULL, $edit, t('Save field settings'));
126     $this->drupalPostForm(NULL, NULL, t('Save settings'));
127     $this->drupalGet('admin/structure/types/manage/testcontent/form-display');
128     $page->selectFieldOption('fields[field_testparagraphfield][type]', 'paragraphs');
129     $this->assertSession()->assertWaitOnAjaxRequest();
130     $this->drupalPostForm(NULL, [], t('Save'));
131     $this->drupalGet('node/add/testcontent');
132     $style_selector = $page->find('css', '.paragraphs-tabs');
133     $this->assertFalse($style_selector->isVisible());
134   }
135
136   /**
137    * Test edit perspectives works fine with multiple fields.
138    */
139   public function testPerspectivesWithMultipleFields() {
140     $this->loginAsAdmin([
141       'edit behavior plugin settings'
142     ]);
143
144     // Add a nested Paragraph type.
145     $paragraph_type = 'nested_paragraph';
146     $this->addParagraphsType($paragraph_type);
147     $this->addParagraphsField('nested_paragraph', 'paragraphs', 'paragraph');
148     $edit = [
149       'behavior_plugins[test_bold_text][enabled]' => TRUE,
150     ];
151     $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, t('Save'));
152
153     $this->addParagraphedContentType('testcontent');
154     $this->addParagraphsField('testcontent', 'field_paragraphs2', 'node');
155
156     // Disable the default paragraph on both the node and the nested paragraph
157     // to explicitly test with no paragraph and avoid a loop.
158     EntityFormDisplay::load('node.testcontent.default')
159       ->setComponent('field_paragraphs', ['type' => 'paragraphs', 'settings' => ['default_paragraph_type' => '_none']])
160       ->setComponent('field_paragraphs2', ['type' => 'paragraphs', 'settings' => ['default_paragraph_type' => '_none']])
161       ->save();
162     EntityFormDisplay::load('paragraph' . '.' . $paragraph_type . '.default')
163       ->setComponent('paragraphs', ['type' => 'paragraphs', 'settings' => ['default_paragraph_type' => '_none']])
164       ->save();
165
166     $assert_session = $this->assertSession();
167
168     $this->drupalGet('node/add/testcontent');
169     $assert_session->elementNotExists('css', '.paragraphs-nested');
170
171     // Add a nested paragraph to the first field.
172     $button = $this->getSession()->getPage()->findButton('Add nested_paragraph');
173     $button->press();
174
175     $assert_session->assertWaitOnAjaxRequest();
176     $assert_session->elementExists('css', '.paragraphs-nested');
177
178     // Add a paragraph to the second field.
179     $region_field2 = $this->getSession()->getPage()->find('css', '.field--name-field-paragraphs2');
180     $button_field2 = $region_field2->findButton('Add nested_paragraph');
181     $button_field2->press();
182     $assert_session->assertWaitOnAjaxRequest();
183
184     // Ge the style checkboxes from each field, make sure they are not visible
185     // by default.
186     $page = $this->getSession()->getPage();
187     $style_selector = $page->findField('field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]');
188     $this->assertFalse($style_selector->isVisible());
189     $style_selector2 = $page->findField('field_paragraphs2[0][behavior_plugins][test_bold_text][bold_text]');
190     $this->assertFalse($style_selector2->isVisible());
191
192     // Switch to Behavior on the first field, then the second, make sure
193     // the visibility of the checkboxes is correct after each change.
194     $this->clickLink('Behavior', 0);
195     $this->assertTrue($style_selector->isVisible());
196     $this->assertFalse($style_selector2->isVisible());
197     $this->clickLink('Behavior', 1);
198     $this->assertTrue($style_selector->isVisible());
199     $this->assertTrue($style_selector2->isVisible());
200
201     // Switch the second field back to Content, verify visibility again.
202     $this->clickLink('Content', 1);
203     $this->assertTrue($style_selector->isVisible());
204     $this->assertFalse($style_selector2->isVisible());
205   }
206
207 }