class DenormalizeTest
Same name and namespace in other branches
- 9 core/modules/hal/tests/src/Kernel/DenormalizeTest.php \Drupal\Tests\hal\Kernel\DenormalizeTest
Tests HAL denormalization edge cases for EntityResource.
@group hal
Hierarchy
- class \Drupal\KernelTests\KernelTestBase extends \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\AssertHelperTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\PhpunitCompatibilityTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\Tests\hal\Kernel\NormalizerTestBase implements \Drupal\KernelTests\KernelTestBase
- class \Drupal\Tests\hal\Kernel\DenormalizeTest implements \Drupal\Tests\hal\Kernel\NormalizerTestBase
- class \Drupal\Tests\hal\Kernel\NormalizerTestBase implements \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of DenormalizeTest
File
-
core/
modules/ hal/ tests/ src/ Kernel/ DenormalizeTest.php, line 15
Namespace
Drupal\Tests\hal\KernelView source
class DenormalizeTest extends NormalizerTestBase {
/**
* Tests that the type link relation in incoming data is handled correctly.
*/
public function testTypeHandling() {
// Valid type.
$data_with_valid_type = [
'_links' => [
'type' => [
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', [
'absolute' => TRUE,
])->toString(),
],
],
];
$denormalized = $this->serializer
->denormalize($data_with_valid_type, $this->entityClass, $this->format);
$this->assertEqual(get_class($denormalized), $this->entityClass, 'Request with valid type results in creation of correct bundle.');
// Multiple types.
$data_with_multiple_types = [
'_links' => [
'type' => [
[
'href' => Url::fromUri('base:rest/types/foo', [
'absolute' => TRUE,
])->toString(),
],
[
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', [
'absolute' => TRUE,
])->toString(),
],
],
],
];
$denormalized = $this->serializer
->denormalize($data_with_multiple_types, $this->entityClass, $this->format);
$this->assertEqual(get_class($denormalized), $this->entityClass, 'Request with multiple types results in creation of correct bundle.');
// Invalid type.
$data_with_invalid_type = [
'_links' => [
'type' => [
'href' => Url::fromUri('base:rest/types/foo', [
'absolute' => TRUE,
])->toString(),
],
],
];
try {
$this->serializer
->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
$this->fail('Exception should be thrown when type is invalid.');
} catch (UnexpectedValueException $e) {
// Expected exception; just continue testing.
}
// No type.
$data_with_no_type = [
'_links' => [],
];
try {
$this->serializer
->denormalize($data_with_no_type, $this->entityClass, $this->format);
$this->fail('Exception should be thrown when no type is provided.');
} catch (UnexpectedValueException $e) {
// Expected exception; just continue testing.
}
}
/**
* Tests link relation handling with an invalid type.
*/
public function testTypeHandlingWithInvalidType() {
$data_with_invalid_type = [
'_links' => [
'type' => [
'href' => Url::fromUri('base:rest/type/entity_test/entity_test_invalid', [
'absolute' => TRUE,
])->toString(),
],
],
];
$this->expectException(UnexpectedValueException::class);
$this->serializer
->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
}
/**
* Tests link relation handling with no types.
*/
public function testTypeHandlingWithNoTypes() {
$data_with_no_types = [
'_links' => [
'type' => [],
],
];
$this->expectException(UnexpectedValueException::class);
$this->serializer
->denormalize($data_with_no_types, $this->entityClass, $this->format);
}
/**
* Test that a field set to an empty array is different than an absent field.
*/
public function testMarkFieldForDeletion() {
// Add a default value for a field.
$field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test_text');
$field->setDefaultValue([
[
'value' => 'Llama',
],
]);
$field->save();
// Denormalize data that contains no entry for the field, and check that
// the default value is present in the resulting entity.
$data = [
'_links' => [
'type' => [
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', [
'absolute' => TRUE,
])->toString(),
],
],
];
$entity = $this->serializer
->denormalize($data, $this->entityClass, $this->format);
$this->assertEqual($entity->field_test_text
->count(), 1);
$this->assertEqual($entity->field_test_text->value, 'Llama');
// Denormalize data that contains an empty entry for the field, and check
// that the field is empty in the resulting entity.
$data = [
'_links' => [
'type' => [
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', [
'absolute' => TRUE,
])->toString(),
],
],
'field_test_text' => [],
];
$entity = $this->serializer
->denormalize($data, get_class($entity), $this->format, [
'target_instance' => $entity,
]);
$this->assertEqual($entity->field_test_text
->count(), 0);
}
/**
* Tests normalizing/denormalizing serialized columns.
*/
public function testDenormalizeSerializedItem() {
$entity = EntitySerializedField::create([
'serialized' => 'boo',
]);
$normalized = $this->serializer
->normalize($entity, $this->format);
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The generic FieldItemNormalizer cannot denormalize string values for "value" properties of the "serialized" field (field item class: Drupal\\entity_test\\Plugin\\Field\\FieldType\\SerializedItem).');
$this->serializer
->denormalize($normalized, EntitySerializedField::class, $this->format);
}
/**
* Tests normalizing/denormalizing invalid custom serialized fields.
*/
public function testDenormalizeInvalidCustomSerializedField() {
$entity = EntitySerializedField::create([
'serialized_long' => serialize([
'Hello world!',
]),
]);
$normalized = $this->serializer
->normalize($entity);
$this->assertEquals($normalized['serialized_long'][0]['value'], [
'Hello world!',
]);
$normalized['serialized_long'][0]['value'] = 'boo';
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The generic FieldItemNormalizer cannot denormalize string values for "value" properties of the "serialized_long" field (field item class: Drupal\\Core\\Field\\Plugin\\Field\\FieldType\\StringLongItem).');
$this->serializer
->denormalize($normalized, EntitySerializedField::class);
}
/**
* Tests normalizing/denormalizing empty custom serialized fields.
*/
public function testDenormalizeEmptyCustomSerializedField() {
$entity = EntitySerializedField::create([
'serialized_long' => serialize([]),
]);
$normalized = $this->serializer
->normalize($entity);
$this->assertEquals([], $normalized['serialized_long'][0]['value']);
$entity = $this->serializer
->denormalize($normalized, EntitySerializedField::class);
$this->assertEquals(serialize([]), $entity->get('serialized_long')->value);
}
/**
* Tests normalizing/denormalizing valid custom serialized fields.
*/
public function testDenormalizeValidCustomSerializedField() {
$entity = EntitySerializedField::create([
'serialized_long' => serialize([
'key' => 'value',
]),
]);
$normalized = $this->serializer
->normalize($entity);
$this->assertEquals([
'key' => 'value',
], $normalized['serialized_long'][0]['value']);
$entity = $this->serializer
->denormalize($normalized, EntitySerializedField::class);
$this->assertEquals(serialize([
'key' => 'value',
]), $entity->get('serialized_long')->value);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
DenormalizeTest::testDenormalizeEmptyCustomSerializedField | public | function | Tests normalizing/denormalizing empty custom serialized fields. | ||
DenormalizeTest::testDenormalizeInvalidCustomSerializedField | public | function | Tests normalizing/denormalizing invalid custom serialized fields. | ||
DenormalizeTest::testDenormalizeSerializedItem | public | function | Tests normalizing/denormalizing serialized columns. | ||
DenormalizeTest::testDenormalizeValidCustomSerializedField | public | function | Tests normalizing/denormalizing valid custom serialized fields. | ||
DenormalizeTest::testMarkFieldForDeletion | public | function | Test that a field set to an empty array is different than an absent field. | ||
DenormalizeTest::testTypeHandling | public | function | Tests that the type link relation in incoming data is handled correctly. | ||
DenormalizeTest::testTypeHandlingWithInvalidType | public | function | Tests link relation handling with an invalid type. | ||
DenormalizeTest::testTypeHandlingWithNoTypes | public | function | Tests link relation handling with no types. | ||
KernelTestBase::assertPostConditions | protected | function | 1 | ||
KernelTestBase::bootEnvironment | protected | function | Bootstraps a basic test environment. | ||
KernelTestBase::bootKernel | private | function | Bootstraps a kernel for a test. | ||
KernelTestBase::config | protected | function | Configuration accessor for tests. Returns non-overridden configuration. | ||
KernelTestBase::getConfigSchemaExclusions | protected | function | Gets the config schema exclusions for this test. | ||
KernelTestBase::getDatabaseConnectionInfo | protected | function | Returns the Database connection info to be used for this test. | 1 | |
KernelTestBase::getDatabasePrefix | public | function | |||
KernelTestBase::getExtensionsForModules | private | function | Returns Extension objects for $modules to enable. | ||
KernelTestBase::initFileCache | protected | function | Initializes the FileCache component. | ||
KernelTestBase::installConfig | protected | function | Installs default configuration for a given list of modules. | ||
KernelTestBase::installEntitySchema | protected | function | Installs the storage schema for a specific entity type. | ||
KernelTestBase::installSchema | protected | function | Installs database tables from a module schema definition. | ||
KernelTestBase::register | public | function | Registers test-specific services. | Overrides ServiceProviderInterface::register | 26 |
KernelTestBase::setUpBeforeClass | public static | function | 1 | ||
KernelTestBase::setUpFilesystem | protected | function | Sets up the filesystem, so things like the file directory. | 2 | |
KernelTestBase::tearDown | protected | function | 5 | ||
KernelTestBase::tearDownCloseDatabaseConnection | public | function | @after | ||
NormalizerTestBase::setUp | protected | function | Overrides KernelTestBase::setUp | 3 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.