Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663
This commit is contained in:
parent
eb34d130a8
commit
f32e58e4b1
8476 changed files with 211648 additions and 170042 deletions
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\serialization\Unit\Normalizer;
|
||||
|
||||
use Drupal\Core\TypedData\ComplexDataInterface;
|
||||
use Drupal\Core\TypedData\TraversableTypedDataInterface;
|
||||
use Drupal\serialization\Normalizer\ComplexDataNormalizer;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\serialization\Normalizer\ComplexDataNormalizer
|
||||
* @group serialization
|
||||
*/
|
||||
class ComplexDataNormalizerTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* Test format string.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const TEST_FORMAT = 'test_format';
|
||||
|
||||
/**
|
||||
* The Complex data normalizer under test.
|
||||
*
|
||||
* @var \Drupal\serialization\Normalizer\ComplexDataNormalizer
|
||||
*/
|
||||
protected $normalizer;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->normalizer = new ComplexDataNormalizer();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::supportsNormalization
|
||||
*/
|
||||
public function testSupportsNormalization() {
|
||||
$this->assertTrue($this->normalizer->supportsNormalization(new TestComplexData()));
|
||||
// Also test that an object not implementing ComplexDataInterface fails.
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::normalize
|
||||
*/
|
||||
public function testNormalize() {
|
||||
$context = ['test' => 'test'];
|
||||
|
||||
$serializer_prophecy = $this->prophesize(Serializer::class);
|
||||
|
||||
$serializer_prophecy->normalize('A', static::TEST_FORMAT, $context)
|
||||
->shouldBeCalled();
|
||||
$serializer_prophecy->normalize('B', static::TEST_FORMAT, $context)
|
||||
->shouldBeCalled();
|
||||
|
||||
$this->normalizer->setSerializer($serializer_prophecy->reveal());
|
||||
|
||||
$complex_data = new TestComplexData(['a' => 'A', 'b' => 'B']);
|
||||
$this->normalizer->normalize($complex_data, static::TEST_FORMAT, $context);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test class implementing ComplexDataInterface and IteratorAggregate.
|
||||
*/
|
||||
class TestComplexData implements \IteratorAggregate, ComplexDataInterface {
|
||||
|
||||
private $values;
|
||||
|
||||
public function __construct(array $values = []) {
|
||||
$this->values = $values;
|
||||
}
|
||||
|
||||
public function getIterator() {
|
||||
return new \ArrayIterator($this->values);
|
||||
}
|
||||
|
||||
public function applyDefaultValue($notify = TRUE) {
|
||||
}
|
||||
|
||||
public static function createInstance($definition, $name = NULL, TraversableTypedDataInterface $parent = NULL) {
|
||||
}
|
||||
|
||||
public function get($property_name) {
|
||||
}
|
||||
|
||||
public function getConstraints() {
|
||||
}
|
||||
|
||||
public function getDataDefinition() {
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
}
|
||||
|
||||
public function getParent() {
|
||||
}
|
||||
|
||||
public function getProperties($include_computed = FALSE) {
|
||||
}
|
||||
|
||||
public function getPropertyPath() {
|
||||
}
|
||||
|
||||
public function getRoot() {
|
||||
}
|
||||
|
||||
public function getString() {
|
||||
}
|
||||
|
||||
public function getValue() {
|
||||
}
|
||||
|
||||
public function isEmpty() {
|
||||
}
|
||||
|
||||
public function onChange($name) {
|
||||
}
|
||||
|
||||
public function set($property_name, $value, $notify = TRUE) {
|
||||
}
|
||||
|
||||
public function setContext($name = NULL, TraversableTypedDataInterface $parent = NULL) {
|
||||
}
|
||||
|
||||
public function setValue($value, $notify = TRUE) {
|
||||
}
|
||||
|
||||
public function toArray() {
|
||||
}
|
||||
|
||||
public function validate() {
|
||||
}
|
||||
|
||||
}
|
|
@ -99,14 +99,14 @@ class EntityNormalizerTest extends UnitTestCase {
|
|||
*
|
||||
* @covers ::denormalize
|
||||
*/
|
||||
public function testDenormalizeWithBundle() {
|
||||
$test_data = array(
|
||||
public function testDenormalizeWithValidBundle() {
|
||||
$test_data = [
|
||||
'key_1' => 'value_1',
|
||||
'key_2' => 'value_2',
|
||||
'test_type' => array(
|
||||
array('value' => 'test_bundle'),
|
||||
),
|
||||
);
|
||||
'test_type' => [
|
||||
['name' => 'test_bundle'],
|
||||
],
|
||||
];
|
||||
|
||||
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
|
||||
$entity_type->expects($this->once())
|
||||
|
@ -117,11 +117,47 @@ class EntityNormalizerTest extends UnitTestCase {
|
|||
->method('getKey')
|
||||
->with('bundle')
|
||||
->will($this->returnValue('test_type'));
|
||||
$entity_type->expects($this->once())
|
||||
->method('getBundleEntityType')
|
||||
->will($this->returnValue('test_bundle'));
|
||||
|
||||
$this->entityManager->expects($this->once())
|
||||
$entity_type_storage_definition = $this->getmock('Drupal\Core\Field\FieldStorageDefinitionInterface');
|
||||
$entity_type_storage_definition->expects($this->once())
|
||||
->method('getMainPropertyName')
|
||||
->will($this->returnValue('name'));
|
||||
|
||||
$entity_type_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
|
||||
$entity_type_definition->expects($this->once())
|
||||
->method('getFieldStorageDefinition')
|
||||
->will($this->returnValue($entity_type_storage_definition));
|
||||
|
||||
$base_definitions = [
|
||||
'test_type' => $entity_type_definition,
|
||||
];
|
||||
|
||||
$this->entityManager->expects($this->at(0))
|
||||
->method('getDefinition')
|
||||
->with('test')
|
||||
->will($this->returnValue($entity_type));
|
||||
$this->entityManager->expects($this->at(1))
|
||||
->method('getBaseFieldDefinitions')
|
||||
->with('test')
|
||||
->will($this->returnValue($base_definitions));
|
||||
|
||||
$entity_query_mock = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
|
||||
$entity_query_mock->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(['test_bundle' => 'test_bundle']));
|
||||
|
||||
$entity_type_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
|
||||
$entity_type_storage->expects($this->once())
|
||||
->method('getQuery')
|
||||
->will($this->returnValue($entity_query_mock));
|
||||
|
||||
$this->entityManager->expects($this->at(2))
|
||||
->method('getStorage')
|
||||
->with('test_bundle')
|
||||
->will($this->returnValue($entity_type_storage));
|
||||
|
||||
// The expected test data should have a modified test_type property.
|
||||
$expected_test_data = array(
|
||||
|
@ -136,12 +172,83 @@ class EntityNormalizerTest extends UnitTestCase {
|
|||
->with($expected_test_data)
|
||||
->will($this->returnValue($this->getMock('Drupal\Core\Entity\EntityInterface')));
|
||||
|
||||
$this->entityManager->expects($this->once())
|
||||
$this->entityManager->expects($this->at(3))
|
||||
->method('getStorage')
|
||||
->with('test')
|
||||
->will($this->returnValue($storage));
|
||||
|
||||
$this->assertNotNull($this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, array('entity_type' => 'test')));
|
||||
$this->assertNotNull($this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, ['entity_type' => 'test']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the denormalize method with a bundle property.
|
||||
*
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*
|
||||
* @covers ::denormalize
|
||||
*/
|
||||
public function testDenormalizeWithInvalidBundle() {
|
||||
$test_data = [
|
||||
'key_1' => 'value_1',
|
||||
'key_2' => 'value_2',
|
||||
'test_type' => [
|
||||
['name' => 'test_bundle'],
|
||||
],
|
||||
];
|
||||
|
||||
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
|
||||
$entity_type->expects($this->once())
|
||||
->method('hasKey')
|
||||
->with('bundle')
|
||||
->will($this->returnValue(TRUE));
|
||||
$entity_type->expects($this->once())
|
||||
->method('getKey')
|
||||
->with('bundle')
|
||||
->will($this->returnValue('test_type'));
|
||||
$entity_type->expects($this->once())
|
||||
->method('getBundleEntityType')
|
||||
->will($this->returnValue('test_bundle'));
|
||||
|
||||
$entity_type_storage_definition = $this->getmock('Drupal\Core\Field\FieldStorageDefinitionInterface');
|
||||
$entity_type_storage_definition->expects($this->once())
|
||||
->method('getMainPropertyName')
|
||||
->will($this->returnValue('name'));
|
||||
|
||||
$entity_type_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
|
||||
$entity_type_definition->expects($this->once())
|
||||
->method('getFieldStorageDefinition')
|
||||
->will($this->returnValue($entity_type_storage_definition));
|
||||
|
||||
$base_definitions = [
|
||||
'test_type' => $entity_type_definition,
|
||||
];
|
||||
|
||||
$this->entityManager->expects($this->at(0))
|
||||
->method('getDefinition')
|
||||
->with('test')
|
||||
->will($this->returnValue($entity_type));
|
||||
$this->entityManager->expects($this->at(1))
|
||||
->method('getBaseFieldDefinitions')
|
||||
->with('test')
|
||||
->will($this->returnValue($base_definitions));
|
||||
|
||||
$entity_query_mock = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
|
||||
$entity_query_mock->expects($this->once())
|
||||
->method('execute')
|
||||
->will($this->returnValue(['test_bundle_other' => 'test_bundle_other']));
|
||||
|
||||
$entity_type_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
|
||||
$entity_type_storage->expects($this->once())
|
||||
->method('getQuery')
|
||||
->will($this->returnValue($entity_query_mock));
|
||||
|
||||
$this->entityManager->expects($this->at(2))
|
||||
->method('getStorage')
|
||||
->with('test_bundle')
|
||||
->will($this->returnValue($entity_type_storage));
|
||||
|
||||
|
||||
$this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, ['entity_type' => 'test']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -179,6 +286,9 @@ class EntityNormalizerTest extends UnitTestCase {
|
|||
->with('test')
|
||||
->will($this->returnValue($storage));
|
||||
|
||||
$this->entityManager->expects($this->never())
|
||||
->method('getBaseFieldDefinitions');
|
||||
|
||||
$this->assertNotNull($this->entityNormalizer->denormalize($test_data, 'Drupal\Core\Entity\ContentEntityBase', NULL, array('entity_type' => 'test')));
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\serialization\Unit\Normalizer\EntityReferenceFieldItemNormalizerTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\serialization\Unit\Normalizer;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInterface;
|
||||
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
|
||||
use Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Prophecy\Argument;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer
|
||||
* @group serialization
|
||||
*/
|
||||
class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The mock serializer.
|
||||
*
|
||||
* @var \Symfony\Component\Serializer\SerializerInterface|\Prophecy\Prophecy\ObjectProphecy
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* The normalizer under test.
|
||||
*
|
||||
* @var \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer
|
||||
*/
|
||||
protected $normalizer;
|
||||
|
||||
/**
|
||||
* The mock field item.
|
||||
*
|
||||
* @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem|\Prophecy\Prophecy\ObjectProphecy
|
||||
*/
|
||||
protected $fieldItem;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->normalizer = new EntityReferenceFieldItemNormalizer();
|
||||
|
||||
$this->serializer = $this->prophesize(Serializer::class);
|
||||
// Set up the serializer to return an entity property.
|
||||
$this->serializer->normalize(Argument::cetera())
|
||||
->willReturn(['value' => 'test']);
|
||||
|
||||
$this->normalizer->setSerializer($this->serializer->reveal());
|
||||
|
||||
$this->fieldItem = $this->prophesize(EntityReferenceItem::class);
|
||||
$this->fieldItem->getIterator()
|
||||
->willReturn(new \ArrayIterator(['target_id' => []]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::supportsNormalization
|
||||
*/
|
||||
public function testSupportsNormalization() {
|
||||
$this->assertTrue($this->normalizer->supportsNormalization($this->fieldItem->reveal()));
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::normalize
|
||||
*/
|
||||
public function testNormalize() {
|
||||
$test_url = '/test/100';
|
||||
|
||||
$entity = $this->prophesize(EntityInterface::class);
|
||||
$entity->url('canonical')
|
||||
->willReturn($test_url)
|
||||
->shouldBeCalled();
|
||||
|
||||
$entity_reference = $this->prophesize(TypedDataInterface::class);
|
||||
$entity_reference->getValue()
|
||||
->willReturn($entity->reveal())
|
||||
->shouldBeCalled();
|
||||
|
||||
$this->fieldItem->get('entity')
|
||||
->willReturn($entity_reference)
|
||||
->shouldBeCalled();
|
||||
|
||||
$normalized = $this->normalizer->normalize($this->fieldItem->reveal());
|
||||
|
||||
$expected = [
|
||||
'target_id' => ['value' => 'test'],
|
||||
'url' => $test_url,
|
||||
];
|
||||
$this->assertSame($expected, $normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::normalize
|
||||
*/
|
||||
public function testNormalizeWithNoEntity() {
|
||||
$entity_reference = $this->prophesize(TypedDataInterface::class);
|
||||
$entity_reference->getValue()
|
||||
->willReturn(NULL)
|
||||
->shouldBeCalled();
|
||||
|
||||
$this->fieldItem->get('entity')
|
||||
->willReturn($entity_reference->reveal())
|
||||
->shouldBeCalled();
|
||||
|
||||
$normalized = $this->normalizer->normalize($this->fieldItem->reveal());
|
||||
|
||||
$expected = [
|
||||
'target_id' => ['value' => 'test'],
|
||||
];
|
||||
$this->assertSame($expected, $normalized);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue