OptionalContextConditionTest.php in Drupal 9
File
core/tests/Drupal/KernelTests/Core/Plugin/Condition/OptionalContextConditionTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Plugin\Condition;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
class OptionalContextConditionTest extends KernelTestBase {
protected static $modules = [
'system',
'user',
'condition_test',
'node',
];
public function testContextMissing() {
$condition = \Drupal::service('plugin.manager.condition')
->createInstance('condition_test_optional_context')
->setContextMapping([
'node' => 'node',
]);
\Drupal::service('context.handler')
->applyContextMapping($condition, []);
$this
->assertTrue($condition
->execute());
}
public function testContextNoValue() {
$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());
}
public function testContextAvailable() {
NodeType::create([
'type' => 'example',
'name' => 'Example',
])
->save();
$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());
}
}