annotate core/modules/comment/tests/src/Functional/CommentTitleTest.php @ 19:fa3358dc1485 tip

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