View source
<?php
namespace Drupal\comment\Tests;
use Drupal\comment\CommentInterface;
use Drupal\node\Entity\Node;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
use Drupal\user\Entity\User;
class CommentValidationTest extends EntityUnitTestBase {
public static $modules = array(
'comment',
'node',
);
protected function setUp() {
parent::setUp();
$this
->installSchema('comment', array(
'comment_entity_statistics',
));
}
public function testValidation() {
$user = User::create(array(
'name' => 'test',
'status' => TRUE,
));
$user
->save();
$this->entityManager
->getStorage('comment_type')
->create(array(
'id' => 'comment',
'label' => 'comment',
'target_entity_type_id' => 'node',
))
->save();
$this->entityManager
->getStorage('field_storage_config')
->create(array(
'entity_type' => 'node',
'field_name' => 'comment',
'type' => 'comment',
'settings' => array(
'comment_type' => 'comment',
),
))
->save();
$this->entityManager
->getStorage('node_type')
->create(array(
'type' => 'page',
'name' => 'page',
))
->save();
$field = $this->entityManager
->getStorage('field_config')
->create(array(
'field_name' => 'comment',
'entity_type' => 'node',
'bundle' => 'page',
'label' => 'Comment settings',
));
$field
->save();
$node = $this->entityManager
->getStorage('node')
->create(array(
'type' => 'page',
'title' => 'test',
));
$node
->save();
$comment = $this->entityManager
->getStorage('comment')
->create(array(
'entity_id' => $node
->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this
->randomMachineName(),
));
$violations = $comment
->validate();
$this
->assertEqual(count($violations), 0, '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
->assertEqual(count($violations), 1, "Violation found on author name collision");
$this
->assertEqual($violations[0]
->getPropertyPath(), "name");
$this
->assertEqual($violations[0]
->getMessage(), t('The name you used (%name) belongs to a registered user.', array(
'%name' => 'test',
)));
$comment
->set('name', 'valid unused name');
$comment
->set('mail', 'invalid');
$violations = $comment
->validate();
$this
->assertEqual(count($violations), 1, 'Violation found when email is invalid');
$this
->assertEqual($violations[0]
->getPropertyPath(), 'mail.0.value');
$this
->assertEqual($violations[0]
->getMessage(), t('This value is not a valid email address.'));
$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
->assertEqual(count($violations), 1, 'Violation found when homepage is invalid');
$this
->assertEqual($violations[0]
->getPropertyPath(), 'homepage.0.value');
$this
->assertEqual($violations[0]
->getMessage(), t('This value should be of the correct primitive type.'));
$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', COMMENT_ANONYMOUS_MUST_CONTACT);
$field
->save();
\Drupal::entityManager()
->getStorage('node')
->resetCache([
$node
->id(),
]);
$node = Node::load($node
->id());
$comment = $this->entityManager
->getStorage('comment')
->create(array(
'entity_id' => $node
->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this
->randomMachineName(),
'uid' => 0,
'name' => '',
));
$violations = $comment
->validate();
$this
->assertEqual(count($violations), 1, 'Violation found when name is required, but empty and UID is anonymous.');
$this
->assertEqual($violations[0]
->getPropertyPath(), 'name');
$this
->assertEqual($violations[0]
->getMessage(), t('You have to specify a valid author.'));
$comment = $this->entityManager
->getStorage('comment')
->create(array(
'entity_id' => $node
->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this
->randomMachineName(),
'uid' => $user
->id(),
));
$violations = $comment
->validate();
$this
->assertEqual(count($violations), 0, 'No violations when validating a default comment with an author.');
$comment = $this->entityManager
->getStorage('comment')
->create(array(
'entity_id' => $node
->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => $this
->randomMachineName(),
'uid' => $user
->id(),
'name' => 'not-test',
));
$violations = $comment
->validate();
$this
->assertEqual(count($violations), 1, 'Violation found when author name and comment author do not match.');
$this
->assertEqual($violations[0]
->getPropertyPath(), 'name');
$this
->assertEqual($violations[0]
->getMessage(), t('The specified author name does not match the comment author.'));
}
protected function assertLengthViolation(CommentInterface $comment, $field_name, $length) {
$violations = $comment
->validate();
$this
->assertEqual(count($violations), 1, "Violation found when {$field_name} is too long.");
$this
->assertEqual($violations[0]
->getPropertyPath(), "{$field_name}.0.value");
$field_label = $comment
->get($field_name)
->getFieldDefinition()
->getLabel();
$this
->assertEqual($violations[0]
->getMessage(), t('%name: may not be longer than @max characters.', array(
'%name' => $field_label,
'@max' => $length,
)));
}
}