Tests the normalize function.
public function testNormalize() {
// Add German as a language.
$language = new Language(array(
'langcode' => 'de',
'name' => 'Deutsch',
));
language_save($language);
// Create a German entity.
$values = array(
'langcode' => 'de',
'name' => $this
->randomName(),
'user_id' => $GLOBALS['user']->uid,
'field_test_text' => array(
'value' => $this
->randomName(),
'format' => 'full_html',
),
);
// Array of translated values.
$translationValues = array(
'name' => $this
->randomName(),
);
$entity = entity_create('entity_test', $values);
$entity
->save();
// Add an English value for name property.
$entity
->getTranslation('en')
->set('name', array(
0 => array(
'value' => $translationValues['name'],
),
));
$expectedArray = array(
'@id' => $this
->getEntityId($entity),
'uuid' => array(
'und' => array(
array(
'value' => $entity
->uuid(),
),
),
),
'user_id' => array(
'de' => array(
array(
'@id' => url('user/' . $values['user_id'], array(
'absolute' => TRUE,
)),
),
),
),
'name' => array(
'de' => array(
array(
'value' => $values['name'],
),
),
'en' => array(
array(
'value' => $translationValues['name'],
),
),
),
'field_test_text' => array(
'und' => array(
array(
'value' => $values['field_test_text']['value'],
'format' => $values['field_test_text']['format'],
),
),
),
);
$normalized = $this->normalizers['entity']
->normalize($entity, static::$format);
// Test ordering. The @context and @id properties should always be first.
$keys = array_keys($normalized);
$this
->assertEqual($keys[0], '@id', '@id and @context attributes placed correctly.');
// Test @id value.
$this
->assertEqual($normalized['@id'], $expectedArray['@id'], '@id uses correct value.');
// Test non-translatable field.
$this
->assertEqual($normalized['uuid'], $expectedArray['uuid'], 'Non-translatable fields are nested correctly.');
// Test single-language translatable.
$this
->assertEqual($normalized['user_id'], $expectedArray['user_id'], 'Translatable field with single language value is nested correctly.');
// Test multi-language translatable.
$this
->assertEqual($normalized['name'], $expectedArray['name'], 'Translatable field with multiple language values is nested correctly.');
// Test multi-property untranslatable field.
$this
->assertEqual($normalized['field_test_text'], $expectedArray['field_test_text'], 'Field with properties is nested correctly.');
}