public function TypedDataTest::testTypedDataMaps in Drupal 8
Same name and namespace in other branches
- 9 core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataMaps()
- 10 core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataMaps()
Tests using a typed data map.
File
- core/
tests/ Drupal/ KernelTests/ Core/ TypedData/ TypedDataTest.php, line 475
Class
- TypedDataTest
- Tests the functionality of all core data types.
Namespace
Drupal\KernelTests\Core\TypedDataCode
public function testTypedDataMaps() {
// Test working with a simple map.
$value = [
'one' => 'eins',
'two' => 'zwei',
'three' => 'drei',
];
$definition = MapDataDefinition::create()
->setPropertyDefinition('one', DataDefinition::create('string'))
->setPropertyDefinition('two', DataDefinition::create('string'))
->setPropertyDefinition('three', DataDefinition::create('string'));
$typed_data = $this
->createTypedData($definition, $value);
// Test iterating.
$count = 0;
foreach ($typed_data as $item) {
$this
->assertInstanceOf(TypedDataInterface::class, $item);
$count++;
}
$this
->assertEqual($count, 3);
// Test retrieving metadata.
$this
->assertEqual(array_keys($typed_data
->getDataDefinition()
->getPropertyDefinitions()), array_keys($value));
$definition = $typed_data
->getDataDefinition()
->getPropertyDefinition('one');
$this
->assertEqual($definition
->getDataType(), 'string');
$this
->assertNull($typed_data
->getDataDefinition()
->getPropertyDefinition('invalid'));
// Test getting and setting properties.
$this
->assertEqual($typed_data
->get('one')
->getValue(), 'eins');
$this
->assertEqual($typed_data
->toArray(), $value);
$typed_data
->set('one', 'uno');
$this
->assertEqual($typed_data
->get('one')
->getValue(), 'uno');
// Make sure the update is reflected in the value of the map also.
$value = $typed_data
->getValue();
$this
->assertEqual($value, [
'one' => 'uno',
'two' => 'zwei',
'three' => 'drei',
]);
$properties = $typed_data
->getProperties();
$this
->assertEqual(array_keys($properties), array_keys($value));
$this
->assertIdentical($properties['one'], $typed_data
->get('one'), 'Properties are identical.');
// Test setting a not defined property. It shouldn't show up in the
// properties, but be kept in the values.
$typed_data
->setValue([
'foo' => 'bar',
]);
$this
->assertEqual(array_keys($typed_data
->getProperties()), [
'one',
'two',
'three',
]);
$this
->assertEqual(array_keys($typed_data
->getValue()), [
'foo',
'one',
'two',
'three',
]);
// Test getting the string representation.
$typed_data
->setValue([
'one' => 'eins',
'two' => '',
'three' => 'drei',
]);
$this
->assertEqual($typed_data
->getString(), 'eins, drei');
// Test isEmpty and cloning.
$this
->assertFalse($typed_data
->isEmpty());
$clone = clone $typed_data;
$this
->assertTrue($typed_data
->getValue() === $clone
->getValue());
$this
->assertTrue($typed_data
->get('one') !== $clone
->get('one'));
$clone
->setValue([]);
$this
->assertTrue($clone
->isEmpty());
// Make sure the difference between NULL (not set) and an empty array is
// kept.
$typed_data
->setValue(NULL);
$this
->assertNull($typed_data
->getValue());
$typed_data
->setValue([]);
$value = $typed_data
->getValue();
$this
->assertIsArray($value);
// Test accessing invalid properties.
$typed_data
->setValue($value);
try {
$typed_data
->get('invalid');
$this
->fail('No exception has been thrown when getting an invalid value.');
} catch (\Exception $e) {
// Expected exception; just continue testing.
}
// Test setting invalid values.
try {
$typed_data
->setValue('invalid');
$this
->fail('No exception has been thrown when setting an invalid value.');
} catch (\Exception $e) {
// Expected exception; just continue testing.
}
// Test adding a new property to the map.
$typed_data
->getDataDefinition()
->setPropertyDefinition('zero', DataDefinition::create('any'));
$typed_data
->set('zero', 'null');
$this
->assertEqual($typed_data
->get('zero')
->getValue(), 'null');
$definition = $typed_data
->get('zero')
->getDataDefinition();
$this
->assertEqual($definition
->getDataType(), 'any', 'Definition for a new map entry returned.');
}