3 namespace Drupal\paragraphs\Tests\Classic;
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;
16 * Base class for tests.
18 abstract class ParagraphsTestBase extends WebTestBase {
20 use FieldUiTestTrait, ParagraphsCoreVersionUiTestTrait, ParagraphsTestBaseTrait;
23 * Drupal user object created by loginAsAdmin().
25 * @var \Drupal\user\UserInterface
27 protected $admin_user = NULL;
30 * List of permissions used by loginAsAdmin().
34 protected $admin_permissions = [];
41 public static $modules = [
53 protected function 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');
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',
72 * Creates an user with admin permissions and log in.
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.
81 * Newly created and logged in user object.
83 function loginAsAdmin($additional_permissions = [], $reset_permissions = FALSE) {
84 $permissions = $this->admin_permissions;
86 if ($reset_permissions) {
87 $permissions = $additional_permissions;
89 elseif (!empty($additional_permissions)) {
90 $permissions = array_merge($permissions, $additional_permissions);
93 $this->admin_user = $this->drupalCreateUser($permissions);
94 $this->drupalLogin($this->admin_user);
95 return $this->admin_user;
99 * Sets the Paragraphs widget add mode.
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').
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]
114 $form_display->save();
118 * Sets the allowed Paragraphs types that can be added.
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.
129 protected function setAllowedParagraphsTypes($content_type, $paragraphs_types, $selected, $paragraphs_field) {
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;
135 $this->drupalPostForm(NULL, $edit, t('Save settings'));
139 * Sets the weight of a given Paragraphs type.
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.
147 * @param string $paragraphs_field
148 * Paragraphs field name that does the reference.
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'));
157 * Sets the default paragraph type.
159 * @param $content_type
160 * Content type name that contains the paragraphs field.
161 * @param $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.
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'));
176 * Removes the default paragraph type.
178 * @param $content_type
179 * Content type name that contains the paragraphs field.
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.');
189 * Sets the Paragraphs widget display mode.
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.
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');