You are here

public function FlagCountsTest::testFlagDeletion in Flag 8.4

Tests flaggings are deleted and counts are removed when a flag is deleted.

File

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

Class

FlagCountsTest
Tests the Flag counts API.

Namespace

Drupal\Tests\flag\Kernel

Code

public function testFlagDeletion() {

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

  // Create a second article.
  $article2 = Node::create([
    'type' => 'article',
    'title' => $this
      ->randomMachineName(8),
  ]);
  $article2
    ->save();

  // Flag both.
  $this->flagService
    ->flag($this->flag, $article1, $this->adminUser);
  $this->flagService
    ->flag($this->flag, $article2, $this->adminUser);

  // Confirm the counts have been incremented.
  $article1_count_before = $this->flagCountService
    ->getEntityFlagCounts($article1);
  $this
    ->assertEqual($article1_count_before[$this->flag
    ->id()], 1, 'The article1 has been flagged.');
  $article2_count_before = $this->flagCountService
    ->getEntityFlagCounts($article2);
  $this
    ->assertEqual($article2_count_before[$this->flag
    ->id()], 1, 'The article2 has been flagged.');

  // Confirm the flagging have been created.
  $flaggings_before = $this
    ->getFlagFlaggings($this->flag);
  $this
    ->assertEqual(count($flaggings_before), 2, 'There are two flaggings associated with the flag');

  // Delete the flag.
  $this->flag
    ->delete();

  // The list of all flaggings MUST now be empty.
  $flaggings_after = $this
    ->getFlagFlaggings($this->flag);
  $this
    ->assertEmpty($flaggings_after, 'The flaggings were removed, when the flag was deleted');

  // The flag id is now stale, so instead of searching for the flag in the
  // count array as before we require the entire array should be empty.
  $article1_counts_after = $this->flagCountService
    ->getEntityFlagCounts($article1);
  $this
    ->assertEmpty($article1_counts_after, 'Article1 counts has been removed.');
  $article2_counts_after = $this->flagCountService
    ->getEntityFlagCounts($article2);
  $this
    ->assertEmpty($article2_counts_after, 'Article2 counts has been removed.');
}