Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / taxonomy / tests / src / Kernel / TermValidationTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Kernel;
4
5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
6
7 /**
8  * Tests term validation constraints.
9  *
10  * @group taxonomy
11  */
12 class TermValidationTest extends EntityKernelTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['taxonomy'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26     $this->installEntitySchema('taxonomy_term');
27   }
28
29   /**
30    * Tests the term validation constraints.
31    */
32   public function testValidation() {
33     $this->entityManager->getStorage('taxonomy_vocabulary')->create([
34       'vid' => 'tags',
35       'name' => 'Tags',
36     ])->save();
37     $term = $this->entityManager->getStorage('taxonomy_term')->create([
38       'name' => 'test',
39       'vid' => 'tags',
40     ]);
41     $violations = $term->validate();
42     $this->assertEqual(count($violations), 0, 'No violations when validating a default term.');
43
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]));
50
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');
57
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]));
62
63     $term->set('parent', 0);
64     $violations = $term->validate();
65     $this->assertEqual(count($violations), 0, 'No violations for parent id 0.');
66   }
67
68 }