You are here

class PaymentMethodConfigurationAccessControlHandlerTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Entity\PaymentMethodConfiguration\PaymentMethodConfigurationAccessControlHandler

@group Payment

Hierarchy

Expanded class hierarchy of PaymentMethodConfigurationAccessControlHandlerTest

File

tests/src/Unit/Entity/PaymentMethodConfiguration/PaymentMethodConfigurationAccessControlHandlerTest.php, line 22

Namespace

Drupal\Tests\payment\Unit\Entity\PaymentMethodConfiguration
View source
class PaymentMethodConfigurationAccessControlHandlerTest extends UnitTestCase {

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

  /**
   * The class under test.
   *
   * @var \Drupal\payment\Entity\PaymentMethodConfiguration\PaymentMethodConfigurationAccessControlHandler
   */
  protected $sut;

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

  /**
   * @covers ::createInstance
   * @covers ::__construct
   */
  public function testCreateInstance() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'module_handler',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->moduleHandler,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $entity_type = $this
      ->createMock(EntityTypeInterface::class);
    $handler = PaymentMethodConfigurationAccessControlHandler::createInstance($container, $entity_type);
    $this
      ->assertInstanceOf(PaymentMethodConfigurationAccessControlHandler::class, $handler);
  }

  /**
   * Gets a mock payment method configuration.
   *
   * @return \Drupal\payment\Entity\PaymentMethodConfiguration|\PHPUnit\Framework\MockObject\MockObject
   */
  protected function getMockPaymentMethodConfiguration() {
    $payment_method_configuration = $this
      ->createMock(PaymentMethodConfigurationInterface::class);
    $payment_method_configuration
      ->expects($this
      ->any())
      ->method('getCacheContexts')
      ->willReturn([]);
    $payment_method_configuration
      ->expects($this
      ->any())
      ->method('getCacheTags')
      ->willReturn([
      'payment_method_configuration',
    ]);
    return $payment_method_configuration;
  }

  /**
   * @covers ::checkAccess
   */
  public function testCheckAccessWithoutPermission() {
    $operation = $this
      ->randomMachineName();
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->any())
      ->method('hasPermission')
      ->willReturn(FALSE);
    $payment_method_configuration = $this
      ->getMockPaymentMethodConfiguration();
    $class = new \ReflectionClass($this->sut);
    $method = $class
      ->getMethod('checkAccess');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertFalse($method
      ->invokeArgs($this->sut, [
      $payment_method_configuration,
      $operation,
      $account,
    ])
      ->isAllowed());
  }

  /**
   * @covers ::checkAccess
   */
  public function testCheckAccessWithAnyPermission() {
    $operation = $this
      ->randomMachineName();
    $account = $this
      ->createMock(AccountInterface::class);
    $map = [
      [
        'payment.payment_method_configuration.' . $operation . '.any',
        TRUE,
      ],
      [
        'payment.payment_method_configuration.' . $operation . '.own',
        FALSE,
      ],
    ];
    $account
      ->expects($this
      ->any())
      ->method('hasPermission')
      ->willReturnMap($map);
    $payment_method_configuration = $this
      ->getMockPaymentMethodConfiguration();
    $class = new \ReflectionClass($this->sut);
    $method = $class
      ->getMethod('checkAccess');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertTrue($method
      ->invokeArgs($this->sut, [
      $payment_method_configuration,
      $operation,
      $account,
    ])
      ->isAllowed());
  }

  /**
   * @covers ::checkAccess
   */
  public function testCheckAccessWithOwnPermission() {
    $owner_id = mt_rand();
    $operation = $this
      ->randomMachineName();
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn($owner_id);
    $map = [
      [
        'payment.payment_method_configuration.' . $operation . '.any',
        FALSE,
      ],
      [
        'payment.payment_method_configuration.' . $operation . '.own',
        TRUE,
      ],
    ];
    $account
      ->expects($this
      ->any())
      ->method('hasPermission')
      ->willReturnMap($map);
    $payment_method_configuration = $this
      ->getMockPaymentMethodConfiguration();
    $payment_method_configuration
      ->expects($this
      ->at(0))
      ->method('getOwnerId')
      ->willReturn($owner_id);
    $payment_method_configuration
      ->expects($this
      ->at(1))
      ->method('getOwnerId')
      ->willReturn($owner_id + 1);
    $class = new \ReflectionClass($this->sut);
    $method = $class
      ->getMethod('checkAccess');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertTrue($method
      ->invokeArgs($this->sut, [
      $payment_method_configuration,
      $operation,
      $account,
    ])
      ->isAllowed());
    $this
      ->assertFalse($method
      ->invokeArgs($this->sut, [
      $payment_method_configuration,
      $operation,
      $account,
    ])
      ->isAllowed());
  }

  /**
   * @covers ::checkAccess
   *
   * @dataProvider providerTestCheckAccessEnable
   */
  public function testCheckAccessEnable($expected, $payment_method_configuration_status, $has_update_permission) {
    $operation = 'enable';
    $account = $this
      ->createMock(AccountInterface::class);
    $map = [
      [
        'payment.payment_method_configuration.update.any',
        $has_update_permission,
      ],
      [
        'payment.payment_method_configuration.update.own',
        FALSE,
      ],
    ];
    $account
      ->expects($this
      ->atLeastOnce())
      ->method('hasPermission')
      ->willReturnMap($map);
    $payment_method_configuration = $this
      ->getMockPaymentMethodConfiguration();
    $payment_method_configuration
      ->expects($this
      ->atLeastOnce())
      ->method('status')
      ->willReturn($payment_method_configuration_status);
    $this
      ->setUpLanguage($payment_method_configuration);
    $class = new \ReflectionClass($this->sut);
    $method = $class
      ->getMethod('checkAccess');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertSame($expected, $method
      ->invokeArgs($this->sut, [
      $payment_method_configuration,
      $operation,
      $account,
    ])
      ->isAllowed());
  }

  /**
   * Provides data to self::testCheckAccessEnable().
   */
  public function providerTestCheckAccessEnable() {
    return [
      // Enabled with permission.
      [
        FALSE,
        TRUE,
        TRUE,
      ],
      // Disabled with permission.
      [
        TRUE,
        FALSE,
        TRUE,
      ],
      // Disabled without permission.
      [
        FALSE,
        FALSE,
        FALSE,
      ],
    ];
  }

  /**
   * @covers ::checkAccess
   *
   * @dataProvider providerTestCheckAccessDisable
   */
  public function testCheckAccessDisable($expected, $payment_method_configuration_status, $has_update_permission) {
    $operation = 'disable';
    $account = $this
      ->createMock(AccountInterface::class);
    $map = [
      [
        'payment.payment_method_configuration.update.any',
        $has_update_permission,
      ],
      [
        'payment.payment_method_configuration.update.own',
        FALSE,
      ],
    ];
    $account
      ->expects($this
      ->atLeastOnce())
      ->method('hasPermission')
      ->willReturnMap($map);
    $payment_method_configuration = $this
      ->getMockPaymentMethodConfiguration();
    $payment_method_configuration
      ->expects($this
      ->atLeastOnce())
      ->method('status')
      ->willReturn($payment_method_configuration_status);
    $this
      ->setUpLanguage($payment_method_configuration);
    $class = new \ReflectionClass($this->sut);
    $method = $class
      ->getMethod('checkAccess');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertSame($expected, $method
      ->invokeArgs($this->sut, [
      $payment_method_configuration,
      $operation,
      $account,
    ])
      ->isAllowed());
  }

  /**
   * Provides data to self::testCheckAccessDisable().
   */
  public function providerTestCheckAccessDisable() {
    return [
      // Disabled with permission.
      [
        FALSE,
        FALSE,
        TRUE,
      ],
      // Enabled with permission.
      [
        TRUE,
        TRUE,
        TRUE,
      ],
      // Enabled without permission.
      [
        FALSE,
        TRUE,
        FALSE,
      ],
    ];
  }

  /**
   * @covers ::checkAccess
   *
   * @dataProvider providerTestCheckAccessDuplicate
   */
  public function testCheckAccessDuplicate($expected, $has_create_permission, $has_view_permission) {
    $operation = 'duplicate';
    $bundle = $this
      ->randomMachineName();
    $account = $this
      ->createMock(AccountInterface::class);
    $map = [
      [
        'payment.payment_method_configuration.create.' . $bundle,
        $has_create_permission,
      ],
      [
        'payment.payment_method_configuration.view.any',
        $has_view_permission,
      ],
    ];
    $account
      ->expects($this
      ->any())
      ->method('hasPermission')
      ->willReturnMap($map);
    $language = $this
      ->createMock(LanguageInterface::class);
    $payment_method_configuration = $this
      ->getMockPaymentMethodConfiguration();
    $payment_method_configuration
      ->expects($this
      ->atLeastOnce())
      ->method('bundle')
      ->willReturn($bundle);
    $payment_method_configuration
      ->expects($this
      ->any())
      ->method('language')
      ->willReturn($language);
    $class = new \ReflectionClass($this->sut);
    $method = $class
      ->getMethod('checkAccess');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertSame($expected, $method
      ->invokeArgs($this->sut, [
      $payment_method_configuration,
      $operation,
      $account,
    ])
      ->isAllowed());
  }

  /**
   * Provides data to self::testCheckAccessDuplicate().
   */
  public function providerTestCheckAccessDuplicate() {
    return [
      // No create access.
      [
        FALSE,
        FALSE,
        TRUE,
      ],
      // Create access, with view permission.
      [
        TRUE,
        TRUE,
        TRUE,
      ],
      // Create access, without view permission.
      [
        FALSE,
        TRUE,
        FALSE,
      ],
      // No access.
      [
        FALSE,
        FALSE,
        FALSE,
      ],
    ];
  }

  /**
   * @covers ::checkCreateAccess
   */
  public function testCheckCreateAccess() {
    $bundle = $this
      ->randomMachineName();
    $context = [];
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->once())
      ->method('hasPermission')
      ->with('payment.payment_method_configuration.create.' . $bundle)
      ->willReturn(TRUE);
    $class = new \ReflectionClass($this->sut);
    $method = $class
      ->getMethod('checkCreateAccess');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertTrue($method
      ->invokeArgs($this->sut, [
      $account,
      $context,
      $bundle,
    ])
      ->isAllowed());
  }

  /**
   * @covers ::getCache
   */
  public function testGetCache() {
    $account = $this
      ->createMock(AccountInterface::class);
    $cache_id = $this
      ->randomMachineName();
    $operation = $this
      ->randomMachineName();
    $language_code = $this
      ->randomMachineName();
    $class = new \ReflectionClass($this->sut);
    $method = $class
      ->getMethod('getCache');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertNull($method
      ->invokeArgs($this->sut, [
      $cache_id,
      $operation,
      $language_code,
      $account,
    ]));
  }

  /**
   * Sets up the mock definitions for the language() method.
   *
   * @param \PHPUnit\Framework\MockObject\MockObject $payment_method_configuration
   *   A mock entity.
   */
  protected function setUpLanguage(MockObject $payment_method_configuration) {
    $language = $this
      ->createMock(LanguageInterface::class);
    $language
      ->expects($this
      ->any())
      ->method('getId')
      ->willReturn($this
      ->randomMachineName(2));
    $payment_method_configuration
      ->expects($this
      ->any())
      ->method('language')
      ->willReturn($language);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentMethodConfigurationAccessControlHandlerTest::$moduleHandler protected property The module handler.
PaymentMethodConfigurationAccessControlHandlerTest::$sut protected property The class under test.
PaymentMethodConfigurationAccessControlHandlerTest::getMockPaymentMethodConfiguration protected function Gets a mock payment method configuration.
PaymentMethodConfigurationAccessControlHandlerTest::providerTestCheckAccessDisable public function Provides data to self::testCheckAccessDisable().
PaymentMethodConfigurationAccessControlHandlerTest::providerTestCheckAccessDuplicate public function Provides data to self::testCheckAccessDuplicate().
PaymentMethodConfigurationAccessControlHandlerTest::providerTestCheckAccessEnable public function Provides data to self::testCheckAccessEnable().
PaymentMethodConfigurationAccessControlHandlerTest::setUp public function Overrides UnitTestCase::setUp
PaymentMethodConfigurationAccessControlHandlerTest::setUpLanguage protected function Sets up the mock definitions for the language() method.
PaymentMethodConfigurationAccessControlHandlerTest::testCheckAccessDisable public function @covers ::checkAccess
PaymentMethodConfigurationAccessControlHandlerTest::testCheckAccessDuplicate public function @covers ::checkAccess
PaymentMethodConfigurationAccessControlHandlerTest::testCheckAccessEnable public function @covers ::checkAccess
PaymentMethodConfigurationAccessControlHandlerTest::testCheckAccessWithAnyPermission public function @covers ::checkAccess
PaymentMethodConfigurationAccessControlHandlerTest::testCheckAccessWithoutPermission public function @covers ::checkAccess
PaymentMethodConfigurationAccessControlHandlerTest::testCheckAccessWithOwnPermission public function @covers ::checkAccess
PaymentMethodConfigurationAccessControlHandlerTest::testCheckCreateAccess public function @covers ::checkCreateAccess
PaymentMethodConfigurationAccessControlHandlerTest::testCreateInstance public function @covers ::createInstance @covers ::__construct
PaymentMethodConfigurationAccessControlHandlerTest::testGetCache public function @covers ::getCache
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.