You are here

protected function FlagCountsTest::setUp in Flag 8.4

Overrides FlagKernelTestBase::setUp

File

tests/src/Kernel/FlagCountsTest.php, line 77

Class

FlagCountsTest
Tests the Flag counts API.

Namespace

Drupal\Tests\flag\Kernel

Code

protected function setUp() {
  parent::setUp();
  $this
    ->installSchema('user', 'users_data');

  // Create the anonymous role.
  $this
    ->installConfig([
    'user',
  ]);
  $this->flagCountService = \Drupal::service('flag.count');

  // Create a non-global flag.
  $this->flag = Flag::create([
    'id' => strtolower($this
      ->randomMachineName()),
    'label' => $this
      ->randomString(),
    'global' => FALSE,
    'entity_type' => 'node',
    'bundles' => [
      'article',
    ],
    'flag_type' => 'entity:node',
    'link_type' => 'reload',
    'flagTypeConfig' => [],
    'linkTypeConfig' => [],
  ]);
  $this->flag
    ->save();

  // Create another flag whose flaggings won't show in counts for the flag.
  $this->otherFlag = Flag::create([
    'id' => strtolower($this
      ->randomMachineName()),
    'label' => $this
      ->randomString(),
    'global' => FALSE,
    'entity_type' => 'node',
    'bundles' => [
      'article',
    ],
    'flag_type' => 'entity:node',
    'link_type' => 'reload',
    'flagTypeConfig' => [],
    'linkTypeConfig' => [],
  ]);
  $this->otherFlag
    ->save();

  // Create admin user who may flag everything.
  $this->adminUser = $this
    ->createUser([
    'administer flags',
  ]);

  // Create another admin user who won't show in counts for the user.
  $this->otherAdminUser = $this
    ->createUser([
    'administer flags',
  ]);

  // Grant the anonymous role permission to flag.

  /* @var \Drupal\user\RoleInterface $anonymous_role */
  $anonymous_role = Role::load(Role::ANONYMOUS_ID);
  $anonymous_role
    ->grantPermission('flag ' . $this->flag
    ->id());
  $anonymous_role
    ->grantPermission('unflag ' . $this->flag
    ->id());
  $anonymous_role
    ->save();

  // Get the anonymous user.
  $this->anonymousUser = User::getAnonymousUser();
  $article = NodeType::create([
    'type' => 'article',
  ]);
  $article
    ->save();

  // Create nodes to flag.
  $this->node = Node::create([
    'type' => 'article',
    'title' => $this
      ->randomMachineName(8),
  ]);
  $this->node
    ->save();
  $this->otherNode = Node::create([
    'type' => 'article',
    'title' => $this
      ->randomMachineName(8),
  ]);
  $this->otherNode
    ->save();
}