function CommentInterfaceTest::testAutoFilledSubject

Same name in other branches
  1. 9 core/modules/comment/tests/src/Functional/CommentInterfaceTest.php \Drupal\Tests\comment\Functional\CommentInterfaceTest::testAutoFilledSubject()
  2. 8.9.x core/modules/comment/tests/src/Functional/CommentInterfaceTest.php \Drupal\Tests\comment\Functional\CommentInterfaceTest::testAutoFilledSubject()
  3. 11.x core/modules/comment/tests/src/Functional/CommentInterfaceTest.php \Drupal\Tests\comment\Functional\CommentInterfaceTest::testAutoFilledSubject()

Tests that the subject is automatically filled if disabled or left blank.

When the subject field is blank or disabled, the first 29 characters of the comment body are used for the subject. If this would break within a word, then the break is put at the previous word boundary instead.

File

core/modules/comment/tests/src/Functional/CommentInterfaceTest.php, line 230

Class

CommentInterfaceTest
Tests comment user interfaces.

Namespace

Drupal\Tests\comment\Functional

Code

public function testAutoFilledSubject() : void {
    $this->drupalLogin($this->webUser);
    $this->drupalGet('node/' . $this->node
        ->id());
    // Break when there is a word boundary before 29 characters.
    $body_text = 'A quick brown fox jumped over the lazy dog';
    $comment1 = $this->postComment(NULL, $body_text, '', TRUE);
    $this->assertTrue($this->commentExists($comment1), 'Form comment found.');
    $this->assertEquals('A quick brown fox jumped…', $comment1->getSubject());
    // Break at 29 characters where there's no boundary before that.
    $body_text2 = 'AQuickBrownFoxJumpedOverTheLazyDog';
    $comment2 = $this->postComment(NULL, $body_text2, '', TRUE);
    $this->assertEquals('AQuickBrownFoxJumpedOverTheL…', $comment2->getSubject());
    // Make the body field non required.
    $comment_body_field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
    $comment_body_field->setRequired(FALSE)
        ->save();
    // Try to post a comment without any value in body and subject fields.
    $this->drupalGet('node/' . $this->node
        ->id());
    // Ensure that there are no PHP errors or warnings when automatically
    // generating the subject. This occurs when the comment body is empty.
    $comment2 = $this->postComment(NULL, '', '', TRUE);
    $this->assertEquals('(No subject)', $comment2->getSubject());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.