Chris@0: installSchema('comment', ['comment_entity_statistics']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the comment validation constraints. Chris@0: */ Chris@0: public function testValidation() { Chris@0: // Add a user. Chris@0: $user = User::create(['name' => 'test', 'status' => TRUE]); Chris@0: $user->save(); Chris@0: Chris@0: // Add comment type. Chris@0: $this->entityManager->getStorage('comment_type')->create([ Chris@0: 'id' => 'comment', Chris@0: 'label' => 'comment', Chris@0: 'target_entity_type_id' => 'node', Chris@0: ])->save(); Chris@0: Chris@0: // Add comment field to content. Chris@0: $this->entityManager->getStorage('field_storage_config')->create([ Chris@0: 'entity_type' => 'node', Chris@0: 'field_name' => 'comment', Chris@0: 'type' => 'comment', Chris@0: 'settings' => [ Chris@0: 'comment_type' => 'comment', Chris@17: ], Chris@0: ])->save(); Chris@0: Chris@0: // Create a page node type. Chris@0: $this->entityManager->getStorage('node_type')->create([ Chris@0: 'type' => 'page', Chris@0: 'name' => 'page', Chris@0: ])->save(); Chris@0: Chris@0: // Add comment field to page content. Chris@0: /** @var \Drupal\field\FieldConfigInterface $field */ Chris@0: $field = $this->entityManager->getStorage('field_config')->create([ Chris@0: 'field_name' => 'comment', Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => 'page', Chris@0: 'label' => 'Comment settings', Chris@0: ]); Chris@0: $field->save(); Chris@0: Chris@0: $node = $this->entityManager->getStorage('node')->create([ Chris@0: 'type' => 'page', Chris@0: 'title' => 'test', Chris@0: ]); Chris@0: $node->save(); Chris@0: Chris@0: $comment = $this->entityManager->getStorage('comment')->create([ Chris@0: 'entity_id' => $node->id(), Chris@0: 'entity_type' => 'node', Chris@0: 'field_name' => 'comment', Chris@0: 'comment_body' => $this->randomMachineName(), Chris@0: ]); Chris@0: Chris@0: $violations = $comment->validate(); Chris@0: $this->assertEqual(count($violations), 0, 'No violations when validating a default comment.'); Chris@0: Chris@0: $comment->set('subject', $this->randomString(65)); Chris@0: $this->assertLengthViolation($comment, 'subject', 64); Chris@0: Chris@0: // Make the subject valid. Chris@0: $comment->set('subject', $this->randomString()); Chris@0: $comment->set('name', $this->randomString(61)); Chris@0: $this->assertLengthViolation($comment, 'name', 60); Chris@0: Chris@0: // Validate a name collision between an anonymous comment author name and an Chris@0: // existing user account name. Chris@0: $comment->set('name', 'test'); Chris@0: $comment->set('uid', 0); Chris@0: $violations = $comment->validate(); Chris@0: $this->assertEqual(count($violations), 1, "Violation found on author name collision"); Chris@0: $this->assertEqual($violations[0]->getPropertyPath(), "name"); Chris@0: $this->assertEqual($violations[0]->getMessage(), t('The name you used (%name) belongs to a registered user.', ['%name' => 'test'])); Chris@0: Chris@0: // Make the name valid. Chris@0: $comment->set('name', 'valid unused name'); Chris@0: $comment->set('mail', 'invalid'); Chris@0: $violations = $comment->validate(); Chris@0: $this->assertEqual(count($violations), 1, 'Violation found when email is invalid'); Chris@0: $this->assertEqual($violations[0]->getPropertyPath(), 'mail.0.value'); Chris@0: $this->assertEqual($violations[0]->getMessage(), t('This value is not a valid email address.')); Chris@0: Chris@0: $comment->set('mail', NULL); Chris@0: $comment->set('homepage', 'http://example.com/' . $this->randomMachineName(237)); Chris@0: $this->assertLengthViolation($comment, 'homepage', 255); Chris@0: Chris@0: $comment->set('homepage', 'invalid'); Chris@0: $violations = $comment->validate(); Chris@0: $this->assertEqual(count($violations), 1, 'Violation found when homepage is invalid'); Chris@0: $this->assertEqual($violations[0]->getPropertyPath(), 'homepage.0.value'); Chris@0: Chris@0: // @todo This message should be improved in Chris@0: // https://www.drupal.org/node/2012690. Chris@0: $this->assertEqual($violations[0]->getMessage(), t('This value should be of the correct primitive type.')); Chris@0: Chris@0: $comment->set('homepage', NULL); Chris@0: $comment->set('hostname', $this->randomString(129)); Chris@0: $this->assertLengthViolation($comment, 'hostname', 128); Chris@0: Chris@0: $comment->set('hostname', NULL); Chris@0: $comment->set('thread', $this->randomString(256)); Chris@0: $this->assertLengthViolation($comment, 'thread', 255); Chris@0: Chris@0: $comment->set('thread', NULL); Chris@0: Chris@0: // Force anonymous users to enter contact details. Chris@18: $field->setSetting('anonymous', CommentInterface::ANONYMOUS_MUST_CONTACT); Chris@0: $field->save(); Chris@0: // Reset the node entity. Chris@0: \Drupal::entityManager()->getStorage('node')->resetCache([$node->id()]); Chris@0: $node = Node::load($node->id()); Chris@0: // Create a new comment with the new field. Chris@0: $comment = $this->entityManager->getStorage('comment')->create([ Chris@0: 'entity_id' => $node->id(), Chris@0: 'entity_type' => 'node', Chris@0: 'field_name' => 'comment', Chris@0: 'comment_body' => $this->randomMachineName(), Chris@0: 'uid' => 0, Chris@0: 'name' => '', Chris@0: ]); Chris@0: $violations = $comment->validate(); Chris@0: $this->assertEqual(count($violations), 1, 'Violation found when name is required, but empty and UID is anonymous.'); Chris@0: $this->assertEqual($violations[0]->getPropertyPath(), 'name'); Chris@0: $this->assertEqual($violations[0]->getMessage(), t('You have to specify a valid author.')); Chris@0: Chris@0: // Test creating a default comment with a given user id works. Chris@0: $comment = $this->entityManager->getStorage('comment')->create([ Chris@0: 'entity_id' => $node->id(), Chris@0: 'entity_type' => 'node', Chris@0: 'field_name' => 'comment', Chris@0: 'comment_body' => $this->randomMachineName(), Chris@0: 'uid' => $user->id(), Chris@0: ]); Chris@0: $violations = $comment->validate(); Chris@0: $this->assertEqual(count($violations), 0, 'No violations when validating a default comment with an author.'); Chris@0: Chris@0: // Test specifying a wrong author name does not work. Chris@0: $comment = $this->entityManager->getStorage('comment')->create([ Chris@0: 'entity_id' => $node->id(), Chris@0: 'entity_type' => 'node', Chris@0: 'field_name' => 'comment', Chris@0: 'comment_body' => $this->randomMachineName(), Chris@0: 'uid' => $user->id(), Chris@0: 'name' => 'not-test', Chris@0: ]); Chris@0: $violations = $comment->validate(); Chris@0: $this->assertEqual(count($violations), 1, 'Violation found when author name and comment author do not match.'); Chris@0: $this->assertEqual($violations[0]->getPropertyPath(), 'name'); Chris@0: $this->assertEqual($violations[0]->getMessage(), t('The specified author name does not match the comment author.')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies that a length violation exists for the given field. Chris@0: * Chris@0: * @param \Drupal\comment\CommentInterface $comment Chris@0: * The comment object to validate. Chris@0: * @param string $field_name Chris@0: * The field that violates the maximum length. Chris@0: * @param int $length Chris@0: * Number of characters that was exceeded. Chris@0: */ Chris@0: protected function assertLengthViolation(CommentInterface $comment, $field_name, $length) { Chris@0: $violations = $comment->validate(); Chris@0: $this->assertEqual(count($violations), 1, "Violation found when $field_name is too long."); Chris@0: $this->assertEqual($violations[0]->getPropertyPath(), "$field_name.0.value"); Chris@0: $field_label = $comment->get($field_name)->getFieldDefinition()->getLabel(); Chris@0: $this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', ['%name' => $field_label, '@max' => $length])); Chris@0: } Chris@0: Chris@0: }