Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / ContextHandlerTest.php
index 4ed5e6eb602fe88c90a954b11c2ba137fae612e1..5cf8d9cc8bf24b9a3b04fb9fa868f32566a4e9df 100644 (file)
@@ -9,12 +9,19 @@ namespace Drupal\Tests\Core\Plugin;
 
 use Drupal\Component\Plugin\ConfigurablePluginInterface;
 use Drupal\Component\Plugin\Exception\ContextException;
+use Drupal\Core\Cache\NullBackend;
+use Drupal\Core\DependencyInjection\ClassResolverInterface;
+use Drupal\Core\DependencyInjection\ContainerBuilder;
+use Drupal\Core\Extension\ModuleHandlerInterface;
 use Drupal\Core\Plugin\Context\ContextDefinition;
 use Drupal\Core\Plugin\Context\ContextHandler;
 use Drupal\Core\Plugin\ContextAwarePluginInterface;
 use Drupal\Core\TypedData\DataDefinition;
 use Drupal\Core\TypedData\Plugin\DataType\StringData;
+use Drupal\Core\TypedData\TypedDataManager;
+use Drupal\Core\Validation\ConstraintManager;
 use Drupal\Tests\UnitTestCase;
+use Prophecy\Argument;
 
 /**
  * @coversDefaultClass \Drupal\Core\Plugin\Context\ContextHandler
@@ -36,6 +43,26 @@ class ContextHandlerTest extends UnitTestCase {
     parent::setUp();
 
     $this->contextHandler = new ContextHandler();
+
+    $namespaces = new \ArrayObject([
+      'Drupal\\Core\\TypedData' => $this->root . '/core/lib/Drupal/Core/TypedData',
+      'Drupal\\Core\\Validation' => $this->root . '/core/lib/Drupal/Core/Validation',
+    ]);
+    $cache_backend = new NullBackend('cache');
+    $module_handler = $this->prophesize(ModuleHandlerInterface::class);
+    $class_resolver = $this->prophesize(ClassResolverInterface::class);
+    $class_resolver->getInstanceFromDefinition(Argument::type('string'))->will(function ($arguments) {
+      $class_name = $arguments[0];
+      return new $class_name();
+    });
+    $type_data_manager = new TypedDataManager($namespaces, $cache_backend, $module_handler->reveal(), $class_resolver->reveal());
+    $type_data_manager->setValidationConstraintManager(
+      new ConstraintManager($namespaces, $cache_backend, $module_handler->reveal())
+    );
+
+    $container = new ContainerBuilder();
+    $container->set('typed_data_manager', $type_data_manager);
+    \Drupal::setContainer($container);
   }
 
   /**
@@ -60,10 +87,10 @@ class ContextHandlerTest extends UnitTestCase {
     $context_any = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
     $context_any->expects($this->atLeastOnce())
       ->method('getContextDefinition')
-      ->will($this->returnValue(new ContextDefinition('empty')));
+      ->will($this->returnValue(new ContextDefinition('any')));
 
-    $requirement_specific = new ContextDefinition('specific');
-    $requirement_specific->setConstraints(['bar' => 'baz']);
+    $requirement_specific = new ContextDefinition('string');
+    $requirement_specific->setConstraints(['Blank' => []]);
 
     $context_constraint_mismatch = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
     $context_constraint_mismatch->expects($this->atLeastOnce())
@@ -74,8 +101,8 @@ class ContextHandlerTest extends UnitTestCase {
       ->method('getContextDefinition')
       ->will($this->returnValue(new ContextDefinition('fuzzy')));
 
-    $context_definition_specific = new ContextDefinition('specific');
-    $context_definition_specific->setConstraints(['bar' => 'baz']);
+    $context_definition_specific = new ContextDefinition('string');
+    $context_definition_specific->setConstraints(['Blank' => []]);
     $context_specific = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
     $context_specific->expects($this->atLeastOnce())
       ->method('getContextDefinition')
@@ -112,13 +139,13 @@ class ContextHandlerTest extends UnitTestCase {
   public function providerTestGetMatchingContexts() {
     $requirement_any = new ContextDefinition();
 
-    $requirement_specific = new ContextDefinition('specific');
-    $requirement_specific->setConstraints(['bar' => 'baz']);
+    $requirement_specific = new ContextDefinition('string');
+    $requirement_specific->setConstraints(['Blank' => []]);
 
     $context_any = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
     $context_any->expects($this->atLeastOnce())
       ->method('getContextDefinition')
-      ->will($this->returnValue(new ContextDefinition('empty')));
+      ->will($this->returnValue(new ContextDefinition('any')));
     $context_constraint_mismatch = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
     $context_constraint_mismatch->expects($this->atLeastOnce())
       ->method('getContextDefinition')
@@ -127,8 +154,8 @@ class ContextHandlerTest extends UnitTestCase {
     $context_datatype_mismatch->expects($this->atLeastOnce())
       ->method('getContextDefinition')
       ->will($this->returnValue(new ContextDefinition('fuzzy')));
-    $context_definition_specific = new ContextDefinition('specific');
-    $context_definition_specific->setConstraints(['bar' => 'baz']);
+    $context_definition_specific = new ContextDefinition('string');
+    $context_definition_specific->setConstraints(['Blank' => []]);
     $context_specific = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
     $context_specific->expects($this->atLeastOnce())
       ->method('getContextDefinition')
@@ -158,7 +185,7 @@ class ContextHandlerTest extends UnitTestCase {
   public function testFilterPluginDefinitionsByContexts($has_context, $definitions, $expected) {
     if ($has_context) {
       $context = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
-      $expected_context_definition = (new ContextDefinition('expected_data_type'))->setConstraints(['expected_constraint_name' => 'expected_constraint_value']);
+      $expected_context_definition = (new ContextDefinition('string'))->setConstraints(['Blank' => []]);
       $context->expects($this->atLeastOnce())
         ->method('getContextDefinition')
         ->will($this->returnValue($expected_context_definition));
@@ -189,7 +216,7 @@ class ContextHandlerTest extends UnitTestCase {
     // No context, all plugins available.
     $data[] = [FALSE, $plugins, $plugins];
 
-    $plugins = ['expected_plugin' => ['context' => ['context1' => new ContextDefinition('expected_data_type')]]];
+    $plugins = ['expected_plugin' => ['context' => ['context1' => new ContextDefinition('string')]]];
     // Missing context, no plugins available.
     $data[] = [FALSE, $plugins, []];
     // Satisfied context, all plugins available.
@@ -206,7 +233,7 @@ class ContextHandlerTest extends UnitTestCase {
     // Optional mismatched constraint, all plugins available.
     $data[] = [FALSE, $plugins, $plugins];
 
-    $expected_context_definition = (new ContextDefinition('expected_data_type'))->setConstraints(['expected_constraint_name' => 'expected_constraint_value']);
+    $expected_context_definition = (new ContextDefinition('string'))->setConstraints(['Blank' => []]);
     $plugins = ['expected_plugin' => ['context' => ['context1' => $expected_context_definition]]];
     // Satisfied context with constraint, all plugins available.
     $data[] = [TRUE, $plugins, $plugins];
@@ -220,7 +247,7 @@ class ContextHandlerTest extends UnitTestCase {
     $unexpected_context_definition = (new ContextDefinition('unexpected_data_type'))->setConstraints(['mismatched_constraint_name' => 'mismatched_constraint_value']);
     $plugins = [
       'unexpected_plugin' => ['context' => ['context1' => $unexpected_context_definition]],
-      'expected_plugin' => ['context' => ['context2' => new ContextDefinition('expected_data_type')]],
+      'expected_plugin' => ['context' => ['context2' => new ContextDefinition('string')]],
     ];
     // Context only satisfies one plugin.
     $data[] = [TRUE, $plugins, ['expected_plugin' => $plugins['expected_plugin']]];