Version 1
[yaffs-website] / web / modules / contrib / paragraphs / tests / src / Kernel / ParagraphsReplicateTest.php
1 <?php
2
3 namespace Drupal\Tests\paragraphs\Kernel;
4
5 use Drupal\Core\Entity\Entity;
6 use Drupal\Core\Site\Settings;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\node\Entity\Node;
10 use Drupal\node\Entity\NodeType;
11 use Drupal\paragraphs\Entity\Paragraph;
12 use Drupal\paragraphs\Entity\ParagraphsType;
13 use Drupal\KernelTests\KernelTestBase;
14 use Drupal\user\Entity\User;
15
16 /**
17  * Tests the replication functionality provided by Replicate module.
18  *
19  * @group paragraphs
20  */
21 class ParagraphsReplicateTest extends KernelTestBase {
22
23   /**
24    * Modules to enable.
25    *
26    * @var array
27    */
28   public static $modules = [
29     'paragraphs',
30     'replicate',
31     'node',
32     'user',
33     'system',
34     'field',
35     'entity_reference_revisions',
36   ];
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp() {
42     parent::setUp();
43     // Create paragraphs and article content types.
44     $values = ['type' => 'article', 'name' => 'Article'];
45     $node_type = NodeType::create($values);
46     $node_type->save();
47     $this->installEntitySchema('user');
48     $this->installEntitySchema('node');
49     $this->installEntitySchema('paragraph');
50     $this->installSchema('system', ['sequences']);
51     $this->installSchema('node', ['node_access']);
52     \Drupal::moduleHandler()->loadInclude('paragraphs', 'install');
53   }
54
55   /**
56    * Tests the replication of the parent entity.
57    */
58   public function testReplication() {
59     // Create the paragraph type.
60     $paragraph_type = ParagraphsType::create([
61       'label' => 'test_text',
62       'id' => 'test_text',
63     ]);
64     $paragraph_type->save();
65
66     $paragraph_type_nested = ParagraphsType::create([
67       'label' => 'test_nested',
68       'id' => 'test_nested',
69     ]);
70     $paragraph_type_nested->save();
71
72     // Add a title field to both paragraph bundles.
73     $field_storage = FieldStorageConfig::create([
74       'field_name' => 'title',
75       'entity_type' => 'paragraph',
76       'type' => 'string',
77       'cardinality' => '1',
78     ]);
79     $field_storage->save();
80     $field = FieldConfig::create([
81       'field_storage' => $field_storage,
82       'bundle' => 'test_text',
83     ]);
84     $field->save();
85     $field = FieldConfig::create([
86       'field_storage' => $field_storage,
87       'bundle' => 'test_nested',
88     ]);
89     $field->save();
90
91     // Add a paragraph field to the nested paragraph.
92     $field_storage = FieldStorageConfig::create([
93       'field_name' => 'nested_paragraph_field',
94       'entity_type' => 'paragraph',
95       'type' => 'entity_reference_revisions',
96       'cardinality' => '-1',
97       'settings' => [
98         'target_type' => 'paragraph',
99       ],
100     ]);
101     $field_storage->save();
102     $field = FieldConfig::create([
103       'field_storage' => $field_storage,
104       'bundle' => 'test_nested',
105     ]);
106     $field->save();
107
108     // Add a paragraph field to the article.
109     $field_storage = FieldStorageConfig::create([
110       'field_name' => 'node_paragraph_field',
111       'entity_type' => 'node',
112       'type' => 'entity_reference_revisions',
113       'cardinality' => '-1',
114       'settings' => [
115         'target_type' => 'paragraph',
116       ],
117     ]);
118     $field_storage->save();
119     $field = FieldConfig::create([
120       'field_storage' => $field_storage,
121       'bundle' => 'article',
122     ]);
123     $field->save();
124
125     // Create a paragraph.
126     $paragraph = Paragraph::create([
127       'title' => 'Simple paragraph',
128       'type' => 'test_text',
129     ]);
130     $paragraph->save();
131
132     // Create nested paragraph.
133     $paragraph_nested = Paragraph::create([
134       'title' => 'Nested paragraph',
135       'type' => 'test_text',
136     ]);
137     $paragraph_nested->save();
138
139     // Create another paragraph.
140     $paragraph_nested_parent = Paragraph::create([
141       'title' => 'Parent paragraph',
142       'type' => 'test_nested',
143       'nested_paragraph_field' => [$paragraph_nested],
144     ]);
145     $paragraph_nested_parent->save();
146
147     // Create a node with two paragraphs.
148     $node = Node::create([
149       'title' => $this->randomMachineName(),
150       'type' => 'article',
151       'node_paragraph_field' => array($paragraph, $paragraph_nested_parent),
152       ]);
153     $node->save();
154
155     $replicated_node = $this->container->get('replicate.replicator')
156       ->replicateEntity($node);
157
158     // Check that all paragraphs on the replicated node were replicated too.
159     $this->assertNotEquals($replicated_node->id(), $node->id(), 'We have two different nodes.');
160     $this->assertNotEquals($replicated_node->node_paragraph_field[0]->target_id, $node->node_paragraph_field[0]->target_id, 'Simple paragraph was duplicated.');
161     $this->assertEquals('Simple paragraph', $replicated_node->node_paragraph_field[0]->entity->title->value, "Simple paragraph inherited title from it's original.");
162     $this->assertNotEquals($replicated_node->node_paragraph_field[1]->target_id, $node->node_paragraph_field[1]->target_id, 'Parent paragraph was duplicated.');
163     $this->assertEquals('Parent paragraph', $replicated_node->node_paragraph_field[1]->entity->title->value, "Parent paragraph inherited title from it's original.");
164     $this->assertNotEquals($replicated_node->node_paragraph_field[1]->entity->nested_paragraph_field[0]->target_id, $node->node_paragraph_field[1]->entity->nested_paragraph_field[0]->target_id, 'Nested paragraph was duplicated.');
165     $this->assertEquals('Nested paragraph', $replicated_node->node_paragraph_field[1]->entity->nested_paragraph_field[0]->entity->title->value, "Nested paragraph inherited title from it's original.");
166
167     // Try to edit replicated paragraphs and check that changes do not propagate.
168     /** @var \Drupal\paragraphs\ParagraphInterface $simple_paragraph */
169     $simple_paragraph = $replicated_node->node_paragraph_field[0]->entity;
170     $simple_paragraph->title->value = 'Simple paragraph - replicated';
171     $simple_paragraph->save();
172     /** @var \Drupal\paragraphs\ParagraphInterface $parent_paragraph */
173     $parent_paragraph = $replicated_node->node_paragraph_field[1]->entity;
174     $parent_paragraph->title->value = 'Parent paragraph - replicated';
175     $parent_paragraph->save();
176     /** @var \Drupal\paragraphs\ParagraphInterface $nested_paragraph */
177     $nested_paragraph = $replicated_node->node_paragraph_field[1]->entity->nested_paragraph_field[0]->entity;
178     $nested_paragraph->title->value = 'Nested paragraph - replicated';
179     $nested_paragraph->save();
180
181     $this->assertEquals('Simple paragraph', $node->node_paragraph_field[0]->entity->title->value, 'Field value on the original simple paragraph are unchanged.');
182     $this->assertEquals('Parent paragraph', $node->node_paragraph_field[1]->entity->title->value, 'Field value on the original parent paragraph are unchanged.');
183     $this->assertEquals('Nested paragraph', $node->node_paragraph_field[1]->entity->nested_paragraph_field[0]->entity->title->value, 'Field value on the original nested paragraph are unchanged.');
184
185     $this->assertEquals('Simple paragraph - replicated', $replicated_node->node_paragraph_field[0]->entity->title->value, 'Field value on the replicated simple paragraph are updated.');
186     $this->assertEquals('Parent paragraph - replicated', $replicated_node->node_paragraph_field[1]->entity->title->value, 'Field value on the replicated parent paragraph are updated.');
187     $this->assertEquals('Nested paragraph - replicated', $replicated_node->node_paragraph_field[1]->entity->nested_paragraph_field[0]->entity->title->value, 'Field value on the replicated nested paragraph are updated.');
188   }
189 }