You are here

public function DenormalizeTest::testBasicFieldDenormalization in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/hal/src/Tests/DenormalizeTest.php \Drupal\hal\Tests\DenormalizeTest::testBasicFieldDenormalization()

Test that non-reference fields can be denormalized.

File

core/modules/hal/src/Tests/DenormalizeTest.php, line 121
Contains \Drupal\hal\Tests\DenormalizeTest.

Class

DenormalizeTest
Tests that entities can be denormalized from HAL.

Namespace

Drupal\hal\Tests

Code

public function testBasicFieldDenormalization() {
  $data = array(
    '_links' => array(
      'type' => array(
        'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array(
          'absolute' => TRUE,
        ))
          ->toString(),
      ),
    ),
    'uuid' => array(
      array(
        'value' => 'e5c9fb96-3acf-4a8d-9417-23de1b6c3311',
      ),
    ),
    'field_test_text' => array(
      array(
        'value' => $this
          ->randomMachineName(),
        'format' => 'full_html',
      ),
    ),
    'field_test_translatable_text' => array(
      array(
        'value' => $this
          ->randomMachineName(),
        'format' => 'full_html',
      ),
      array(
        'value' => $this
          ->randomMachineName(),
        'format' => 'filtered_html',
      ),
      array(
        'value' => $this
          ->randomMachineName(),
        'format' => 'filtered_html',
        'lang' => 'de',
      ),
      array(
        'value' => $this
          ->randomMachineName(),
        'format' => 'full_html',
        'lang' => 'de',
      ),
    ),
  );
  $expected_value_default = array(
    array(
      'value' => $data['field_test_translatable_text'][0]['value'],
      'format' => 'full_html',
    ),
    array(
      'value' => $data['field_test_translatable_text'][1]['value'],
      'format' => 'filtered_html',
    ),
  );
  $expected_value_de = array(
    array(
      'value' => $data['field_test_translatable_text'][2]['value'],
      'format' => 'filtered_html',
    ),
    array(
      'value' => $data['field_test_translatable_text'][3]['value'],
      'format' => 'full_html',
    ),
  );
  $denormalized = $this->serializer
    ->denormalize($data, $this->entityClass, $this->format);
  $this
    ->assertEqual($data['uuid'], $denormalized
    ->get('uuid')
    ->getValue(), 'A preset value (e.g. UUID) is overridden by incoming data.');
  $this
    ->assertEqual($data['field_test_text'], $denormalized
    ->get('field_test_text')
    ->getValue(), 'A basic text field is denormalized.');
  $this
    ->assertEqual($expected_value_default, $denormalized
    ->get('field_test_translatable_text')
    ->getValue(), 'Values in the default language are properly handled for a translatable field.');
  $this
    ->assertEqual($expected_value_de, $denormalized
    ->getTranslation('de')
    ->get('field_test_translatable_text')
    ->getValue(), 'Values in a translation language are properly handled for a translatable field.');
}