From 3a8cb0d5f679d69efde42161e809d3881df14a36 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Thu, 22 Oct 2015 12:34:30 -0700 Subject: [PATCH] Issue #2583135 by Torenware, klausi: Context::getContextData() sometimes returns NULL which violates ContextInterface --- .../Drupal/Core/Plugin/Context/Context.php | 8 +++---- .../Tests/Core/Plugin/Context/ContextTest.php | 24 +++++++++++++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/core/lib/Drupal/Core/Plugin/Context/Context.php b/core/lib/Drupal/Core/Plugin/Context/Context.php index 7b66dcb0f04..3b44ab825b1 100644 --- a/core/lib/Drupal/Core/Plugin/Context/Context.php +++ b/core/lib/Drupal/Core/Plugin/Context/Context.php @@ -121,11 +121,9 @@ class Context extends ComponentContext implements ContextInterface { if (!isset($this->contextData)) { $definition = $this->getContextDefinition(); $default_value = $definition->getDefaultValue(); - if (isset($default_value)) { - // Store the default value so that subsequent calls don't have to look - // it up again. - $this->contextData = $this->getTypedDataManager()->create($definition->getDataDefinition(), $default_value); - } + // Store the default value so that subsequent calls don't have to look + // it up again. + $this->contextData = $this->getTypedDataManager()->create($definition->getDataDefinition(), $default_value); } return $this->contextData; } diff --git a/core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php b/core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php index 892b57450ed..5d1c22d7b1d 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php @@ -56,7 +56,7 @@ class ContextTest extends UnitTestCase { * @covers ::getContextValue */ public function testDefaultValue() { - $this->setUpDefaultValue(); + $this->setUpDefaultValue('test'); $context = new Context($this->contextDefinition); $context->setTypedDataManager($this->typedDataManager); @@ -67,7 +67,18 @@ class ContextTest extends UnitTestCase { * @covers ::getContextData */ public function testDefaultDataValue() { - $this->setUpDefaultValue(); + $this->setUpDefaultValue('test'); + + $context = new Context($this->contextDefinition); + $context->setTypedDataManager($this->typedDataManager); + $this->assertEquals($this->typedData, $context->getContextData()); + } + + /** + * @covers ::getContextData + */ + public function testNullDataValue() { + $this->setUpDefaultValue(NULL); $context = new Context($this->contextDefinition); $context->setTypedDataManager($this->typedDataManager); @@ -127,8 +138,11 @@ class ContextTest extends UnitTestCase { /** * Set up mocks for the getDefaultValue() method call. + * + * @param mixed $default_value + * The default value to assign to the mock context definition. */ - protected function setUpDefaultValue() { + protected function setUpDefaultValue($default_value = NULL) { $mock_data_definition = $this->getMock('Drupal\Core\TypedData\DataDefinitionInterface'); $this->contextDefinition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinitionInterface') @@ -137,7 +151,7 @@ class ContextTest extends UnitTestCase { $this->contextDefinition->expects($this->once()) ->method('getDefaultValue') - ->willReturn('test'); + ->willReturn($default_value); $this->contextDefinition->expects($this->once()) ->method('getDataDefinition') @@ -147,7 +161,7 @@ class ContextTest extends UnitTestCase { $this->typedDataManager->expects($this->once()) ->method('create') - ->with($mock_data_definition, 'test') + ->with($mock_data_definition, $default_value) ->willReturn($this->typedData); } }