class CurrencyLocaleAccessControlHandlerTest in Currency 8.3
@coversDefaultClass Drupal\currency\Entity\CurrencyLocale\CurrencyLocaleAccessControlHandler
@group Currency
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\currency\Unit\Entity\CurrencyLocale\CurrencyLocaleAccessControlHandlerTest
 
 
Expanded class hierarchy of CurrencyLocaleAccessControlHandlerTest
File
- tests/
src/ Unit/ Entity/ CurrencyLocale/ CurrencyLocaleAccessControlHandlerTest.php, line 21  
Namespace
Drupal\Tests\currency\Unit\Entity\CurrencyLocaleView source
class CurrencyLocaleAccessControlHandlerTest 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\CurrencyLocale\CurrencyLocaleAccessControlHandler
   */
  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 CurrencyLocaleAccessControlHandler($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 = CurrencyLocaleAccessControlHandler::createInstance($container, $this->entityType);
    $this
      ->assertInstanceOf(CurrencyLocaleAccessControlHandler::class, $sut);
  }
  /**
   * @covers ::checkAccess
   *
   * @dataProvider providerTestCheckAccess
   */
  function testCheckAccess($expected_value, $operation, $has_permission, $permission, $locale = NULL) {
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->any())
      ->method('hasPermission')
      ->with($permission)
      ->willReturn((bool) $has_permission);
    $currency_locale = $this
      ->createMock(CurrencyLocaleInterface::class);
    $currency_locale
      ->expects($this
      ->any())
      ->method('getLocale')
      ->willReturn($locale);
    $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_locale, $operation, $account)
      ->isAllowed());
  }
  /**
   * Provides data to self::testCheckAccess().
   */
  function providerTestCheckAccess() {
    return array(
      // The default currency locale cannot be deleted, even with permission.
      array(
        FALSE,
        'delete',
        TRUE,
        'currency.currency_locale.delete',
        LocaleResolverInterface::DEFAULT_LOCALE,
      ),
      // Any non-default currency locale can be deleted with permission.
      array(
        TRUE,
        'delete',
        TRUE,
        'currency.currency_locale.delete',
        $this
          ->randomMachineName(),
      ),
      // No currency locale can be deleted without permission.
      array(
        FALSE,
        'delete',
        FALSE,
        'currency.currency_locale.delete',
        $this
          ->randomMachineName(),
      ),
      // Any currency locale can be updated with permission.
      array(
        TRUE,
        'update',
        TRUE,
        'currency.currency_locale.update',
        $this
          ->randomMachineName(),
      ),
      // No currency locale can be updated without permission.
      array(
        FALSE,
        'update',
        FALSE,
        'currency.currency_locale.update',
        $this
          ->randomMachineName(),
      ),
    );
  }
  /**
   * @covers ::checkCreateAccess
   *
   * @dataProvider providerTestCheckCreateAccess
   */
  function testCheckCreateAccess($expected_value, $has_permission) {
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->once())
      ->method('hasPermission')
      ->with('currency.currency_locale.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
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            CurrencyLocaleAccessControlHandlerTest:: | 
                  protected | property | The cache contexts manager. | |
| 
            CurrencyLocaleAccessControlHandlerTest:: | 
                  protected | property | Information about the entity type. | |
| 
            CurrencyLocaleAccessControlHandlerTest:: | 
                  protected | property | The module handler. | |
| 
            CurrencyLocaleAccessControlHandlerTest:: | 
                  protected | property | The class under test. | |
| 
            CurrencyLocaleAccessControlHandlerTest:: | 
                  function | Provides data to self::testCheckAccess(). | ||
| 
            CurrencyLocaleAccessControlHandlerTest:: | 
                  function | Provides data to self::testCheckCreateAccess(). | ||
| 
            CurrencyLocaleAccessControlHandlerTest:: | 
                  public | function | 
            Overrides UnitTestCase:: | 
                  |
| 
            CurrencyLocaleAccessControlHandlerTest:: | 
                  function | @covers ::checkAccess | ||
| 
            CurrencyLocaleAccessControlHandlerTest:: | 
                  function | @covers ::checkCreateAccess | ||
| 
            CurrencyLocaleAccessControlHandlerTest:: | 
                  function | @covers ::createInstance @covers ::__construct | ||
| 
            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. |