Unverified Commit c0c662c4 authored by larowlan's avatar larowlan

Issue #2986735 by phenaproxima, Berdir, tim.plunkett, MegaChriz: ...

Issue #2986735 by phenaproxima, Berdir, tim.plunkett, MegaChriz:  Drupal\Core\Plugin\Context\Context needs DependencySerializationTrait

(cherry picked from commit b1ad4a8d)
parent afd686b4
......@@ -6,6 +6,7 @@
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\TypedData\TypedDataTrait;
......@@ -15,6 +16,7 @@
class Context extends ComponentContext implements ContextInterface {
use TypedDataTrait;
use DependencySerializationTrait;
/**
* The data associated with the context.
......
<?php
namespace Drupal\Tests\Core\Plugin\Context;
namespace Drupal\KernelTests\Core\Plugin;
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\Core\TypedData\TypedDataManagerInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests that contexts work properly with the typed data manager.
......@@ -16,14 +15,24 @@
* @coversDefaultClass \Drupal\Core\Plugin\Context\Context
* @group Context
*/
class ContextTypedDataTest extends UnitTestCase {
class ContextTypedDataTest extends KernelTestBase {
/**
* The typed data object used during testing.
*
* @var \Drupal\Core\TypedData\Plugin\DataType\StringData
* Tests that contexts can be serialized.
*/
protected $typedData;
public function testSerialize() {
$definition = new ContextDefinition('any');
$data_definition = DataDefinition::create('string');
$typed_data = new StringData($data_definition);
$typed_data->setValue('example string');
$context = new Context($definition, $typed_data);
// getContextValue() will cause the context to reference the typed data
// manager service.
$value = $context->getContextValue();
$context = serialize($context);
$context = unserialize($context);
$this->assertSame($value, $context->getContextValue());
}
/**
* Tests that getting a context value does not throw fatal errors.
......@@ -34,29 +43,23 @@ class ContextTypedDataTest extends UnitTestCase {
* @covers ::getContextValue
*/
public function testGetContextValue() {
$data_definition = DataDefinition::create('string');
$typed_data = new StringData($data_definition);
$typed_data->setValue('example string');
// Prepare a container that holds the typed data manager mock.
$typed_data_manager = $this->getMock(TypedDataManagerInterface::class);
$typed_data_manager->expects($this->once())
->method('getCanonicalRepresentation')
->will($this->returnCallback([$this, 'getCanonicalRepresentation']));
$container = new ContainerBuilder();
$container->set('typed_data_manager', $typed_data_manager);
\Drupal::setContainer($container);
$typed_data_manager = $this->prophesize(TypedDataManagerInterface::class);
$typed_data_manager->getCanonicalRepresentation($typed_data)
->shouldBeCalledOnce()
->will(function () use ($typed_data) {
return $typed_data->getValue();
});
$this->container->set('typed_data_manager', $typed_data_manager->reveal());
$definition = new ContextDefinition('any');
$data_definition = DataDefinition::create('string');
$this->typedData = new StringData($data_definition);
$this->typedData->setValue('example string');
$context = new Context($definition, $this->typedData);
$context = new Context($definition, $typed_data);
$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();
$this->assertSame($value, $typed_data->getValue());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment