Version 1
[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
14 /**
15  * Base class for tests.
16  */
17 abstract class ParagraphsTestBase extends WebTestBase {
18
19   use FieldUiTestTrait;
20
21   /**
22    * Drupal user object created by loginAsAdmin().
23    *
24    * @var \Drupal\user\UserInterface
25    */
26   protected $admin_user = NULL;
27
28   /**
29    * List of permissions used by loginAsAdmin().
30    *
31    * @var array
32    */
33   protected $admin_permissions = [];
34
35   /**
36    * Modules to enable.
37    *
38    * @var array
39    */
40   public static $modules = [
41     'node',
42     'paragraphs',
43     'field',
44     'field_ui',
45     'block',
46     'paragraphs_test',
47   ];
48
49   /**
50    * {@inheritdoc}
51    */
52   protected function setUp() {
53     parent::setUp();
54     // Place the breadcrumb, tested in fieldUIAddNewField().
55     $this->drupalPlaceBlock('system_breadcrumb_block');
56     $this->drupalPlaceBlock('local_tasks_block');
57     $this->drupalPlaceBlock('local_actions_block');
58     $this->drupalPlaceBlock('page_title_block');
59
60     $this->admin_permissions = [
61       'administer nodes',
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    * Adds a content type with a Paragraphs field.
100    *
101    * @param string $content_type_name
102    *   Content type name to be used.
103    * @param string $paragraphs_field_name
104    *   Paragraphs field name to be used.
105    */
106   protected function addParagraphedContentType($content_type_name, $paragraphs_field_name) {
107     // Create the content type.
108     $node_type = NodeType::create([
109       'type' => $content_type_name,
110       'name' => $content_type_name,
111     ]);
112     $node_type->save();
113
114     $this->addParagraphsField($content_type_name, $paragraphs_field_name, 'node');
115   }
116
117   /**
118    * Adds a Paragraphs field to a given $entity_type.
119    *
120    * @param string $entity_type_name
121    *   Entity type name to be used.
122    * @param string $paragraphs_field_name
123    *   Paragraphs field name to be used.
124    * @param string $entity_type
125    *   Entity type where to add the field.
126    */
127   protected function addParagraphsField($entity_type_name, $paragraphs_field_name, $entity_type) {
128     // Add a paragraphs field.
129     $field_storage = FieldStorageConfig::create([
130       'field_name' => $paragraphs_field_name,
131       'entity_type' => $entity_type,
132       'type' => 'entity_reference_revisions',
133       'cardinality' => '-1',
134       'settings' => [
135         'target_type' => 'paragraph',
136       ],
137     ]);
138     $field_storage->save();
139     $field = FieldConfig::create([
140       'field_storage' => $field_storage,
141       'bundle' => $entity_type_name,
142       'settings' => [
143         'handler' => 'default:paragraph',
144         'handler_settings' => ['target_bundles' => NULL],
145       ],
146     ]);
147     $field->save();
148
149     $form_display = EntityFormDisplay::create([
150       'targetEntityType' => $entity_type,
151       'bundle' => $entity_type_name,
152       'mode' => 'default',
153       'status' => TRUE,
154     ])
155       ->setComponent($paragraphs_field_name, ['type' => 'entity_reference_paragraphs']);
156     $form_display->save();
157
158     $view_display = EntityViewDisplay::create([
159       'targetEntityType' => $entity_type,
160       'bundle' => $entity_type_name,
161       'mode' => 'default',
162       'status' => TRUE,
163     ])->setComponent($paragraphs_field_name, ['type' => 'entity_reference_revisions_entity_view']);
164     $view_display->save();
165   }
166
167   /**
168    * Adds a Paragraphs type.
169    *
170    * @param string $paragraphs_type_name
171    *   Paragraph type name used to create.
172    */
173   protected function addParagraphsType($paragraphs_type_name) {
174     $paragraphs_type = ParagraphsType::create([
175       'id' => $paragraphs_type_name,
176       'label' => $paragraphs_type_name,
177     ]);
178     $paragraphs_type->save();
179   }
180
181   /**
182    * Sets the Paragraphs widget add mode.
183    *
184    * @param string $content_type
185    *   Content type name where to set the widget mode.
186    * @param string $paragraphs_field
187    *   Paragraphs field to change the mode.
188    * @param string $mode
189    *   Mode to be set. ('dropdown', 'select' or 'button').
190    */
191   protected function setAddMode($content_type, $paragraphs_field, $mode) {
192     $form_display = EntityFormDisplay::load('node.' . $content_type . '.default')
193       ->setComponent($paragraphs_field, [
194         'type' => 'entity_reference_paragraphs',
195         'settings' => ['add_mode' => $mode]
196       ]);
197     $form_display->save();
198   }
199
200   /**
201    * Sets the allowed Paragraphs types that can be added.
202    *
203    * @param string $content_type
204    *   Content type name that contains the paragraphs field.
205    * @param array $paragraphs_types
206    *   Array of paragraphs types that will be modified.
207    * @param bool $selected
208    *   Whether or not the paragraphs types will be enabled.
209    * @param string $paragraphs_field
210    *   Paragraphs field name that does the reference.
211    */
212   protected function setAllowedParagraphsTypes($content_type, $paragraphs_types, $selected, $paragraphs_field) {
213     $edit = [];
214     $this->drupalGet('admin/structure/types/manage/' . $content_type . '/fields/node.' . $content_type . '.' . $paragraphs_field);
215     foreach ($paragraphs_types as $paragraphs_type) {
216       $edit['settings[handler_settings][target_bundles_drag_drop][' . $paragraphs_type . '][enabled]'] = $selected;
217     }
218     $this->drupalPostForm(NULL, $edit, t('Save settings'));
219   }
220
221   /**
222    * Sets the weight of a given Paragraphs type.
223    *
224    * @param string $content_type
225    *   Content type name that contains the paragraphs field.
226    * @param string $paragraphs_type
227    *   ID of Paragraph type that will be modified.
228    * @param int $weight
229    *   Weight to be set.
230    * @param string $paragraphs_field
231    *   Paragraphs field name that does the reference.
232    */
233   protected function setParagraphsTypeWeight($content_type, $paragraphs_type, $weight, $paragraphs_field) {
234     $this->drupalGet('admin/structure/types/manage/' . $content_type . '/fields/node.' . $content_type . '.' . $paragraphs_field);
235     $edit['settings[handler_settings][target_bundles_drag_drop][' . $paragraphs_type . '][weight]'] = $weight;
236     $this->drupalPostForm(NULL, $edit, t('Save settings'));
237   }
238
239   /**
240    * Sets the default paragraph type.
241    *
242    * @param $content_type
243    *   Content type name that contains the paragraphs field.
244    * @param $paragraphs_name
245    *   Paragraphs name.
246    * @param $paragraphs_field_name
247    *   Paragraphs field name to be used.
248    * @param $default_type
249    *   Default paragraph type which should be set.
250    */
251   protected function setDefaultParagraphType($content_type, $paragraphs_name, $paragraphs_field_name, $default_type) {
252     $this->drupalGet('admin/structure/types/manage/' . $content_type . '/form-display');
253     $this->drupalPostAjaxForm(NULL, [], $paragraphs_field_name);
254     $this->drupalPostForm(NULL, ['fields[' . $paragraphs_name . '][settings_edit_form][settings][default_paragraph_type]' => $default_type], t('Update'));
255     $this->drupalPostForm(NULL, [], t('Save'));
256   }
257
258   /**
259    * Removes the default paragraph type.
260    *
261    * @param $content_type
262    *   Content type name that contains the paragraphs field.
263    */
264   protected function removeDefaultParagraphType($content_type) {
265     $this->drupalGet('node/add/' . $content_type);
266     $this->drupalPostForm(NULL, [], 'Remove');
267     $this->drupalPostForm(NULL, [], 'Confirm removal');
268     $this->assertNoText('No paragraphs added yet.');
269   }
270
271   /**
272    * Sets the Paragraphs widget display mode.
273    *
274    * @param string $content_type
275    *   Content type name where to set the widget mode.
276    * @param string $paragraphs_field
277    *   Paragraphs field to change the mode.
278    * @param string $mode
279    *   Mode to be set. ('closed', 'preview' or 'open').
280    */
281   protected function setParagraphsWidgetMode($content_type, $paragraphs_field, $mode) {
282     $this->drupalGet('admin/structure/types/manage/' . $content_type . '/form-display');
283     $this->drupalPostAjaxForm(NULL, [], $paragraphs_field . '_settings_edit');
284     $this->drupalPostForm(NULL, ['fields[' . $paragraphs_field . '][settings_edit_form][settings][edit_mode]' => $mode], t('Update'));
285     $this->drupalPostForm(NULL, [], 'Save');
286   }
287
288 }