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

8.3.x
Alex Pott 2016-10-12 12:38:18 +01:00
parent 505eb55fa4
commit a74640a640
2 changed files with 17 additions and 11 deletions

View File

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

View File

@ -7,6 +7,7 @@ use Drupal\Core\TypedData\TypedDataManagerInterface;
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 @@ class ListNormalizerTest extends UnitTestCase {
* 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);
}