Tests language related methods of the Entity class.
function testEntityLanguageMethods() {
$entity = entity_create('entity_test', array(
'name' => 'test',
'user_id' => $GLOBALS['user']->uid,
));
$this
->assertEqual($entity
->language()->langcode, LANGUAGE_NOT_SPECIFIED, 'Entity language not specified.');
$this
->assertFalse($entity
->getTranslationLanguages(FALSE), 'No translations are available');
// Set the value in default language.
$entity
->set($this->field_name, array(
0 => array(
'value' => 'default value',
),
));
// Get the value.
$this
->assertEqual($entity
->getTranslation(LANGUAGE_DEFAULT)
->get($this->field_name)->value, 'default value', 'Untranslated value retrieved.');
// Set the value in a certain language. As the entity is not
// language-specific it should use the default language and so ignore the
// specified language.
$entity
->getTranslation($this->langcodes[1])
->set($this->field_name, array(
0 => array(
'value' => 'default value2',
),
));
$this
->assertEqual($entity
->get($this->field_name)->value, 'default value2', 'Untranslated value updated.');
$this
->assertFalse($entity
->getTranslationLanguages(FALSE), 'No translations are available');
// Test getting a field value using a specific language for a not
// language-specific entity.
$this
->assertEqual($entity
->getTranslation($this->langcodes[1])
->get($this->field_name)->value, 'default value2', 'Untranslated value retrieved.');
// Now, make the entity language-specific by assigning a language and test
// translating it.
$entity->langcode->value = $this->langcodes[0];
$entity->{$this->field_name} = array();
$this
->assertEqual($entity
->language(), language_load($this->langcodes[0]), 'Entity language retrieved.');
$this
->assertFalse($entity
->getTranslationLanguages(FALSE), 'No translations are available');
// Set the value in default language.
$entity
->set($this->field_name, array(
0 => array(
'value' => 'default value',
),
));
// Get the value.
$this
->assertEqual($entity
->get($this->field_name)->value, 'default value', 'Untranslated value retrieved.');
// Set a translation.
$entity
->getTranslation($this->langcodes[1])
->set($this->field_name, array(
0 => array(
'value' => 'translation 1',
),
));
$this
->assertEqual($entity
->getTranslation($this->langcodes[1])->{$this->field_name}->value, 'translation 1', 'Translated value set.');
// Make sure the untranslated value stays.
$this
->assertEqual($entity
->get($this->field_name)->value, 'default value', 'Untranslated value stays.');
$translations[$this->langcodes[1]] = language_load($this->langcodes[1]);
$this
->assertEqual($entity
->getTranslationLanguages(FALSE), $translations, 'Translations retrieved.');
// Try to get a not available translation.
$this
->assertNull($entity
->getTranslation($this->langcodes[2])
->get($this->field_name)->value, 'A translation that is not available is NULL.');
// Try to get a value using an invalid language code.
try {
$entity
->getTranslation('invalid')
->get($this->field_name)->value;
$this
->fail('Getting a translation for an invalid language is NULL.');
} catch (InvalidArgumentException $e) {
$this
->pass('A translation for an invalid language is NULL.');
}
// Try to get an untranslatable value from a translation in strict mode.
try {
$field_name = 'field_test_text';
$value = $entity
->getTranslation($this->langcodes[1])
->get($field_name);
$this
->fail('Getting an untranslatable value from a translation in strict mode throws an exception.');
} catch (InvalidArgumentException $e) {
$this
->pass('Getting an untranslatable value from a translation in strict mode throws an exception.');
}
// Try to get an untranslatable value from a translation in non-strict
// mode.
$entity
->set($field_name, array(
0 => array(
'value' => 'default value',
),
));
$value = $entity
->getTranslation($this->langcodes[1], FALSE)
->get($field_name)->value;
$this
->assertEqual($value, 'default value', 'Untranslated value retrieved from translation in non-strict mode.');
// Try to set a value using an invalid language code.
try {
$entity
->getTranslation('invalid')
->set($this->field_name, NULL);
$this
->fail("Setting a translation for an invalid language throws an exception.");
} catch (InvalidArgumentException $e) {
$this
->pass("Setting a translation for an invalid language throws an exception.");
}
// Try to set an untranslatable value into a translation in strict mode.
try {
$entity
->getTranslation($this->langcodes[1])
->set($field_name, NULL);
$this
->fail("Setting an untranslatable value into a translation in strict mode throws an exception.");
} catch (InvalidArgumentException $e) {
$this
->pass("Setting an untranslatable value into a translation in strict mode throws an exception.");
}
// Set the value in default language.
$entity
->getTranslation($this->langcodes[1], FALSE)
->set($field_name, array(
0 => array(
'value' => 'default value2',
),
));
// Get the value.
$this
->assertEqual($entity
->get($field_name)->value, 'default value2', 'Untranslated value set into a translation in non-strict mode.');
}