View source
<?php
namespace Drupal\Tests\flag\Kernel;
use Drupal\flag\Entity\Flag;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class FlagCountsTest extends FlagKernelTestBase {
protected $flag;
protected $otherFlag;
protected $node;
protected $otherNode;
protected $flagCountService;
protected $adminUser;
protected $otherAdminUser;
protected $anonymousUser;
protected function setUp() {
parent::setUp();
$this
->installSchema('user', 'users_data');
$this
->installConfig([
'user',
]);
$this->flagCountService = \Drupal::service('flag.count');
$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();
$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();
$this->adminUser = $this
->createUser([
'administer flags',
]);
$this->otherAdminUser = $this
->createUser([
'administer flags',
]);
$anonymous_role = Role::load(Role::ANONYMOUS_ID);
$anonymous_role
->grantPermission('flag ' . $this->flag
->id());
$anonymous_role
->grantPermission('unflag ' . $this->flag
->id());
$anonymous_role
->save();
$this->anonymousUser = User::getAnonymousUser();
$article = NodeType::create([
'type' => 'article',
]);
$article
->save();
$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();
}
public function testFlagCounts() {
$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);
$this->flagService
->flag($this->flag, $this->otherNode, $this->adminUser);
$this->flagService
->flag($this->otherFlag, $this->otherNode, $this->adminUser);
$flag_get_entity_flag_counts = $this->flagCountService
->getFlagFlaggingCount($this->flag);
$this
->assertEqual($flag_get_entity_flag_counts, 3, "getFlagFlaggingCount() returns the expected 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.");
$flag_get_flag_counts = $this->flagCountService
->getFlagEntityCount($this->flag);
$this
->assertEqual($flag_get_flag_counts, 2, "getFlagEntityCount() returns the expected count.");
$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.");
}
public function testAnonymousFlagCount() {
$anon1_session_id = 'Unknown user 1';
$anon2_session_id = 'Unknown user 2';
$this->flagService
->flag($this->flag, $this->node, $this->anonymousUser, $anon1_session_id);
$this->flagService
->flag($this->flag, $this->node, $this->anonymousUser, $anon2_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.");
$this->flag
->setGlobal(TRUE);
$this->flag
->save();
$rejected_count = $this->flagCountService
->getUserFlagFlaggingCount($this->flag, $this->anonymousUser, $anon1_session_id);
$this
->assertEqual($rejected_count, 1, "getUserFlagFlaggingCount() ignores the session id.");
}
public function testFlagDeletion() {
$article1 = Node::create([
'type' => 'article',
'title' => $this
->randomMachineName(8),
]);
$article1
->save();
$article2 = Node::create([
'type' => 'article',
'title' => $this
->randomMachineName(8),
]);
$article2
->save();
$this->flagService
->flag($this->flag, $article1, $this->adminUser);
$this->flagService
->flag($this->flag, $article2, $this->adminUser);
$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.');
$flaggings_before = $this
->getFlagFlaggings($this->flag);
$this
->assertEqual(count($flaggings_before), 2, 'There are two flaggings associated with the flag');
$this->flag
->delete();
$flaggings_after = $this
->getFlagFlaggings($this->flag);
$this
->assertEmpty($flaggings_after, 'The flaggings were removed, when the flag was deleted');
$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.');
}
public function testEntityDeletion() {
$article1 = Node::create([
'type' => 'article',
'title' => $this
->randomMachineName(8),
]);
$article1
->save();
$article2 = Node::create([
'type' => 'article',
'title' => $this
->randomMachineName(8),
]);
$article2
->save();
$this->flagService
->flag($this->flag, $article1, $this->adminUser);
$this->flagService
->flag($this->flag, $article2, $this->adminUser);
$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.');
$flaggings_before = $this
->getFlagFlaggings($this->flag);
$this
->assertEqual(count($flaggings_before), 2, 'There are two flaggings associated with the flag');
$article1
->delete();
$article2
->delete();
$flaggings_after = $this
->getFlagFlaggings($this->flag);
$this
->assert(empty($flaggings_after), 'The flaggings were removed, when the flag was deleted');
$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.');
}
public function testUserDeletion() {
$auth_user = $this
->createUser();
$user_flag = Flag::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
'entity_type' => 'user',
'flag_type' => 'entity:user',
'link_type' => 'reload',
'flagTypeConfig' => [],
'linkTypeConfig' => [],
]);
$user_flag
->save();
$article = Node::create([
'type' => 'article',
'title' => $this
->randomMachineName(8),
]);
$article
->save();
$this->flagService
->flag($user_flag, $auth_user, $this->adminUser);
$this->flagService
->flag($this->flag, $article, $auth_user);
$user_before_count = $this->flagCountService
->getEntityFlagCounts($auth_user);
$this
->assertEqual($user_before_count[$user_flag
->id()], 1, 'The user has been flagged.');
$article_count_before = $this->flagCountService
->getEntityFlagCounts($article);
$this
->assertEqual($article_count_before[$this->flag
->id()], 1, 'The article has been flagged by the user.');
$auth_user
->delete();
$flaggings_after = $this
->getFlagFlaggings($user_flag);
$this
->assertEmpty($flaggings_after, 'The user flaggings were removed when the user was deleted.');
$flaggings_after = $this
->getFlagFlaggings($this->flag);
$this
->assert(empty($flaggings_after), 'The node flaggings were removed when the user was deleted');
}
}