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;
15 * Base class for tests.
17 abstract class ParagraphsTestBase extends WebTestBase {
22 * Drupal user object created by loginAsAdmin().
24 * @var \Drupal\user\UserInterface
26 protected $admin_user = NULL;
29 * List of permissions used by loginAsAdmin().
33 protected $admin_permissions = [];
40 public static $modules = [
52 protected function 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');
60 $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 * Adds a content type with a Paragraphs field.
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.
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,
114 $this->addParagraphsField($content_type_name, $paragraphs_field_name, 'node');
118 * Adds a Paragraphs field to a given $entity_type.
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.
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',
135 'target_type' => 'paragraph',
138 $field_storage->save();
139 $field = FieldConfig::create([
140 'field_storage' => $field_storage,
141 'bundle' => $entity_type_name,
143 'handler' => 'default:paragraph',
144 'handler_settings' => ['target_bundles' => NULL],
149 $form_display = EntityFormDisplay::create([
150 'targetEntityType' => $entity_type,
151 'bundle' => $entity_type_name,
155 ->setComponent($paragraphs_field_name, ['type' => 'entity_reference_paragraphs']);
156 $form_display->save();
158 $view_display = EntityViewDisplay::create([
159 'targetEntityType' => $entity_type,
160 'bundle' => $entity_type_name,
163 ])->setComponent($paragraphs_field_name, ['type' => 'entity_reference_revisions_entity_view']);
164 $view_display->save();
168 * Adds a Paragraphs type.
170 * @param string $paragraphs_type_name
171 * Paragraph type name used to create.
173 protected function addParagraphsType($paragraphs_type_name) {
174 $paragraphs_type = ParagraphsType::create([
175 'id' => $paragraphs_type_name,
176 'label' => $paragraphs_type_name,
178 $paragraphs_type->save();
182 * Sets the Paragraphs widget add mode.
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').
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]
197 $form_display->save();
201 * Sets the allowed Paragraphs types that can be added.
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.
212 protected function setAllowedParagraphsTypes($content_type, $paragraphs_types, $selected, $paragraphs_field) {
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;
218 $this->drupalPostForm(NULL, $edit, t('Save settings'));
222 * Sets the weight of a given Paragraphs type.
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.
230 * @param string $paragraphs_field
231 * Paragraphs field name that does the reference.
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'));
240 * Sets the default paragraph type.
242 * @param $content_type
243 * Content type name that contains the paragraphs field.
244 * @param $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.
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'));
259 * Removes the default paragraph type.
261 * @param $content_type
262 * Content type name that contains the paragraphs field.
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.');
272 * Sets the Paragraphs widget display mode.
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').
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');