You are here

class EntityCreateAccessCheckTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityCreateAccessCheckTest

@coversDefaultClass \Drupal\Core\Entity\EntityCreateAccessCheck

@group Access @group Entity

Hierarchy

Expanded class hierarchy of EntityCreateAccessCheckTest

File

core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php, line 23
Contains \Drupal\Tests\Core\Entity\EntityCreateAccessCheckTest.

Namespace

Drupal\Tests\Core\Entity
View source
class EntityCreateAccessCheckTest extends UnitTestCase {

  /**
   * The mocked entity manager.
   *
   * @var \PHPUnit_Framework_MockObject_MockObject
   */
  public $entityManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    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);
  }

  /**
   * Provides test data for testAccess.
   *
   * @return array
   */
  public function providerTestAccess() {
    $no_access = FALSE;
    $access = TRUE;
    return array(
      array(
        '',
        'entity_test',
        $no_access,
        $no_access,
      ),
      array(
        '',
        'entity_test',
        $access,
        $access,
      ),
      array(
        'test_entity',
        'entity_test:test_entity',
        $access,
        $access,
      ),
      array(
        'test_entity',
        'entity_test:test_entity',
        $no_access,
        $no_access,
      ),
      array(
        'test_entity',
        'entity_test:{bundle_argument}',
        $access,
        $access,
      ),
      array(
        'test_entity',
        'entity_test:{bundle_argument}',
        $no_access,
        $no_access,
      ),
      array(
        '',
        '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.
      array(
        '',
        '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) {

    // 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();
    }
    $entity_manager = $this
      ->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');

    // 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 || strpos($requirement, '{') === FALSE) {
      $access_control_handler = $this
        ->getMock('Drupal\\Core\\Entity\\EntityAccessControlHandlerInterface');
      $access_control_handler
        ->expects($this
        ->once())
        ->method('createAccess')
        ->with($entity_bundle)
        ->will($this
        ->returnValue($access_result));
      $entity_manager
        ->expects($this
        ->any())
        ->method('getAccessControlHandler')
        ->will($this
        ->returnValue($access_control_handler));
    }
    $applies_check = new EntityCreateAccessCheck($entity_manager);
    $route = $this
      ->getMockBuilder('Symfony\\Component\\Routing\\Route')
      ->disableOriginalConstructor()
      ->getMock();
    $route
      ->expects($this
      ->any())
      ->method('getRequirement')
      ->with('_entity_create_access')
      ->will($this
      ->returnValue($requirement));
    $raw_variables = new ParameterBag();
    if ($entity_bundle) {
      $raw_variables
        ->set('bundle_argument', $entity_bundle);
    }
    $route_match = $this
      ->getMock('Drupal\\Core\\Routing\\RouteMatchInterface');
    $route_match
      ->expects($this
      ->any())
      ->method('getRawParameters')
      ->will($this
      ->returnValue($raw_variables));
    $account = $this
      ->getMock('Drupal\\Core\\Session\\AccountInterface');
    $this
      ->assertEquals($expected_access_result, $applies_check
      ->access($route, $route_match, $account));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityCreateAccessCheckTest::$entityManager public property The mocked entity manager.
EntityCreateAccessCheckTest::providerTestAccess public 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.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.