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