assertValidTokens example

/** * Tests the checkAccess() tree manipulator. * * @covers ::checkAccess * @covers ::menuLinkCheckAccess */
  public function testCheckAccess() {
    // Those menu links that are non-external will have their access checks     // performed. 9 routes, but 1 is external, 2 already have their 'access'     // property set, and 1 is a child if an inaccessible menu link, so only 5     // calls will be made.     $this->cacheContextManager->assertValidTokens(['user.permissions'])->shouldBeCalled()->willReturn(TRUE);
    $this->accessManager->expects($this->exactly(5))
      ->method('checkNamedRoute')
      ->willReturnMap([
        ['example1', []$this->currentUser, TRUE, AccessResult::forbidden()],
        [
          'example2',
          ['foo' => 'bar'],
          $this->currentUser,
          TRUE,
          AccessResult::allowed()->cachePerPermissions(),
        ],
        [

class SortTest extends UnitTestCase {

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $container = new Container();
    $cache_context_manager = $this->prophesize(CacheContextsManager::class);
    $cache_context_manager->assertValidTokens(Argument::any())
      ->willReturn(TRUE);
    $container->set('cache_contexts_manager', $cache_context_manager->reveal());
    \Drupal::setContainer($container);
  }

  /** * @covers ::createFromQueryParameter * @dataProvider parameterProvider */
  public function testCreateFromQueryParameter($input$expected) {
    $sort = Sort::createFromQueryParameter($input);
    

  protected $container;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $this->container = new ContainerBuilder();
    $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
    $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
    $cache_contexts_manager->reveal();
    $this->container->set('cache_contexts_manager', $cache_contexts_manager);
    \Drupal::setContainer($this->container);
  }

  /** * Provides data for the testAccess method. * * @return array */
  public function providerTestAccess() {
    
static::assertEquals($view_label_access_result$this->accessControlHandler->access($entity, 'view label', $user, TRUE));
    static::assertEquals($view_access_result$this->accessControlHandler->access($entity, 'view', $user, TRUE));
    static::assertEquals($update_access_result$this->accessControlHandler->access($entity, 'update', $user, TRUE));
    static::assertEquals($delete_access_result$this->accessControlHandler->access($entity, 'delete', $user, TRUE));
    static::assertEquals($create_access_result$this->accessControlHandler->createAccess(NULL, $user[], TRUE));
  }

  public function testAccessProvider() {
    $c = new ContainerBuilder();
    $cache_contexts_manager = (new Prophet())->prophesize(CacheContextsManager::class);
    $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
    $cache_contexts_manager->reveal();
    $c->set('cache_contexts_manager', $cache_contexts_manager);
    \Drupal::setContainer($c);

    return [
      'permissionless + unlocked' => [
        'permissionless',
        'unlocked',
        AccessResult::allowed(),
        AccessResult::neutral()->addCacheContexts(['user.permissions'])->setReason("The 'administer site configuration' permission is required."),
        AccessResult::neutral()->addCacheContexts(['user.permissions'])->setReason("The 'administer site configuration' permission is required.")->addCacheTags(['rendered']),
        

  protected $items;

  /** * {@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->viewer = $this->createMock('\Drupal\user\UserInterface');
    $this->viewer
      ->expects($this->any())
      ->method('hasPermission')
      ->willReturn(FALSE);
    $this->viewer
      

  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 */
/** * Merges arrays of cache contexts and removes duplicates. * * @param string[] ... * Cache contexts arrays to merge. * * @return string[] * The merged array of cache contexts. */
  public static function mergeContexts(array ...$cache_contexts) {
    $cache_contexts = array_unique(array_merge(...$cache_contexts));
    assert(\Drupal::service('cache_contexts_manager')->assertValidTokens($cache_contexts)sprintf('Failed to assert that "%s" are valid cache contexts.', implode(', ', $cache_contexts)));
    return $cache_contexts;
  }

  /** * Merges arrays of cache tags and removes duplicates. * * The cache tags array is returned in a format that is valid for * \Drupal\Core\Cache\CacheBackendInterface::set(). * * When caching elements, it is necessary to collect all cache tags into a * single array, from both the element itself and all child elements. This * allows items to be invalidated based on all tags attached to the content * they're constituted from. * * @param string[] ... * Cache tags arrays to merge. * * @return string[] * The merged array of cache tags. */
'bar', 'foo', 'llama']['foo', 'bar', 'llama']['foo']],
    ];
  }

  /** * @covers ::mergeContexts * * @dataProvider mergeCacheContextsProvide */
  public function testMergeCacheContexts(array $expected, ...$contexts) {
    $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
    $cache_contexts_manager->assertValidTokens(Argument::any())->willReturn(TRUE);
    $container = $this->prophesize(Container::class);
    $container->get('cache_contexts_manager')->willReturn($cache_contexts_manager->reveal());
    \Drupal::setContainer($container->reveal());
    $this->assertEqualsCanonicalizing($expected, Cache::mergeContexts(...$contexts));
  }

  /** * Provides a list of pairs of (prefix, suffixes) to build cache tags from. * * @return array */
  

  public function convertTokensToKeys(array $context_tokens) {
    assert($this->assertValidTokens($context_tokens));
    $cacheable_metadata = new CacheableMetadata();
    $optimized_tokens = $this->optimizeTokens($context_tokens);
    // Iterate over cache contexts that have been optimized away and get their     // cacheability metadata.     foreach (static::parseTokens(array_diff($context_tokens$optimized_tokens)) as $context_token) {
      [$context_id$parameter] = $context_token;
      $context = $this->getService($context_id);
      $cacheable_metadata = $cacheable_metadata->merge($context->getCacheableMetadata($parameter));
    }

    sort($optimized_tokens);
    

class OffsetPageTest extends UnitTestCase {

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $container = new Container();
    $cache_context_manager = $this->prophesize(CacheContextsManager::class);
    $cache_context_manager->assertValidTokens(Argument::any())
      ->willReturn(TRUE);
    $container->set('cache_contexts_manager', $cache_context_manager->reveal());
    \Drupal::setContainer($container);
  }

  /** * @covers ::createFromQueryParameter * @dataProvider parameterProvider */
  public function testCreateFromQueryParameter($original$expected) {
    $actual = OffsetPage::createFromQueryParameter($original);
    

  public function testRoleAccess($path$grant_accounts$deny_accounts) {
    $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);

    $role_access_check = new RoleAccessCheck();
    $collection = $this->getTestRouteCollection();

    foreach ($grant_accounts as $account) {
      $message = sprintf('Access granted for user with the roles %s on path: %s', implode(', ', $account->getRoles())$path);
      $this->assertEquals(AccessResult::allowed()->addCacheContexts(['user.roles'])$role_access_check->access($collection->get($path)$account)$message);
    }
$this->factory->expects($this->any())
      ->method('createInstance')
      ->willReturnMap($map);
  }

  protected function setupNullCacheabilityMetadataValidation() {
    $container = \Drupal::hasContainer() ? \Drupal::getContainer() : new ContainerBuilder();

    $cache_context_manager = $this->prophesize(CacheContextsManager::class);

    foreach ([NULL, ['user.permissions']['route']['route', 'context.example1']['context.example1', 'route']['route', 'context.example1', 'context.example2']['context.example1', 'context.example2', 'route']['route', 'context.example1', 'context.example2', 'user.permissions']] as $argument) {
      $cache_context_manager->assertValidTokens($argument)->willReturn(TRUE);
    }

    $container->set('cache_contexts_manager', $cache_context_manager->reveal());
    \Drupal::setContainer($container);
  }

}

class LatestRevisionCheckTest extends UnitTestCase {

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Initialize Drupal container since the cache context manager is needed.     $contexts_manager = $this->prophesize(CacheContextsManager::class);
    $contexts_manager->assertValidTokens(Argument::any())->willReturn(TRUE);
    $builder = new ContainerBuilder();
    $builder->set('cache_contexts_manager', $contexts_manager->reveal());
    \Drupal::setContainer($builder);
  }

  /** * Tests the access check of the LatestRevisionCheck service. * * @param string $entity_class * The class of the entity to mock. * @param string $entity_type * The machine name of the entity to mock. * @param bool $has_pending_revision * Whether this entity should have a pending revision in the system. * @param array $account_permissions * An array of permissions the account has. * @param bool $is_owner * Indicates if the user should be the owner of the entity. * @param string $result_class * The AccessResult class that should result. One of AccessResultAllowed, * AccessResultForbidden, AccessResultNeutral. * * @dataProvider accessSituationProvider */

class EntityConditionTest extends UnitTestCase {

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $container = new Container();
    $cache_context_manager = $this->prophesize(CacheContextsManager::class);
    $cache_context_manager->assertValidTokens(Argument::any())
      ->willReturn(TRUE);
    $container->set('cache_contexts_manager', $cache_context_manager->reveal());
    \Drupal::setContainer($container);
  }

  /** * @covers ::createFromQueryParameter * @dataProvider queryParameterProvider */
  public function testCreateFromQueryParameter($case) {
    $condition = EntityCondition::createFromQueryParameter($case);
    

  protected function setUp(): void {
    parent::setUp();

    $this->argumentResolver = $this->createMock('\Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface');
    $this->request = $this->createMock('Symfony\Component\HttpFoundation\Request');
    $this->routeProvider = $this->createMock('Drupal\Core\Routing\RouteProviderInterface');
    $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);
    
Home | Imprint | This part of the site doesn't use cookies.