function OptionalContextConditionTest::testContextAvailable

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Plugin/Condition/OptionalContextConditionTest.php \Drupal\KernelTests\Core\Plugin\Condition\OptionalContextConditionTest::testContextAvailable()
  2. 10 core/tests/Drupal/KernelTests/Core/Plugin/Condition/OptionalContextConditionTest.php \Drupal\KernelTests\Core\Plugin\Condition\OptionalContextConditionTest::testContextAvailable()
  3. 11.x core/tests/Drupal/KernelTests/Core/Plugin/Condition/OptionalContextConditionTest.php \Drupal\KernelTests\Core\Plugin\Condition\OptionalContextConditionTest::testContextAvailable()

Tests with both contexts mapped to the same user.

File

core/tests/Drupal/KernelTests/Core/Plugin/Condition/OptionalContextConditionTest.php, line 57

Class

OptionalContextConditionTest
Tests a condition with optional context.

Namespace

Drupal\KernelTests\Core\Plugin\Condition

Code

public function testContextAvailable() {
    NodeType::create([
        'type' => 'example',
        'name' => 'Example',
    ])->save();
    
    /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
    $condition = \Drupal::service('plugin.manager.condition')->createInstance('condition_test_optional_context')
        ->setContextMapping([
        'node' => 'node',
    ]);
    $node = Node::create([
        'type' => 'example',
    ]);
    $contexts['node'] = EntityContext::fromEntity($node);
    \Drupal::service('context.handler')->applyContextMapping($condition, $contexts);
    $this->assertFalse($condition->execute());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.