You are here

protected function EntityTranslationTest::doTestMultilingualProperties in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityTranslationTest.php \Drupal\system\Tests\Entity\EntityTranslationTest::doTestMultilingualProperties()

Executes the multilingual property tests for the given entity type.

Parameters

string $entity_type: The entity type to run the tests with.

1 call to EntityTranslationTest::doTestMultilingualProperties()
EntityTranslationTest::testMultilingualProperties in core/modules/system/src/Tests/Entity/EntityTranslationTest.php
Tests multilingual properties.

File

core/modules/system/src/Tests/Entity/EntityTranslationTest.php, line 156
Contains \Drupal\system\Tests\Entity\EntityTranslationTest.

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\system\Tests\Entity

Code

protected function doTestMultilingualProperties($entity_type) {
  $langcode_key = $this->entityManager
    ->getDefinition($entity_type)
    ->getKey('langcode');
  $default_langcode_key = $this->entityManager
    ->getDefinition($entity_type)
    ->getKey('default_langcode');
  $name = $this
    ->randomMachineName();
  $uid = mt_rand(0, 127);
  $langcode = $this->langcodes[0];

  // Create a language neutral entity and check that properties are stored
  // as language neutral.
  $entity = entity_create($entity_type, array(
    'name' => $name,
    'user_id' => $uid,
    $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ));
  $entity
    ->save();
  $entity = entity_load($entity_type, $entity
    ->id());
  $default_langcode = $entity
    ->language()
    ->getId();
  $this
    ->assertEqual($default_langcode, LanguageInterface::LANGCODE_NOT_SPECIFIED, format_string('%entity_type: Entity created as language neutral.', array(
    '%entity_type' => $entity_type,
  )));
  $field = $entity
    ->getTranslation(LanguageInterface::LANGCODE_DEFAULT)
    ->get('name');
  $this
    ->assertEqual($name, $field->value, format_string('%entity_type: The entity name has been correctly stored as language neutral.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($default_langcode, $field
    ->getLangcode(), format_string('%entity_type: The field object has the expect langcode.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($uid, $entity
    ->getTranslation(LanguageInterface::LANGCODE_DEFAULT)
    ->get('user_id')->target_id, format_string('%entity_type: The entity author has been correctly stored as language neutral.', array(
    '%entity_type' => $entity_type,
  )));
  $translation = $entity
    ->getTranslation(LanguageInterface::LANGCODE_DEFAULT);
  $field = $translation
    ->get('name');
  $this
    ->assertEqual($name, $field->value, format_string('%entity_type: The entity name defaults to neutral language.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($default_langcode, $field
    ->getLangcode(), format_string('%entity_type: The field object has the expect langcode.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($uid, $translation
    ->get('user_id')->target_id, format_string('%entity_type: The entity author defaults to neutral language.', array(
    '%entity_type' => $entity_type,
  )));
  $field = $entity
    ->get('name');
  $this
    ->assertEqual($name, $field->value, format_string('%entity_type: The entity name can be retrieved without specifying a language.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($default_langcode, $field
    ->getLangcode(), format_string('%entity_type: The field object has the expect langcode.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($uid, $entity
    ->get('user_id')->target_id, format_string('%entity_type: The entity author can be retrieved without specifying a language.', array(
    '%entity_type' => $entity_type,
  )));

  // Create a language-aware entity and check that properties are stored
  // as language-aware.
  $entity = entity_create($entity_type, array(
    'name' => $name,
    'user_id' => $uid,
    $langcode_key => $langcode,
  ));
  $entity
    ->save();
  $entity = entity_load($entity_type, $entity
    ->id());
  $default_langcode = $entity
    ->language()
    ->getId();
  $this
    ->assertEqual($default_langcode, $langcode, format_string('%entity_type: Entity created as language specific.', array(
    '%entity_type' => $entity_type,
  )));
  $field = $entity
    ->getTranslation($langcode)
    ->get('name');
  $this
    ->assertEqual($name, $field->value, format_string('%entity_type: The entity name has been correctly stored as a language-aware property.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($default_langcode, $field
    ->getLangcode(), format_string('%entity_type: The field object has the expect langcode.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($uid, $entity
    ->getTranslation($langcode)
    ->get('user_id')->target_id, format_string('%entity_type: The entity author has been correctly stored as a language-aware property.', array(
    '%entity_type' => $entity_type,
  )));

  // Create property translations.
  $properties = array();
  $default_langcode = $langcode;
  foreach ($this->langcodes as $langcode) {
    if ($langcode != $default_langcode) {
      $properties[$langcode] = array(
        'name' => array(
          0 => $this
            ->randomMachineName(),
        ),
        'user_id' => array(
          0 => mt_rand(128, 256),
        ),
      );
    }
    else {
      $properties[$langcode] = array(
        'name' => array(
          0 => $name,
        ),
        'user_id' => array(
          0 => $uid,
        ),
      );
    }
    $translation = $entity
      ->hasTranslation($langcode) ? $entity
      ->getTranslation($langcode) : $entity
      ->addTranslation($langcode);
    foreach ($properties[$langcode] as $field_name => $values) {
      $translation
        ->set($field_name, $values);
    }
  }
  $entity
    ->save();

  // Check that property translation were correctly stored.
  $entity = entity_load($entity_type, $entity
    ->id());
  foreach ($this->langcodes as $langcode) {
    $args = array(
      '%entity_type' => $entity_type,
      '%langcode' => $langcode,
    );
    $field = $entity
      ->getTranslation($langcode)
      ->get('name');
    $this
      ->assertEqual($properties[$langcode]['name'][0], $field->value, format_string('%entity_type: The entity name has been correctly stored for language %langcode.', $args));
    $field_langcode = $langcode == $entity
      ->language()
      ->getId() ? $default_langcode : $langcode;
    $this
      ->assertEqual($field_langcode, $field
      ->getLangcode(), format_string('%entity_type: The field object has the expected langcode  %langcode.', $args));
    $this
      ->assertEqual($properties[$langcode]['user_id'][0], $entity
      ->getTranslation($langcode)
      ->get('user_id')->target_id, format_string('%entity_type: The entity author has been correctly stored for language %langcode.', $args));
  }

  // Test query conditions (cache is reset at each call).
  $translated_id = $entity
    ->id();

  // Create an additional entity with only the uid set. The uid for the
  // original language is the same of one used for a translation.
  $langcode = $this->langcodes[1];
  entity_create($entity_type, array(
    'user_id' => $properties[$langcode]['user_id'],
    'name' => 'some name',
    $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ))
    ->save();
  $entities = entity_load_multiple($entity_type);
  $this
    ->assertEqual(count($entities), 3, format_string('%entity_type: Three entities were created.', array(
    '%entity_type' => $entity_type,
  )));
  $entities = entity_load_multiple($entity_type, array(
    $translated_id,
  ));
  $this
    ->assertEqual(count($entities), 1, format_string('%entity_type: One entity correctly loaded by id.', array(
    '%entity_type' => $entity_type,
  )));
  $entities = entity_load_multiple_by_properties($entity_type, array(
    'name' => $name,
  ));
  $this
    ->assertEqual(count($entities), 2, format_string('%entity_type: Two entities correctly loaded by name.', array(
    '%entity_type' => $entity_type,
  )));

  // @todo The default language condition should go away in favor of an
  // explicit parameter.
  $entities = entity_load_multiple_by_properties($entity_type, array(
    'name' => $properties[$langcode]['name'][0],
    $default_langcode_key => 0,
  ));
  $this
    ->assertEqual(count($entities), 1, format_string('%entity_type: One entity correctly loaded by name translation.', array(
    '%entity_type' => $entity_type,
  )));
  $entities = entity_load_multiple_by_properties($entity_type, array(
    $langcode_key => $default_langcode,
    'name' => $name,
  ));
  $this
    ->assertEqual(count($entities), 1, format_string('%entity_type: One entity correctly loaded by name and language.', array(
    '%entity_type' => $entity_type,
  )));
  $entities = entity_load_multiple_by_properties($entity_type, array(
    $langcode_key => $langcode,
    'name' => $properties[$langcode]['name'][0],
  ));
  $this
    ->assertEqual(count($entities), 0, format_string('%entity_type: No entity loaded by name translation specifying the translation language.', array(
    '%entity_type' => $entity_type,
  )));
  $entities = entity_load_multiple_by_properties($entity_type, array(
    $langcode_key => $langcode,
    'name' => $properties[$langcode]['name'][0],
    $default_langcode_key => 0,
  ));
  $this
    ->assertEqual(count($entities), 1, format_string('%entity_type: One entity loaded by name translation and language specifying to look for translations.', array(
    '%entity_type' => $entity_type,
  )));
  $entities = entity_load_multiple_by_properties($entity_type, array(
    'user_id' => $properties[$langcode]['user_id'][0],
    $default_langcode_key => NULL,
  ));
  $this
    ->assertEqual(count($entities), 2, format_string('%entity_type: Two entities loaded by uid without caring about property translatability.', array(
    '%entity_type' => $entity_type,
  )));

  // Test property conditions and orders with multiple languages in the same
  // query.
  $query = \Drupal::entityQuery($entity_type);
  $group = $query
    ->andConditionGroup()
    ->condition('user_id', $properties[$default_langcode]['user_id'][0], '=', $default_langcode)
    ->condition('name', $properties[$default_langcode]['name'][0], '=', $default_langcode);
  $result = $query
    ->condition($group)
    ->condition('name', $properties[$langcode]['name'][0], '=', $langcode)
    ->execute();
  $this
    ->assertEqual(count($result), 1, format_string('%entity_type: One entity loaded by name and uid using different language meta conditions.', array(
    '%entity_type' => $entity_type,
  )));

  // Test mixed property and field conditions.
  $entity = entity_load($entity_type, reset($result), TRUE);
  $field_value = $this
    ->randomString();
  $entity
    ->getTranslation($langcode)
    ->set($this->fieldName, array(
    array(
      'value' => $field_value,
    ),
  ));
  $entity
    ->save();
  $query = \Drupal::entityQuery($entity_type);
  $default_langcode_group = $query
    ->andConditionGroup()
    ->condition('user_id', $properties[$default_langcode]['user_id'][0], '=', $default_langcode)
    ->condition('name', $properties[$default_langcode]['name'][0], '=', $default_langcode);
  $langcode_group = $query
    ->andConditionGroup()
    ->condition('name', $properties[$langcode]['name'][0], '=', $langcode)
    ->condition("{$this->fieldName}.value", $field_value, '=', $langcode);
  $result = $query
    ->condition($langcode_key, $default_langcode)
    ->condition($default_langcode_group)
    ->condition($langcode_group)
    ->execute();
  $this
    ->assertEqual(count($result), 1, format_string('%entity_type: One entity loaded by name, uid and field value using different language meta conditions.', array(
    '%entity_type' => $entity_type,
  )));
}