View source
<?php
namespace Drupal\Tests\flag\Kernel;
use Drupal\flag\Entity\Flag;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
class FlagServiceTest extends FlagKernelTestBase {
public function testFlagServiceGetFlag() {
$flag = Flag::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
'entity_type' => 'node',
'bundles' => [
'article',
],
'flag_type' => 'entity:node',
'link_type' => 'reload',
'flagTypeConfig' => [],
'linkTypeConfig' => [],
]);
$flag
->save();
$user_with_access = $this
->createUser([
'flag ' . $flag
->id(),
]);
$result = $this->flagService
->getAllFlags('node', 'article');
$this
->assertIdentical(count($result), 1, 'Found flag type');
$this
->assertEquals([
$flag
->id(),
], array_keys($result));
}
public function testFlagServiceFlagExceptions() {
$not_article = NodeType::create([
'type' => 'not_article',
]);
$not_article
->save();
$account = $this
->createUser();
$session_id = 'anonymouse user 1 session_id';
$flag = Flag::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
'entity_type' => 'node',
'bundles' => [
'article',
],
'flag_type' => 'entity:node',
'link_type' => 'reload',
'flagTypeConfig' => [],
'linkTypeConfig' => [],
]);
$flag
->save();
try {
$this->flagService
->flag($flag, $account, $account, $session_id);
$this
->fail("The exception was not thrown.");
} catch (\LogicException $e) {
$this
->pass("The flag() method throws an exception when the flag does not apply to the entity type of the flaggable entity.");
}
$wrong_node = Node::create([
'type' => 'not_article',
'title' => $this
->randomMachineName(8),
]);
$wrong_node
->save();
try {
$this->flagService
->flag($flag, $wrong_node, $account, $session_id);
$this
->fail("The exception was not thrown.");
} catch (\LogicException $e) {
$this
->pass("The flag() method throws an exception when the flag does not apply to the bundle of the flaggable entity.");
}
$flaggable_node = Node::create([
'type' => 'article',
'title' => $this
->randomMachineName(8),
]);
$flaggable_node
->save();
$this->flagService
->flag($flag, $flaggable_node, $account, $session_id);
try {
$this->flagService
->flag($flag, $flaggable_node, $account, $session_id);
$this
->fail("The exception was not thrown.");
} catch (\LogicException $e) {
$this
->pass("The flag() method throws an exception when the flaggable entity is already flagged by the user with the flag.");
}
try {
$this->flagService
->flag($flag, $flaggable_node, $account);
$this
->fail("The exception was not thrown.");
} catch (\LogicException $e) {
$this
->pass("The flag() method throws an exception when a non-global flag is associated with a poorly specified anonymous user.");
}
try {
$this->flagService
->unflag($flag, $account, $account, $session_id);
$this
->fail("The exception was not thrown.");
} catch (\LogicException $e) {
$this
->pass("The unflag() method throws an exception when the flag does not apply to the entity type of the flaggable entity.");
}
try {
$this->flagService
->unflag($flag, $wrong_node, $account, $session_id);
$this
->fail("The exception was not thrown.");
} catch (\LogicException $e) {
$this
->pass("The unflag() method throws an exception when the flag does not apply to the bundle of the flaggable entity.");
}
$unflagged_node = Node::create([
'type' => 'article',
'title' => $this
->randomMachineName(8),
]);
$unflagged_node
->save();
try {
$this->flagService
->unflag($flag, $unflagged_node, $account, $session_id);
$this
->fail("The exception was not thrown.");
} catch (\LogicException $e) {
$this
->pass("The unflag() method throws an exception when the flaggable entity is not flagged by the user with the flag.");
}
try {
$this->flagService
->unflag($flag, $unflagged_node, $account);
$this
->fail("The exception was not thrown.");
} catch (\LogicException $e) {
$this
->pass("The unflag() method throws an exception when a non-global flag is associated with a poorly specified anonymous user.");
}
try {
$this->flagService
->unflag($flag, $flaggable_node, $account, $session_id);
$this
->pass('The unflag() method throws no exception when the flaggable entity and user is correct');
} catch (\LogicException $e) {
$this
->fail('The unfag() method threw an exception where processing a valid unflag request.');
}
}
public function testFlagServiceGetFlaggingUsers() {
$accounts = [
$this
->createUser(),
$this
->createUser(),
];
$flag = Flag::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
'entity_type' => 'node',
'bundles' => [
'article',
],
'flag_type' => 'entity:node',
'link_type' => 'reload',
'flagTypeConfig' => [],
'linkTypeConfig' => [],
]);
$flag
->save();
$flaggable_node = Node::create([
'type' => 'article',
'title' => $this
->randomMachineName(8),
]);
$flaggable_node
->save();
foreach ($accounts as $account) {
$this->flagService
->flag($flag, $flaggable_node, $account);
}
$flagging_users = $this->flagService
->getFlaggingUsers($flaggable_node, $flag);
$this
->assertTrue(is_array($flagging_users), "The method getFlaggingUsers() returns an array.");
foreach ($accounts as $account) {
foreach ($flagging_users as $flagging_user) {
if ($flagging_user
->id() == $account
->id()) {
break;
}
}
$this
->assertTrue($flagging_user
->id() == $account
->id(), "The returned array has the flagged account included.");
}
}
public function testGlobalFlaggingRetrieval() {
$flag = Flag::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
'entity_type' => 'node',
'bundles' => [
'article',
],
'flag_type' => 'entity:node',
'link_type' => 'reload',
'flagTypeConfig' => [],
'linkTypeConfig' => [],
'global' => TRUE,
]);
$flag
->save();
$flaggable_node = Node::create([
'type' => 'article',
'title' => $this
->randomMachineName(8),
]);
$flaggable_node
->save();
$account_1 = $this
->createUser();
$account_2 = $this
->createUser();
$this->flagService
->flag($flag, $flaggable_node, $account_1);
$flaggings = $this->flagService
->getAllEntityFlaggings($flaggable_node);
$this
->assertEquals(1, count($flaggings));
$flaggings = $this->flagService
->getAllEntityFlaggings($flaggable_node, $account_1);
$this
->assertEquals(1, count($flaggings));
$flaggings = $this->flagService
->getAllEntityFlaggings($flaggable_node, $account_2);
$this
->assertEquals(1, count($flaggings));
$flag = Flag::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
'entity_type' => 'node',
'bundles' => [
'article',
],
'flag_type' => 'entity:node',
'link_type' => 'reload',
'flagTypeConfig' => [],
'linkTypeConfig' => [],
'global' => FALSE,
]);
$flag
->save();
$this->flagService
->flag($flag, $flaggable_node, $account_2);
$flaggings = $this->flagService
->getAllEntityFlaggings($flaggable_node);
$this
->assertEquals(2, count($flaggings));
$flaggings = $this->flagService
->getAllEntityFlaggings($flaggable_node, $account_2);
$this
->assertEquals(2, count($flaggings));
$flaggings = $this->flagService
->getAllEntityFlaggings($flaggable_node, $account_1);
$this
->assertEquals(1, count($flaggings));
}
}