class OptionalContextConditionTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/KernelTests/Core/Plugin/Condition/OptionalContextConditionTest.php \Drupal\KernelTests\Core\Plugin\Condition\OptionalContextConditionTest
- 10 core/tests/Drupal/KernelTests/Core/Plugin/Condition/OptionalContextConditionTest.php \Drupal\KernelTests\Core\Plugin\Condition\OptionalContextConditionTest
- 8.9.x core/tests/Drupal/KernelTests/Core/Plugin/Condition/OptionalContextConditionTest.php \Drupal\KernelTests\Core\Plugin\Condition\OptionalContextConditionTest
Tests a condition with optional context.
@group condition_test
Hierarchy
- class \Drupal\KernelTests\KernelTestBase extends \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\Core\Plugin\Condition\OptionalContextConditionTest implements \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of OptionalContextConditionTest
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Plugin/ Condition/ OptionalContextConditionTest.php, line 17
Namespace
Drupal\KernelTests\Core\Plugin\ConditionView source
class OptionalContextConditionTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'user',
'condition_test',
'node',
];
/**
* 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([
'node' => 'node',
]);
$definition = EntityContextDefinition::fromEntityTypeId('node');
$contexts['node'] = new Context($definition);
\Drupal::service('context.handler')->applyContextMapping($condition, $contexts);
$this->assertTrue($condition->execute());
}
/**
* Tests with both contexts mapped to the same user.
*/
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.