View source
<?php
namespace Drupal\Tests\taxonomy\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\taxonomy\Entity\Vocabulary;
class TermHierarchyValidationTest extends EntityKernelTestBase {
protected static $modules = [
'taxonomy',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('taxonomy_term');
}
public function testTermHierarchyValidation() {
$vocabulary_id = mb_strtolower($this
->randomMachineName());
$vocabulary = Vocabulary::create([
'name' => $vocabulary_id,
'vid' => $vocabulary_id,
]);
$vocabulary
->save();
$term_storage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$root = $term_storage
->create([
'name' => $this
->randomMachineName(),
'vid' => $vocabulary_id,
]);
$root
->save();
$parent1 = $term_storage
->create([
'name' => $this
->randomMachineName(),
'vid' => $vocabulary_id,
'parent' => $root
->id(),
]);
$parent1
->save();
$parent2 = $term_storage
->create([
'name' => $this
->randomMachineName(),
'vid' => $vocabulary_id,
'parent' => $root
->id(),
]);
$parent2
->save();
$parent3 = $term_storage
->create([
'name' => $this
->randomMachineName(),
'vid' => $vocabulary_id,
'parent' => $root
->id(),
]);
$parent3
->save();
$child1 = $term_storage
->create([
'name' => $this
->randomMachineName(),
'vid' => $vocabulary_id,
'parent' => $parent1
->id(),
]);
$violations = $child1
->validate();
$this
->assertEmpty($violations);
$child1
->save();
$validation_message = 'You can only change the hierarchy for the <em>published</em> version of this term.';
$pending_name = $this
->randomMachineName();
$child_pending = $term_storage
->createRevision($child1, FALSE);
$child_pending->name = $pending_name;
$violations = $child_pending
->validate();
$this
->assertEmpty($violations);
$child_pending = $term_storage
->createRevision($child1, FALSE);
$child_pending->parent = $parent2;
$violations = $child_pending
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals($validation_message, $violations[0]
->getMessage());
$this
->assertEquals('parent', $violations[0]
->getPropertyPath());
$child_pending = $term_storage
->createRevision($child1, FALSE);
$child_pending->parent[0] = $parent1;
$child_pending->parent[1] = $parent3;
$violations = $child_pending
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals($validation_message, $violations[0]
->getMessage());
$this
->assertEquals('parent', $violations[0]
->getPropertyPath());
$child_pending = $term_storage
->createRevision($child1, FALSE);
$child_pending->parent[0] = $root;
$violations = $child_pending
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals($validation_message, $violations[0]
->getMessage());
$this
->assertEquals('parent', $violations[0]
->getPropertyPath());
$child_pending = $term_storage
->createRevision($child1, FALSE);
$child_pending->parent[0] = NULL;
$violations = $child_pending
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals($validation_message, $violations[0]
->getMessage());
$this
->assertEquals('parent', $violations[0]
->getPropertyPath());
$child_pending = $term_storage
->createRevision($child1, FALSE);
$child_pending->weight = 10;
$violations = $child_pending
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals($validation_message, $violations[0]
->getMessage());
$this
->assertEquals('weight', $violations[0]
->getPropertyPath());
$child_pending = $term_storage
->createRevision($child1, FALSE);
$child_pending->parent = $parent2;
$child_pending->weight = 10;
$violations = $child_pending
->validate();
$this
->assertCount(2, $violations);
$this
->assertEquals($validation_message, $violations[0]
->getMessage());
$this
->assertEquals($validation_message, $violations[1]
->getMessage());
$this
->assertEquals('parent', $violations[0]
->getPropertyPath());
$this
->assertEquals('weight', $violations[1]
->getPropertyPath());
$child_pending = $term_storage
->createRevision($child1, TRUE);
$child_pending->parent[0] = $parent2;
$violations = $child_pending
->validate();
$this
->assertEmpty($violations);
$child2 = $term_storage
->create([
'name' => $this
->randomMachineName(),
'vid' => $vocabulary_id,
'parent' => $child1
->id(),
]);
$child2
->save();
$child1 = $term_storage
->createRevision($child1, FALSE);
$child1
->save();
$child2_pending = $term_storage
->createRevision($child2, FALSE);
$child2_pending->parent = $parent3;
$violations = $child2_pending
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals($validation_message, $violations[0]
->getMessage());
$this
->assertEquals('parent', $violations[0]
->getPropertyPath());
$parent3_pending = $term_storage
->createRevision($parent3, FALSE);
$parent3_pending->parent = $child1;
$violations = $parent3_pending
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals($validation_message, $violations[0]
->getMessage());
$this
->assertEquals('parent', $violations[0]
->getPropertyPath());
$child3 = $term_storage
->create([
'name' => $this
->randomMachineName(),
'vid' => $vocabulary_id,
'parent' => $child1
->id(),
]);
$violations = $child3
->validate();
$this
->assertEmpty($violations);
}
}