Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/comment/tests/src/Kernel/CommentValidationTest.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\comment\Kernel; | |
4 | |
5 use Drupal\comment\CommentInterface; | |
6 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; | |
7 use Drupal\node\Entity\Node; | |
8 use Drupal\user\Entity\User; | |
9 | |
10 /** | |
11 * Tests comment validation constraints. | |
12 * | |
13 * @group comment | |
14 */ | |
15 class CommentValidationTest extends EntityKernelTestBase { | |
16 | |
17 /** | |
18 * Modules to install. | |
19 * | |
20 * @var array | |
21 */ | |
22 public static $modules = ['comment', 'node']; | |
23 | |
24 /** | |
25 * {@inheritdoc} | |
26 */ | |
27 protected function setUp() { | |
28 parent::setUp(); | |
29 $this->installSchema('comment', ['comment_entity_statistics']); | |
30 } | |
31 | |
32 /** | |
33 * Tests the comment validation constraints. | |
34 */ | |
35 public function testValidation() { | |
36 // Add a user. | |
37 $user = User::create(['name' => 'test', 'status' => TRUE]); | |
38 $user->save(); | |
39 | |
40 // Add comment type. | |
41 $this->entityManager->getStorage('comment_type')->create([ | |
42 'id' => 'comment', | |
43 'label' => 'comment', | |
44 'target_entity_type_id' => 'node', | |
45 ])->save(); | |
46 | |
47 // Add comment field to content. | |
48 $this->entityManager->getStorage('field_storage_config')->create([ | |
49 'entity_type' => 'node', | |
50 'field_name' => 'comment', | |
51 'type' => 'comment', | |
52 'settings' => [ | |
53 'comment_type' => 'comment', | |
54 ] | |
55 ])->save(); | |
56 | |
57 // Create a page node type. | |
58 $this->entityManager->getStorage('node_type')->create([ | |
59 'type' => 'page', | |
60 'name' => 'page', | |
61 ])->save(); | |
62 | |
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', | |
68 'bundle' => 'page', | |
69 'label' => 'Comment settings', | |
70 ]); | |
71 $field->save(); | |
72 | |
73 $node = $this->entityManager->getStorage('node')->create([ | |
74 'type' => 'page', | |
75 'title' => 'test', | |
76 ]); | |
77 $node->save(); | |
78 | |
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(), | |
84 ]); | |
85 | |
86 $violations = $comment->validate(); | |
87 $this->assertEqual(count($violations), 0, 'No violations when validating a default comment.'); | |
88 | |
89 $comment->set('subject', $this->randomString(65)); | |
90 $this->assertLengthViolation($comment, 'subject', 64); | |
91 | |
92 // Make the subject valid. | |
93 $comment->set('subject', $this->randomString()); | |
94 $comment->set('name', $this->randomString(61)); | |
95 $this->assertLengthViolation($comment, 'name', 60); | |
96 | |
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'])); | |
105 | |
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.')); | |
113 | |
114 $comment->set('mail', NULL); | |
115 $comment->set('homepage', 'http://example.com/' . $this->randomMachineName(237)); | |
116 $this->assertLengthViolation($comment, 'homepage', 255); | |
117 | |
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'); | |
122 | |
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.')); | |
126 | |
127 $comment->set('homepage', NULL); | |
128 $comment->set('hostname', $this->randomString(129)); | |
129 $this->assertLengthViolation($comment, 'hostname', 128); | |
130 | |
131 $comment->set('hostname', NULL); | |
132 $comment->set('thread', $this->randomString(256)); | |
133 $this->assertLengthViolation($comment, 'thread', 255); | |
134 | |
135 $comment->set('thread', NULL); | |
136 | |
137 // Force anonymous users to enter contact details. | |
138 $field->setSetting('anonymous', COMMENT_ANONYMOUS_MUST_CONTACT); | |
139 $field->save(); | |
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(), | |
149 'uid' => 0, | |
150 'name' => '', | |
151 ]); | |
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.')); | |
156 | |
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(), | |
164 ]); | |
165 $violations = $comment->validate(); | |
166 $this->assertEqual(count($violations), 0, 'No violations when validating a default comment with an author.'); | |
167 | |
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', | |
176 ]); | |
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.')); | |
181 } | |
182 | |
183 /** | |
184 * Verifies that a length violation exists for the given field. | |
185 * | |
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. | |
190 * @param int $length | |
191 * Number of characters that was exceeded. | |
192 */ | |
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])); | |
199 } | |
200 | |
201 } |