CommentTitleTest.php 3.5 KB

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