View source
<?php
namespace Drupal\Tests\flag\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\flag\Entity\Flag;
use Drupal\flag\Plugin\Action\DeleteFlaggingAction;
class FlagActionTest extends FlagKernelTestBase {
protected $account;
protected $admin;
protected $entityTypeManager;
public static $modules = [
'entity_test',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('entity_test');
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->admin = $this
->createUser();
$this->account = $this
->createUser();
$switcher = $this->container
->get('account_switcher');
$switcher
->switchTo($this->account);
}
public function testFlagActionsCreation() {
$selfies_flag = Flag::create([
'id' => 'selfies',
'label' => $this
->randomString(),
'entity_type' => 'user',
'flag_type' => 'entity:user',
'link_type' => 'reload',
'flagTypeConfig' => [],
'linkTypeConfig' => [],
]);
$selfies_flag
->save();
$flag_action = $this->entityTypeManager
->getStorage('action')
->load('flag_action.selfies_flag');
$this
->assertEquals('flag_action.selfies_flag', $flag_action
->id());
$unflag_action = $this->entityTypeManager
->getStorage('action')
->load('flag_action.selfies_unflag');
$this
->assertEquals('flag_action.selfies_unflag', $unflag_action
->id());
$selfies_flag
->delete();
$this->entityTypeManager
->getStorage('action')
->resetCache();
$this
->assertNull($this->entityTypeManager
->getStorage('action')
->load('flag_action.selfies_flag'));
$this
->assertNull($this->entityTypeManager
->getStorage('action')
->load('flag_action.selfies_unflag'));
}
public function testFlagActions() {
$entity_flag = Flag::create([
'id' => mb_strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
'entity_type' => 'entity_test',
'flag_type' => 'entity:entity_test',
'link_type' => 'reload',
'flagTypeConfig' => [],
'linkTypeConfig' => [],
]);
$entity_flag
->save();
$test_entity = EntityTest::create();
$test_entity
->save();
$action = $this->container
->get('entity_type.manager')
->getStorage('action')
->load('flag_action.' . $entity_flag
->id() . '_flag');
$plugin = $action
->getPlugin();
$plugin
->execute($test_entity);
$this
->assertTrue($entity_flag
->isFlagged($test_entity, $this->account));
$this
->assertFalse($plugin
->access($test_entity, $this->account));
$this
->assertTrue($plugin
->access($test_entity, $this->admin));
$this->entityTypeManager
->getStorage('flagging')
->resetCache();
$action = $this->entityTypeManager
->getStorage('action')
->load('flag_action.' . $entity_flag
->id() . '_unflag');
$plugin = $action
->getPlugin();
$plugin
->execute($test_entity);
$this
->assertFalse($plugin
->access($test_entity, $this->account));
$this
->assertTrue($plugin
->access($test_entity, $this->admin));
}
public function testFlaggingDeleteAction() {
$action = $this->container
->get('entity_type.manager')
->getStorage('action')
->load('flag_delete_flagging');
$plugin = $action
->getPlugin();
$this
->assertInstanceOf(DeleteFlaggingAction::class, $plugin);
$entity_flag = Flag::create([
'id' => mb_strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
'entity_type' => 'entity_test',
'flag_type' => 'entity:entity_test',
'link_type' => 'reload',
'flagTypeConfig' => [],
'linkTypeConfig' => [],
]);
$entity_flag
->save();
$test_entity = EntityTest::create();
$test_entity
->save();
$this->flagService
->flag($entity_flag, $test_entity);
$flaggings = $this->flagService
->getEntityFlaggings($entity_flag, $test_entity);
$flagging = reset($flaggings);
$other_user = $this
->createUser();
$this
->assertFalse($plugin
->access($flagging, $other_user));
$access = $plugin
->access($flagging, $other_user, TRUE);
$this
->assertFalse($access
->isAllowed());
$this
->assertFalse($plugin
->access($flagging));
$access = $plugin
->access($flagging, NULL, TRUE);
$this
->assertFalse($access
->isAllowed());
$plugin
->execute($flagging);
$this
->assertEmpty($this->flagService
->getEntityFlaggings($entity_flag, $test_entity));
}
}