Pull merge.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / Context / ContextDefinitionIsSatisfiedTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin\Context;
4
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;
16
17 /**
18  * @coversDefaultClass \Drupal\Core\Plugin\Context\ContextDefinition
19  * @group Plugin
20  */
21 class ContextDefinitionIsSatisfiedTest extends UnitTestCase {
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
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',
32     ]);
33     $cache_backend = new NullBackend('cache');
34     $module_handler = $this->prophesize(ModuleHandlerInterface::class);
35
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();
40     });
41
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()));
44
45     $container = new ContainerBuilder();
46     $container->set('typed_data_manager', $type_data_manager);
47     \Drupal::setContainer($container);
48   }
49
50   /**
51    * Tests that context requirements is satisfied as expected.
52    *
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.
59    * @param mixed $value
60    *   (optional) The value to set on the context, defaults to NULL.
61    *
62    * @covers ::isSatisfiedBy
63    * @covers ::getSampleValues
64    * @covers ::getConstraintObjects
65    *
66    * @dataProvider providerTestIsSatisfiedBy
67    */
68   public function testIsSatisfiedBy($expected, ContextDefinition $requirement, ContextDefinition $definition, $value = NULL) {
69     $context = new Context($definition, $value);
70     $this->assertSame($expected, $requirement->isSatisfiedBy($context));
71   }
72
73   /**
74    * Provides test data for ::testIsSatisfiedBy().
75    */
76   public function providerTestIsSatisfiedBy() {
77     $data = [];
78
79     // Simple data types.
80     $data['both any'] = [
81       TRUE,
82       new ContextDefinition('any'),
83       new ContextDefinition('any'),
84     ];
85     $data['requirement any'] = [
86       TRUE,
87       new ContextDefinition('any'),
88       new ContextDefinition('integer'),
89     ];
90     $data['integer, out of range'] = [
91       FALSE,
92       (new ContextDefinition('integer'))->addConstraint('Range', ['min' => 0, 'max' => 10]),
93       new ContextDefinition('integer'),
94       20,
95     ];
96     $data['integer, within range'] = [
97       TRUE,
98       (new ContextDefinition('integer'))->addConstraint('Range', ['min' => 0, 'max' => 10]),
99       new ContextDefinition('integer'),
100       5,
101     ];
102     $data['integer, no value'] = [
103       TRUE,
104       (new ContextDefinition('integer'))->addConstraint('Range', ['min' => 0, 'max' => 10]),
105       new ContextDefinition('integer'),
106     ];
107     $data['non-integer, within range'] = [
108       FALSE,
109       (new ContextDefinition('integer'))->addConstraint('Range', ['min' => 0, 'max' => 10]),
110       new ContextDefinition('any'),
111       5,
112     ];
113     // Inherited context definition class.
114     $data['both any, inherited context requirement definition'] = [
115       TRUE,
116       new InheritedContextDefinition('any'),
117       new ContextDefinition('any'),
118     ];
119
120     return $data;
121   }
122
123 }