You are here

public function FlagCountsTest::testFlagCounts in Flag 8.4

Tests that counts are kept in sync and can be retrieved.

File

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

Class

FlagCountsTest
Tests the Flag counts API.

Namespace

Drupal\Tests\flag\Kernel

Code

public function testFlagCounts() {

  // Flag the node with the flag we're counting and the other flag.
  $this->flagService
    ->flag($this->flag, $this->node, $this->adminUser);
  $this->flagService
    ->flag($this->flag, $this->node, $this->otherAdminUser);
  $this->flagService
    ->flag($this->otherFlag, $this->node, $this->adminUser);

  // Flag the other node with both flags.
  $this->flagService
    ->flag($this->flag, $this->otherNode, $this->adminUser);
  $this->flagService
    ->flag($this->otherFlag, $this->otherNode, $this->adminUser);

  // Check each of the count API functions.
  // Get the count of flaggings for the flag. The other flag also has
  // flaggings, which should not be included in the count.
  $flag_get_entity_flag_counts = $this->flagCountService
    ->getFlagFlaggingCount($this->flag);
  $this
    ->assertEqual($flag_get_entity_flag_counts, 3, "getFlagFlaggingCount() returns the expected count.");

  // Get the counts of all flaggings on the entity. The other node is also
  // flagged, but should not be included in the count.
  $flag_get_counts = $this->flagCountService
    ->getEntityFlagCounts($this->node);
  $this
    ->assertEqual($flag_get_counts[$this->flag
    ->id()], 2, "getEntityFlagCounts() returns the expected count.");
  $this
    ->assertEqual($flag_get_counts[$this->otherFlag
    ->id()], 1, "getEntityFlagCounts() returns the expected count.");

  // Get the number of entities for the flag. Two users have flagged one node
  // with the flag, but that should count only once.
  $flag_get_flag_counts = $this->flagCountService
    ->getFlagEntityCount($this->flag);
  $this
    ->assertEqual($flag_get_flag_counts, 2, "getFlagEntityCount() returns the expected count.");

  // Unflag everything with the main flag.
  $this->flagService
    ->unflagAllByFlag($this->flag);
  $flag_get_flag_counts = $this->flagCountService
    ->getFlagEntityCount($this->flag);
  $this
    ->assertEqual($flag_get_flag_counts, 0, "getFlagEntityCount() on reset flag returns the expected count.");
}