3 namespace Drupal\Tests\taxonomy\Kernel;
5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
8 * Tests term validation constraints.
12 class TermValidationTest extends EntityKernelTestBase {
19 public static $modules = ['taxonomy'];
24 protected function setUp() {
26 $this->installEntitySchema('taxonomy_term');
30 * Tests the term validation constraints.
32 public function testValidation() {
33 $this->entityManager->getStorage('taxonomy_vocabulary')->create([
37 $term = $this->entityManager->getStorage('taxonomy_term')->create([
41 $violations = $term->validate();
42 $this->assertEqual(count($violations), 0, 'No violations when validating a default term.');
44 $term->set('name', $this->randomString(256));
45 $violations = $term->validate();
46 $this->assertEqual(count($violations), 1, 'Violation found when name is too long.');
47 $this->assertEqual($violations[0]->getPropertyPath(), 'name.0.value');
48 $field_label = $term->get('name')->getFieldDefinition()->getLabel();
49 $this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', ['%name' => $field_label, '@max' => 255]));
51 $term->set('name', NULL);
52 $violations = $term->validate();
53 $this->assertEqual(count($violations), 1, 'Violation found when name is NULL.');
54 $this->assertEqual($violations[0]->getPropertyPath(), 'name');
55 $this->assertEqual($violations[0]->getMessage(), t('This value should not be null.'));
56 $term->set('name', 'test');
58 $term->set('parent', 9999);
59 $violations = $term->validate();
60 $this->assertEqual(count($violations), 1, 'Violation found when term parent is invalid.');
61 $this->assertEqual($violations[0]->getMessage(), format_string('The referenced entity (%type: %id) does not exist.', ['%type' => 'taxonomy_term', '%id' => 9999]));
63 $term->set('parent', 0);
64 $violations = $term->validate();
65 $this->assertEqual(count($violations), 0, 'No violations for parent id 0.');