Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\comment\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\comment\CommentInterface;
|
Chris@0
|
6 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
Chris@0
|
7 use Drupal\node\Entity\Node;
|
Chris@0
|
8 use Drupal\user\Entity\User;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Tests comment validation constraints.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @group comment
|
Chris@0
|
14 */
|
Chris@0
|
15 class CommentValidationTest extends EntityKernelTestBase {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Modules to install.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var array
|
Chris@0
|
21 */
|
Chris@0
|
22 public static $modules = ['comment', 'node'];
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * {@inheritdoc}
|
Chris@0
|
26 */
|
Chris@0
|
27 protected function setUp() {
|
Chris@0
|
28 parent::setUp();
|
Chris@0
|
29 $this->installSchema('comment', ['comment_entity_statistics']);
|
Chris@0
|
30 }
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Tests the comment validation constraints.
|
Chris@0
|
34 */
|
Chris@0
|
35 public function testValidation() {
|
Chris@0
|
36 // Add a user.
|
Chris@0
|
37 $user = User::create(['name' => 'test', 'status' => TRUE]);
|
Chris@0
|
38 $user->save();
|
Chris@0
|
39
|
Chris@0
|
40 // Add comment type.
|
Chris@0
|
41 $this->entityManager->getStorage('comment_type')->create([
|
Chris@0
|
42 'id' => 'comment',
|
Chris@0
|
43 'label' => 'comment',
|
Chris@0
|
44 'target_entity_type_id' => 'node',
|
Chris@0
|
45 ])->save();
|
Chris@0
|
46
|
Chris@0
|
47 // Add comment field to content.
|
Chris@0
|
48 $this->entityManager->getStorage('field_storage_config')->create([
|
Chris@0
|
49 'entity_type' => 'node',
|
Chris@0
|
50 'field_name' => 'comment',
|
Chris@0
|
51 'type' => 'comment',
|
Chris@0
|
52 'settings' => [
|
Chris@0
|
53 'comment_type' => 'comment',
|
Chris@17
|
54 ],
|
Chris@0
|
55 ])->save();
|
Chris@0
|
56
|
Chris@0
|
57 // Create a page node type.
|
Chris@0
|
58 $this->entityManager->getStorage('node_type')->create([
|
Chris@0
|
59 'type' => 'page',
|
Chris@0
|
60 'name' => 'page',
|
Chris@0
|
61 ])->save();
|
Chris@0
|
62
|
Chris@0
|
63 // Add comment field to page content.
|
Chris@0
|
64 /** @var \Drupal\field\FieldConfigInterface $field */
|
Chris@0
|
65 $field = $this->entityManager->getStorage('field_config')->create([
|
Chris@0
|
66 'field_name' => 'comment',
|
Chris@0
|
67 'entity_type' => 'node',
|
Chris@0
|
68 'bundle' => 'page',
|
Chris@0
|
69 'label' => 'Comment settings',
|
Chris@0
|
70 ]);
|
Chris@0
|
71 $field->save();
|
Chris@0
|
72
|
Chris@0
|
73 $node = $this->entityManager->getStorage('node')->create([
|
Chris@0
|
74 'type' => 'page',
|
Chris@0
|
75 'title' => 'test',
|
Chris@0
|
76 ]);
|
Chris@0
|
77 $node->save();
|
Chris@0
|
78
|
Chris@0
|
79 $comment = $this->entityManager->getStorage('comment')->create([
|
Chris@0
|
80 'entity_id' => $node->id(),
|
Chris@0
|
81 'entity_type' => 'node',
|
Chris@0
|
82 'field_name' => 'comment',
|
Chris@0
|
83 'comment_body' => $this->randomMachineName(),
|
Chris@0
|
84 ]);
|
Chris@0
|
85
|
Chris@0
|
86 $violations = $comment->validate();
|
Chris@0
|
87 $this->assertEqual(count($violations), 0, 'No violations when validating a default comment.');
|
Chris@0
|
88
|
Chris@0
|
89 $comment->set('subject', $this->randomString(65));
|
Chris@0
|
90 $this->assertLengthViolation($comment, 'subject', 64);
|
Chris@0
|
91
|
Chris@0
|
92 // Make the subject valid.
|
Chris@0
|
93 $comment->set('subject', $this->randomString());
|
Chris@0
|
94 $comment->set('name', $this->randomString(61));
|
Chris@0
|
95 $this->assertLengthViolation($comment, 'name', 60);
|
Chris@0
|
96
|
Chris@0
|
97 // Validate a name collision between an anonymous comment author name and an
|
Chris@0
|
98 // existing user account name.
|
Chris@0
|
99 $comment->set('name', 'test');
|
Chris@0
|
100 $comment->set('uid', 0);
|
Chris@0
|
101 $violations = $comment->validate();
|
Chris@0
|
102 $this->assertEqual(count($violations), 1, "Violation found on author name collision");
|
Chris@0
|
103 $this->assertEqual($violations[0]->getPropertyPath(), "name");
|
Chris@0
|
104 $this->assertEqual($violations[0]->getMessage(), t('The name you used (%name) belongs to a registered user.', ['%name' => 'test']));
|
Chris@0
|
105
|
Chris@0
|
106 // Make the name valid.
|
Chris@0
|
107 $comment->set('name', 'valid unused name');
|
Chris@0
|
108 $comment->set('mail', 'invalid');
|
Chris@0
|
109 $violations = $comment->validate();
|
Chris@0
|
110 $this->assertEqual(count($violations), 1, 'Violation found when email is invalid');
|
Chris@0
|
111 $this->assertEqual($violations[0]->getPropertyPath(), 'mail.0.value');
|
Chris@0
|
112 $this->assertEqual($violations[0]->getMessage(), t('This value is not a valid email address.'));
|
Chris@0
|
113
|
Chris@0
|
114 $comment->set('mail', NULL);
|
Chris@0
|
115 $comment->set('homepage', 'http://example.com/' . $this->randomMachineName(237));
|
Chris@0
|
116 $this->assertLengthViolation($comment, 'homepage', 255);
|
Chris@0
|
117
|
Chris@0
|
118 $comment->set('homepage', 'invalid');
|
Chris@0
|
119 $violations = $comment->validate();
|
Chris@0
|
120 $this->assertEqual(count($violations), 1, 'Violation found when homepage is invalid');
|
Chris@0
|
121 $this->assertEqual($violations[0]->getPropertyPath(), 'homepage.0.value');
|
Chris@0
|
122
|
Chris@0
|
123 // @todo This message should be improved in
|
Chris@0
|
124 // https://www.drupal.org/node/2012690.
|
Chris@0
|
125 $this->assertEqual($violations[0]->getMessage(), t('This value should be of the correct primitive type.'));
|
Chris@0
|
126
|
Chris@0
|
127 $comment->set('homepage', NULL);
|
Chris@0
|
128 $comment->set('hostname', $this->randomString(129));
|
Chris@0
|
129 $this->assertLengthViolation($comment, 'hostname', 128);
|
Chris@0
|
130
|
Chris@0
|
131 $comment->set('hostname', NULL);
|
Chris@0
|
132 $comment->set('thread', $this->randomString(256));
|
Chris@0
|
133 $this->assertLengthViolation($comment, 'thread', 255);
|
Chris@0
|
134
|
Chris@0
|
135 $comment->set('thread', NULL);
|
Chris@0
|
136
|
Chris@0
|
137 // Force anonymous users to enter contact details.
|
Chris@18
|
138 $field->setSetting('anonymous', CommentInterface::ANONYMOUS_MUST_CONTACT);
|
Chris@0
|
139 $field->save();
|
Chris@0
|
140 // Reset the node entity.
|
Chris@0
|
141 \Drupal::entityManager()->getStorage('node')->resetCache([$node->id()]);
|
Chris@0
|
142 $node = Node::load($node->id());
|
Chris@0
|
143 // Create a new comment with the new field.
|
Chris@0
|
144 $comment = $this->entityManager->getStorage('comment')->create([
|
Chris@0
|
145 'entity_id' => $node->id(),
|
Chris@0
|
146 'entity_type' => 'node',
|
Chris@0
|
147 'field_name' => 'comment',
|
Chris@0
|
148 'comment_body' => $this->randomMachineName(),
|
Chris@0
|
149 'uid' => 0,
|
Chris@0
|
150 'name' => '',
|
Chris@0
|
151 ]);
|
Chris@0
|
152 $violations = $comment->validate();
|
Chris@0
|
153 $this->assertEqual(count($violations), 1, 'Violation found when name is required, but empty and UID is anonymous.');
|
Chris@0
|
154 $this->assertEqual($violations[0]->getPropertyPath(), 'name');
|
Chris@0
|
155 $this->assertEqual($violations[0]->getMessage(), t('You have to specify a valid author.'));
|
Chris@0
|
156
|
Chris@0
|
157 // Test creating a default comment with a given user id works.
|
Chris@0
|
158 $comment = $this->entityManager->getStorage('comment')->create([
|
Chris@0
|
159 'entity_id' => $node->id(),
|
Chris@0
|
160 'entity_type' => 'node',
|
Chris@0
|
161 'field_name' => 'comment',
|
Chris@0
|
162 'comment_body' => $this->randomMachineName(),
|
Chris@0
|
163 'uid' => $user->id(),
|
Chris@0
|
164 ]);
|
Chris@0
|
165 $violations = $comment->validate();
|
Chris@0
|
166 $this->assertEqual(count($violations), 0, 'No violations when validating a default comment with an author.');
|
Chris@0
|
167
|
Chris@0
|
168 // Test specifying a wrong author name does not work.
|
Chris@0
|
169 $comment = $this->entityManager->getStorage('comment')->create([
|
Chris@0
|
170 'entity_id' => $node->id(),
|
Chris@0
|
171 'entity_type' => 'node',
|
Chris@0
|
172 'field_name' => 'comment',
|
Chris@0
|
173 'comment_body' => $this->randomMachineName(),
|
Chris@0
|
174 'uid' => $user->id(),
|
Chris@0
|
175 'name' => 'not-test',
|
Chris@0
|
176 ]);
|
Chris@0
|
177 $violations = $comment->validate();
|
Chris@0
|
178 $this->assertEqual(count($violations), 1, 'Violation found when author name and comment author do not match.');
|
Chris@0
|
179 $this->assertEqual($violations[0]->getPropertyPath(), 'name');
|
Chris@0
|
180 $this->assertEqual($violations[0]->getMessage(), t('The specified author name does not match the comment author.'));
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 /**
|
Chris@0
|
184 * Verifies that a length violation exists for the given field.
|
Chris@0
|
185 *
|
Chris@0
|
186 * @param \Drupal\comment\CommentInterface $comment
|
Chris@0
|
187 * The comment object to validate.
|
Chris@0
|
188 * @param string $field_name
|
Chris@0
|
189 * The field that violates the maximum length.
|
Chris@0
|
190 * @param int $length
|
Chris@0
|
191 * Number of characters that was exceeded.
|
Chris@0
|
192 */
|
Chris@0
|
193 protected function assertLengthViolation(CommentInterface $comment, $field_name, $length) {
|
Chris@0
|
194 $violations = $comment->validate();
|
Chris@0
|
195 $this->assertEqual(count($violations), 1, "Violation found when $field_name is too long.");
|
Chris@0
|
196 $this->assertEqual($violations[0]->getPropertyPath(), "$field_name.0.value");
|
Chris@0
|
197 $field_label = $comment->get($field_name)->getFieldDefinition()->getLabel();
|
Chris@0
|
198 $this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', ['%name' => $field_label, '@max' => $length]));
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 }
|