function CommentAdminViewTest::setUp

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

Overrides ViewsKernelTestBase::setUp

File

core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php, line 48

Class

CommentAdminViewTest
Tests comment admin view filters.

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 user 1 so that the user created later in the test has a different
    // user ID.
    // @todo Remove in https://www.drupal.org/node/540008.
    User::create([
        'uid' => 1,
        'name' => 'user1',
    ])->save();
    // Enable another language.
    ConfigurableLanguage::createFromLangcode('ur')->save();
    // Rebuild the container to update the default language container variable.
    $this->container
        ->get('kernel')
        ->rebuildContainer();
    // Create an anonymous user.
    $storage = \Drupal::entityTypeManager()->getStorage('user');
    // Insert a row for the anonymous user.
    $storage->create([
        'uid' => 0,
        'name' => '',
        'status' => 0,
    ])
        ->save();
    // Created admin role.
    $admin_role = Role::create([
        'id' => 'admin',
        'permissions' => [
            'administer comments',
            'skip comment approval',
        ],
        'label' => 'Admin',
    ]);
    $admin_role->save();
    // Create the admin user.
    $this->adminUser = User::create([
        'name' => $this->randomMachineName(),
        'roles' => [
            $admin_role->id(),
        ],
    ]);
    $this->adminUser
        ->save();
    // Create a comment type.
    CommentType::create([
        'id' => 'comment',
        'label' => 'Default comments',
        'target_entity_type_id' => 'entity_test',
        'description' => 'Default comment field',
    ])->save();
    // Create a commented entity.
    $entity = EntityTest::create();
    $entity->name->value = $this->randomMachineName();
    $entity->save();
    // Create some comments.
    $comment = Comment::create([
        'subject' => 'My comment title',
        'uid' => $this->adminUser
            ->id(),
        'entity_type' => 'entity_test',
        'field_name' => 'comment',
        'comment_type' => 'comment',
        'status' => 1,
        'entity_id' => $entity->id(),
    ]);
    $comment->save();
    $this->comments[] = $comment;
    $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',
        'comment_type' => 'comment',
        'created' => 123456,
        'status' => 1,
        'entity_id' => $entity->id(),
    ]);
    $comment_anonymous->save();
    $this->comments[] = $comment_anonymous;
}

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