efa726fb48ccbf09b4dfe8317e7347dbc1d5ef6e
[yaffs-website] / web / modules / contrib / paragraphs / src / Tests / Classic / ParagraphsTestBase.php
1 <?php
2
3 namespace Drupal\paragraphs\Tests\Classic;
4
5 use Drupal\Core\Entity\Entity\EntityFormDisplay;
6 use Drupal\Core\Entity\Entity\EntityViewDisplay;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\field_ui\Tests\FieldUiTestTrait;
10 use Drupal\node\Entity\NodeType;
11 use Drupal\paragraphs\Entity\ParagraphsType;
12 use Drupal\simpletest\WebTestBase;
13 use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
14
15 /**
16  * Base class for tests.
17  */
18 abstract class ParagraphsTestBase extends WebTestBase {
19
20   use FieldUiTestTrait, ParagraphsCoreVersionUiTestTrait, ParagraphsTestBaseTrait;
21
22   /**
23    * Drupal user object created by loginAsAdmin().
24    *
25    * @var \Drupal\user\UserInterface
26    */
27   protected $admin_user = NULL;
28
29   /**
30    * List of permissions used by loginAsAdmin().
31    *
32    * @var array
33    */
34   protected $admin_permissions = [];
35
36   /**
37    * Modules to enable.
38    *
39    * @var array
40    */
41   public static $modules = [
42     'node',
43     'paragraphs',
44     'field',
45     'field_ui',
46     'block',
47     'paragraphs_test',
48   ];
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function setUp() {
54     parent::setUp();
55     // Place the breadcrumb, tested in fieldUIAddNewField().
56     $this->drupalPlaceBlock('system_breadcrumb_block');
57     $this->drupalPlaceBlock('local_tasks_block');
58     $this->drupalPlaceBlock('local_actions_block');
59     $this->drupalPlaceBlock('page_title_block');
60
61     $this->admin_permissions = [
62       'administer content types',
63       'administer node fields',
64       'administer paragraphs types',
65       'administer node form display',
66       'administer paragraph fields',
67       'administer paragraph form display',
68     ];
69   }
70
71   /**
72    * Creates an user with admin permissions and log in.
73    *
74    * @param array $additional_permissions
75    *   Additional permissions that will be granted to admin user.
76    * @param bool $reset_permissions
77    *   Flag to determine if default admin permissions will be replaced by
78    *   $additional_permissions.
79    *
80    * @return object
81    *   Newly created and logged in user object.
82    */
83   function loginAsAdmin($additional_permissions = [], $reset_permissions = FALSE) {
84     $permissions = $this->admin_permissions;
85
86     if ($reset_permissions) {
87       $permissions = $additional_permissions;
88     }
89     elseif (!empty($additional_permissions)) {
90       $permissions = array_merge($permissions, $additional_permissions);
91     }
92
93     $this->admin_user = $this->drupalCreateUser($permissions);
94     $this->drupalLogin($this->admin_user);
95     return $this->admin_user;
96   }
97
98   /**
99    * Sets the Paragraphs widget add mode.
100    *
101    * @param string $content_type
102    *   Content type name where to set the widget mode.
103    * @param string $paragraphs_field
104    *   Paragraphs field to change the mode.
105    * @param string $mode
106    *   Mode to be set. ('dropdown', 'select' or 'button').
107    */
108   protected function setAddMode($content_type, $paragraphs_field, $mode) {
109     $form_display = EntityFormDisplay::load('node.' . $content_type . '.default')
110       ->setComponent($paragraphs_field, [
111         'type' => 'entity_reference_paragraphs',
112         'settings' => ['add_mode' => $mode]
113       ]);
114     $form_display->save();
115   }
116
117   /**
118    * Sets the allowed Paragraphs types that can be added.
119    *
120    * @param string $content_type
121    *   Content type name that contains the paragraphs field.
122    * @param array $paragraphs_types
123    *   Array of paragraphs types that will be modified.
124    * @param bool $selected
125    *   Whether or not the paragraphs types will be enabled.
126    * @param string $paragraphs_field
127    *   Paragraphs field name that does the reference.
128    */
129   protected function setAllowedParagraphsTypes($content_type, $paragraphs_types, $selected, $paragraphs_field) {
130     $edit = [];
131     $this->drupalGet('admin/structure/types/manage/' . $content_type . '/fields/node.' . $content_type . '.' . $paragraphs_field);
132     foreach ($paragraphs_types as $paragraphs_type) {
133       $edit['settings[handler_settings][target_bundles_drag_drop][' . $paragraphs_type . '][enabled]'] = $selected;
134     }
135     $this->drupalPostForm(NULL, $edit, t('Save settings'));
136   }
137
138   /**
139    * Sets the weight of a given Paragraphs type.
140    *
141    * @param string $content_type
142    *   Content type name that contains the paragraphs field.
143    * @param string $paragraphs_type
144    *   ID of Paragraph type that will be modified.
145    * @param int $weight
146    *   Weight to be set.
147    * @param string $paragraphs_field
148    *   Paragraphs field name that does the reference.
149    */
150   protected function setParagraphsTypeWeight($content_type, $paragraphs_type, $weight, $paragraphs_field) {
151     $this->drupalGet('admin/structure/types/manage/' . $content_type . '/fields/node.' . $content_type . '.' . $paragraphs_field);
152     $edit['settings[handler_settings][target_bundles_drag_drop][' . $paragraphs_type . '][weight]'] = $weight;
153     $this->drupalPostForm(NULL, $edit, t('Save settings'));
154   }
155
156   /**
157    * Sets the default paragraph type.
158    *
159    * @param $content_type
160    *   Content type name that contains the paragraphs field.
161    * @param $paragraphs_name
162    *   Paragraphs name.
163    * @param $paragraphs_field_name
164    *   Paragraphs field name to be used.
165    * @param $default_type
166    *   Default paragraph type which should be set.
167    */
168   protected function setDefaultParagraphType($content_type, $paragraphs_name, $paragraphs_field_name, $default_type) {
169     $this->drupalGet('admin/structure/types/manage/' . $content_type . '/form-display');
170     $this->drupalPostAjaxForm(NULL, [], $paragraphs_field_name);
171     $this->drupalPostForm(NULL, ['fields[' . $paragraphs_name . '][settings_edit_form][settings][default_paragraph_type]' => $default_type], t('Update'));
172     $this->drupalPostForm(NULL, [], t('Save'));
173   }
174
175   /**
176    * Removes the default paragraph type.
177    *
178    * @param $content_type
179    *   Content type name that contains the paragraphs field.
180    */
181   protected function removeDefaultParagraphType($content_type) {
182     $this->drupalGet('node/add/' . $content_type);
183     $this->drupalPostForm(NULL, [], 'Remove');
184     $this->drupalPostForm(NULL, [], 'Confirm removal');
185     $this->assertNoText('No paragraphs added yet.');
186   }
187
188   /**
189    * Sets the Paragraphs widget display mode.
190    *
191    * @param string $content_type
192    *   Content type name where to set the widget mode.
193    * @param string $paragraphs_field
194    *   Paragraphs field to change the mode.
195    * @param string $mode
196    *   Mode to be set. ('closed', 'preview' or 'open').
197    *   'preview' is only allowed in the classic widget. Use
198    *   setParagraphsWidgetSettings for the experimental widget, instead.
199    */
200   protected function setParagraphsWidgetMode($content_type, $paragraphs_field, $mode) {
201     $this->drupalGet('admin/structure/types/manage/' . $content_type . '/form-display');
202     $this->drupalPostAjaxForm(NULL, [], $paragraphs_field . '_settings_edit');
203     $this->drupalPostForm(NULL, ['fields[' . $paragraphs_field . '][settings_edit_form][settings][edit_mode]' => $mode], t('Update'));
204     $this->drupalPostForm(NULL, [], 'Save');
205   }
206
207 }