public function FlagCountsTest::testEntityDeletion in Flag 8.4
Tests flaggings and counts are deleted when its entity is deleted.
File
- tests/
src/ Kernel/ FlagCountsTest.php, line 270
Class
- FlagCountsTest
- Tests the Flag counts API.
Namespace
Drupal\Tests\flag\KernelCode
public function testEntityDeletion() {
// 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 entities.
$article1
->delete();
$article2
->delete();
// The list of all flaggings MUST now be empty.
$flaggings_after = $this
->getFlagFlaggings($this->flag);
$this
->assert(empty($flaggings_after), 'The flaggings were removed, when the flag was deleted');
// Confirm the counts have been removed.
$article1_count_after = $this->flagCountService
->getEntityFlagCounts($article1);
$this
->assertTrue(empty($article1_count_after), 'Article1 counts has been removed.');
$article2_count_after = $this->flagCountService
->getEntityFlagCounts($article2);
$this
->assertTrue(empty($article2_count_after), 'Article2 counts has been removed.');
}