diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/Validation/Constraint/CommentNameConstraint.php b/core/modules/comment/lib/Drupal/comment/Plugin/Validation/Constraint/CommentNameConstraint.php index 51a3a64..575a393 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/Validation/Constraint/CommentNameConstraint.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/Validation/Constraint/CommentNameConstraint.php @@ -20,4 +20,5 @@ class CommentNameConstraint extends Constraint { public $message = '%name belongs to a registered user.'; + } diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/Validation/Constraint/CommentNameConstraintValidator.php b/core/modules/comment/lib/Drupal/comment/Plugin/Validation/Constraint/CommentNameConstraintValidator.php index 94d59b5..f1e6c1c 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/Validation/Constraint/CommentNameConstraintValidator.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/Validation/Constraint/CommentNameConstraintValidator.php @@ -20,7 +20,7 @@ class CommentNameConstraintValidator extends ConstraintValidator { */ public function validate($field_item, Constraint $constraint) { $author_name = $field_item->value; - if (isset($author_name) && ($author_name !== '')) { + if (isset($author_name) && $author_name !== '') { // Do not allow unauthenticated comment authors to use a name that is // taken by a registered user. if ($field_item->getEntity()->getOwnerId() === 0) { @@ -32,4 +32,5 @@ public function validate($field_item, Constraint $constraint) { } } } + } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php index 220c4f8..33b9a84 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php @@ -7,6 +7,7 @@ namespace Drupal\comment\Tests; +use Drupal\comment\CommentInterface; use Drupal\system\Tests\Entity\EntityUnitTestBase; /** @@ -69,16 +70,18 @@ public function testValidation() { 'entity_type' => 'node', 'field_name' => 'comment', )); + $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); + // 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. $user = entity_create('user', array('name' => 'test')); @@ -91,14 +94,13 @@ public function testValidation() { // Make the name valid. $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('mail', NULL); $comment->set('homepage', 'http://example.com/' . $this->randomName(237)); $this->assertLengthViolation($comment, 'homepage', 255); @@ -106,14 +108,15 @@ public function testValidation() { $violations = $comment->validate(); $this->assertEqual(count($violations), 1, 'Violation found when homepage is invalid'); $this->assertEqual($violations[0]->getPropertyPath(), 'homepage.0.value'); + // @todo This message should be improved in https://drupal.org/node/2012690 $this->assertEqual($violations[0]->getMessage(), t('This value should be of the correct primitive type.')); - $comment->set('homepage', NULL); + $comment->set('homepage', NULL); $comment->set('hostname', $this->randomString(129)); $this->assertLengthViolation($comment, 'hostname', 128); - $comment->set('hostname', NULL); + $comment->set('hostname', NULL); $comment->set('thread', $this->randomString(256)); $this->assertLengthViolation($comment, 'thread', 255); } @@ -121,14 +124,14 @@ public function testValidation() { /** * Verifies that a length violation exists for the given field. * - * @param \Drupal\Core\Entity\ContentEntityInterface\CommentInterface $comment + * @param \Drupal\comment\CommentInterface $comment * The comment object to validate. * @param string $field_name * The field that violates the maximum length. * @param int $length * Number of characters that was exceeded. */ - protected function assertLengthViolation($comment, $field_name, $length) { + 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");