3 namespace Drupal\Tests\comment\Functional;
6 * Tests to ensure that appropriate and accessible markup is created for comment
11 class CommentTitleTest extends CommentTestBase {
13 * Tests markup for comments with empty titles.
15 public function testCommentEmptyTitles() {
16 // Installs module that sets comments to an empty string.
17 \Drupal::service('module_installer')->install(['comment_empty_title_test']);
19 // Set comments to have a subject with preview disabled.
20 $this->setCommentPreview(DRUPAL_DISABLED);
21 $this->setCommentForm(TRUE);
22 $this->setCommentSubject(TRUE);
25 $this->drupalLogin($this->webUser);
26 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
28 // Post comment #1 and verify that h3's are not rendered.
29 $subject_text = $this->randomMachineName();
30 $comment_text = $this->randomMachineName();
31 $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
33 // The entity fields for name and mail have no meaning if the user is not
35 $this->assertNull($comment->name->value);
36 $this->assertNull($comment->mail->value);
38 // Confirm that the comment was created.
39 $regex = '/<a id="comment-' . $comment->id() . '"(.*?)';
40 $regex .= $comment->comment_body->value . '(.*?)';
42 $this->assertPattern($regex, 'Comment is created successfully');
43 // Tests that markup is not generated for the comment without header.
44 $this->assertNoPattern('|<h3[^>]*></h3>|', 'Comment title H3 element not found when title is an empty string.');
48 * Tests markup for comments with populated titles.
50 public function testCommentPopulatedTitles() {
51 // Set comments to have a subject with preview disabled.
52 $this->setCommentPreview(DRUPAL_DISABLED);
53 $this->setCommentForm(TRUE);
54 $this->setCommentSubject(TRUE);
57 $this->drupalLogin($this->webUser);
58 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
60 // Post comment #1 and verify that title is rendered in h3.
61 $subject_text = $this->randomMachineName();
62 $comment_text = $this->randomMachineName();
63 $comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
65 // The entity fields for name and mail have no meaning if the user is not
67 $this->assertNull($comment1->name->value);
68 $this->assertNull($comment1->mail->value);
70 // Confirm that the comment was created.
71 $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
72 // Tests that markup is created for comment with heading.
73 $this->assertPattern('|<h3[^>]*><a[^>]*>' . $subject_text . '</a></h3>|', 'Comment title is rendered in h3 when title populated.');
74 // Tests that the comment's title link is the permalink of the comment.
75 $comment_permalink = $this->cssSelect('.permalink');
76 $comment_permalink = $comment_permalink[0]->getAttribute('href');
77 // Tests that the comment's title link contains the url fragment.
78 $this->assertTrue(strpos($comment_permalink, '#comment-' . $comment1->id()), "The comment's title link contains the url fragment.");
79 $this->assertEqual($comment1->permalink()->toString(), $comment_permalink, "The comment's title has the correct link.");