You are here

class FlagActionTest in Flag 8.4

Same name in this branch
  1. 8.4 tests/src/Kernel/FlagActionTest.php \Drupal\Tests\flag\Kernel\FlagActionTest
  2. 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

Expanded class hierarchy of FlagActionTest

File

tests/src/Unit/Plugin/Action/FlagActionTest.php, line 21

Namespace

Drupal\Tests\flag\Unit\Plugin\Action
View 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

Namesort descending Modifiers Type Description Overrides
FlagActionTest::$flag protected property Mock flag.
FlagActionTest::setUp protected function Overrides UnitTestCase::setUp
FlagActionTest::testAccess public function Tests the access method.
FlagActionTest::testExecute public function Tests the execute method.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.