ParagraphsAccessTest.php in Paragraphs 8
File
tests/src/Kernel/ParagraphsAccessTest.php
View source
<?php
namespace Drupal\Tests\paragraphs\Kernel;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
class ParagraphsAccessTest extends KernelTestBase {
public static $modules = [
'paragraphs',
];
public function testCreateAccess($request_format, $expected_result) {
$request = new Request();
$request
->setRequestFormat($request_format);
$this->container
->get('request_stack')
->push($request);
$result = $this->container
->get('entity_type.manager')
->getAccessControlHandler('paragraph')
->createAccess(NULL, NULL, [], TRUE);
$this
->assertEquals($expected_result, $result);
}
public function createAccessTestCases() {
$container = new ContainerBuilder();
$cache_contexts_manager = $this
->prophesize(CacheContextsManager::class);
$cache_contexts_manager
->assertValidTokens()
->willReturn(TRUE);
$cache_contexts_manager
->reveal();
$container
->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
return [
'Allowed HTML request format' => [
'html',
AccessResult::allowed()
->addCacheContexts([
'request_format',
]),
],
'Forbidden other formats' => [
'json',
AccessResult::neutral()
->addCacheContexts([
'request_format',
]),
],
];
}
}