function CommentUserNameTest::setUp
Same name and namespace in other branches
- 9 core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php \Drupal\Tests\comment\Kernel\Views\CommentUserNameTest::setUp()
- 8.9.x core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php \Drupal\Tests\comment\Kernel\Views\CommentUserNameTest::setUp()
- 10 core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php \Drupal\Tests\comment\Kernel\Views\CommentUserNameTest::setUp()
Parameters
bool $import_test_views: Should the views specified on the test class be imported. If you need to setup some additional stuff, like fields, you need to call false and then call createTestViews for your own.
Overrides ViewsKernelTestBase::setUp
File
-
core/
modules/ comment/ tests/ src/ Kernel/ Views/ CommentUserNameTest.php, line 38
Class
- CommentUserNameTest
- Tests comment user name field.
Namespace
Drupal\Tests\comment\Kernel\ViewsCode
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' => [
'view test entity',
'administer comments',
'access user profiles',
'access comments',
],
'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();
$commentType = CommentType::create([
'id' => 'entity_test_comment',
'label' => 'Entity Test Comment',
'target_entity_type_id' => 'entity_test',
]);
$commentType->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_comment',
'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_comment',
'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.