class EntityCreateAccessCheckTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityCreateAccessCheckTest
- 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityCreateAccessCheckTest
- 10 core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityCreateAccessCheckTest
@coversDefaultClass \Drupal\Core\Entity\EntityCreateAccessCheck
@group Access @group Entity
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Entity\EntityCreateAccessCheckTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of EntityCreateAccessCheckTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Entity/ EntityCreateAccessCheckTest.php, line 20
Namespace
Drupal\Tests\Core\EntityView source
class EntityCreateAccessCheckTest extends UnitTestCase {
/**
* The mocked entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
public $entityTypeManager;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
$cache_contexts_manager->assertValidTokens()
->willReturn(TRUE);
$cache_contexts_manager->reveal();
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
$this->entityTypeManager = $this->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
}
/**
* Provides test data for testAccess.
*
* @return array
*/
public static function providerTestAccess() {
$no_access = FALSE;
$access = TRUE;
return [
[
'',
'entity_test',
$no_access,
$no_access,
],
[
'',
'entity_test',
$access,
$access,
],
[
'test_entity',
'entity_test:test_entity',
$access,
$access,
],
[
'test_entity',
'entity_test:test_entity',
$no_access,
$no_access,
],
[
'test_entity',
'entity_test:{bundle_argument}',
$access,
$access,
],
[
'test_entity',
'entity_test:{bundle_argument}',
$no_access,
$no_access,
],
[
'',
'entity_test:{bundle_argument}',
$no_access,
$no_access,
FALSE,
],
// When the bundle is not provided, access should be denied even if the
// access control handler would allow access.
[
'',
'entity_test:{bundle_argument}',
$access,
$no_access,
FALSE,
],
];
}
/**
* Tests the method for checking access to routes.
*
* @dataProvider providerTestAccess
*/
public function testAccess($entity_bundle, $requirement, $access, $expected, $expect_permission_context = TRUE) : void {
// Set up the access result objects for allowing or denying access.
$access_result = $access ? AccessResult::allowed()->cachePerPermissions() : AccessResult::neutral()->cachePerPermissions();
$expected_access_result = $expected ? AccessResult::allowed() : AccessResult::neutral();
if ($expect_permission_context) {
$expected_access_result->cachePerPermissions();
}
if (!$entity_bundle && !$expect_permission_context) {
$expected_access_result->setReason("Could not find '{bundle_argument}' request argument, therefore cannot check create access.");
}
// Don't expect a call to the access control handler when we have a bundle
// argument requirement but no bundle is provided.
if ($entity_bundle || !str_contains($requirement, '{')) {
$access_control_handler = $this->createMock('Drupal\\Core\\Entity\\EntityAccessControlHandlerInterface');
$access_control_handler->expects($this->once())
->method('createAccess')
->with($entity_bundle)
->willReturn($access_result);
$this->entityTypeManager
->expects($this->any())
->method('getAccessControlHandler')
->willReturn($access_control_handler);
}
$applies_check = new EntityCreateAccessCheck($this->entityTypeManager);
$route = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')
->disableOriginalConstructor()
->getMock();
$route->expects($this->any())
->method('getRequirement')
->with('_entity_create_access')
->willReturn($requirement);
$raw_variables = new InputBag();
if ($entity_bundle) {
$raw_variables->set('bundle_argument', $entity_bundle);
}
$route_match = $this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
$route_match->expects($this->any())
->method('getRawParameters')
->willReturn($raw_variables);
$account = $this->createMock('Drupal\\Core\\Session\\AccountInterface');
$this->assertEquals($expected_access_result, $applies_check->access($route, $route_match, $account));
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
EntityCreateAccessCheckTest::$entityTypeManager | public | property | The mocked entity type manager. | |
EntityCreateAccessCheckTest::providerTestAccess | public static | function | Provides test data for testAccess. | |
EntityCreateAccessCheckTest::setUp | protected | function | Overrides UnitTestCase::setUp | |
EntityCreateAccessCheckTest::testAccess | public | function | Tests the method for checking access to routes. | |
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | |
ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
UnitTestCase::$root | protected | property | The app root. | |
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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.