applyContextMapping example

$this->assertSame($this->plugin->reveal()$result);
  }

  /** * @covers ::load */
  public function testLoad() {
    $contexts = [
      'the_context' => $this->prophesize(ContextInterface::class)->reveal(),
    ];

    $this->contextHandler->applyContextMapping($this->plugin, $contexts)->shouldBeCalled();

    $result = $this->manager->load('the_plugin_id', $contexts);
    $this->assertSame($this->plugin->reveal()$result);
  }

  /** * @covers ::load */
  public function testLoadNull() {
    $contexts = [
      'the_context' => $this->prophesize(ContextInterface::class)->reveal(),
    ];
/** * Tests with both contexts mapped to the same user. */
  public function testContextMissing() {
    /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
    $condition = \Drupal::service('plugin.manager.condition')
      ->createInstance('condition_test_optional_context')
      ->setContextMapping([
        'node' => 'node',
      ]);
    \Drupal::service('context.handler')->applyContextMapping($condition[]);
    $this->assertTrue($condition->execute());
  }

  /** * Tests with both contexts mapped to the same user. */
  public function testContextNoValue() {
    /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
    $condition = \Drupal::service('plugin.manager.condition')
      ->createInstance('condition_test_optional_context')
      ->setContextMapping([
        
$ids = array_keys($definitions);
    array_multisort($weights$ids$definitions);
    return $definitions;
  }

  /** * {@inheritdoc} */
  public function load($type, array $contexts = []) {
    $plugin = $this->loadEmpty($type);
    try {
      $this->contextHandler->applyContextMapping($plugin$contexts);
    }
    catch (ContextException $e) {
      return NULL;
    }
    return $plugin;
  }

  /** * {@inheritdoc} */
  public function findByContext(array $contexts, RefinableCacheableDependencyInterface $cacheability) {
    
/** * @covers ::getLayout * @dataProvider providerTestGetLayout */
  public function testGetLayout(array $contexts, bool $should_context_apply) {
    $layout = $this->prophesize(LayoutInterface::class);
    $layout_plugin_manager = $this->prophesize(LayoutPluginManagerInterface::class);
    $layout_plugin_manager->createInstance('layout_onecol', [])->willReturn($layout->reveal());

    $context_handler = $this->prophesize(ContextHandlerInterface::class);
    if ($should_context_apply) {
      $context_handler->applyContextMapping($layout->reveal()$contexts)->shouldBeCalled();
    }
    else {
      $context_handler->applyContextMapping($layout->reveal()$contexts)->shouldNotBeCalled();
    }

    $container = new ContainerBuilder();
    $container->set('plugin.manager.core.layout', $layout_plugin_manager->reveal());
    $container->set('context.handler', $context_handler->reveal());
    \Drupal::setContainer($container);

    $output = $this->section->getLayout($contexts);
    

  public function getPlugin(array $contexts = []) {
    $plugin = $this->pluginManager()->createInstance($this->getPluginId()$this->getConfiguration());
    if ($contexts && $plugin instanceof ContextAwarePluginInterface) {
      $this->contextHandler()->applyContextMapping($plugin$contexts);
    }
    return $plugin;
  }

  /** * Wraps the component plugin manager. * * @return \Drupal\Core\Block\BlockManagerInterface * The plugin manager. */
  protected function pluginManager() {
    

  public function getLayout(array $contexts = []) {
    $layout = $this->layoutPluginManager()->createInstance($this->getLayoutId()$this->layoutSettings);
    if ($contexts) {
      $this->contextHandler()->applyContextMapping($layout$contexts);
    }
    return $layout;
  }

  /** * Gets the layout plugin ID for this section. * * @return string * The layout plugin ID. * * @internal * This method should only be used by code responsible for storing the data. */
// Make sure that the cacheability metadata is passed to the plugin context.     $plugin_context = $this->createMock('Drupal\Core\Plugin\Context\ContextInterface');
    $plugin_context->expects($this->once())
      ->method('addCacheableDependency')
      ->with($context_hit);
    $plugin->expects($this->once())
      ->method('getContext')
      ->with('hit')
      ->willReturn($plugin_context);

    $this->contextHandler->applyContextMapping($plugin$contexts);
  }

  /** * @covers ::applyContextMapping */
  public function testApplyContextMappingMissingRequired() {
    $context = $this->createMock('Drupal\Core\Plugin\Context\ContextInterface');
    $context->expects($this->never())
      ->method('getContextValue');

    $contexts = [
      
/** * @covers ::applyContextMapping */
  public function testApplyContextMapping() {
    $entity = EntityTest::create([]);
    $context_definition = EntityContextDefinition::fromEntity($entity);
    $context = EntityContext::fromEntity($entity);

    $definition = ['context_definitions' => ['a_context_id' => $context_definition]];
    $plugin = new TestContextAwarePlugin([], 'test_plugin_id', $definition);
    (new ContextHandler())->applyContextMapping($plugin['a_context_id' => $context]);

    $result = $plugin->getContext('a_context_id');

    $this->assertInstanceOf(EntityContext::class$result);
    $this->assertSame($context$result);
  }

  /** * @covers ::applyContextMapping */
  public function testApplyContextMappingAlreadyApplied() {
    
    if (!$entity->status()) {
      return AccessResult::forbidden()->addCacheableDependency($entity);
    }
    else {
      $conditions = [];
      $missing_context = FALSE;
      $missing_value = FALSE;
      foreach ($entity->getVisibilityConditions() as $condition_id => $condition) {
        if ($condition instanceof ContextAwarePluginInterface) {
          try {
            $contexts = $this->contextRepository->getRuntimeContexts(array_values($condition->getContextMapping()));
            $this->contextHandler->applyContextMapping($condition$contexts);
          }
          catch (MissingValueContextException $e) {
            $missing_value = TRUE;
          }
          catch (ContextException $e) {
            $missing_context = TRUE;
          }
        }
        $conditions[$condition_id] = $condition;
      }

      

  protected function doTestIdenticalUser() {
    /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
    $condition = \Drupal::service('plugin.manager.condition')
      ->createInstance('condition_test_dual_user')
      // Map the anonymous user to both contexts.       ->setContextMapping([
        'user1' => 'anonymous',
        'user2' => 'anonymous',
      ]);
    $contexts['anonymous'] = EntityContext::fromEntity($this->anonymous);
    \Drupal::service('context.handler')->applyContextMapping($condition$contexts);
    $this->assertTrue($condition->execute());
  }

  /** * Tests with each context mapped to different users. */
  protected function doTestDifferentUser() {
    /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
    $condition = \Drupal::service('plugin.manager.condition')
      ->createInstance('condition_test_dual_user')
      ->setContextMapping([
        

  protected static function buildPreRenderableBlock(BlockInterface $entity, ModuleHandlerInterface $module_handler) {
    $plugin = $entity->getPlugin();
    $plugin_id = $plugin->getPluginId();
    $base_id = $plugin->getBaseId();
    $derivative_id = $plugin->getDerivativeId();
    $configuration = $plugin->getConfiguration();

    // Inject runtime contexts.     if ($plugin instanceof ContextAwarePluginInterface) {
      $contexts = \Drupal::service('context.repository')->getRuntimeContexts($plugin->getContextMapping());
      \Drupal::service('context.handler')->applyContextMapping($plugin$contexts);
    }

    // Create the render array for the block as a whole.     // @see template_preprocess_block().     $build = [
      '#theme' => 'block',
      '#attributes' => [],
      // All blocks get a "Configure block" contextual link.       '#contextual_links' => [
        'block' => [
          'route_parameters' => ['block' => $entity->id()],
        ],
Home | Imprint | This part of the site doesn't use cookies.