class FlagActionTest in Flag 8.4
Same name in this branch
- 8.4 tests/src/Kernel/FlagActionTest.php \Drupal\Tests\flag\Kernel\FlagActionTest
- 8.4 tests/src/Unit/Plugin/Action/FlagActionTest.php \Drupal\Tests\flag\Unit\Plugin\Action\FlagActionTest
Unit tests for the flag action plugin.
@group flag
@coversDefaultClass \Drupal\flag\Plugin\Action\FlagAction
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\flag\Unit\Plugin\Action\FlagActionTest
Expanded class hierarchy of FlagActionTest
File
- tests/
src/ Unit/ Plugin/ Action/ FlagActionTest.php, line 21
Namespace
Drupal\Tests\flag\Unit\Plugin\ActionView source
class FlagActionTest extends UnitTestCase {
/**
* Mock flag.
*
* @var \Drupal\flag\FlagInterface
*/
protected $flag;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$flag = $this
->prophesize(FlagInterface::class);
$flag
->id()
->willReturn(mb_strtolower($this
->randomMachineName()));
$this->flag = $flag
->reveal();
}
/**
* Tests the execute method.
*
* @covers ::execute
*/
public function testExecute() {
// Test 'flag' op.
$config = [
'flag_id' => $this->flag
->id(),
'flag_action' => 'flag',
];
$flag_service = $this
->prophesize(FlagServiceInterface::class);
$flag_service
->getFlagById($this->flag
->id())
->willReturn($this->flag);
$entity = $this
->prophesize(EntityInterface::class)
->reveal();
$flag_service
->flag($this->flag, $entity)
->shouldBeCalled();
$plugin = new FlagAction($config, 'flag_action:' . $this->flag
->id() . '_flag', [], $flag_service
->reveal());
$plugin
->execute($entity);
// Test 'uflag' op.
$config = [
'flag_id' => $this->flag
->id(),
'flag_action' => 'unflag',
];
$flag_service = $this
->prophesize(FlagServiceInterface::class);
$flag_service
->getFlagById($this->flag
->id())
->willReturn($this->flag);
$entity = $this
->prophesize(EntityInterface::class)
->reveal();
$flag_service
->unflag($this->flag, $entity)
->shouldBeCalled();
$plugin = new FlagAction($config, 'flag_action:' . $this->flag
->id() . '_flag', [], $flag_service
->reveal());
$plugin
->execute($entity);
}
/**
* Tests the access method.
*
* @covers ::access
*/
public function testAccess() {
// Test access denied.
$entity = $this
->prophesize(EntityInterface::class)
->reveal();
$account = $this
->prophesize(UserInterface::class)
->reveal();
$flag = $this
->prophesize(FlagInterface::class);
$flag
->id()
->willReturn(mb_strtolower($this
->randomMachineName()));
$denied = $this
->prophesize(AccessResultForbidden::class);
$denied
->isAllowed()
->willReturn(FALSE);
$denied = $denied
->reveal();
$flag
->actionAccess('flag', $account, $entity)
->willReturn($denied);
$this->flag = $flag
->reveal();
$flag_service = $this
->prophesize(FlagServiceInterface::class);
$flag_service
->getFlagById($this->flag
->id())
->willReturn($this->flag);
$config = [
'flag_id' => $this->flag
->id(),
'flag_action' => 'flag',
];
$plugin = new FlagAction($config, 'flag_action:' . $this->flag
->id() . '_flag', [], $flag_service
->reveal());
$this
->assertFalse($plugin
->access($entity, $account));
$this
->assertEquals($denied, $plugin
->access($entity, $account, TRUE));
// Test access allowed.
$flag = $this
->prophesize(FlagInterface::class);
$flag
->id()
->willReturn(mb_strtolower($this
->randomMachineName()));
$allowed = $this
->prophesize(AccessResult::class);
$allowed
->isAllowed()
->willReturn(TRUE);
$allowed = $allowed
->reveal();
$flag
->actionAccess('flag', $account, $entity)
->willReturn($allowed);
$this->flag = $flag
->reveal();
$flag_service = $this
->prophesize(FlagServiceInterface::class);
$flag_service
->getFlagById($this->flag
->id())
->willReturn($this->flag);
$config = [
'flag_id' => $this->flag
->id(),
'flag_action' => 'flag',
];
$plugin = new FlagAction($config, 'flag_action:' . $this->flag
->id() . '_flag', [], $flag_service
->reveal());
$this
->assertTrue($plugin
->access($entity, $account));
$this
->assertEquals($allowed, $plugin
->access($entity, $account, TRUE));
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FlagActionTest:: |
protected | property | Mock flag. | |
FlagActionTest:: |
protected | function |
Overrides UnitTestCase:: |
|
FlagActionTest:: |
public | function | Tests the access method. | |
FlagActionTest:: |
public | function | Tests the execute method. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |