3 namespace Drupal\Tests\paragraphs\Kernel;
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;
17 * Tests the replication functionality provided by Replicate module.
21 class ParagraphsReplicateTest extends KernelTestBase {
28 public static $modules = [
35 'entity_reference_revisions',
41 protected function setUp() {
43 // Create paragraphs and article content types.
44 $values = ['type' => 'article', 'name' => 'Article'];
45 $node_type = NodeType::create($values);
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');
56 * Tests the replication of the parent entity.
58 public function testReplication() {
59 // Create the paragraph type.
60 $paragraph_type = ParagraphsType::create([
61 'label' => 'test_text',
64 $paragraph_type->save();
66 $paragraph_type_nested = ParagraphsType::create([
67 'label' => 'test_nested',
68 'id' => 'test_nested',
70 $paragraph_type_nested->save();
72 // Add a title field to both paragraph bundles.
73 $field_storage = FieldStorageConfig::create([
74 'field_name' => 'title',
75 'entity_type' => 'paragraph',
79 $field_storage->save();
80 $field = FieldConfig::create([
81 'field_storage' => $field_storage,
82 'bundle' => 'test_text',
85 $field = FieldConfig::create([
86 'field_storage' => $field_storage,
87 'bundle' => 'test_nested',
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',
98 'target_type' => 'paragraph',
101 $field_storage->save();
102 $field = FieldConfig::create([
103 'field_storage' => $field_storage,
104 'bundle' => 'test_nested',
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',
115 'target_type' => 'paragraph',
118 $field_storage->save();
119 $field = FieldConfig::create([
120 'field_storage' => $field_storage,
121 'bundle' => 'article',
125 // Create a paragraph.
126 $paragraph = Paragraph::create([
127 'title' => 'Simple paragraph',
128 'type' => 'test_text',
132 // Create nested paragraph.
133 $paragraph_nested = Paragraph::create([
134 'title' => 'Nested paragraph',
135 'type' => 'test_text',
137 $paragraph_nested->save();
139 // Create another paragraph.
140 $paragraph_nested_parent = Paragraph::create([
141 'title' => 'Parent paragraph',
142 'type' => 'test_nested',
143 'nested_paragraph_field' => [$paragraph_nested],
145 $paragraph_nested_parent->save();
147 // Create a node with two paragraphs.
148 $node = Node::create([
149 'title' => $this->randomMachineName(),
151 'node_paragraph_field' => array($paragraph, $paragraph_nested_parent),
155 $replicated_node = $this->container->get('replicate.replicator')
156 ->replicateEntity($node);
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.");
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();
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.');
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.');