annotate core/modules/node/tests/src/Kernel/NodeValidationTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\node\Kernel;
Chris@0 4
Chris@0 5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
Chris@0 6 use Drupal\node\Entity\Node;
Chris@0 7 use Drupal\node\Entity\NodeType;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Tests node validation constraints.
Chris@0 11 *
Chris@0 12 * @group node
Chris@0 13 */
Chris@0 14 class NodeValidationTest extends EntityKernelTestBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Modules to enable.
Chris@0 18 *
Chris@0 19 * @var array
Chris@0 20 */
Chris@0 21 public static $modules = ['node'];
Chris@0 22
Chris@0 23 /**
Chris@0 24 * Set the default field storage backend for fields created during tests.
Chris@0 25 */
Chris@0 26 protected function setUp() {
Chris@0 27 parent::setUp();
Chris@0 28
Chris@0 29 // Create a node type for testing.
Chris@0 30 $type = NodeType::create(['type' => 'page', 'name' => 'page']);
Chris@0 31 $type->save();
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Tests the node validation constraints.
Chris@0 36 */
Chris@0 37 public function testValidation() {
Chris@0 38 $this->createUser();
Chris@0 39 $node = Node::create(['type' => 'page', 'title' => 'test', 'uid' => 1]);
Chris@0 40 $violations = $node->validate();
Chris@0 41 $this->assertEqual(count($violations), 0, 'No violations when validating a default node.');
Chris@0 42
Chris@0 43 $node->set('title', $this->randomString(256));
Chris@0 44 $violations = $node->validate();
Chris@0 45 $this->assertEqual(count($violations), 1, 'Violation found when title is too long.');
Chris@0 46 $this->assertEqual($violations[0]->getPropertyPath(), 'title.0.value');
Chris@0 47 $this->assertEqual($violations[0]->getMessage(), '<em class="placeholder">Title</em>: may not be longer than 255 characters.');
Chris@0 48
Chris@0 49 $node->set('title', NULL);
Chris@0 50 $violations = $node->validate();
Chris@0 51 $this->assertEqual(count($violations), 1, 'Violation found when title is not set.');
Chris@0 52 $this->assertEqual($violations[0]->getPropertyPath(), 'title');
Chris@0 53 $this->assertEqual($violations[0]->getMessage(), 'This value should not be null.');
Chris@0 54
Chris@0 55 $node->set('title', '');
Chris@0 56 $violations = $node->validate();
Chris@0 57 $this->assertEqual(count($violations), 1, 'Violation found when title is set to an empty string.');
Chris@0 58 $this->assertEqual($violations[0]->getPropertyPath(), 'title');
Chris@0 59
Chris@0 60 // Make the title valid again.
Chris@0 61 $node->set('title', $this->randomString());
Chris@0 62 // Save the node so that it gets an ID and a changed date.
Chris@0 63 $node->save();
Chris@0 64 // Set the changed date to something in the far past.
Chris@0 65 $node->set('changed', 433918800);
Chris@0 66 $violations = $node->validate();
Chris@0 67 $this->assertEqual(count($violations), 1, 'Violation found when changed date is before the last changed date.');
Chris@0 68 $this->assertEqual($violations[0]->getPropertyPath(), '');
Chris@0 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.');
Chris@0 70 }
Chris@0 71
Chris@0 72 }