Issue #2583135 by Torenware, klausi: Context::getContextData() sometimes returns NULL which violates ContextInterface

8.0.x
Alex Pott 2015-10-22 12:34:30 -07:00
parent 5d450cb8c4
commit 3a8cb0d5f6
2 changed files with 22 additions and 10 deletions

View File

@ -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;
}

View File

@ -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);
}
}