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