diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityApiTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityApiTest.php index a5488e5..a7ac1a9 100644 --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityApiTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityApiTest.php @@ -42,8 +42,8 @@ class EntityApiTest extends WebTestBase { $entities = array_values(entity_test_load_multiple(FALSE, array('name' => 'test'))); - $this->assertEqual($entities[0]->name, 'test', 'Created and loaded entity.'); - $this->assertEqual($entities[1]->name, 'test', 'Created and loaded entity.'); + $this->assertEqual($entities[0]->get('name'), 'test', 'Created and loaded entity.'); + $this->assertEqual($entities[1]->get('name'), 'test', 'Created and loaded entity.'); // Test loading a single entity. $loaded_entity = entity_test_load($entity->id); diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityTranslationTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityTranslationTest.php index 734971f..63a3835 100644 --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityTranslationTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityTranslationTest.php @@ -92,7 +92,7 @@ class EntityTranslationTest extends WebTestBase { // Now, make the entity language-specific by assigning a language and test // translating it. - $entity->langcode = $this->langcodes[0]; + $entity->setLangcode($this->langcodes[0]); $entity->{$this->field_name} = array(); $this->assertEqual($entity->language(), language_load($this->langcodes[0]), 'Entity language retrieved.'); $this->assertFalse($entity->translations(), 'No translations are available'); @@ -117,6 +117,21 @@ class EntityTranslationTest extends WebTestBase { // Try to get a not available translation. $value = $entity->get($this->field_name, $this->langcodes[2]); $this->assertNull($value, 'A translation that is not available is NULL.'); + + // Try to get a value using an invalid language code. + $value = $entity->get($this->field_name, 'invalid'); + $this->assertNull($value, 'An translation for an invalid language is NULL.'); + + // Try to set a value using an invalid language code. + $message = "An exception is thrown when trying to set an invalid translation."; + try { + $entity->set($this->field_name, NULL, 'invalid'); + // This line is not expected to be executed unless something goes wrong. + $this->assertTrue(FALSE, $message); + } + catch (\Exception $e) { + $this->assertTrue($e instanceof \InvalidArgumentException, $message); + } } /** @@ -137,6 +152,8 @@ class EntityTranslationTest extends WebTestBase { $this->assertEqual($uid, $entity->get('uid', LANGUAGE_NOT_SPECIFIED), 'The entity author has been correctly stored as language neutral.'); $this->assertFalse($entity->get('name', $langcode), 'The entity name is not available as a language-aware property.'); $this->assertFalse($entity->get('uid', $langcode), 'The entity author is not available as a language-aware property.'); + $this->assertEqual($name, $entity->get('name'), 'The entity name can be retrieved without specifying a language.'); + $this->assertEqual($uid, $entity->get('uid'), 'The entity author can be retrieved without specifying a language.'); // Create a language-aware entity and check that properties are stored // as language-aware. @@ -148,6 +165,8 @@ class EntityTranslationTest extends WebTestBase { $this->assertEqual($uid, $entity->get('uid', $langcode), 'The entity author has been correctly stored as a language-aware property.'); $this->assertFalse($entity->get('name', LANGUAGE_NOT_SPECIFIED), 'The entity name is not available as a language neutral property.'); $this->assertFalse($entity->get('uid', LANGUAGE_NOT_SPECIFIED), 'The entity author is not available as a language neutral property.'); + $this->assertEqual($name, $entity->get('name'), 'The entity name can be retrieved without specifying a language.'); + $this->assertEqual($uid, $entity->get('uid'), 'The entity author can be retrieved without specifying a language.'); // Create property translations. $properties = array(); @@ -179,18 +198,24 @@ class EntityTranslationTest extends WebTestBase { // Test query conditions (cache is reset at each call). $translated_id = $entity->id(); - entity_create('entity_test', array())->save(); + // Create an additional entity with only the uid set. The uid for the + // original language is the same of one used for a translation. + entity_create('entity_test', array('uid' => $properties[$langcode]['uid']))->save(); $entities = entity_test_load_multiple(FALSE, array(), TRUE); $this->assertEqual(count($entities), 3, 'Three entities were created.'); $entities = entity_test_load_multiple(array($translated_id), array(), TRUE); $this->assertEqual(count($entities), 1, 'One entity correctly loaded by id.'); $entities = entity_test_load_multiple(array(), array('name' => $name), TRUE); $this->assertEqual(count($entities), 2, 'Two entities correctly loaded by name.'); - $entities = entity_test_load_multiple(array(), array('name' => $properties[$langcode]['name']), TRUE); + $entities = entity_test_load_multiple(array(), array('name' => $properties[$langcode]['name'], 'default_langcode' => 0), TRUE); $this->assertEqual(count($entities), 1, 'One entity correctly loaded by name translation.'); $entities = entity_test_load_multiple(array(), array('langcode' => $default_langcode, 'name' => $name), TRUE); $this->assertEqual(count($entities), 1, 'One entity correctly loaded by name and language.'); $entities = entity_test_load_multiple(array(), array('langcode' => $langcode, 'name' => $properties[$langcode]['name']), TRUE); $this->assertEqual(count($entities), 0, 'No entity loaded by name translation specifying the translation language.'); + $entities = entity_test_load_multiple(array(), array('langcode' => $langcode, 'name' => $properties[$langcode]['name'], 'default_langcode' => 0), TRUE); + $this->assertEqual(count($entities), 1, 'One entity loaded by name translation and language specifying to look for translations.'); + $entities = entity_test_load_multiple(array(), array('uid' => $properties[$langcode]['uid'], 'default_langcode' => NULL), TRUE); + $this->assertEqual(count($entities), 2, 'Two entities loaded by uid without caring about property translatability.'); } } diff --git a/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTest.php b/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTest.php index 3cf3823..bbd7648 100644 --- a/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTest.php +++ b/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTest.php @@ -34,8 +34,14 @@ class EntityTest extends Entity { public function __construct(array $values = array(), $entity_type) { parent::__construct($values, $entity_type); - $this->langcodes = drupal_map_assoc(array_keys(language_list())); - $this->langcode = $this->resolvePropertyLangcode(!empty($values['langcode']) ? $values['langcode'] : NULL); + // The allowed languages are simply all the ones available in the system. + $this->langcodes = drupal_map_assoc(array_keys(language_list(LANGUAGE_ALL))); + + // Initialize the original entity language with the provided value or fall + // back to LANGUAGE_NOT_SPECIFIED if none was specified. We do not check + // against allowed languages here, since throwing an exception would make an + // entity created in a subsequently uninstalled language not instantiable. + $this->langcode = !empty($values['langcode']) ? $values['langcode'] : LANGUAGE_NOT_SPECIFIED; // Set initial values ensuring that only real properties are stored. foreach ($values as $property_name => $value) { @@ -46,11 +52,17 @@ class EntityTest extends Entity { } /** - * Sets the entity default langcode. + * Sets the entity original langcode. * * @param $langcode */ public function setLangcode($langcode) { + // If the original language is changed the related properties must change + // their language accordingly. + if (isset($this->properties[$this->langcode])) { + $this->properties[$langcode] = $this->properties[$this->langcode]; + unset($this->properties[$this->langcode]); + } $this->langcode = $langcode; } @@ -58,12 +70,12 @@ class EntityTest extends Entity { * Overrides EntityInterface::get(). */ public function get($property_name, $langcode = NULL) { + $langcode = !empty($langcode) ? $langcode : $this->langcode; $entity_info = $this->entityInfo(); if ($entity_info['fieldable'] && field_info_instance($this->entityType, $property_name, $this->bundle())) { return parent::get($property_name, $langcode); } else { - $langcode = $this->resolvePropertyLangcode($langcode); return isset($this->properties[$langcode][$property_name]) ? $this->properties[$langcode][$property_name] : NULL; } } @@ -72,12 +84,15 @@ class EntityTest extends Entity { * Overrides EntityInterface::set(). */ public function set($property_name, $value, $langcode = NULL) { + $langcode = !empty($langcode) ? $langcode : $this->langcode; + if (!isset($this->langcodes[$langcode])) { + throw new \InvalidArgumentException("Detected an invalid language '$langcode' while setting '$property_name' to '$value."); + } $entity_info = $this->entityInfo(); if ($entity_info['fieldable'] && field_info_instance($this->entityType, $property_name, $this->bundle())) { parent::set($property_name, $value, $langcode); } else { - $langcode = $this->resolvePropertyLangcode($langcode); $this->properties[$langcode][$property_name] = $value; } } @@ -88,7 +103,7 @@ class EntityTest extends Entity { public function translations() { $translations = !empty($this->properties) ? $this->properties : array(); $languages = array_intersect_key($this->langcodes, $translations); - unset($languages[$this->resolvePropertyLangcode()]); + unset($languages[$this->langcode]); return $languages + parent::translations(); } @@ -99,7 +114,7 @@ class EntityTest extends Entity { * (optional) The language code to be used to retrieve the properties. */ public function getProperties($langcode = NULL) { - $langcode = $this->resolvePropertyLangcode($langcode); + $langcode = !empty($langcode) ? $langcode : $this->langcode; return isset($this->properties[$langcode]) ? $this->properties[$langcode] : array(); } @@ -108,22 +123,12 @@ class EntityTest extends Entity { * * @param array $properties * A keyed array of properties to be set with their 'langcode' as one of the - * keys. If no language is provided the entity langauge is used. + * keys. If no language is provided the entity language is used. + * @param string $langcode + * (optional) The language code to be used to set the properties. */ public function setProperties(array $properties, $langcode = NULL) { - // Make sure only real properties are stored. - unset($properties['id'], $properties['langcode'], $properties['default_langcode']); - $langcode = $this->resolvePropertyLangcode($langcode); + $langcode = !empty($langcode) ? $langcode : $this->langcode; $this->properties[$langcode] = $properties; } - - /** - * Returns a valid language code to be used to manipulate entity properties. - */ - protected function resolvePropertyLangcode($langcode = NULL) { - if (empty($langcode)) { - $langcode = $this->langcode; - } - return isset($this->langcodes[$langcode]) ? $langcode : LANGUAGE_NOT_SPECIFIED; - } } diff --git a/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php b/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php index 5ec4315..8062cff 100644 --- a/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php +++ b/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php @@ -32,14 +32,21 @@ class EntityTestStorageController extends DatabaseStorageController { if ($ids) { $query->condition("data.id", $ids, 'IN'); } + if ($conditions) { + // Default to the original entity language if not explicitly specified + // otherwise. + if (!array_key_exists('default_langcode', $conditions)) { + $conditions['default_langcode'] = 1; + } + // If the 'default_langcode' flag is esplicitly not set, we do not care + // whether the queried values are in the original entity language or not. + elseif ($conditions['default_langcode'] === NULL) { + unset($conditions['default_langcode']); + } + foreach ($conditions as $field => $value) { $query->condition('data.' . $field, $value); - if ($field == 'langcode') { - // Ensure that conditions involving the 'langcode' property concern - // only values in the original entity language. - $query->condition('data.default_langcode', 1); - } } } @@ -58,10 +65,13 @@ class EntityTestStorageController extends DatabaseStorageController { foreach ($data as $values) { $entity = $queried_entities[$values['id']]; - $entity->setProperties($values, $values['langcode']); + $langcode = $values['langcode']; if (!empty($values['default_langcode'])) { - $entity->setLangcode($values['langcode']); + $entity->setLangcode($langcode); } + // Make sure only real properties are stored. + unset($values['id'], $values['default_langcode']); + $entity->setProperties($values, $langcode); } parent::attachLoad($queried_entities, $revision_id);