function CommentViewsFieldAccessTest::testCommentFields

Same name in other branches
  1. 9 core/modules/comment/tests/src/Kernel/Views/CommentViewsFieldAccessTest.php \Drupal\Tests\comment\Kernel\Views\CommentViewsFieldAccessTest::testCommentFields()
  2. 8.9.x core/modules/comment/tests/src/Kernel/Views/CommentViewsFieldAccessTest.php \Drupal\Tests\comment\Kernel\Views\CommentViewsFieldAccessTest::testCommentFields()
  3. 11.x core/modules/comment/tests/src/Kernel/Views/CommentViewsFieldAccessTest.php \Drupal\Tests\comment\Kernel\Views\CommentViewsFieldAccessTest::testCommentFields()

Check access for comment fields.

File

core/modules/comment/tests/src/Kernel/Views/CommentViewsFieldAccessTest.php, line 37

Class

CommentViewsFieldAccessTest
Tests base field access in Views for the comment entity.

Namespace

Drupal\Tests\comment\Kernel\Views

Code

public function testCommentFields() : void {
    $user = User::create([
        'name' => 'test user',
    ]);
    $user->save();
    $host = EntityTest::create([
        'name' => $this->randomString(),
    ]);
    $host->save();
    $comment = Comment::create([
        'subject' => 'My comment title',
        'uid' => $user->id(),
        'entity_type' => 'entity_test',
        'field_name' => 'comment',
        'entity_id' => $host->id(),
        'comment_type' => 'entity_test',
    ]);
    $comment->save();
    $comment_anonymous = Comment::create([
        'subject' => 'Anonymous comment title',
        'uid' => 0,
        'name' => 'anonymous',
        'mail' => 'test@example.com',
        'homepage' => 'https://example.com',
        'entity_type' => 'entity_test',
        'field_name' => 'comment',
        'entity_id' => $host->id(),
        'comment_type' => 'entity_test',
        'created' => 123456,
        'status' => 1,
    ]);
    $comment_anonymous->save();
    // @todo Expand the test coverage in https://www.drupal.org/node/2464635
    $this->assertFieldAccess('comment', 'cid', $comment->id());
    $this->assertFieldAccess('comment', 'cid', $comment_anonymous->id());
    $this->assertFieldAccess('comment', 'uuid', $comment->uuid());
    $this->assertFieldAccess('comment', 'subject', 'My comment title');
    $this->assertFieldAccess('comment', 'subject', 'Anonymous comment title');
    $this->assertFieldAccess('comment', 'name', 'anonymous');
    $this->assertFieldAccess('comment', 'mail', 'test@example.com');
    $this->assertFieldAccess('comment', 'homepage', 'https://example.com');
    $this->assertFieldAccess('comment', 'uid', $user->getAccountName());
    // $this->assertFieldAccess('comment', 'created', \Drupal::service('date.formatter')->format(123456));
    // $this->assertFieldAccess('comment', 'changed', \Drupal::service('date.formatter')->format(\Drupal::time()->getRequestTime()));
    $this->assertFieldAccess('comment', 'status', 'On');
}

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