Executes the entity language method tests for the given entity type.
string $entity_type: The entity type to run the tests with.
protected function assertEntityLanguageMethods($entity_type) {
$entity = entity_create($entity_type, array(
'name' => 'test',
'user_id' => $GLOBALS['user']->uid,
));
$this
->assertEqual($entity
->language()->langcode, Language::LANGCODE_NOT_SPECIFIED, format_string('%entity_type: Entity language not specified.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse($entity
->getTranslationLanguages(FALSE), format_string('%entity_type: No translations are available', array(
'%entity_type' => $entity_type,
)));
// 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::LANGCODE_DEFAULT)
->get($this->field_name)->value, 'default value', format_string('%entity_type: Untranslated value retrieved.', array(
'%entity_type' => $entity_type,
)));
// 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', format_string('%entity_type: Untranslated value updated.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse($entity
->getTranslationLanguages(FALSE), format_string('%entity_type: No translations are available', array(
'%entity_type' => $entity_type,
)));
// 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', format_string('%entity_type: Untranslated value retrieved.', array(
'%entity_type' => $entity_type,
)));
// 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]), format_string('%entity_type: Entity language retrieved.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse($entity
->getTranslationLanguages(FALSE), format_string('%entity_type: No translations are available', array(
'%entity_type' => $entity_type,
)));
// 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', format_string('%entity_type: Untranslated value retrieved.', array(
'%entity_type' => $entity_type,
)));
// 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', format_string('%entity_type: Translated value set.', array(
'%entity_type' => $entity_type,
)));
// 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, format_string('%entity_type: A translation that is not available is NULL.', array(
'%entity_type' => $entity_type,
)));
// 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(format_string('%entity_type: Getting an untranslatable value from a translation in strict mode throws an exception.', array(
'%entity_type' => $entity_type,
)));
} catch (InvalidArgumentException $e) {
$this
->pass(format_string('%entity_type: Getting an untranslatable value from a translation in strict mode throws an exception.', array(
'%entity_type' => $entity_type,
)));
}
// 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', format_string('%entity_type: Untranslated value retrieved from translation in non-strict mode.', array(
'%entity_type' => $entity_type,
)));
// Try to set a value using an invalid language code.
try {
$entity
->getTranslation('invalid')
->set($this->field_name, NULL);
$this
->fail(format_string('%entity_type: Setting a translation for an invalid language throws an exception.', array(
'%entity_type' => $entity_type,
)));
} catch (InvalidArgumentException $e) {
$this
->pass(format_string('%entity_type: Setting a translation for an invalid language throws an exception.', array(
'%entity_type' => $entity_type,
)));
}
// Try to set an untranslatable value into a translation in strict mode.
try {
$entity
->getTranslation($this->langcodes[1])
->set($field_name, NULL);
$this
->fail(format_string('%entity_type: Setting an untranslatable value into a translation in strict mode throws an exception.', array(
'%entity_type' => $entity_type,
)));
} catch (InvalidArgumentException $e) {
$this
->pass(format_string('%entity_type: Setting an untranslatable value into a translation in strict mode throws an exception.', array(
'%entity_type' => $entity_type,
)));
}
// 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', format_string('%entity_type: Untranslated value set into a translation in non-strict mode.', array(
'%entity_type' => $entity_type,
)));
}