3 namespace Drupal\Tests\Core\Plugin\Context;
5 use Drupal\Core\Cache\NullBackend;
6 use Drupal\Core\DependencyInjection\ClassResolverInterface;
7 use Drupal\Core\DependencyInjection\ContainerBuilder;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Plugin\Context\Context;
10 use Drupal\Core\Plugin\Context\ContextDefinition;
11 use Drupal\Core\TypedData\TypedDataManager;
12 use Drupal\Core\Validation\ConstraintManager;
13 use Drupal\Tests\Core\Plugin\Fixtures\InheritedContextDefinition;
14 use Drupal\Tests\UnitTestCase;
15 use Prophecy\Argument;
18 * @coversDefaultClass \Drupal\Core\Plugin\Context\ContextDefinition
21 class ContextDefinitionIsSatisfiedTest extends UnitTestCase {
26 protected function setUp() {
29 $namespaces = new \ArrayObject([
30 'Drupal\\Core\\TypedData' => $this->root . '/core/lib/Drupal/Core/TypedData',
31 'Drupal\\Core\\Validation' => $this->root . '/core/lib/Drupal/Core/Validation',
33 $cache_backend = new NullBackend('cache');
34 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
36 $class_resolver = $this->prophesize(ClassResolverInterface::class);
37 $class_resolver->getInstanceFromDefinition(Argument::type('string'))->will(function ($arguments) {
38 $class_name = $arguments[0];
39 return new $class_name();
42 $type_data_manager = new TypedDataManager($namespaces, $cache_backend, $module_handler->reveal(), $class_resolver->reveal());
43 $type_data_manager->setValidationConstraintManager(new ConstraintManager($namespaces, $cache_backend, $module_handler->reveal()));
45 $container = new ContainerBuilder();
46 $container->set('typed_data_manager', $type_data_manager);
47 \Drupal::setContainer($container);
51 * Tests that context requirements is satisfied as expected.
53 * @param bool $expected
54 * The expected outcome.
55 * @param \Drupal\Core\Plugin\Context\ContextDefinition $requirement
56 * The requirement to check against.
57 * @param \Drupal\Core\Plugin\Context\ContextDefinition $definition
58 * The context definition to check.
60 * (optional) The value to set on the context, defaults to NULL.
62 * @covers ::isSatisfiedBy
63 * @covers ::getSampleValues
64 * @covers ::getConstraintObjects
66 * @dataProvider providerTestIsSatisfiedBy
68 public function testIsSatisfiedBy($expected, ContextDefinition $requirement, ContextDefinition $definition, $value = NULL) {
69 $context = new Context($definition, $value);
70 $this->assertSame($expected, $requirement->isSatisfiedBy($context));
74 * Provides test data for ::testIsSatisfiedBy().
76 public function providerTestIsSatisfiedBy() {
82 new ContextDefinition('any'),
83 new ContextDefinition('any'),
85 $data['requirement any'] = [
87 new ContextDefinition('any'),
88 new ContextDefinition('integer'),
90 $data['integer, out of range'] = [
92 (new ContextDefinition('integer'))->addConstraint('Range', ['min' => 0, 'max' => 10]),
93 new ContextDefinition('integer'),
96 $data['integer, within range'] = [
98 (new ContextDefinition('integer'))->addConstraint('Range', ['min' => 0, 'max' => 10]),
99 new ContextDefinition('integer'),
102 $data['integer, no value'] = [
104 (new ContextDefinition('integer'))->addConstraint('Range', ['min' => 0, 'max' => 10]),
105 new ContextDefinition('integer'),
107 $data['non-integer, within range'] = [
109 (new ContextDefinition('integer'))->addConstraint('Range', ['min' => 0, 'max' => 10]),
110 new ContextDefinition('any'),
113 // Inherited context definition class.
114 $data['both any, inherited context requirement definition'] = [
116 new InheritedContextDefinition('any'),
117 new ContextDefinition('any'),