You are here

public function FlagCountsTest::testAnonymousFlagCount in Flag 8.4

Tests the differing counting rules between global and non-global flags.

Global flags count all users as if they were are single user. Non-global flags uniquely identify anonymous users by session_id.

File

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

Class

FlagCountsTest
Tests the Flag counts API.

Namespace

Drupal\Tests\flag\Kernel

Code

public function testAnonymousFlagCount() {

  // Consider two distinct anonymous users.
  $anon1_session_id = 'Unknown user 1';
  $anon2_session_id = 'Unknown user 2';

  // Both users flag the node - using a non-global flag.
  $this->flagService
    ->flag($this->flag, $this->node, $this->anonymousUser, $anon1_session_id);
  $this->flagService
    ->flag($this->flag, $this->node, $this->anonymousUser, $anon2_session_id);

  // For non-global flags anonymous users can uniquely identified by
  // session_id.
  $anon1_count = $this->flagCountService
    ->getUserFlagFlaggingCount($this->flag, $this->anonymousUser, $anon1_session_id);
  $this
    ->assertEqual($anon1_count, 1, "getUserFlagFlaggingCount() counts only the first user.");
  $anon2_count = $this->flagCountService
    ->getUserFlagFlaggingCount($this->flag, $this->anonymousUser, $anon2_session_id);
  $this
    ->assertEqual($anon2_count, 1, "getUserFlagFlaggingCount() counts only the second user.");

  // Switch to a global flag, the accounting rules.
  $this->flag
    ->setGlobal(TRUE);
  $this->flag
    ->save();

  // Despite being a global flag, queries about specific anonymous users can
  // still be made.
  $rejected_count = $this->flagCountService
    ->getUserFlagFlaggingCount($this->flag, $this->anonymousUser, $anon1_session_id);
  $this
    ->assertEqual($rejected_count, 1, "getUserFlagFlaggingCount() ignores the session id.");
}