function CommentUserNameTest::setUp

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

Overrides ViewsKernelTestBase::setUp

File

core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php, line 35

Class

CommentUserNameTest
Tests comment user name field.

Namespace

Drupal\Tests\comment\Kernel\Views

Code

protected function setUp($import_test_views = TRUE) : void {
    parent::setUp($import_test_views);
    $this->installEntitySchema('user');
    $this->installEntitySchema('comment');
    $this->installEntitySchema('entity_test');
    // Create the anonymous role.
    $this->installConfig([
        'user',
    ]);
    // Create an anonymous user.
    $storage = \Drupal::entityTypeManager()->getStorage('user');
    // Insert a row for the anonymous user.
    $storage->create([
        'uid' => 0,
        'name' => '',
        'status' => 0,
    ])
        ->save();
    $admin_role = Role::create([
        'id' => 'admin',
        'permissions' => [
            'administer comments',
            'access user profiles',
        ],
        'label' => 'Admin',
    ]);
    $admin_role->save();
    
    /** @var \Drupal\user\RoleInterface $anonymous_role */
    $anonymous_role = Role::load(Role::ANONYMOUS_ID);
    $anonymous_role->grantPermission('access comments');
    $anonymous_role->save();
    $this->adminUser = User::create([
        'name' => $this->randomMachineName(),
        'roles' => [
            $admin_role->id(),
        ],
    ]);
    $this->adminUser
        ->save();
    $host = EntityTest::create([
        'name' => $this->randomString(),
    ]);
    $host->save();
    // Create some comments.
    $comment = Comment::create([
        'subject' => 'My comment title',
        'uid' => $this->adminUser
            ->id(),
        'name' => $this->adminUser
            ->label(),
        'entity_type' => 'entity_test',
        'field_name' => 'comment',
        'entity_id' => $host->id(),
        'comment_type' => 'entity_test',
        'status' => 1,
    ]);
    $comment->save();
    $comment_anonymous = Comment::create([
        'subject' => 'Anonymous comment title',
        'uid' => 0,
        'name' => 'barry',
        '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();
}

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