View source
<?php
namespace Drupal\Tests\payment\Unit\Entity\PaymentMethodConfiguration;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\Container;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\payment\Entity\PaymentMethodConfiguration\PaymentMethodConfigurationAccessControlHandler;
use Drupal\payment\Entity\PaymentMethodConfigurationInterface;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PaymentMethodConfigurationAccessControlHandlerTest extends UnitTestCase {
protected $moduleHandler;
protected $sut;
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);
}
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);
}
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;
}
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());
}
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());
}
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());
}
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());
}
public function providerTestCheckAccessEnable() {
return [
[
FALSE,
TRUE,
TRUE,
],
[
TRUE,
FALSE,
TRUE,
],
[
FALSE,
FALSE,
FALSE,
],
];
}
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());
}
public function providerTestCheckAccessDisable() {
return [
[
FALSE,
FALSE,
TRUE,
],
[
TRUE,
TRUE,
TRUE,
],
[
FALSE,
TRUE,
FALSE,
],
];
}
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());
}
public function providerTestCheckAccessDuplicate() {
return [
[
FALSE,
FALSE,
TRUE,
],
[
TRUE,
TRUE,
TRUE,
],
[
FALSE,
TRUE,
FALSE,
],
[
FALSE,
FALSE,
FALSE,
],
];
}
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());
}
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,
]));
}
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);
}
}