Same name and namespace in other branches
- 8.9.x core/modules/node/tests/src/Kernel/NodeValidationTest.php \Drupal\Tests\node\Kernel\NodeValidationTest
- 9 core/modules/node/tests/src/Kernel/NodeValidationTest.php \Drupal\Tests\node\Kernel\NodeValidationTest
Tests node validation constraints.
@group node
Hierarchy
- class \Drupal\KernelTests\Core\Entity\EntityKernelTestBase extends \Drupal\KernelTests\KernelTestBase uses UserCreationTrait
- class \Drupal\Tests\node\Kernel\NodeValidationTest
Expanded class hierarchy of NodeValidationTest
File
- core/
modules/ node/ tests/ src/ Kernel/ NodeValidationTest.php, line 16
Namespace
Drupal\Tests\node\KernelView source
class NodeValidationTest extends EntityKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'node',
];
/**
* Set the default field storage backend for fields created during tests.
*/
protected function setUp() : void {
parent::setUp();
// Create a node type for testing.
$type = NodeType::create([
'type' => 'page',
'name' => 'page',
]);
$type
->save();
}
/**
* Tests the node validation constraints.
*/
public function testValidation() {
$this
->createUser();
$node = Node::create([
'type' => 'page',
'title' => 'test',
'uid' => 1,
]);
$violations = $node
->validate();
$this
->assertCount(0, $violations, 'No violations when validating a default node.');
$node
->set('title', $this
->randomString(256));
$violations = $node
->validate();
$this
->assertCount(1, $violations, 'Violation found when title is too long.');
$this
->assertEquals('title.0.value', $violations[0]
->getPropertyPath());
$this
->assertEquals('Title: may not be longer than 255 characters.', $violations[0]
->getMessage());
$node
->set('title', NULL);
$violations = $node
->validate();
$this
->assertCount(1, $violations, 'Violation found when title is not set.');
$this
->assertEquals('title', $violations[0]
->getPropertyPath());
$this
->assertEquals('This value should not be null.', $violations[0]
->getMessage());
$node
->set('title', '');
$violations = $node
->validate();
$this
->assertCount(1, $violations, 'Violation found when title is set to an empty string.');
$this
->assertEquals('title', $violations[0]
->getPropertyPath());
// Make the title valid again.
$node
->set('title', $this
->randomString());
// Save the node so that it gets an ID and a changed date.
$node
->save();
// Set the changed date to something in the far past.
$node
->set('changed', 433918800);
$violations = $node
->validate();
$this
->assertCount(1, $violations, 'Violation found when changed date is before the last changed date.');
$this
->assertEquals('', $violations[0]
->getPropertyPath());
$this
->assertEquals('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.', $violations[0]
->getMessage());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityKernelTestBase:: |
protected | property | The entity type manager service. | 1 |
EntityKernelTestBase:: |
protected | property | A list of generated identifiers. | |
EntityKernelTestBase:: |
protected | property | The state service. | |
EntityKernelTestBase:: |
protected | function | Creates a user. | |
EntityKernelTestBase:: |
protected | function | Generates a random ID avoiding collisions. | |
EntityKernelTestBase:: |
protected | function | Returns the entity_test hook invocation info. | |
EntityKernelTestBase:: |
protected | function | Installs a module and refreshes services. | |
EntityKernelTestBase:: |
protected | function | Refresh services. | |
EntityKernelTestBase:: |
protected | function | Reloads the given entity from the storage and returns it. | |
EntityKernelTestBase:: |
protected | function | Uninstalls a module and refreshes services. | |
NodeValidationTest:: |
protected static | property |
Modules to enable. Overrides EntityKernelTestBase:: |
|
NodeValidationTest:: |
protected | function |
Set the default field storage backend for fields created during tests. Overrides EntityKernelTestBase:: |
|
NodeValidationTest:: |
public | function | Tests the node validation constraints. | |
UserCreationTrait:: |
protected | function | Checks whether a given list of permission names is valid. Aliased as: drupalCheckPermissions | |
UserCreationTrait:: |
protected | function | Creates an administrative role. Aliased as: drupalCreateAdminRole | |
UserCreationTrait:: |
protected | function | Creates a role with specified permissions. Aliased as: drupalCreateRole | |
UserCreationTrait:: |
protected | function | Create a user with a given set of permissions. Aliased as: drupalCreateUser | |
UserCreationTrait:: |
protected | function | Grant permissions to a user role. Aliased as: drupalGrantPermissions | |
UserCreationTrait:: |
protected | function | Switch the current logged in user. Aliased as: drupalSetCurrentUser | |
UserCreationTrait:: |
protected | function | Creates a random user account and sets it as current user. Aliased as: drupalSetUpCurrentUser |