Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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     'file',
37   ];
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
44     // Create paragraphs and article content types.
45     $values = ['type' => 'article', 'name' => 'Article'];
46     $node_type = NodeType::create($values);
47     $node_type->save();
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');
54   }
55
56   /**
57    * Tests the replication of the parent entity.
58    */
59   public function testReplication() {
60     // Create the paragraph type.
61     $paragraph_type = ParagraphsType::create([
62       'label' => 'test_text',
63       'id' => 'test_text',
64     ]);
65     $paragraph_type->save();
66
67     $paragraph_type_nested = ParagraphsType::create([
68       'label' => 'test_nested',
69       'id' => 'test_nested',
70     ]);
71     $paragraph_type_nested->save();
72
73     // Add a title field to both paragraph bundles.
74     $field_storage = FieldStorageConfig::create([
75       'field_name' => 'title',
76       'entity_type' => 'paragraph',
77       'type' => 'string',
78       'cardinality' => '1',
79     ]);
80     $field_storage->save();
81     $field = FieldConfig::create([
82       'field_storage' => $field_storage,
83       'bundle' => 'test_text',
84     ]);
85     $field->save();
86     $field = FieldConfig::create([
87       'field_storage' => $field_storage,
88       'bundle' => 'test_nested',
89     ]);
90     $field->save();
91
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',
98       'settings' => [
99         'target_type' => 'paragraph',
100       ],
101     ]);
102     $field_storage->save();
103     $field = FieldConfig::create([
104       'field_storage' => $field_storage,
105       'bundle' => 'test_nested',
106     ]);
107     $field->save();
108
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',
115       'settings' => [
116         'target_type' => 'paragraph',
117       ],
118     ]);
119     $field_storage->save();
120     $field = FieldConfig::create([
121       'field_storage' => $field_storage,
122       'bundle' => 'article',
123     ]);
124     $field->save();
125
126     // Create a paragraph.
127     $paragraph = Paragraph::create([
128       'title' => 'Simple paragraph',
129       'type' => 'test_text',
130     ]);
131     $paragraph->save();
132
133     // Create nested paragraph.
134     $paragraph_nested = Paragraph::create([
135       'title' => 'Nested paragraph',
136       'type' => 'test_text',
137     ]);
138     $paragraph_nested->save();
139
140     // Create another paragraph.
141     $paragraph_nested_parent = Paragraph::create([
142       'title' => 'Parent paragraph',
143       'type' => 'test_nested',
144       'nested_paragraph_field' => [$paragraph_nested],
145     ]);
146     $paragraph_nested_parent->save();
147
148     // Create a node with two paragraphs.
149     $node = Node::create([
150       'title' => $this->randomMachineName(),
151       'type' => 'article',
152       'node_paragraph_field' => array($paragraph, $paragraph_nested_parent),
153       ]);
154     $node->save();
155
156     $replicated_node = $this->container->get('replicate.replicator')
157       ->replicateEntity($node);
158
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.");
167
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();
181
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.');
185
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.');
189   }
190 }