You are here

class CurrencyAccessControlHandlerTest in Currency 8.3

@coversDefaultClass Drupal\currency\Entity\Currency\CurrencyAccessControlHandler

@group Currency

Hierarchy

Expanded class hierarchy of CurrencyAccessControlHandlerTest

File

tests/src/Unit/Entity/Currency/CurrencyAccessControlHandlerTest.php, line 21

Namespace

Drupal\Tests\currency\Unit\Entity\Currency
View source
class CurrencyAccessControlHandlerTest extends UnitTestCase {

  /**
   * The cache contexts manager.
   *
   * @var \Drupal\Core\Cache\Context\CacheContextsManager|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cacheContextsManager;

  /**
   * Information about the entity type.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityType;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

  /**
   * The class under test.
   *
   * @var \Drupal\currency\Entity\Currency\CurrencyAccessControlHandler
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    parent::setUp();
    $this->cacheContextsManager = $this
      ->getMockBuilder(CacheContextsManager::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->cacheContextsManager
      ->expects($this
      ->any())
      ->method('assertValidTokens')
      ->willReturn(TRUE);
    $container = new Container();
    $container
      ->set('cache_contexts_manager', $this->cacheContextsManager);
    \Drupal::setContainer($container);
    $this->entityType = $this
      ->createMock(EntityTypeInterface::class);
    $this->moduleHandler = $this
      ->createMock(ModuleHandlerInterface::class);
    $this->sut = new CurrencyAccessControlHandler($this->entityType, $this->moduleHandler);
  }

  /**
   * @covers ::createInstance
   * @covers ::__construct
   */
  function testCreateInstance() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $container
      ->expects($this
      ->once())
      ->method('get')
      ->with('module_handler')
      ->willReturn($this->moduleHandler);
    $sut = CurrencyAccessControlHandler::createInstance($container, $this->entityType);
    $this
      ->assertInstanceOf(CurrencyAccessControlHandler::class, $sut);
  }

  /**
   * @covers ::checkAccess
   *
   * @dataProvider providerTestCheckAccess
   */
  function testCheckAccess($expected_value, $operation, $has_permission, $permission, $entity_status = FALSE, $currency_code = NULL) {
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->any())
      ->method('hasPermission')
      ->with($permission)
      ->willReturn((bool) $has_permission);
    $language = $this
      ->createMock(LanguageInterface::class);
    $currency = $this
      ->createMock(CurrencyInterface::class);
    $currency
      ->expects($this
      ->any())
      ->method('getCurrencyCode')
      ->willReturn($currency_code);
    $currency
      ->expects($this
      ->any())
      ->method('language')
      ->willReturn($language);
    $currency
      ->expects($this
      ->any())
      ->method('status')
      ->willReturn($entity_status);
    $this->moduleHandler
      ->expects($this
      ->any())
      ->method('invokeAll')
      ->willReturn([]);
    $method = new \ReflectionMethod($this->sut, 'checkAccess');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertSame($expected_value, $method
      ->invoke($this->sut, $currency, $operation, $account)
      ->isAllowed());
  }

  /**
   * Provides data to self::testCheckAccess().
   */
  function providerTestCheckAccess() {
    return array(
      // The default currency cannot be deleted, even with permission.
      array(
        FALSE,
        'delete',
        TRUE,
        'currency.currency.delete',
        TRUE,
        'XXX',
      ),
      // A disabled currency cannot be disabled.
      array(
        FALSE,
        'disable',
        TRUE,
        'currency.currency.update',
        FALSE,
      ),
      // An enabled currency cannot be enabled.
      array(
        FALSE,
        'enable',
        TRUE,
        'currency.currency.update',
        TRUE,
      ),
      // A disabled currency cannot be enabled without permission.
      array(
        FALSE,
        'disable',
        FALSE,
        'currency.currency.update',
        TRUE,
      ),
      // A disabled currency cannot be enabled without permission.
      array(
        FALSE,
        'enable',
        FALSE,
        'currency.currency.update',
        FALSE,
      ),
      // A disabled currency can be enabled.
      array(
        TRUE,
        'disable',
        TRUE,
        'currency.currency.update',
        TRUE,
      ),
      // A disabled currency can be enabled.
      array(
        TRUE,
        'enable',
        TRUE,
        'currency.currency.update',
        FALSE,
      ),
      // A currency cannot be updated without permission.
      array(
        FALSE,
        'update',
        FALSE,
        'currency.currency.update',
      ),
      // A currency can be updated with permission.
      array(
        TRUE,
        'update',
        TRUE,
        'currency.currency.update',
      ),
      // A currency cannot be deleted without permission.
      array(
        FALSE,
        'delete',
        FALSE,
        'currency.currency.delete',
      ),
      // A currency can be deleted with permission.
      array(
        TRUE,
        'delete',
        TRUE,
        'currency.currency.delete',
      ),
    );
  }

  /**
   * @covers ::checkCreateAccess
   *
   * @dataProvider providerTestCheckCreateAccess
   */
  function testCheckCreateAccess($expected_value, $has_permission) {
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->once())
      ->method('hasPermission')
      ->with('currency.currency.create')
      ->willReturn($has_permission);
    $context = array();
    $method = new \ReflectionMethod($this->sut, 'checkCreateAccess');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertSame($expected_value, $method
      ->invoke($this->sut, $account, $context)
      ->isAllowed());
  }

  /**
   * Provides data to self::testCheckCreateAccess().
   */
  function providerTestCheckCreateAccess() {
    return array(
      array(
        TRUE,
        TRUE,
      ),
      array(
        FALSE,
        FALSE,
      ),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrencyAccessControlHandlerTest::$cacheContextsManager protected property The cache contexts manager.
CurrencyAccessControlHandlerTest::$entityType protected property Information about the entity type.
CurrencyAccessControlHandlerTest::$moduleHandler protected property The module handler.
CurrencyAccessControlHandlerTest::$sut protected property The class under test.
CurrencyAccessControlHandlerTest::providerTestCheckAccess function Provides data to self::testCheckAccess().
CurrencyAccessControlHandlerTest::providerTestCheckCreateAccess function Provides data to self::testCheckCreateAccess().
CurrencyAccessControlHandlerTest::setUp public function Overrides UnitTestCase::setUp
CurrencyAccessControlHandlerTest::testCheckAccess function @covers ::checkAccess
CurrencyAccessControlHandlerTest::testCheckCreateAccess function @covers ::checkCreateAccess
CurrencyAccessControlHandlerTest::testCreateInstance function @covers ::createInstance @covers ::__construct
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.