3 namespace Drupal\Tests\node\Kernel;
5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\Entity\NodeType;
10 * Tests node validation constraints.
14 class NodeValidationTest extends EntityKernelTestBase {
21 public static $modules = ['node'];
24 * Set the default field storage backend for fields created during tests.
26 protected function setUp() {
29 // Create a node type for testing.
30 $type = NodeType::create(['type' => 'page', 'name' => 'page']);
35 * Tests the node validation constraints.
37 public function testValidation() {
39 $node = Node::create(['type' => 'page', 'title' => 'test', 'uid' => 1]);
40 $violations = $node->validate();
41 $this->assertEqual(count($violations), 0, 'No violations when validating a default node.');
43 $node->set('title', $this->randomString(256));
44 $violations = $node->validate();
45 $this->assertEqual(count($violations), 1, 'Violation found when title is too long.');
46 $this->assertEqual($violations[0]->getPropertyPath(), 'title.0.value');
47 $this->assertEqual($violations[0]->getMessage(), '<em class="placeholder">Title</em>: may not be longer than 255 characters.');
49 $node->set('title', NULL);
50 $violations = $node->validate();
51 $this->assertEqual(count($violations), 1, 'Violation found when title is not set.');
52 $this->assertEqual($violations[0]->getPropertyPath(), 'title');
53 $this->assertEqual($violations[0]->getMessage(), 'This value should not be null.');
55 $node->set('title', '');
56 $violations = $node->validate();
57 $this->assertEqual(count($violations), 1, 'Violation found when title is set to an empty string.');
58 $this->assertEqual($violations[0]->getPropertyPath(), 'title');
60 // Make the title valid again.
61 $node->set('title', $this->randomString());
62 // Save the node so that it gets an ID and a changed date.
64 // Set the changed date to something in the far past.
65 $node->set('changed', 433918800);
66 $violations = $node->validate();
67 $this->assertEqual(count($violations), 1, 'Violation found when changed date is before the last changed date.');
68 $this->assertEqual($violations[0]->getPropertyPath(), '');
69 $this->assertEqual($violations[0]->getMessage(), 'The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.');