You are here

public function PaymentMethodConfigurationAccessControlHandlerTest::testCheckAccessWithOwnPermission in Payment 8.2

@covers ::checkAccess

File

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

Class

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

Namespace

Drupal\Tests\payment\Unit\Entity\PaymentMethodConfiguration

Code

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());
}