View source
<?php
namespace Drupal\Tests\currency\Unit\Entity\CurrencyLocale;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\Container;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\currency\Entity\CurrencyLocale\CurrencyLocaleAccessControlHandler;
use Drupal\currency\Entity\CurrencyLocaleInterface;
use Drupal\currency\LocaleResolverInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CurrencyLocaleAccessControlHandlerTest extends UnitTestCase {
protected $cacheContextsManager;
protected $entityType;
protected $moduleHandler;
protected $sut;
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);
}
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);
}
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());
}
function providerTestCheckAccess() {
return array(
array(
FALSE,
'delete',
TRUE,
'currency.currency_locale.delete',
LocaleResolverInterface::DEFAULT_LOCALE,
),
array(
TRUE,
'delete',
TRUE,
'currency.currency_locale.delete',
$this
->randomMachineName(),
),
array(
FALSE,
'delete',
FALSE,
'currency.currency_locale.delete',
$this
->randomMachineName(),
),
array(
TRUE,
'update',
TRUE,
'currency.currency_locale.update',
$this
->randomMachineName(),
),
array(
FALSE,
'update',
FALSE,
'currency.currency_locale.update',
$this
->randomMachineName(),
),
);
}
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());
}
function providerTestCheckCreateAccess() {
return array(
array(
TRUE,
TRUE,
),
array(
FALSE,
FALSE,
),
);
}
}