Version 1
[yaffs-website] / web / modules / contrib / paragraphs / src / Tests / Experimental / ParagraphsExperimentalTestBase.php
1 <?php
2
3 namespace Drupal\paragraphs\Tests\Experimental;
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\paragraphs\Tests\Classic\ParagraphsTestBase;
11
12 /**
13  * Base class for tests.
14  */
15 abstract class ParagraphsExperimentalTestBase extends ParagraphsTestBase {
16
17   use FieldUiTestTrait;
18
19   /**
20    * Adds a Paragraphs field to a given $entity_type.
21    *
22    * @param string $entity_type_name
23    *   Entity type name to be used.
24    * @param string $paragraphs_field_name
25    *   Paragraphs field name to be used.
26    * @param string $entity_type
27    *   Entity type where to add the field.
28    */
29   protected function addParagraphsField($entity_type_name, $paragraphs_field_name, $entity_type) {
30     // Add a paragraphs field.
31     $field_storage = FieldStorageConfig::create([
32       'field_name' => $paragraphs_field_name,
33       'entity_type' => $entity_type,
34       'type' => 'entity_reference_revisions',
35       'cardinality' => '-1',
36       'settings' => [
37         'target_type' => 'paragraph',
38       ],
39     ]);
40     $field_storage->save();
41     $field = FieldConfig::create([
42       'field_storage' => $field_storage,
43       'bundle' => $entity_type_name,
44       'settings' => [
45         'handler' => 'default:paragraph',
46         'handler_settings' => ['target_bundles' => NULL],
47       ],
48     ]);
49     $field->save();
50
51     $form_display = EntityFormDisplay::create([
52       'targetEntityType' => $entity_type,
53       'bundle' => $entity_type_name,
54       'mode' => 'default',
55       'status' => TRUE,
56     ])
57       ->setComponent($paragraphs_field_name, ['type' => 'paragraphs']);
58     $form_display->save();
59
60     $view_display = EntityViewDisplay::create([
61       'targetEntityType' => $entity_type,
62       'bundle' => $entity_type_name,
63       'mode' => 'default',
64       'status' => TRUE,
65     ])->setComponent($paragraphs_field_name, ['type' => 'entity_reference_revisions_entity_view']);
66     $view_display->save();
67   }
68
69   /**
70    * Sets the Paragraphs widget add mode.
71    *
72    * @param string $content_type
73    *   Content type name where to set the widget mode.
74    * @param string $paragraphs_field
75    *   Paragraphs field to change the mode.
76    * @param string $mode
77    *   Mode to be set. ('dropdown', 'select' or 'button').
78    */
79   protected function setAddMode($content_type, $paragraphs_field, $mode) {
80     $form_display = EntityFormDisplay::load('node.' . $content_type . '.default')
81       ->setComponent($paragraphs_field, [
82         'type' => 'paragraphs',
83         'settings' => ['add_mode' => $mode]
84       ]);
85     $form_display->save();
86   }
87
88   /**
89    * Removes the default paragraph type.
90    *
91    * @param $content_type
92    *   Content type name that contains the paragraphs field.
93    */
94   protected function removeDefaultParagraphType($content_type) {
95     $this->drupalGet('node/add/' . $content_type);
96     $this->drupalPostForm(NULL, [], 'Remove');
97     $this->assertNoText('No paragraphs added yet.');
98   }
99
100 }