From d4431a10d7573d43aa8e0cdd77fb8785b9858a5f Mon Sep 17 00:00:00 2001
From: webchick <drupal@webchick.net>
Date: Fri, 17 Apr 2015 03:34:51 -0700
Subject: [PATCH] Issue #2467411 by klausi, fago: Context class does not use
 typed data trait correctly, leading to fatal errors

---
 .../Drupal/Core/Plugin/Context/Context.php    |  2 +-
 .../Plugin/Context/ContextTypedDataTest.php   | 69 +++++++++++++++++++
 2 files changed, 70 insertions(+), 1 deletion(-)
 create mode 100644 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTypedDataTest.php

diff --git a/core/lib/Drupal/Core/Plugin/Context/Context.php b/core/lib/Drupal/Core/Plugin/Context/Context.php
index 7cd64aa4532f..fb06a1ec485a 100644
--- a/core/lib/Drupal/Core/Plugin/Context/Context.php
+++ b/core/lib/Drupal/Core/Plugin/Context/Context.php
@@ -46,7 +46,7 @@ public function getContextValue() {
       }
       return NULL;
     }
-    return $this->typedDataManager->getCanonicalRepresentation($this->contextData);
+    return $this->getTypedDataManager()->getCanonicalRepresentation($this->contextData);
   }
 
   /**
diff --git a/core/tests/Drupal/Tests/Core/Plugin/Context/ContextTypedDataTest.php b/core/tests/Drupal/Tests/Core/Plugin/Context/ContextTypedDataTest.php
new file mode 100644
index 000000000000..a5de4a480845
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Plugin/Context/ContextTypedDataTest.php
@@ -0,0 +1,69 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\Tests\Core\Plugin\Context\ContextTypedDataTest.
+ */
+
+namespace Drupal\Tests\Core\Plugin\Context;
+
+use Drupal\Core\DependencyInjection\ContainerBuilder;
+use Drupal\Core\Plugin\Context\Context;
+use Drupal\Core\Plugin\Context\ContextDefinition;
+use Drupal\Core\TypedData\DataDefinition;
+use Drupal\Core\TypedData\Plugin\DataType\StringData;
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * Tests that contexts work properly with the typed data manager.
+ *
+ * @coversDefaultClass \Drupal\Core\Plugin\Context\Context
+ * @group Context
+ */
+class ContextTypedDataTest extends UnitTestCase {
+
+  /**
+   * The typed data object used during testing.
+   *
+   * @var \Drupal\Core\TypedData\Plugin\DataType\StringData
+   */
+  protected $typedData;
+
+  /**
+   * Tests that getting a context value does not throw fatal errors.
+   *
+   * This test ensures that the typed data manager is set correctly on the
+   * Context class.
+   *
+   * @covers ::getContextValue
+   */
+  public function testGetContextValue() {
+    // Prepare a container that holds the typed data manager mock.
+    $typed_data_manager = $this->getMockBuilder('Drupal\Core\TypedData\TypedDataManager')
+      ->disableOriginalConstructor()
+      ->getMock();
+    $typed_data_manager->expects($this->once())
+      ->method('getCanonicalRepresentation')
+      ->will($this->returnCallback(array($this, 'getCanonicalRepresentation')));
+    $container = new ContainerBuilder();
+    $container->set('typed_data_manager', $typed_data_manager);
+    \Drupal::setContainer($container);
+
+    $definition = new ContextDefinition('any');
+    $context = new Context($definition);
+    $data_definition = DataDefinition::create('string');
+    $this->typedData = new StringData($data_definition);
+    $this->typedData->setValue('example string');
+    $context->setContextData($this->typedData);
+    $value = $context->getContextValue();
+    $this->assertSame($value, $this->typedData->getValue());
+  }
+
+  /**
+   * Helper mock callback to return the typed data value.
+   */
+  public function getCanonicalRepresentation() {
+    return $this->typedData->getValue();
+  }
+
+}
-- 
GitLab