You are here

protected function CommentAdminViewTest::setUp in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php \Drupal\Tests\comment\Kernel\Views\CommentAdminViewTest::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/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',
    ],
  ]);
  $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;
}