class TermValidationTest
Same name and namespace in other branches
- 11.x core/modules/taxonomy/tests/src/Kernel/TermValidationTest.php \Drupal\Tests\taxonomy\Kernel\TermValidationTest
- 10 core/modules/taxonomy/tests/src/Kernel/TermValidationTest.php \Drupal\Tests\taxonomy\Kernel\TermValidationTest
- 8.9.x core/modules/taxonomy/tests/src/Kernel/TermValidationTest.php \Drupal\Tests\taxonomy\Kernel\TermValidationTest
Tests term validation constraints.
@group taxonomy
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\Core\Entity\EntityKernelTestBase uses \Drupal\Tests\user\Traits\UserCreationTrait extends \Drupal\KernelTests\KernelTestBase
- class \Drupal\Tests\taxonomy\Kernel\TermValidationTest extends \Drupal\KernelTests\Core\Entity\EntityKernelTestBase
- class \Drupal\KernelTests\Core\Entity\EntityKernelTestBase uses \Drupal\Tests\user\Traits\UserCreationTrait extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of TermValidationTest
File
-
core/
modules/ taxonomy/ tests/ src/ Kernel/ TermValidationTest.php, line 13
Namespace
Drupal\Tests\taxonomy\KernelView source
class TermValidationTest extends EntityKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'taxonomy',
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->installEntitySchema('taxonomy_term');
}
/**
* Tests the term validation constraints.
*/
public function testValidation() {
$this->entityTypeManager
->getStorage('taxonomy_vocabulary')
->create([
'vid' => 'tags',
'name' => 'Tags',
])
->save();
$term = $this->entityTypeManager
->getStorage('taxonomy_term')
->create([
'name' => 'test',
'vid' => 'tags',
]);
$violations = $term->validate();
$this->assertCount(0, $violations, 'No violations when validating a default term.');
$term->set('name', $this->randomString(256));
$violations = $term->validate();
$this->assertCount(1, $violations, 'Violation found when name is too long.');
$this->assertEquals('name.0.value', $violations[0]->getPropertyPath());
$field_label = $term->get('name')
->getFieldDefinition()
->getLabel();
$this->assertEquals(t('%name: may not be longer than @max characters.', [
'%name' => $field_label,
'@max' => 255,
]), $violations[0]->getMessage());
$term->set('name', NULL);
$violations = $term->validate();
$this->assertCount(1, $violations, 'Violation found when name is NULL.');
$this->assertEquals('name', $violations[0]->getPropertyPath());
$this->assertEquals('This value should not be null.', $violations[0]->getMessage());
$term->set('name', 'test');
$term->set('parent', 9999);
$violations = $term->validate();
$this->assertCount(1, $violations, 'Violation found when term parent is invalid.');
$this->assertEquals(new FormattableMarkup('The referenced entity (%type: %id) does not exist.', [
'%type' => 'taxonomy_term',
'%id' => 9999,
]), $violations[0]->getMessage());
$term->set('parent', 0);
$violations = $term->validate();
$this->assertCount(0, $violations, 'No violations for parent id 0.');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.