You are here

class CustomAccessCheckTest in Drupal 8

@coversDefaultClass \Drupal\Core\Access\CustomAccessCheck @group Access

Hierarchy

Expanded class hierarchy of CustomAccessCheckTest

File

core/tests/Drupal/Tests/Core/Access/CustomAccessCheckTest.php, line 24
Contains \Drupal\Tests\Core\Access\CustomAccessCheckTest.

Namespace

Drupal\Tests\Core\Access
View source
class CustomAccessCheckTest extends UnitTestCase {

  /**
   * The access checker to test.
   *
   * @var \Drupal\Core\Access\CustomAccessCheck
   */
  protected $accessChecker;

  /**
   * The mocked controller resolver.
   *
   * @var \Drupal\Core\Controller\ControllerResolverInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $controllerResolver;

  /**
   * The mocked arguments resolver.
   *
   * @var \Drupal\Core\Access\AccessArgumentsResolverFactoryInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $argumentsResolverFactory;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->controllerResolver = $this
      ->createMock('Drupal\\Core\\Controller\\ControllerResolverInterface');
    $this->argumentsResolverFactory = $this
      ->createMock('Drupal\\Core\\Access\\AccessArgumentsResolverFactoryInterface');
    $this->accessChecker = new CustomAccessCheck($this->controllerResolver, $this->argumentsResolverFactory);
  }

  /**
   * Test the access method.
   */
  public function testAccess() {
    $route_match = $this
      ->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
    $this->controllerResolver
      ->expects($this
      ->at(0))
      ->method('getControllerFromDefinition')
      ->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny')
      ->will($this
      ->returnValue([
      new TestController(),
      'accessDeny',
    ]));
    $resolver0 = $this
      ->createMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface');
    $resolver0
      ->expects($this
      ->once())
      ->method('getArguments')
      ->will($this
      ->returnValue([]));
    $this->argumentsResolverFactory
      ->expects($this
      ->at(0))
      ->method('getArgumentsResolver')
      ->will($this
      ->returnValue($resolver0));
    $this->controllerResolver
      ->expects($this
      ->at(1))
      ->method('getControllerFromDefinition')
      ->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow')
      ->will($this
      ->returnValue([
      new TestController(),
      'accessAllow',
    ]));
    $resolver1 = $this
      ->createMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface');
    $resolver1
      ->expects($this
      ->once())
      ->method('getArguments')
      ->will($this
      ->returnValue([]));
    $this->argumentsResolverFactory
      ->expects($this
      ->at(1))
      ->method('getArgumentsResolver')
      ->will($this
      ->returnValue($resolver1));
    $this->controllerResolver
      ->expects($this
      ->at(2))
      ->method('getControllerFromDefinition')
      ->with('\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter')
      ->will($this
      ->returnValue([
      new TestController(),
      'accessParameter',
    ]));
    $resolver2 = $this
      ->createMock('Drupal\\Component\\Utility\\ArgumentsResolverInterface');
    $resolver2
      ->expects($this
      ->once())
      ->method('getArguments')
      ->will($this
      ->returnValue([
      'parameter' => 'TRUE',
    ]));
    $this->argumentsResolverFactory
      ->expects($this
      ->at(2))
      ->method('getArgumentsResolver')
      ->will($this
      ->returnValue($resolver2));
    $route = new Route('/test-route', [], [
      '_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessDeny',
    ]);
    $account = $this
      ->createMock('Drupal\\Core\\Session\\AccountInterface');
    $this
      ->assertEquals(AccessResult::neutral(), $this->accessChecker
      ->access($route, $route_match, $account));
    $route = new Route('/test-route', [], [
      '_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessAllow',
    ]);
    $this
      ->assertEquals(AccessResult::allowed(), $this->accessChecker
      ->access($route, $route_match, $account));
    $route = new Route('/test-route', [
      'parameter' => 'TRUE',
    ], [
      '_custom_access' => '\\Drupal\\Tests\\Core\\Access\\TestController::accessParameter',
    ]);
    $this
      ->assertEquals(AccessResult::allowed(), $this->accessChecker
      ->access($route, $route_match, $account));
  }

  /**
   * Tests the access method exception for invalid access callbacks.
   */
  public function testAccessException() {

    // Create two mocks for the ControllerResolver constructor.
    $httpMessageFactory = $this
      ->getMockBuilder(HttpMessageFactoryInterface::class)
      ->getMock();
    $controllerResolver = $this
      ->getMockBuilder(ClassResolverInterface::class)
      ->getMock();

    // Re-create the controllerResolver mock with proxy to original methods.
    $this->controllerResolver = $this
      ->getMockBuilder(ControllerResolver::class)
      ->setConstructorArgs([
      $httpMessageFactory,
      $controllerResolver,
    ])
      ->enableProxyingToOriginalMethods()
      ->getMock();

    // Overwrite the access checker using the newly mocked controller resolve.
    $this->accessChecker = new CustomAccessCheck($this->controllerResolver, $this->argumentsResolverFactory);

    // Add a route with a _custom_access route that doesn't exist.
    $route = new Route('/test-route', [], [
      '_custom_access' => '\\Drupal\\Tests\\Core\\Access\\NonExistentController::nonExistentMethod',
    ]);
    $route_match = $this
      ->createMock(RouteMatchInterface::class);
    $account = $this
      ->createMock(AccountInterface::class);
    $this
      ->expectException(\BadMethodCallException::class);
    $this
      ->expectExceptionMessage('The "\\Drupal\\Tests\\Core\\Access\\NonExistentController::nonExistentMethod" method is not callable as a _custom_access callback in route "/test-route"');

    // Run the access check.
    $this->accessChecker
      ->access($route, $route_match, $account);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CustomAccessCheckTest::$accessChecker protected property The access checker to test.
CustomAccessCheckTest::$argumentsResolverFactory protected property The mocked arguments resolver.
CustomAccessCheckTest::$controllerResolver protected property The mocked controller resolver.
CustomAccessCheckTest::setUp protected function Overrides UnitTestCase::setUp
CustomAccessCheckTest::testAccess public function Test the access method.
CustomAccessCheckTest::testAccessException public function Tests the access method exception for invalid access callbacks.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::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.