You are here

protected function CommentUserNameTest::setUp in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php \Drupal\Tests\comment\Kernel\Views\CommentUserNameTest::setUp()
  2. 9 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 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();
}