CommentTitleTest.php 3.4 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\comment\Functional;
4 5

/**
6 7 8 9
 * Tests to ensure that appropriate and accessible markup is created for comment
 * titles.
 *
 * @group comment
10 11
 */
class CommentTitleTest extends CommentTestBase {
12

13 14 15 16
  /**
   * Tests markup for comments with empty titles.
   */
  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 21 22 23 24 25

    // Set comments to have a subject with preview disabled.
    $this->setCommentPreview(DRUPAL_DISABLED);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(TRUE);

    // 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 31
    $subject_text = $this->randomMachineName();
    $comment_text = $this->randomMachineName();
32
    $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
33 34 35 36 37 38

    // The entity fields for name and mail have no meaning if the user is not
    // Anonymous.
    $this->assertNull($comment->name->value);
    $this->assertNull($comment->mail->value);

39 40 41 42
    // Confirm that the comment was created.
    $regex = '/<a id="comment-' . $comment->id() . '"(.*?)';
    $regex .= $comment->comment_body->value . '(.*?)';
    $regex .= '/s';
43
    $this->assertPattern($regex, 'Comment is created successfully');
44 45 46 47 48 49 50 51 52 53 54 55 56 57
    // Tests that markup is not generated for the comment without header.
    $this->assertNoPattern('|<h3[^>]*></h3>|', 'Comment title H3 element not found when title is an empty string.');
  }

  /**
   * Tests markup for comments with populated titles.
   */
  public function testCommentPopulatedTitles() {
    // Set comments to have a subject with preview disabled.
    $this->setCommentPreview(DRUPAL_DISABLED);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(TRUE);

    // 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 63
    $subject_text = $this->randomMachineName();
    $comment_text = $this->randomMachineName();
64
    $comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
65 66 67 68 69 70

    // The entity fields for name and mail have no meaning if the user is not
    // Anonymous.
    $this->assertNull($comment1->name->value);
    $this->assertNull($comment1->mail->value);

71 72 73 74
    // Confirm that the comment was created.
    $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
    // Tests that markup is created for comment with heading.
    $this->assertPattern('|<h3[^>]*><a[^>]*>' . $subject_text . '</a></h3>|', 'Comment title is rendered in h3 when title populated.');
75 76
    // Tests that the comment's title link is the permalink of the comment.
    $comment_permalink = $this->cssSelect('.permalink');
77
    $comment_permalink = $comment_permalink[0]->getAttribute('href');
78 79 80
    // Tests that the comment's title link contains the url fragment.
    $this->assertTrue(strpos($comment_permalink, '#comment-' . $comment1->id()), "The comment's title link contains the url fragment.");
    $this->assertEqual($comment1->permalink()->toString(), $comment_permalink, "The comment's title has the correct link.");
81
  }
82

83
}