1<?php 2 3namespace Drupal\Tests\node\Kernel; 4 5use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; 6use Drupal\node\Entity\Node; 7use Drupal\node\Entity\NodeType; 8 9/** 10 * Tests node validation constraints. 11 * 12 * @group node 13 */ 14class NodeValidationTest extends EntityKernelTestBase { 15 16 /** 17 * Modules to enable. 18 * 19 * @var array 20 */ 21 public static $modules = ['node']; 22 23 /** 24 * Set the default field storage backend for fields created during tests. 25 */ 26 protected function setUp() { 27 parent::setUp(); 28 29 // Create a node type for testing. 30 $type = NodeType::create(['type' => 'page', 'name' => 'page']); 31 $type->save(); 32 } 33 34 /** 35 * Tests the node validation constraints. 36 */ 37 public function testValidation() { 38 $this->createUser(); 39 $node = Node::create(['type' => 'page', 'title' => 'test', 'uid' => 1]); 40 $violations = $node->validate(); 41 $this->assertCount(0, $violations, 'No violations when validating a default node.'); 42 43 $node->set('title', $this->randomString(256)); 44 $violations = $node->validate(); 45 $this->assertCount(1, $violations, '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.'); 48 49 $node->set('title', NULL); 50 $violations = $node->validate(); 51 $this->assertCount(1, $violations, '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.'); 54 55 $node->set('title', ''); 56 $violations = $node->validate(); 57 $this->assertCount(1, $violations, 'Violation found when title is set to an empty string.'); 58 $this->assertEqual($violations[0]->getPropertyPath(), 'title'); 59 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. 63 $node->save(); 64 // Set the changed date to something in the far past. 65 $node->set('changed', 433918800); 66 $violations = $node->validate(); 67 $this->assertCount(1, $violations, '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.'); 70 } 71 72} 73