public function TypedDataTest::testTypedDataMaps

Tests using a typed data map.

File

drupal/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php, line 351
Definition of Drupal\system\Tests\TypedData\TypedDataTest.

Class

TypedDataTest
Tests primitive data types.

Namespace

Drupal\system\Tests\TypedData

Code

public function testTypedDataMaps() {

  // Test working with a simple map.
  $value = array(
    'one' => 'eins',
    'two' => 'zwei',
    'three' => 'drei',
  );
  $typed_data = $this
    ->createTypedData(array(
    'type' => 'map',
  ), $value);

  // Test iterating.
  $count = 0;
  foreach ($typed_data as $item) {
    $this
      ->assertTrue($item instanceof \Drupal\Core\TypedData\TypedDataInterface);
    $count++;
  }
  $this
    ->assertEqual($count, 3);

  // Test retrieving metadata.
  $this
    ->assertEqual(array_keys($typed_data
    ->getPropertyDefinitions()), array_keys($value));
  $definition = $typed_data
    ->getPropertyDefinition('one');
  $this
    ->assertEqual($definition['type'], 'any');
  $this
    ->assertFalse($typed_data
    ->getPropertyDefinition('invalid'));

  // Test getting and setting properties.
  $this
    ->assertEqual($typed_data
    ->get('one')
    ->getValue(), 'eins');
  $this
    ->assertEqual($typed_data
    ->getPropertyValues(), $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, array(
    '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.');
  $typed_data
    ->setPropertyValues(array(
    'one' => 'eins',
  ));
  $this
    ->assertEqual($typed_data
    ->get('one')
    ->getValue(), 'eins');
  $this
    ->assertEqual($typed_data
    ->get('two')
    ->getValue(), 'zwei');
  $this
    ->assertEqual($typed_data
    ->get('three')
    ->getValue(), 'drei');
  $typed_data
    ->setValue(array(
    'foo' => 'bar',
  ));
  $this
    ->assertEqual(array_keys($typed_data
    ->getProperties()), array(
    'foo',
  ));

  // Test getting the string representation.
  $typed_data
    ->setValue(array(
    '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(array());
  $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(array());
  $value = $typed_data
    ->getValue();
  $this
    ->assertTrue(isset($value) && is_array($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) {
    $this
      ->pass('Exception thrown:' . $e
      ->getMessage());
  }

  // Test setting invalid values.
  try {
    $typed_data
      ->setValue('invalid');
    $this
      ->fail('No exception has been thrown when setting an invalid value.');
  } catch (\Exception $e) {
    $this
      ->pass('Exception thrown:' . $e
      ->getMessage());
  }

  // Test adding a new entry to the map.
  $typed_data
    ->set('zero', 'null');
  $this
    ->assertEqual($typed_data
    ->get('zero')
    ->getValue(), 'null');
  $definition = $typed_data
    ->getPropertyDefinition('zero');
  $this
    ->assertEqual($definition['type'], 'any', 'Definition for a new map entry returned.');
}