30d39357bace059bad4f49c09c8700ba4612b78f
[yaffs-website] / web / core / modules / comment / src / Tests / CommentTitleTest.php
1 <?php
2
3 namespace Drupal\comment\Tests;
4
5 /**
6  * Tests to ensure that appropriate and accessible markup is created for comment
7  * titles.
8  *
9  * @group comment
10  */
11 class CommentTitleTest extends CommentTestBase {
12   /**
13    * Tests markup for comments with empty titles.
14    */
15   public function testCommentEmptyTitles() {
16     // Installs module that sets comments to an empty string.
17     \Drupal::service('module_installer')->install(['comment_empty_title_test']);
18
19     // Set comments to have a subject with preview disabled.
20     $this->setCommentPreview(DRUPAL_DISABLED);
21     $this->setCommentForm(TRUE);
22     $this->setCommentSubject(TRUE);
23
24     // Create a node.
25     $this->drupalLogin($this->webUser);
26     $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
27
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);
32     // Confirm that the comment was created.
33     $regex = '/<a id="comment-' . $comment->id() . '"(.*?)';
34     $regex .= $comment->comment_body->value . '(.*?)';
35     $regex .= '/s';
36     $this->assertPattern($regex, 'Comment is created successfully');
37     // Tests that markup is not generated for the comment without header.
38     $this->assertNoPattern('|<h3[^>]*></h3>|', 'Comment title H3 element not found when title is an empty string.');
39   }
40
41   /**
42    * Tests markup for comments with populated titles.
43    */
44   public function testCommentPopulatedTitles() {
45     // Set comments to have a subject with preview disabled.
46     $this->setCommentPreview(DRUPAL_DISABLED);
47     $this->setCommentForm(TRUE);
48     $this->setCommentSubject(TRUE);
49
50     // Create a node.
51     $this->drupalLogin($this->webUser);
52     $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
53
54     // Post comment #1 and verify that title is rendered in h3.
55     $subject_text = $this->randomMachineName();
56     $comment_text = $this->randomMachineName();
57     $comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
58     // Confirm that the comment was created.
59     $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
60     // Tests that markup is created for comment with heading.
61     $this->assertPattern('|<h3[^>]*><a[^>]*>' . $subject_text . '</a></h3>|', 'Comment title is rendered in h3 when title populated.');
62     // Tests that the comment's title link is the permalink of the comment.
63     $comment_permalink = $this->cssSelect('.permalink');
64     $comment_permalink = (string) $comment_permalink[0]['href'];
65     // Tests that the comment's title link contains the url fragment.
66     $this->assertTrue(strpos($comment_permalink, '#comment-' . $comment1->id()), "The comment's title link contains the url fragment.");
67     $this->assertEqual($comment1->permalink()->toString(), $comment_permalink, "The comment's title has the correct link.");
68   }
69
70 }