Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/taxonomy/tests/src/Kernel/TermHierarchyValidationTest.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\taxonomy\Kernel; | |
4 | |
5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; | |
6 use Drupal\taxonomy\Entity\Vocabulary; | |
7 | |
8 /** | |
9 * Tests handling of pending revisions. | |
10 * | |
11 * @coversDefaultClass \Drupal\taxonomy\Plugin\Validation\Constraint\TaxonomyTermHierarchyConstraintValidator | |
12 * | |
13 * @group taxonomy | |
14 */ | |
15 class TermHierarchyValidationTest extends EntityKernelTestBase { | |
16 | |
17 /** | |
18 * {@inheritdoc} | |
19 */ | |
20 public static $modules = ['taxonomy']; | |
21 | |
22 /** | |
23 * {@inheritdoc} | |
24 */ | |
25 protected function setUp() { | |
26 parent::setUp(); | |
27 | |
28 $this->installEntitySchema('taxonomy_term'); | |
29 } | |
30 | |
31 /** | |
32 * Tests the term hierarchy validation with re-parenting in pending revisions. | |
33 */ | |
34 public function testTermHierarchyValidation() { | |
35 $vocabulary_id = mb_strtolower($this->randomMachineName()); | |
36 $vocabulary = Vocabulary::create([ | |
37 'name' => $vocabulary_id, | |
38 'vid' => $vocabulary_id, | |
39 ]); | |
40 $vocabulary->save(); | |
41 | |
42 // Create a simple hierarchy in the vocabulary, a root term and three parent | |
43 // terms. | |
44 /** @var \Drupal\Core\Entity\RevisionableStorageInterface $term_storage */ | |
45 $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); | |
46 $root = $term_storage->create([ | |
47 'name' => $this->randomMachineName(), | |
48 'vid' => $vocabulary_id, | |
49 ]); | |
50 $root->save(); | |
51 $parent1 = $term_storage->create([ | |
52 'name' => $this->randomMachineName(), | |
53 'vid' => $vocabulary_id, | |
54 'parent' => $root->id(), | |
55 ]); | |
56 $parent1->save(); | |
57 $parent2 = $term_storage->create([ | |
58 'name' => $this->randomMachineName(), | |
59 'vid' => $vocabulary_id, | |
60 'parent' => $root->id(), | |
61 ]); | |
62 $parent2->save(); | |
63 $parent3 = $term_storage->create([ | |
64 'name' => $this->randomMachineName(), | |
65 'vid' => $vocabulary_id, | |
66 'parent' => $root->id(), | |
67 ]); | |
68 $parent3->save(); | |
69 | |
70 // Create a child term and assign one of the parents above. | |
71 $child1 = $term_storage->create([ | |
72 'name' => $this->randomMachineName(), | |
73 'vid' => $vocabulary_id, | |
74 'parent' => $parent1->id(), | |
75 ]); | |
76 $violations = $child1->validate(); | |
77 $this->assertEmpty($violations); | |
78 $child1->save(); | |
79 | |
80 $validation_message = 'You can only change the hierarchy for the <em>published</em> version of this term.'; | |
81 | |
82 // Add a pending revision without changing the term parent. | |
83 $pending_name = $this->randomMachineName(); | |
84 $child_pending = $term_storage->createRevision($child1, FALSE); | |
85 $child_pending->name = $pending_name; | |
86 $violations = $child_pending->validate(); | |
87 $this->assertEmpty($violations); | |
88 | |
89 // Add a pending revision and change the parent. | |
90 $child_pending = $term_storage->createRevision($child1, FALSE); | |
91 $child_pending->parent = $parent2; | |
92 $violations = $child_pending->validate(); | |
93 $this->assertCount(1, $violations); | |
94 $this->assertEquals($validation_message, $violations[0]->getMessage()); | |
95 $this->assertEquals('parent', $violations[0]->getPropertyPath()); | |
96 | |
97 // Add a pending revision and add a new parent. | |
98 $child_pending = $term_storage->createRevision($child1, FALSE); | |
99 $child_pending->parent[0] = $parent1; | |
100 $child_pending->parent[1] = $parent3; | |
101 $violations = $child_pending->validate(); | |
102 $this->assertCount(1, $violations); | |
103 $this->assertEquals($validation_message, $violations[0]->getMessage()); | |
104 $this->assertEquals('parent', $violations[0]->getPropertyPath()); | |
105 | |
106 // Add a pending revision and use the root term as a parent. | |
107 $child_pending = $term_storage->createRevision($child1, FALSE); | |
108 $child_pending->parent[0] = $root; | |
109 $violations = $child_pending->validate(); | |
110 $this->assertCount(1, $violations); | |
111 $this->assertEquals($validation_message, $violations[0]->getMessage()); | |
112 $this->assertEquals('parent', $violations[0]->getPropertyPath()); | |
113 | |
114 // Add a pending revision and remove the parent. | |
115 $child_pending = $term_storage->createRevision($child1, FALSE); | |
116 $child_pending->parent[0] = NULL; | |
117 $violations = $child_pending->validate(); | |
118 $this->assertCount(1, $violations); | |
119 $this->assertEquals($validation_message, $violations[0]->getMessage()); | |
120 $this->assertEquals('parent', $violations[0]->getPropertyPath()); | |
121 | |
122 // Add a pending revision and change the weight. | |
123 $child_pending = $term_storage->createRevision($child1, FALSE); | |
124 $child_pending->weight = 10; | |
125 $violations = $child_pending->validate(); | |
126 $this->assertCount(1, $violations); | |
127 $this->assertEquals($validation_message, $violations[0]->getMessage()); | |
128 $this->assertEquals('weight', $violations[0]->getPropertyPath()); | |
129 | |
130 // Add a pending revision and change both the parent and the weight. | |
131 $child_pending = $term_storage->createRevision($child1, FALSE); | |
132 $child_pending->parent = $parent2; | |
133 $child_pending->weight = 10; | |
134 $violations = $child_pending->validate(); | |
135 $this->assertCount(2, $violations); | |
136 $this->assertEquals($validation_message, $violations[0]->getMessage()); | |
137 $this->assertEquals($validation_message, $violations[1]->getMessage()); | |
138 $this->assertEquals('parent', $violations[0]->getPropertyPath()); | |
139 $this->assertEquals('weight', $violations[1]->getPropertyPath()); | |
140 | |
141 // Add a published revision and change the parent. | |
142 $child_pending = $term_storage->createRevision($child1, TRUE); | |
143 $child_pending->parent[0] = $parent2; | |
144 $violations = $child_pending->validate(); | |
145 $this->assertEmpty($violations); | |
146 | |
147 // Add a new term as a third-level child. | |
148 // The taxonomy tree structure ends up as follows: | |
149 // root | |
150 // - parent1 | |
151 // - parent2 | |
152 // -- child1 <- this will be a term with a pending revision | |
153 // --- child2 | |
154 // - parent3 | |
155 $child2 = $term_storage->create([ | |
156 'name' => $this->randomMachineName(), | |
157 'vid' => $vocabulary_id, | |
158 'parent' => $child1->id(), | |
159 ]); | |
160 $child2->save(); | |
161 | |
162 // Change 'child1' to be a pending revision. | |
163 $child1 = $term_storage->createRevision($child1, FALSE); | |
164 $child1->save(); | |
165 | |
166 // Check that a child of a pending term can not be re-parented. | |
167 $child2_pending = $term_storage->createRevision($child2, FALSE); | |
168 $child2_pending->parent = $parent3; | |
169 $violations = $child2_pending->validate(); | |
170 $this->assertCount(1, $violations); | |
171 $this->assertEquals($validation_message, $violations[0]->getMessage()); | |
172 $this->assertEquals('parent', $violations[0]->getPropertyPath()); | |
173 | |
174 // Check that another term which has a pending revision can not moved under | |
175 // another term which has pending revision. | |
176 $parent3_pending = $term_storage->createRevision($parent3, FALSE); | |
177 $parent3_pending->parent = $child1; | |
178 $violations = $parent3_pending->validate(); | |
179 $this->assertCount(1, $violations); | |
180 $this->assertEquals($validation_message, $violations[0]->getMessage()); | |
181 $this->assertEquals('parent', $violations[0]->getPropertyPath()); | |
182 | |
183 // Check that a new term can be created under a term that has a pending | |
184 // revision. | |
185 $child3 = $term_storage->create([ | |
186 'name' => $this->randomMachineName(), | |
187 'vid' => $vocabulary_id, | |
188 'parent' => $child1->id(), | |
189 ]); | |
190 $violations = $child3->validate(); | |
191 $this->assertEmpty($violations); | |
192 } | |
193 | |
194 } |