3 namespace Drupal\Tests\comment\Kernel;
5 use Drupal\comment\CommentInterface;
6 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
7 use Drupal\node\Entity\Node;
8 use Drupal\user\Entity\User;
11 * Tests comment validation constraints.
15 class CommentValidationTest extends EntityKernelTestBase {
22 public static $modules = ['comment', 'node'];
27 protected function setUp() {
29 $this->installSchema('comment', ['comment_entity_statistics']);
33 * Tests the comment validation constraints.
35 public function testValidation() {
37 $user = User::create(['name' => 'test', 'status' => TRUE]);
41 $this->entityManager->getStorage('comment_type')->create([
44 'target_entity_type_id' => 'node',
47 // Add comment field to content.
48 $this->entityManager->getStorage('field_storage_config')->create([
49 'entity_type' => 'node',
50 'field_name' => 'comment',
53 'comment_type' => 'comment',
57 // Create a page node type.
58 $this->entityManager->getStorage('node_type')->create([
63 // Add comment field to page content.
64 /** @var \Drupal\field\FieldConfigInterface $field */
65 $field = $this->entityManager->getStorage('field_config')->create([
66 'field_name' => 'comment',
67 'entity_type' => 'node',
69 'label' => 'Comment settings',
73 $node = $this->entityManager->getStorage('node')->create([
79 $comment = $this->entityManager->getStorage('comment')->create([
80 'entity_id' => $node->id(),
81 'entity_type' => 'node',
82 'field_name' => 'comment',
83 'comment_body' => $this->randomMachineName(),
86 $violations = $comment->validate();
87 $this->assertEqual(count($violations), 0, 'No violations when validating a default comment.');
89 $comment->set('subject', $this->randomString(65));
90 $this->assertLengthViolation($comment, 'subject', 64);
92 // Make the subject valid.
93 $comment->set('subject', $this->randomString());
94 $comment->set('name', $this->randomString(61));
95 $this->assertLengthViolation($comment, 'name', 60);
97 // Validate a name collision between an anonymous comment author name and an
98 // existing user account name.
99 $comment->set('name', 'test');
100 $comment->set('uid', 0);
101 $violations = $comment->validate();
102 $this->assertEqual(count($violations), 1, "Violation found on author name collision");
103 $this->assertEqual($violations[0]->getPropertyPath(), "name");
104 $this->assertEqual($violations[0]->getMessage(), t('The name you used (%name) belongs to a registered user.', ['%name' => 'test']));
106 // Make the name valid.
107 $comment->set('name', 'valid unused name');
108 $comment->set('mail', 'invalid');
109 $violations = $comment->validate();
110 $this->assertEqual(count($violations), 1, 'Violation found when email is invalid');
111 $this->assertEqual($violations[0]->getPropertyPath(), 'mail.0.value');
112 $this->assertEqual($violations[0]->getMessage(), t('This value is not a valid email address.'));
114 $comment->set('mail', NULL);
115 $comment->set('homepage', 'http://example.com/' . $this->randomMachineName(237));
116 $this->assertLengthViolation($comment, 'homepage', 255);
118 $comment->set('homepage', 'invalid');
119 $violations = $comment->validate();
120 $this->assertEqual(count($violations), 1, 'Violation found when homepage is invalid');
121 $this->assertEqual($violations[0]->getPropertyPath(), 'homepage.0.value');
123 // @todo This message should be improved in
124 // https://www.drupal.org/node/2012690.
125 $this->assertEqual($violations[0]->getMessage(), t('This value should be of the correct primitive type.'));
127 $comment->set('homepage', NULL);
128 $comment->set('hostname', $this->randomString(129));
129 $this->assertLengthViolation($comment, 'hostname', 128);
131 $comment->set('hostname', NULL);
132 $comment->set('thread', $this->randomString(256));
133 $this->assertLengthViolation($comment, 'thread', 255);
135 $comment->set('thread', NULL);
137 // Force anonymous users to enter contact details.
138 $field->setSetting('anonymous', COMMENT_ANONYMOUS_MUST_CONTACT);
140 // Reset the node entity.
141 \Drupal::entityManager()->getStorage('node')->resetCache([$node->id()]);
142 $node = Node::load($node->id());
143 // Create a new comment with the new field.
144 $comment = $this->entityManager->getStorage('comment')->create([
145 'entity_id' => $node->id(),
146 'entity_type' => 'node',
147 'field_name' => 'comment',
148 'comment_body' => $this->randomMachineName(),
152 $violations = $comment->validate();
153 $this->assertEqual(count($violations), 1, 'Violation found when name is required, but empty and UID is anonymous.');
154 $this->assertEqual($violations[0]->getPropertyPath(), 'name');
155 $this->assertEqual($violations[0]->getMessage(), t('You have to specify a valid author.'));
157 // Test creating a default comment with a given user id works.
158 $comment = $this->entityManager->getStorage('comment')->create([
159 'entity_id' => $node->id(),
160 'entity_type' => 'node',
161 'field_name' => 'comment',
162 'comment_body' => $this->randomMachineName(),
163 'uid' => $user->id(),
165 $violations = $comment->validate();
166 $this->assertEqual(count($violations), 0, 'No violations when validating a default comment with an author.');
168 // Test specifying a wrong author name does not work.
169 $comment = $this->entityManager->getStorage('comment')->create([
170 'entity_id' => $node->id(),
171 'entity_type' => 'node',
172 'field_name' => 'comment',
173 'comment_body' => $this->randomMachineName(),
174 'uid' => $user->id(),
175 'name' => 'not-test',
177 $violations = $comment->validate();
178 $this->assertEqual(count($violations), 1, 'Violation found when author name and comment author do not match.');
179 $this->assertEqual($violations[0]->getPropertyPath(), 'name');
180 $this->assertEqual($violations[0]->getMessage(), t('The specified author name does not match the comment author.'));
184 * Verifies that a length violation exists for the given field.
186 * @param \Drupal\comment\CommentInterface $comment
187 * The comment object to validate.
188 * @param string $field_name
189 * The field that violates the maximum length.
191 * Number of characters that was exceeded.
193 protected function assertLengthViolation(CommentInterface $comment, $field_name, $length) {
194 $violations = $comment->validate();
195 $this->assertEqual(count($violations), 1, "Violation found when $field_name is too long.");
196 $this->assertEqual($violations[0]->getPropertyPath(), "$field_name.0.value");
197 $field_label = $comment->get($field_name)->getFieldDefinition()->getLabel();
198 $this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', ['%name' => $field_label, '@max' => $length]));