AccessResultForbidden example

$this->moduleHandler = $this->createMock('Drupal\Core\Extension\ModuleHandlerInterface');
    $this->cacheBackend = $this->createMock('Drupal\Core\Cache\CacheBackendInterface');

    $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
    $cache_contexts_manager->assertValidTokens(Argument::any())
      ->willReturn(TRUE);

    $container = new Container();
    $container->set('cache_contexts_manager', $cache_contexts_manager->reveal());
    \Drupal::setContainer($container);

    $access_result = (new AccessResultForbidden())->cachePerPermissions();
    $this->accessManager = $this->createMock('Drupal\Core\Access\AccessManagerInterface');
    $this->accessManager->expects($this->any())
      ->method('checkNamedRoute')
      ->willReturn($access_result);
    $this->account = $this->createMock('Drupal\Core\Session\AccountInterface');
    $this->discovery = $this->createMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
    $this->factory = $this->createMock('Drupal\Component\Plugin\Factory\FactoryInterface');
    $route_match = $this->createMock('Drupal\Core\Routing\RouteMatchInterface');

    $this->localActionManager = new TestLocalActionManager($this->argumentResolver, $this->request, $route_match$this->routeProvider, $this->moduleHandler, $this->cacheBackend, $this->accessManager, $this->account, $this->discovery, $this->factory);
  }

  

  public static function forbidden($reason = NULL) {
    assert(is_string($reason) || is_null($reason));
    return new AccessResultForbidden($reason);
  }

  /** * Creates an allowed or neutral access result. * * @param bool $condition * The condition to evaluate. * * @return \Drupal\Core\Access\AccessResult * If $condition is TRUE, isAllowed() will be TRUE, otherwise isNeutral() * will be TRUE. */

class AccessResultForbiddenTest extends UnitTestCase {

  /** * Tests the construction of an AccessResultForbidden object. * * @covers ::__construct * @covers ::getReason */
  public function testConstruction() {

    $a = new AccessResultForbidden();
    $this->assertEquals(NULL, $a->getReason());

    $reason = $this->getRandomGenerator()->string();
    $b = new AccessResultForbidden($reason);
    $this->assertEquals($reason$b->getReason());
  }

  /** * Tests setReason() * * @covers ::setReason */
Home | Imprint | This part of the site doesn't use cookies.