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