Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\comment\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\comment\CommentInterface;
|
Chris@0
|
6 use Drupal\comment\Entity\Comment;
|
Chris@0
|
7 use Drupal\comment\Entity\CommentType;
|
Chris@0
|
8 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
Chris@0
|
9 use Drupal\comment\Tests\CommentTestTrait;
|
Chris@0
|
10 use Drupal\entity_test\Entity\EntityTest;
|
Chris@0
|
11 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
12 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
13 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
14 use Drupal\Core\Entity\EntityInterface;
|
Chris@17
|
15 use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
|
Chris@0
|
16 use Drupal\user\RoleInterface;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Tests commenting on a test entity.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @group comment
|
Chris@0
|
22 */
|
Chris@0
|
23 class CommentNonNodeTest extends BrowserTestBase {
|
Chris@0
|
24
|
Chris@0
|
25 use FieldUiTestTrait;
|
Chris@0
|
26 use CommentTestTrait;
|
Chris@0
|
27
|
Chris@0
|
28 public static $modules = ['comment', 'user', 'field_ui', 'entity_test', 'block'];
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * An administrative user with permission to configure comment settings.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var \Drupal\user\UserInterface
|
Chris@0
|
34 */
|
Chris@0
|
35 protected $adminUser;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * The entity to use within tests.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @var \Drupal\entity_test\Entity\EntityTest
|
Chris@0
|
41 */
|
Chris@0
|
42 protected $entity;
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * {@inheritdoc}
|
Chris@0
|
46 */
|
Chris@0
|
47 protected function setUp() {
|
Chris@0
|
48 parent::setUp();
|
Chris@0
|
49 $this->drupalPlaceBlock('system_breadcrumb_block');
|
Chris@0
|
50 $this->drupalPlaceBlock('page_title_block');
|
Chris@0
|
51
|
Chris@0
|
52 // Create a bundle for entity_test.
|
Chris@0
|
53 entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test');
|
Chris@0
|
54 CommentType::create([
|
Chris@0
|
55 'id' => 'comment',
|
Chris@0
|
56 'label' => 'Comment settings',
|
Chris@0
|
57 'description' => 'Comment settings',
|
Chris@0
|
58 'target_entity_type_id' => 'entity_test',
|
Chris@0
|
59 ])->save();
|
Chris@0
|
60 // Create comment field on entity_test bundle.
|
Chris@0
|
61 $this->addDefaultCommentField('entity_test', 'entity_test');
|
Chris@0
|
62
|
Chris@0
|
63 // Verify that bundles are defined correctly.
|
Chris@18
|
64 $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('comment');
|
Chris@0
|
65 $this->assertEqual($bundles['comment']['label'], 'Comment settings');
|
Chris@0
|
66
|
Chris@0
|
67 // Create test user.
|
Chris@0
|
68 $this->adminUser = $this->drupalCreateUser([
|
Chris@0
|
69 'administer comments',
|
Chris@0
|
70 'skip comment approval',
|
Chris@0
|
71 'post comments',
|
Chris@0
|
72 'access comments',
|
Chris@0
|
73 'view test entity',
|
Chris@0
|
74 'administer entity_test content',
|
Chris@0
|
75 ]);
|
Chris@0
|
76
|
Chris@0
|
77 // Enable anonymous and authenticated user comments.
|
Chris@0
|
78 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
|
Chris@0
|
79 'access comments',
|
Chris@0
|
80 'post comments',
|
Chris@0
|
81 'skip comment approval',
|
Chris@0
|
82 ]);
|
Chris@0
|
83 user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
|
Chris@0
|
84 'access comments',
|
Chris@0
|
85 'post comments',
|
Chris@0
|
86 'skip comment approval',
|
Chris@0
|
87 ]);
|
Chris@0
|
88
|
Chris@0
|
89 // Create a test entity.
|
Chris@0
|
90 $random_label = $this->randomMachineName();
|
Chris@0
|
91 $data = ['type' => 'entity_test', 'name' => $random_label];
|
Chris@0
|
92 $this->entity = EntityTest::create($data);
|
Chris@0
|
93 $this->entity->save();
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * Posts a comment.
|
Chris@0
|
98 *
|
Chris@0
|
99 * @param \Drupal\Core\Entity\EntityInterface|null $entity
|
Chris@0
|
100 * Entity to post comment on or NULL to post to the previously loaded page.
|
Chris@0
|
101 * @param string $comment
|
Chris@0
|
102 * Comment body.
|
Chris@0
|
103 * @param string $subject
|
Chris@0
|
104 * Comment subject.
|
Chris@0
|
105 * @param mixed $contact
|
Chris@0
|
106 * Set to NULL for no contact info, TRUE to ignore success checking, and
|
Chris@0
|
107 * array of values to set contact info.
|
Chris@0
|
108 *
|
Chris@0
|
109 * @return \Drupal\comment\CommentInterface
|
Chris@0
|
110 * The new comment entity.
|
Chris@0
|
111 */
|
Chris@0
|
112 public function postComment(EntityInterface $entity, $comment, $subject = '', $contact = NULL) {
|
Chris@0
|
113 $edit = [];
|
Chris@0
|
114 $edit['comment_body[0][value]'] = $comment;
|
Chris@0
|
115
|
Chris@0
|
116 $field = FieldConfig::loadByName('entity_test', 'entity_test', 'comment');
|
Chris@0
|
117 $preview_mode = $field->getSetting('preview');
|
Chris@0
|
118
|
Chris@0
|
119 // Must get the page before we test for fields.
|
Chris@0
|
120 if ($entity !== NULL) {
|
Chris@0
|
121 $this->drupalGet('comment/reply/entity_test/' . $entity->id() . '/comment');
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 // Determine the visibility of subject form field.
|
Chris@0
|
125 if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
|
Chris@0
|
126 // Subject input allowed.
|
Chris@0
|
127 $edit['subject[0][value]'] = $subject;
|
Chris@0
|
128 }
|
Chris@0
|
129 else {
|
Chris@0
|
130 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 if ($contact !== NULL && is_array($contact)) {
|
Chris@0
|
134 $edit += $contact;
|
Chris@0
|
135 }
|
Chris@0
|
136 switch ($preview_mode) {
|
Chris@0
|
137 case DRUPAL_REQUIRED:
|
Chris@0
|
138 // Preview required so no save button should be found.
|
Chris@0
|
139 $this->assertNoFieldByName('op', t('Save'), 'Save button not found.');
|
Chris@0
|
140 $this->drupalPostForm(NULL, $edit, t('Preview'));
|
Chris@0
|
141 // Don't break here so that we can test post-preview field presence and
|
Chris@0
|
142 // function below.
|
Chris@0
|
143 case DRUPAL_OPTIONAL:
|
Chris@0
|
144 $this->assertFieldByName('op', t('Preview'), 'Preview button found.');
|
Chris@0
|
145 $this->assertFieldByName('op', t('Save'), 'Save button found.');
|
Chris@0
|
146 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
147 break;
|
Chris@0
|
148
|
Chris@0
|
149 case DRUPAL_DISABLED:
|
Chris@0
|
150 $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
|
Chris@0
|
151 $this->assertFieldByName('op', t('Save'), 'Save button found.');
|
Chris@0
|
152 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
153 break;
|
Chris@0
|
154 }
|
Chris@0
|
155 $match = [];
|
Chris@0
|
156 // Get comment ID
|
Chris@0
|
157 preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
|
Chris@0
|
158
|
Chris@0
|
159 // Get comment.
|
Chris@0
|
160 if ($contact !== TRUE) {
|
Chris@0
|
161 // If true then attempting to find error message.
|
Chris@0
|
162 if ($subject) {
|
Chris@0
|
163 $this->assertText($subject, 'Comment subject posted.');
|
Chris@0
|
164 }
|
Chris@0
|
165 $this->assertText($comment, 'Comment body posted.');
|
Chris@0
|
166 $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment ID found.');
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 if (isset($match[1])) {
|
Chris@0
|
170 return Comment::load($match[1]);
|
Chris@0
|
171 }
|
Chris@0
|
172 }
|
Chris@0
|
173
|
Chris@0
|
174 /**
|
Chris@0
|
175 * Checks current page for specified comment.
|
Chris@0
|
176 *
|
Chris@0
|
177 * @param \Drupal\comment\CommentInterface $comment
|
Chris@0
|
178 * The comment object.
|
Chris@0
|
179 * @param bool $reply
|
Chris@0
|
180 * Boolean indicating whether the comment is a reply to another comment.
|
Chris@0
|
181 *
|
Chris@0
|
182 * @return bool
|
Chris@0
|
183 * Boolean indicating whether the comment was found.
|
Chris@0
|
184 */
|
Chris@0
|
185 public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) {
|
Chris@0
|
186 if ($comment) {
|
Chris@0
|
187 $regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
|
Chris@18
|
188 $regex .= '<article(.*?)id="comment-' . $comment->id() . '"(.*?)';
|
Chris@0
|
189 $regex .= $comment->getSubject() . '(.*?)';
|
Chris@0
|
190 $regex .= $comment->comment_body->value . '(.*?)';
|
Chris@0
|
191 $regex .= '/s';
|
Chris@0
|
192
|
Chris@17
|
193 return (boolean) preg_match($regex, $this->getSession()->getPage()->getContent());
|
Chris@0
|
194 }
|
Chris@0
|
195 else {
|
Chris@0
|
196 return FALSE;
|
Chris@0
|
197 }
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 /**
|
Chris@0
|
201 * Checks whether the commenter's contact information is displayed.
|
Chris@0
|
202 *
|
Chris@0
|
203 * @return bool
|
Chris@0
|
204 * Contact info is available.
|
Chris@0
|
205 */
|
Chris@0
|
206 public function commentContactInfoAvailable() {
|
Chris@17
|
207 return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getSession()->getPage()->getContent());
|
Chris@0
|
208 }
|
Chris@0
|
209
|
Chris@0
|
210 /**
|
Chris@0
|
211 * Performs the specified operation on the specified comment.
|
Chris@0
|
212 *
|
Chris@0
|
213 * @param object $comment
|
Chris@0
|
214 * Comment to perform operation on.
|
Chris@0
|
215 * @param string $operation
|
Chris@0
|
216 * Operation to perform.
|
Chris@0
|
217 * @param bool $approval
|
Chris@0
|
218 * Operation is found on approval page.
|
Chris@0
|
219 */
|
Chris@0
|
220 public function performCommentOperation($comment, $operation, $approval = FALSE) {
|
Chris@0
|
221 $edit = [];
|
Chris@0
|
222 $edit['operation'] = $operation;
|
Chris@0
|
223 $edit['comments[' . $comment->id() . ']'] = TRUE;
|
Chris@0
|
224 $this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
|
Chris@0
|
225
|
Chris@0
|
226 if ($operation == 'delete') {
|
Chris@0
|
227 $this->drupalPostForm(NULL, [], t('Delete'));
|
Chris@0
|
228 $this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
|
Chris@0
|
229 }
|
Chris@0
|
230 else {
|
Chris@0
|
231 $this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
|
Chris@0
|
232 }
|
Chris@0
|
233 }
|
Chris@0
|
234
|
Chris@0
|
235 /**
|
Chris@0
|
236 * Gets the comment ID for an unapproved comment.
|
Chris@0
|
237 *
|
Chris@0
|
238 * @param string $subject
|
Chris@0
|
239 * Comment subject to find.
|
Chris@0
|
240 *
|
Chris@0
|
241 * @return int
|
Chris@0
|
242 * Comment ID.
|
Chris@0
|
243 */
|
Chris@0
|
244 public function getUnapprovedComment($subject) {
|
Chris@0
|
245 $this->drupalGet('admin/content/comment/approval');
|
Chris@17
|
246 preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getSession()->getPage()->getContent(), $match);
|
Chris@0
|
247
|
Chris@0
|
248 return $match[2];
|
Chris@0
|
249 }
|
Chris@0
|
250
|
Chris@0
|
251 /**
|
Chris@0
|
252 * Tests anonymous comment functionality.
|
Chris@0
|
253 */
|
Chris@0
|
254 public function testCommentFunctionality() {
|
Chris@0
|
255 $limited_user = $this->drupalCreateUser([
|
Chris@17
|
256 'administer entity_test fields',
|
Chris@0
|
257 ]);
|
Chris@0
|
258 $this->drupalLogin($limited_user);
|
Chris@0
|
259 // Test that default field exists.
|
Chris@0
|
260 $this->drupalGet('entity_test/structure/entity_test/fields');
|
Chris@0
|
261 $this->assertText(t('Comments'));
|
Chris@0
|
262 $this->assertLinkByHref('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
|
Chris@0
|
263 // Test widget hidden option is not visible when there's no comments.
|
Chris@0
|
264 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
|
Chris@0
|
265 $this->assertResponse(200);
|
Chris@0
|
266 $this->assertNoField('edit-default-value-input-comment-und-0-status-0');
|
Chris@0
|
267 // Test that field to change cardinality is not available.
|
Chris@0
|
268 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment/storage');
|
Chris@0
|
269 $this->assertResponse(200);
|
Chris@0
|
270 $this->assertNoField('cardinality_number');
|
Chris@0
|
271 $this->assertNoField('cardinality');
|
Chris@0
|
272
|
Chris@0
|
273 $this->drupalLogin($this->adminUser);
|
Chris@0
|
274
|
Chris@0
|
275 // Test breadcrumb on comment add page.
|
Chris@0
|
276 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
|
Chris@0
|
277 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
|
Chris@0
|
278 $this->assertEqual(current($this->xpath($xpath))->getText(), $this->entity->label(), 'Last breadcrumb item is equal to node title on comment reply page.');
|
Chris@0
|
279
|
Chris@0
|
280 // Post a comment.
|
Chris@0
|
281 /** @var \Drupal\comment\CommentInterface $comment1 */
|
Chris@0
|
282 $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName());
|
Chris@0
|
283 $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');
|
Chris@0
|
284
|
Chris@0
|
285 // Test breadcrumb on comment reply page.
|
Chris@0
|
286 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
|
Chris@0
|
287 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
|
Chris@0
|
288 $this->assertEqual(current($this->xpath($xpath))->getText(), $comment1->getSubject(), 'Last breadcrumb item is equal to comment title on comment reply page.');
|
Chris@0
|
289
|
Chris@0
|
290 // Test breadcrumb on comment edit page.
|
Chris@0
|
291 $this->drupalGet('comment/' . $comment1->id() . '/edit');
|
Chris@0
|
292 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
|
Chris@0
|
293 $this->assertEqual(current($this->xpath($xpath))->getText(), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on edit page.');
|
Chris@0
|
294
|
Chris@0
|
295 // Test breadcrumb on comment delete page.
|
Chris@0
|
296 $this->drupalGet('comment/' . $comment1->id() . '/delete');
|
Chris@0
|
297 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
|
Chris@0
|
298 $this->assertEqual(current($this->xpath($xpath))->getText(), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on delete confirm page.');
|
Chris@0
|
299
|
Chris@0
|
300 // Unpublish the comment.
|
Chris@0
|
301 $this->performCommentOperation($comment1, 'unpublish');
|
Chris@0
|
302 $this->drupalGet('admin/content/comment/approval');
|
Chris@0
|
303 $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was unpublished.');
|
Chris@0
|
304
|
Chris@0
|
305 // Publish the comment.
|
Chris@0
|
306 $this->performCommentOperation($comment1, 'publish', TRUE);
|
Chris@0
|
307 $this->drupalGet('admin/content/comment');
|
Chris@0
|
308 $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.');
|
Chris@0
|
309
|
Chris@0
|
310 // Delete the comment.
|
Chris@0
|
311 $this->performCommentOperation($comment1, 'delete');
|
Chris@0
|
312 $this->drupalGet('admin/content/comment');
|
Chris@0
|
313 $this->assertNoRaw('comments[' . $comment1->id() . ']', 'Comment was deleted.');
|
Chris@0
|
314
|
Chris@0
|
315 // Post another comment.
|
Chris@0
|
316 $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName());
|
Chris@0
|
317 $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');
|
Chris@0
|
318
|
Chris@0
|
319 // Check that the comment was found.
|
Chris@0
|
320 $this->drupalGet('admin/content/comment');
|
Chris@0
|
321 $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.');
|
Chris@0
|
322
|
Chris@0
|
323 // Check that entity access applies to administrative page.
|
Chris@0
|
324 $this->assertText($this->entity->label(), 'Name of commented account found.');
|
Chris@0
|
325 $limited_user = $this->drupalCreateUser([
|
Chris@0
|
326 'administer comments',
|
Chris@0
|
327 ]);
|
Chris@0
|
328 $this->drupalLogin($limited_user);
|
Chris@0
|
329 $this->drupalGet('admin/content/comment');
|
Chris@0
|
330 $this->assertNoText($this->entity->label(), 'No commented account name found.');
|
Chris@0
|
331
|
Chris@0
|
332 $this->drupalLogout();
|
Chris@0
|
333
|
Chris@0
|
334 // Deny anonymous users access to comments.
|
Chris@0
|
335 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
|
Chris@0
|
336 'access comments' => FALSE,
|
Chris@0
|
337 'post comments' => FALSE,
|
Chris@0
|
338 'skip comment approval' => FALSE,
|
Chris@0
|
339 'view test entity' => TRUE,
|
Chris@0
|
340 ]);
|
Chris@0
|
341
|
Chris@0
|
342 // Attempt to view comments while disallowed.
|
Chris@0
|
343 $this->drupalGet('entity-test/' . $this->entity->id());
|
Chris@18
|
344 $this->assertSession()->responseNotMatches('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
|
Chris@0
|
345 $this->assertNoLink('Add new comment', 'Link to add comment was found.');
|
Chris@0
|
346
|
Chris@0
|
347 // Attempt to view test entity comment form while disallowed.
|
Chris@0
|
348 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
|
Chris@0
|
349 $this->assertResponse(403);
|
Chris@0
|
350 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
|
Chris@0
|
351 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.');
|
Chris@0
|
352
|
Chris@0
|
353 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
|
Chris@0
|
354 'access comments' => TRUE,
|
Chris@0
|
355 'post comments' => FALSE,
|
Chris@0
|
356 'view test entity' => TRUE,
|
Chris@0
|
357 'skip comment approval' => FALSE,
|
Chris@0
|
358 ]);
|
Chris@0
|
359 $this->drupalGet('entity_test/' . $this->entity->id());
|
Chris@0
|
360 $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.');
|
Chris@0
|
361 $this->assertLink('Log in', 0, 'Link to login was found.');
|
Chris@0
|
362 $this->assertLink('register', 0, 'Link to register was found.');
|
Chris@0
|
363 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
|
Chris@0
|
364 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.');
|
Chris@0
|
365
|
Chris@0
|
366 // Test the combination of anonymous users being able to post, but not view
|
Chris@0
|
367 // comments, to ensure that access to post comments doesn't grant access to
|
Chris@0
|
368 // view them.
|
Chris@0
|
369 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
|
Chris@0
|
370 'access comments' => FALSE,
|
Chris@0
|
371 'post comments' => TRUE,
|
Chris@0
|
372 'skip comment approval' => TRUE,
|
Chris@0
|
373 'view test entity' => TRUE,
|
Chris@0
|
374 ]);
|
Chris@0
|
375 $this->drupalGet('entity_test/' . $this->entity->id());
|
Chris@18
|
376 $this->assertSession()->responseNotMatches('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
|
Chris@0
|
377 $this->assertFieldByName('subject[0][value]', '', 'Subject field found.');
|
Chris@0
|
378 $this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.');
|
Chris@0
|
379
|
Chris@0
|
380 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
|
Chris@0
|
381 $this->assertResponse(403);
|
Chris@0
|
382 $this->assertNoText($comment1->getSubject(), 'Comment not displayed.');
|
Chris@0
|
383
|
Chris@0
|
384 // Test comment field widget changes.
|
Chris@0
|
385 $limited_user = $this->drupalCreateUser([
|
Chris@0
|
386 'administer entity_test fields',
|
Chris@0
|
387 'view test entity',
|
Chris@0
|
388 'administer entity_test content',
|
Chris@0
|
389 'administer comments',
|
Chris@0
|
390 ]);
|
Chris@0
|
391 $this->drupalLogin($limited_user);
|
Chris@0
|
392 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
|
Chris@0
|
393 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0');
|
Chris@0
|
394 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-1');
|
Chris@0
|
395 $this->assertFieldChecked('edit-default-value-input-comment-0-status-2');
|
Chris@0
|
396 // Test comment option change in field settings.
|
Chris@0
|
397 $edit = [
|
Chris@0
|
398 'default_value_input[comment][0][status]' => CommentItemInterface::CLOSED,
|
Chris@18
|
399 'settings[anonymous]' => CommentInterface::ANONYMOUS_MAY_CONTACT,
|
Chris@0
|
400 ];
|
Chris@0
|
401 $this->drupalPostForm(NULL, $edit, t('Save settings'));
|
Chris@0
|
402 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
|
Chris@0
|
403 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0');
|
Chris@0
|
404 $this->assertFieldChecked('edit-default-value-input-comment-0-status-1');
|
Chris@0
|
405 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-2');
|
Chris@18
|
406 $this->assertFieldByName('settings[anonymous]', CommentInterface::ANONYMOUS_MAY_CONTACT);
|
Chris@0
|
407
|
Chris@0
|
408 // Add a new comment-type.
|
Chris@0
|
409 $bundle = CommentType::create([
|
Chris@0
|
410 'id' => 'foobar',
|
Chris@0
|
411 'label' => 'Foobar',
|
Chris@0
|
412 'description' => '',
|
Chris@0
|
413 'target_entity_type_id' => 'entity_test',
|
Chris@0
|
414 ]);
|
Chris@0
|
415 $bundle->save();
|
Chris@0
|
416
|
Chris@0
|
417 // Add a new comment field.
|
Chris@0
|
418 $storage_edit = [
|
Chris@0
|
419 'settings[comment_type]' => 'foobar',
|
Chris@0
|
420 ];
|
Chris@0
|
421 $this->fieldUIAddNewField('entity_test/structure/entity_test', 'foobar', 'Foobar', 'comment', $storage_edit);
|
Chris@0
|
422
|
Chris@0
|
423 // Add a third comment field.
|
Chris@0
|
424 $this->fieldUIAddNewField('entity_test/structure/entity_test', 'barfoo', 'BarFoo', 'comment', $storage_edit);
|
Chris@0
|
425
|
Chris@0
|
426 // Check the field contains the correct comment type.
|
Chris@0
|
427 $field_storage = FieldStorageConfig::load('entity_test.field_barfoo');
|
Chris@0
|
428 $this->assertTrue($field_storage);
|
Chris@0
|
429 $this->assertEqual($field_storage->getSetting('comment_type'), 'foobar');
|
Chris@0
|
430 $this->assertEqual($field_storage->getCardinality(), 1);
|
Chris@0
|
431
|
Chris@0
|
432 // Test the new entity commenting inherits default.
|
Chris@0
|
433 $random_label = $this->randomMachineName();
|
Chris@0
|
434 $data = ['bundle' => 'entity_test', 'name' => $random_label];
|
Chris@0
|
435 $new_entity = EntityTest::create($data);
|
Chris@0
|
436 $new_entity->save();
|
Chris@0
|
437 $this->drupalGet('entity_test/manage/' . $new_entity->id() . '/edit');
|
Chris@0
|
438 $this->assertNoFieldChecked('edit-field-foobar-0-status-1');
|
Chris@0
|
439 $this->assertFieldChecked('edit-field-foobar-0-status-2');
|
Chris@0
|
440 $this->assertNoField('edit-field-foobar-0-status-0');
|
Chris@0
|
441
|
Chris@0
|
442 // @todo Check proper url and form https://www.drupal.org/node/2458323
|
Chris@0
|
443 $this->drupalGet('comment/reply/entity_test/comment/' . $new_entity->id());
|
Chris@0
|
444 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field found.');
|
Chris@0
|
445 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field found.');
|
Chris@0
|
446
|
Chris@0
|
447 // Test removal of comment_body field.
|
Chris@0
|
448 $limited_user = $this->drupalCreateUser([
|
Chris@0
|
449 'administer entity_test fields',
|
Chris@0
|
450 'post comments',
|
Chris@0
|
451 'administer comment fields',
|
Chris@0
|
452 'administer comment types',
|
Chris@12
|
453 'view test entity',
|
Chris@0
|
454 ]);
|
Chris@0
|
455 $this->drupalLogin($limited_user);
|
Chris@0
|
456
|
Chris@0
|
457 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
|
Chris@0
|
458 $this->assertFieldByName('comment_body[0][value]', '', 'Comment body field found.');
|
Chris@0
|
459 $this->fieldUIDeleteField('admin/structure/comment/manage/comment', 'comment.comment.comment_body', 'Comment', 'Comment settings');
|
Chris@0
|
460 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
|
Chris@0
|
461 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment body field not found.');
|
Chris@0
|
462 // Set subject field to autogenerate it.
|
Chris@0
|
463 $edit = ['subject[0][value]' => ''];
|
Chris@0
|
464 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
465 }
|
Chris@0
|
466
|
Chris@0
|
467 /**
|
Chris@0
|
468 * Tests comment fields cannot be added to entity types without integer IDs.
|
Chris@0
|
469 */
|
Chris@0
|
470 public function testsNonIntegerIdEntities() {
|
Chris@0
|
471 // Create a bundle for entity_test_string_id.
|
Chris@0
|
472 entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_string_id');
|
Chris@0
|
473 $limited_user = $this->drupalCreateUser([
|
Chris@0
|
474 'administer entity_test_string_id fields',
|
Chris@0
|
475 ]);
|
Chris@0
|
476 $this->drupalLogin($limited_user);
|
Chris@0
|
477 // Visit the Field UI field add page.
|
Chris@0
|
478 $this->drupalGet('entity_test_string_id/structure/entity_test/fields/add-field');
|
Chris@0
|
479 // Ensure field isn't shown for string IDs.
|
Chris@0
|
480 $this->assertNoOption('edit-new-storage-type', 'comment');
|
Chris@0
|
481 // Ensure a core field type shown.
|
Chris@0
|
482 $this->assertOption('edit-new-storage-type', 'boolean');
|
Chris@0
|
483
|
Chris@0
|
484 // Create a bundle for entity_test_no_id.
|
Chris@0
|
485 entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_no_id');
|
Chris@0
|
486 $this->drupalLogin($this->drupalCreateUser([
|
Chris@0
|
487 'administer entity_test_no_id fields',
|
Chris@0
|
488 ]));
|
Chris@0
|
489 // Visit the Field UI field add page.
|
Chris@0
|
490 $this->drupalGet('entity_test_no_id/structure/entity_test/fields/add-field');
|
Chris@0
|
491 // Ensure field isn't shown for empty IDs.
|
Chris@0
|
492 $this->assertNoOption('edit-new-storage-type', 'comment');
|
Chris@0
|
493 // Ensure a core field type shown.
|
Chris@0
|
494 $this->assertOption('edit-new-storage-type', 'boolean');
|
Chris@0
|
495 }
|
Chris@0
|
496
|
Chris@0
|
497 }
|