function CommentValidationTest::testValidation
Same name in other branches
- 9 core/modules/comment/tests/src/Kernel/CommentValidationTest.php \Drupal\Tests\comment\Kernel\CommentValidationTest::testValidation()
- 8.9.x core/modules/comment/tests/src/Kernel/CommentValidationTest.php \Drupal\Tests\comment\Kernel\CommentValidationTest::testValidation()
- 11.x core/modules/comment/tests/src/Kernel/CommentValidationTest.php \Drupal\Tests\comment\Kernel\CommentValidationTest::testValidation()
Tests the comment validation constraints.
File
-
core/
modules/ comment/ tests/ src/ Kernel/ CommentValidationTest.php, line 42
Class
- CommentValidationTest
- Tests comment validation constraints.
Namespace
Drupal\Tests\comment\KernelCode
public function testValidation() : void {
// Add a user.
$user = User::create([
'name' => 'test',
'status' => TRUE,
]);
$user->save();
// Add comment type.
$this->entityTypeManager
->getStorage('comment_type')
->create([
'id' => 'comment',
'label' => 'comment',
'target_entity_type_id' => 'node',
])
->save();
// Add comment field to content.
$this->entityTypeManager
->getStorage('field_storage_config')
->create([
'entity_type' => 'node',
'field_name' => 'comment',
'type' => 'comment',
'settings' => [
'comment_type' => 'comment',
],
])
->save();
// Create a page node type.
$this->entityTypeManager
->getStorage('node_type')
->create([
'type' => 'page',
'name' => 'page',
])
->save();
// Add comment field to page content.
/** @var \Drupal\field\FieldConfigInterface $field */
$field = $this->entityTypeManager
->getStorage('field_config')
->create([
'field_name' => 'comment',
'entity_type' => 'node',
'bundle' => 'page',
'label' => 'Comment settings',
]);
$field->save();
$node = $this->entityTypeManager
->getStorage('node')
->create([
'type' => 'page',
'title' => 'test',
]);
$node->save();
$comment = $this->entityTypeManager
->getStorage('comment')
->create([
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this->randomMachineName(),
]);
$violations = $comment->validate();
$this->assertCount(0, $violations, 'No violations when validating a default comment.');
$comment->set('subject', $this->randomString(65));
$this->assertLengthViolation($comment, 'subject', 64);
// Make the subject valid.
$comment->set('subject', $this->randomString());
$comment->set('name', $this->randomString(61));
$this->assertLengthViolation($comment, 'name', 60);
// Validate a name collision between an anonymous comment author name and an
// existing user account name.
$comment->set('name', 'test');
$comment->set('uid', 0);
$violations = $comment->validate();
$this->assertCount(1, $violations, "Violation found on author name collision");
$this->assertEquals("name", $violations[0]->getPropertyPath());
$this->assertEquals('The name you used (test) belongs to a registered user.', $violations[0]->getMessage());
// Make the name valid.
$comment->set('name', 'valid unused name');
$comment->set('mail', 'invalid');
$violations = $comment->validate();
$this->assertCount(1, $violations, 'Violation found when email is invalid');
$this->assertEquals('mail.0.value', $violations[0]->getPropertyPath());
$this->assertEquals('This value is not a valid email address.', $violations[0]->getMessage());
$comment->set('mail', NULL);
$comment->set('homepage', 'http://example.com/' . $this->randomMachineName(237));
$this->assertLengthViolation($comment, 'homepage', 255);
$comment->set('homepage', 'invalid');
$violations = $comment->validate();
$this->assertCount(1, $violations, 'Violation found when homepage is invalid');
$this->assertEquals('homepage.0.value', $violations[0]->getPropertyPath());
// @todo This message should be improved in
// https://www.drupal.org/node/2012690.
$this->assertEquals('This value should be of the correct primitive type.', $violations[0]->getMessage());
$comment->set('homepage', NULL);
$comment->set('hostname', $this->randomString(129));
$this->assertLengthViolation($comment, 'hostname', 128);
$comment->set('hostname', NULL);
$comment->set('thread', $this->randomString(256));
$this->assertLengthViolation($comment, 'thread', 255);
$comment->set('thread', NULL);
// Force anonymous users to enter contact details.
$field->setSetting('anonymous', CommentInterface::ANONYMOUS_MUST_CONTACT);
$field->save();
// Reset the node entity.
\Drupal::entityTypeManager()->getStorage('node')
->resetCache([
$node->id(),
]);
$node = Node::load($node->id());
// Create a new comment with the new field.
$comment = $this->entityTypeManager
->getStorage('comment')
->create([
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this->randomMachineName(),
'uid' => 0,
'name' => '',
]);
$violations = $comment->validate();
$this->assertCount(1, $violations, 'Violation found when name is required, but empty and UID is anonymous.');
$this->assertEquals('name', $violations[0]->getPropertyPath());
$this->assertEquals('You have to specify a valid author.', $violations[0]->getMessage());
// Test creating a default comment with a given user id works.
$comment = $this->entityTypeManager
->getStorage('comment')
->create([
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this->randomMachineName(),
'uid' => $user->id(),
]);
$violations = $comment->validate();
$this->assertCount(0, $violations, 'No violations when validating a default comment with an author.');
// Test specifying a wrong author name does not work.
$comment = $this->entityTypeManager
->getStorage('comment')
->create([
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this->randomMachineName(),
'uid' => $user->id(),
'name' => 'not-test',
]);
$violations = $comment->validate();
$this->assertCount(1, $violations, 'Violation found when author name and comment author do not match.');
$this->assertEquals('name', $violations[0]->getPropertyPath());
$this->assertEquals('The specified author name does not match the comment author.', $violations[0]->getMessage());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.