Commit a74640a6 authored by alexpott's avatar alexpott

Issue #2798981 by dawehner, Sam152: ListNormalizer fails to pass on context variable to field items

parent 505eb55f
......@@ -26,7 +26,7 @@ class ListNormalizer extends NormalizerBase {
public function normalize($object, $format = NULL, array $context = array()) {
$attributes = array();
foreach ($object as $fieldItem) {
$attributes[] = $this->serializer->normalize($fieldItem, $format);
$attributes[] = $this->serializer->normalize($fieldItem, $format, $context);
}
return $attributes;
}
......
......@@ -7,6 +7,7 @@
use Drupal\Tests\UnitTestCase;
use Drupal\serialization\Normalizer\ListNormalizer;
use Drupal\Core\TypedData\Plugin\DataType\ItemList;
use Symfony\Component\Serializer\Serializer;
/**
* @coversDefaultClass \Drupal\serialization\Normalizer\ListNormalizer
......@@ -35,13 +36,20 @@ class ListNormalizerTest extends UnitTestCase {
*/
protected $expectedListValues = array('test', 'test', 'test');
/**
* The mocked typed data.
*
* @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Core\TypedData\TypedDataInterface
*/
protected $typedData;
protected function setUp() {
// Mock the TypedDataManager to return a TypedDataInterface mock.
$typed_data = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
$this->typedData = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
$typed_data_manager = $this->getMock(TypedDataManagerInterface::class);
$typed_data_manager->expects($this->any())
->method('getPropertyInstance')
->will($this->returnValue($typed_data));
->will($this->returnValue($this->typedData));
// Set up a mock container as ItemList() will call for the 'typed_data_manager'
// service.
......@@ -73,16 +81,14 @@ public function testSupportsNormalization() {
* Tests the normalize() method.
*/
public function testNormalize() {
$serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer')
->setMethods(array('normalize'))
->getMock();
$serializer->expects($this->exactly(3))
->method('normalize')
->will($this->returnValue('test'));
$serializer = $this->prophesize(Serializer::class);
$serializer->normalize($this->typedData, 'json', ['mu' => 'nu'])
->shouldBeCalledTimes(3)
->willReturn('test');
$this->normalizer->setSerializer($serializer);
$this->normalizer->setSerializer($serializer->reveal());
$normalized = $this->normalizer->normalize($this->list);
$normalized = $this->normalizer->normalize($this->list, 'json', ['mu' => 'nu']);
$this->assertEquals($this->expectedListValues, $normalized);
}
......
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