You are here

public function FieldCollectionEntityTranslationTestCase::testEntityTranslation in Field collection 7

Ensures the right behaviour in all Entity Translation use cases.

File

./field_collection.test, line 1183
Tests for field_collections.

Class

FieldCollectionEntityTranslationTestCase
Test using field collection with content that gets translated with Entity Translation.

Code

public function testEntityTranslation() {
  $source_langcode = 'en';
  $translation_langcode = 'de';

  /*
   * Test with a page with only one value in the field collection
   */

  // Create an article in the original language with only one field collection
  // value.
  $node = $this
    ->createPage(1, $source_langcode);

  // Create a traslation of the page through the entity translation form.
  $node = $this
    ->createTranslationForm($node, $translation_langcode, $source_langcode);

  /*
   * Test with a page with multiple values in the field collection.
   */
  $num_values = 4;

  // Create a page in the original language with multiple field collection
  // values.
  $node = $this
    ->createPage($num_values, $source_langcode);

  // Create a traslation of the page through the entity translation form.
  $node = $this
    ->createTranslationForm($node, $translation_langcode, $source_langcode);

  // Assign a new field collection item to an existing node.
  $values = array();
  $values['field_name'] = $this->field_name;
  $fc_entity = entity_create('field_collection_item', $values);
  $fc_entity
    ->setHostEntity('node', $node, $translation_langcode);
  $fc_entity->{$this->field_untrans_name}[LANGUAGE_NONE][0]['value'] = self::UNTRANS_FIELD_DE_MOD;
  $fc_entity->{$this->field_trans_name}['de'][0]['value'] = self::TRANS_FIELD_DE_MOD;
  $fc_entity
    ->save(TRUE);
  node_save($node);

  // Reload the node to check it.
  $node = node_load($node->nid, NULL, TRUE);

  // Check that there is a new element in the translation.
  $this
    ->assertEqual($num_values + 1, count($node->{$this->field_name}[$translation_langcode]), t('We have one item more in translation.'));

  // Check that the new element is correctly saved.
  $fc_item_values = $this
    ->getFieldValues($node, $translation_langcode, $num_values);
  $this
    ->assertEqual($fc_item_values['field_untrans'], self::UNTRANS_FIELD_DE_MOD);
  $this
    ->assertEqual($fc_item_values['field_trans'], self::TRANS_FIELD_DE_MOD);

  // Check that we have the same items in the original language.
  $this
    ->assertEqual($num_values, count($node->{$this->field_name}[$source_langcode]), t('We have same items in the original language.'));

  // Remove a field collection item from the translation.
  $fc_item_id = $node->{$this->field_name}[$translation_langcode][0]['value'];
  unset($node->{$this->field_name}[$translation_langcode][0]);
  node_save($node);

  // Reload the node.
  $node = node_load($node->nid, NULL, TRUE);

  // Check that we have one item less in the translation.
  // We should take into account that we added a field one step before.
  $this
    ->assertEqual($num_values, count($node->{$this->field_name}[$translation_langcode]), t('We have one item less in translation.'));

  // Check that we have the same items in the original language.
  $this
    ->assertEqual($num_values, count($node->{$this->field_name}[$source_langcode]), t('We have same items in the original language.'));

  // Check that the field collection is removed from the database.
  $fc_items = entity_load('field_collection_item', array(
    $fc_item_id,
  ));
  $this
    ->assert(empty($fc_items), t('The field collection item has been removed from the database.'));

  // Delete the translation.
  $this
    ->removeTranslationForm($node, $translation_langcode, $source_langcode);

  /*
   * Check the revisioning of an entity with translations.
   */
  $num_values = 4;

  // Create a page in the original language with multiple field collection
  // values.
  $node_rev = $this
    ->createPage($num_values, $source_langcode);

  // Create a traslation of the page.
  $node_rev = $this
    ->createTranslationForm($node_rev, $translation_langcode, $source_langcode);
  $original_revision = $node_rev->vid;

  // Create a new revision of the node.
  $node_rev = $this
    ->createRevision($node_rev, $translation_langcode, $source_langcode);

  /*
   * Test creating programmatically.
   */
  $num_values = 4;

  // Create a page in the original language.
  $node_prog = $this
    ->createPage($num_values, $source_langcode);

  // Create programmatically a translation of the page.
  $node_prog = $this
    ->createTranslation($node_prog, $translation_langcode);
  $orig_fc_items = $node_prog->{$this->field_name}[$source_langcode];
  $trans_fc_items = $node_prog->{$this->field_name}[$translation_langcode];
  $orig_fc_item_ids = array();
  $trans_fc_item_ids = array();

  // Check each item.
  foreach ($orig_fc_items as $delta => $value) {
    $orig_fc_item_ids[] = $value['value'];
    $trans_fc_item_ids[] = $trans_fc_items[$delta]['value'];

    // Check if we have new items for the translation.
    $this
      ->assertNotEqual($value['value'], $trans_fc_items[$delta]['value'], t('New item generated for translation.'));
  }

  // Check that the original item still exists in the database.
  $fc_items = entity_load('field_collection_item', $orig_fc_item_ids);
  $this
    ->assert(!empty($fc_items), t('Field Collections in the source language still exist.'));

  // Check that the translated item exists in the database.
  $fc_items = entity_load('field_collection_item', $trans_fc_item_ids);
  $this
    ->assert(!empty($fc_items), t('Translations for the Field Collection exist.'));

  // Remove the translation and check that the original field collection items
  // are still there.
  $node_prog = $this
    ->removeTranslation($node, $translation_langcode);

  // Check the content in the source language.
  $this
    ->checkFieldCollectionContent($node_prog, $source_langcode);

  // Check that the field translated content has been removed.
  $this
    ->assert(empty($node->{$this->field_name}[$translation_langcode]), t('Translated content removed.'));
}