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',
42 protected function setUp() {
44 // Create paragraphs and article content types.
45 $values = ['type' => 'article', 'name' => 'Article'];
46 $node_type = NodeType::create($values);
48 $this->installEntitySchema('user');
49 $this->installEntitySchema('node');
50 $this->installEntitySchema('paragraph');
51 $this->installSchema('system', ['sequences']);
52 $this->installSchema('node', ['node_access']);
53 \Drupal::moduleHandler()->loadInclude('paragraphs', 'install');
57 * Tests the replication of the parent entity.
59 public function testReplication() {
60 // Create the paragraph type.
61 $paragraph_type = ParagraphsType::create([
62 'label' => 'test_text',
65 $paragraph_type->save();
67 $paragraph_type_nested = ParagraphsType::create([
68 'label' => 'test_nested',
69 'id' => 'test_nested',
71 $paragraph_type_nested->save();
73 // Add a title field to both paragraph bundles.
74 $field_storage = FieldStorageConfig::create([
75 'field_name' => 'title',
76 'entity_type' => 'paragraph',
80 $field_storage->save();
81 $field = FieldConfig::create([
82 'field_storage' => $field_storage,
83 'bundle' => 'test_text',
86 $field = FieldConfig::create([
87 'field_storage' => $field_storage,
88 'bundle' => 'test_nested',
92 // Add a paragraph field to the nested paragraph.
93 $field_storage = FieldStorageConfig::create([
94 'field_name' => 'nested_paragraph_field',
95 'entity_type' => 'paragraph',
96 'type' => 'entity_reference_revisions',
97 'cardinality' => '-1',
99 'target_type' => 'paragraph',
102 $field_storage->save();
103 $field = FieldConfig::create([
104 'field_storage' => $field_storage,
105 'bundle' => 'test_nested',
109 // Add a paragraph field to the article.
110 $field_storage = FieldStorageConfig::create([
111 'field_name' => 'node_paragraph_field',
112 'entity_type' => 'node',
113 'type' => 'entity_reference_revisions',
114 'cardinality' => '-1',
116 'target_type' => 'paragraph',
119 $field_storage->save();
120 $field = FieldConfig::create([
121 'field_storage' => $field_storage,
122 'bundle' => 'article',
126 // Create a paragraph.
127 $paragraph = Paragraph::create([
128 'title' => 'Simple paragraph',
129 'type' => 'test_text',
133 // Create nested paragraph.
134 $paragraph_nested = Paragraph::create([
135 'title' => 'Nested paragraph',
136 'type' => 'test_text',
138 $paragraph_nested->save();
140 // Create another paragraph.
141 $paragraph_nested_parent = Paragraph::create([
142 'title' => 'Parent paragraph',
143 'type' => 'test_nested',
144 'nested_paragraph_field' => [$paragraph_nested],
146 $paragraph_nested_parent->save();
148 // Create a node with two paragraphs.
149 $node = Node::create([
150 'title' => $this->randomMachineName(),
152 'node_paragraph_field' => array($paragraph, $paragraph_nested_parent),
156 $replicated_node = $this->container->get('replicate.replicator')
157 ->replicateEntity($node);
159 // Check that all paragraphs on the replicated node were replicated too.
160 $this->assertNotEquals($replicated_node->id(), $node->id(), 'We have two different nodes.');
161 $this->assertNotEquals($replicated_node->node_paragraph_field[0]->target_id, $node->node_paragraph_field[0]->target_id, 'Simple paragraph was duplicated.');
162 $this->assertEquals('Simple paragraph', $replicated_node->node_paragraph_field[0]->entity->title->value, "Simple paragraph inherited title from it's original.");
163 $this->assertNotEquals($replicated_node->node_paragraph_field[1]->target_id, $node->node_paragraph_field[1]->target_id, 'Parent paragraph was duplicated.');
164 $this->assertEquals('Parent paragraph', $replicated_node->node_paragraph_field[1]->entity->title->value, "Parent paragraph inherited title from it's original.");
165 $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.');
166 $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.");
168 // Try to edit replicated paragraphs and check that changes do not propagate.
169 /** @var \Drupal\paragraphs\ParagraphInterface $simple_paragraph */
170 $simple_paragraph = $replicated_node->node_paragraph_field[0]->entity;
171 $simple_paragraph->title->value = 'Simple paragraph - replicated';
172 $simple_paragraph->save();
173 /** @var \Drupal\paragraphs\ParagraphInterface $parent_paragraph */
174 $parent_paragraph = $replicated_node->node_paragraph_field[1]->entity;
175 $parent_paragraph->title->value = 'Parent paragraph - replicated';
176 $parent_paragraph->save();
177 /** @var \Drupal\paragraphs\ParagraphInterface $nested_paragraph */
178 $nested_paragraph = $replicated_node->node_paragraph_field[1]->entity->nested_paragraph_field[0]->entity;
179 $nested_paragraph->title->value = 'Nested paragraph - replicated';
180 $nested_paragraph->save();
182 $this->assertEquals('Simple paragraph', $node->node_paragraph_field[0]->entity->title->value, 'Field value on the original simple paragraph are unchanged.');
183 $this->assertEquals('Parent paragraph', $node->node_paragraph_field[1]->entity->title->value, 'Field value on the original parent paragraph are unchanged.');
184 $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.');
186 $this->assertEquals('Simple paragraph - replicated', $replicated_node->node_paragraph_field[0]->entity->title->value, 'Field value on the replicated simple paragraph are updated.');
187 $this->assertEquals('Parent paragraph - replicated', $replicated_node->node_paragraph_field[1]->entity->title->value, 'Field value on the replicated parent paragraph are updated.');
188 $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.');