function CommentFieldsTest::testCommentFieldLinksNonDefaultName

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

Tests link building with non-default comment field names.

File

core/modules/comment/tests/src/Functional/CommentFieldsTest.php, line 101

Class

CommentFieldsTest
Tests fields on comments.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentFieldLinksNonDefaultName() {
    $this->drupalCreateContentType([
        'type' => 'test_node_type',
    ]);
    $this->addDefaultCommentField('node', 'test_node_type', 'comment2');
    $web_user2 = $this->drupalCreateUser([
        'access comments',
        'post comments',
        'create article content',
        'edit own comments',
        'skip comment approval',
        'access content',
    ]);
    // Create a sample node.
    $node = $this->drupalCreateNode([
        'title' => 'Baloney',
        'type' => 'test_node_type',
    ]);
    // Go to the node first so that webuser2 see new comments.
    $this->drupalLogin($web_user2);
    $this->drupalGet($node->toUrl());
    $this->drupalLogout();
    // Test that buildCommentedEntityLinks() does not break when the 'comment'
    // field does not exist. Requires at least one comment.
    $this->drupalLogin($this->webUser);
    $this->postComment($node, 'Here is a comment', '', NULL, 'comment2');
    $this->drupalLogout();
    $this->drupalLogin($web_user2);
    // We want to check the attached drupalSettings of
    // \Drupal\comment\CommentLinkBuilder::buildCommentedEntityLinks. Therefore
    // we need a node listing, let's use views for that.
    $this->container
        ->get('module_installer')
        ->install([
        'views',
    ], TRUE);
    // We also need a router rebuild, as the router is lazily rebuild in the
    // module installer.
    \Drupal::service('router.builder')->rebuild();
    $this->drupalGet('node');
    $link_info = $this->getDrupalSettings()['comment']['newCommentsLinks']['node']['comment2']['2'];
    $this->assertIdentical($link_info['new_comment_count'], 1);
    $this->assertIdentical($link_info['first_new_comment_link'], $node->toUrl('canonical', [
        'fragment' => 'new',
    ])
        ->toString());
}

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