Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / comment / tests / src / Functional / CommentTitleTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Functional;
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
33     // The entity fields for name and mail have no meaning if the user is not
34     // Anonymous.
35     $this->assertNull($comment->name->value);
36     $this->assertNull($comment->mail->value);
37
38     // Confirm that the comment was created.
39     $regex = '/<a id="comment-' . $comment->id() . '"(.*?)';
40     $regex .= $comment->comment_body->value . '(.*?)';
41     $regex .= '/s';
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.');
45   }
46
47   /**
48    * Tests markup for comments with populated titles.
49    */
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);
55
56     // Create a node.
57     $this->drupalLogin($this->webUser);
58     $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
59
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);
64
65     // The entity fields for name and mail have no meaning if the user is not
66     // Anonymous.
67     $this->assertNull($comment1->name->value);
68     $this->assertNull($comment1->mail->value);
69
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.");
80   }
81
82 }