class ContentEntityNormalizerTest
Same name in other branches
- 9 core/modules/serialization/tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ContentEntityNormalizerTest
- 8.9.x core/modules/serialization/tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ContentEntityNormalizerTest
- 11.x core/modules/serialization/tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ContentEntityNormalizerTest
@coversDefaultClass \Drupal\serialization\Normalizer\ContentEntityNormalizer @group serialization
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\serialization\Unit\Normalizer\ContentEntityNormalizerTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of ContentEntityNormalizerTest
File
-
core/
modules/ serialization/ tests/ src/ Unit/ Normalizer/ ContentEntityNormalizerTest.php, line 24
Namespace
Drupal\Tests\serialization\Unit\NormalizerView source
class ContentEntityNormalizerTest extends UnitTestCase {
/**
* The mock serializer.
*
* @var \Symfony\Component\Serializer\Serializer|\Prophecy\Prophecy\ObjectProphecy
*/
protected $serializer;
/**
* The normalizer under test.
*
* @var \Drupal\serialization\Normalizer\ContentEntityNormalizer
*/
protected $contentEntityNormalizer;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$entity_field_manager = $this->createMock(EntityFieldManagerInterface::class);
$entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
$entity_type_repository = $this->createMock(EntityTypeRepositoryInterface::class);
$this->contentEntityNormalizer = new ContentEntityNormalizer($entity_type_manager, $entity_type_repository, $entity_field_manager);
$this->serializer = $this->prophesize(Serializer::class);
$this->contentEntityNormalizer
->setSerializer($this->serializer
->reveal());
}
/**
* @covers ::supportsNormalization
*/
public function testSupportsNormalization() : void {
$content_mock = $this->createMock('Drupal\\Core\\Entity\\ContentEntityInterface');
$config_mock = $this->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface');
$this->assertTrue($this->contentEntityNormalizer
->supportsNormalization($content_mock));
$this->assertFalse($this->contentEntityNormalizer
->supportsNormalization($config_mock));
}
/**
* Tests the normalize() method.
*
* @covers ::normalize
*/
public function testNormalize() : void {
$this->serializer
->normalize(Argument::type(FieldItemListInterface::class), 'test_format', [
'account' => NULL,
])
->willReturn('test');
$definitions = [
'field_accessible_external' => $this->createMockFieldListItem(TRUE, FALSE),
'field_non-accessible_external' => $this->createMockFieldListItem(FALSE, FALSE),
'field_accessible_internal' => $this->createMockFieldListItem(TRUE, TRUE),
'field_non-accessible_internal' => $this->createMockFieldListItem(FALSE, TRUE),
];
$content_entity_mock = $this->createMockForContentEntity($definitions);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'test_format');
$this->assertArrayHasKey('field_accessible_external', $normalized);
$this->assertEquals('test', $normalized['field_accessible_external']);
$this->assertArrayNotHasKey('field_non-accessible_external', $normalized);
$this->assertArrayNotHasKey('field_accessible_internal', $normalized);
$this->assertArrayNotHasKey('field_non-accessible_internal', $normalized);
}
/**
* Tests the normalize() method with account context passed.
*
* @covers ::normalize
*/
public function testNormalizeWithAccountContext() : void {
$mock_account = $this->createMock('Drupal\\Core\\Session\\AccountInterface');
$context = [
'account' => $mock_account,
];
$this->serializer
->normalize(Argument::type(FieldItemListInterface::class), 'test_format', $context)
->willReturn('test');
// The mock account should get passed directly into the access() method on
// field items from $context['account'].
$definitions = [
'field_1' => $this->createMockFieldListItem(TRUE, FALSE, $mock_account),
'field_2' => $this->createMockFieldListItem(FALSE, FALSE, $mock_account),
];
$content_entity_mock = $this->createMockForContentEntity($definitions);
$normalized = $this->contentEntityNormalizer
->normalize($content_entity_mock, 'test_format', $context);
$this->assertArrayHasKey('field_1', $normalized);
$this->assertEquals('test', $normalized['field_1']);
$this->assertArrayNotHasKey('field_2', $normalized);
}
/**
* Creates a mock content entity.
*
* @param $definitions
* The properties the will be returned.
*
* @return \PHPUnit\Framework\MockObject\MockObject
*/
public function createMockForContentEntity($definitions) {
$content_entity_mock = $this->getMockBuilder(ContentEntityBaseMockableClass::class)
->disableOriginalConstructor()
->onlyMethods([
'getTypedData',
])
->getMock();
$typed_data = $this->prophesize(ComplexDataInterface::class);
$typed_data->getProperties(TRUE)
->willReturn($definitions)
->shouldBeCalled();
$content_entity_mock->expects($this->any())
->method('getTypedData')
->willReturn($typed_data->reveal());
return $content_entity_mock;
}
/**
* Creates a mock field list item.
*
* @param bool $access
* The value that access() will return.
* @param bool $internal
* The value that isInternal() will return.
* @param \Drupal\Core\Session\AccountInterface $user_context
* The user context used for the access check.
*
* @return \Drupal\Core\Field\FieldItemListInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected function createMockFieldListItem($access, $internal, ?AccountInterface $user_context = NULL) {
$data_definition = $this->prophesize(DataDefinitionInterface::class);
$mock = $this->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$mock->expects($this->once())
->method('getDataDefinition')
->willReturn($data_definition->reveal());
$data_definition->isInternal()
->willReturn($internal)
->shouldBeCalled();
if (!$internal) {
$mock->expects($this->once())
->method('access')
->with('view', $user_context)
->willReturn($access);
}
return $mock;
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
ContentEntityNormalizerTest::$contentEntityNormalizer | protected | property | The normalizer under test. | |||
ContentEntityNormalizerTest::$serializer | protected | property | The mock serializer. | |||
ContentEntityNormalizerTest::createMockFieldListItem | protected | function | Creates a mock field list item. | |||
ContentEntityNormalizerTest::createMockForContentEntity | public | function | Creates a mock content entity. | |||
ContentEntityNormalizerTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
ContentEntityNormalizerTest::testNormalize | public | function | Tests the normalize() method. | |||
ContentEntityNormalizerTest::testNormalizeWithAccountContext | public | function | Tests the normalize() method with account context passed. | |||
ContentEntityNormalizerTest::testSupportsNormalization | public | function | @covers ::supportsNormalization | |||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |||
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |||
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
UnitTestCase::$root | protected | property | The app root. | 1 | ||
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |||
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::setUpBeforeClass | public static | function | ||||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.