Skip to content
Snippets Groups Projects
Verified Commit b1ad4a8d authored by Lee Rowlands's avatar Lee Rowlands
Browse files

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

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