View source
<?php
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\CommentInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
class CommentValidationTest extends EntityKernelTestBase {
protected static $modules = [
'comment',
'node',
];
protected function setUp() : void {
parent::setUp();
$this
->installSchema('comment', [
'comment_entity_statistics',
]);
}
public function testValidation() {
$user = User::create([
'name' => 'test',
'status' => TRUE,
]);
$user
->save();
$this->entityTypeManager
->getStorage('comment_type')
->create([
'id' => 'comment',
'label' => 'comment',
'target_entity_type_id' => 'node',
])
->save();
$this->entityTypeManager
->getStorage('field_storage_config')
->create([
'entity_type' => 'node',
'field_name' => 'comment',
'type' => 'comment',
'settings' => [
'comment_type' => 'comment',
],
])
->save();
$this->entityTypeManager
->getStorage('node_type')
->create([
'type' => 'page',
'name' => 'page',
])
->save();
$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);
$comment
->set('subject', $this
->randomString());
$comment
->set('name', $this
->randomString(61));
$this
->assertLengthViolation($comment, 'name', 60);
$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(t('The name you used (%name) belongs to a registered user.', [
'%name' => 'test',
]), $violations[0]
->getMessage());
$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());
$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);
$field
->setSetting('anonymous', CommentInterface::ANONYMOUS_MUST_CONTACT);
$field
->save();
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache([
$node
->id(),
]);
$node = Node::load($node
->id());
$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());
$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.');
$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());
}
protected function assertLengthViolation(CommentInterface $comment, string $field_name, int $length) : void {
$violations = $comment
->validate();
$this
->assertCount(1, $violations, "Violation found when {$field_name} is too long.");
$this
->assertEquals("{$field_name}.0.value", $violations[0]
->getPropertyPath());
$field_label = $comment
->get($field_name)
->getFieldDefinition()
->getLabel();
$this
->assertEquals(t('%name: may not be longer than @max characters.', [
'%name' => $field_label,
'@max' => $length,
]), $violations[0]
->getMessage());
}
}