You are here

public function DraftyTitleTestCase::testProgrammaticTranslationWorkflow in Drafty 7

Tests title module interaction with draft translation creation.

File

tests/DraftyTitleTestCase.test, line 74

Class

DraftyTitleTestCase
Tests for legacy field replacement.

Code

public function testProgrammaticTranslationWorkflow() {

  // Create a node and assign it an original language different from
  // the default language.
  $langcode = 'it';
  $original_values = array(
    'title' => $langcode . '_' . $this
      ->randomName(),
  );
  $node = (object) ($original_values + array(
    'type' => 'page',
    'status' => 1,
  ));
  entity_translation_get_handler('node', $node)
    ->setOriginalLanguage($langcode);
  $node->language = $langcode;
  node_save($node);
  $original_vid = $node->vid;
  $this
    ->assertTrue($this
    ->checkLegacyValues($node, $original_values), 'Legacy field values correctly stored.');
  $node = $this
    ->nodeLoad($node->nid);
  $this
    ->assertTrue($this
    ->checkFieldValues($node, $original_values, $langcode), 'Replacing field values correctly created from the legacy field values.');

  // Pollute synchronization cache to ensure the expected values are stored
  // anyway.
  title_entity_sync('node', $node, $langcode);

  // Create a translation using the default language.
  $translation_langcode = language_default()->language;
  $translated_values = array(
    'title' => $translation_langcode . '_' . $this
      ->randomName(),
  );
  foreach ($translated_values as $name => $value) {
    $node->{$name} = $value;
  }
  $translation = array(
    'language' => $translation_langcode,
    'source' => $langcode,
    'uid' => $this->loggedInUser->uid,
    'status' => 1,
    'translate' => 0,
    'created' => REQUEST_TIME,
    'changed' => REQUEST_TIME,
  );
  entity_translation_get_handler('node', $node)
    ->setTranslation($translation);
  $node->is_draft_revision = TRUE;
  node_save($node);
  $node = $this
    ->nodeLoad($node->nid, $translation_langcode);
  $new_vid = $node->vid;

  //  Drafty doesn't allow us to load the draft revision while it's being
  //  created by design, so find it manually based on the two revisions IDs
  //  we know about.
  $vid = db_select('node_revision')
    ->fields('node_revision', array(
    'vid',
  ))
    ->condition('nid', $node->nid)
    ->condition('vid', $original_vid, '>')
    ->condition('vid', $new_vid, '<')
    ->execute()
    ->fetchField();
  $node = node_load($node->nid, $vid, TRUE);
  $vids = db_query('SELECT vid FROM {node_revision} WHERE nid = :nid', array(
    ':nid' => $node->nid,
  ))
    ->fetchCol();
  $this
    ->assertTrue($this
    ->checkLegacyValues($node, $original_values), 'Legacy field values correctly stored.');
  $node = $this
    ->nodeLoad($node->nid, $translation_langcode);

  // Even though the language is set to that of the translation, no
  // translation should happen since it's in a draft revision.
  $this
    ->assertFalse($this
    ->checkFieldValues($node, $translated_values, $translation_langcode), 'Field translation for draft revision does not override.');
  $this
    ->assertTrue($this
    ->checkFieldValues($node, $original_values, $langcode), 'Replacing field original values correctly preserved.');
  drafty()
    ->publishRevision('node', $node->nid, $vid);
  $node = $this
    ->nodeLoad($node->nid, $translation_langcode);
  $this
    ->assertTrue($this
    ->checkFieldValues($node, $translated_values, $translation_langcode), 'Field translation overrides once draft is published.');
  $this
    ->assertTrue($this
    ->checkFieldValues($node, $original_values, $langcode, FALSE), 'Replacing field original values correctly preserved.');

  // Delete the translation.
  entity_translation_get_handler('node', $node)
    ->removeTranslation($translation_langcode);
  node_save($node);
  $this
    ->assertTrue($this
    ->checkLegacyValues($node, $original_values), 'Legacy field values correctly stored.');
  $node = $this
    ->nodeLoad($node->nid, $langcode);
  $this
    ->assertTrue($this
    ->checkFieldValues($node, $original_values, $langcode), 'Replacing field translations correctly deleted.');

  // Make the node language neutral.
  entity_translation_get_handler('node', $node)
    ->setOriginalLanguage(LANGUAGE_NONE);
  foreach ($original_values as $name => $value) {
    $field_name = $name . '_field';
    $node->{$field_name}[LANGUAGE_NONE] = $node->{$field_name}[$langcode];
    $node->{$field_name}[$langcode] = array();
  }
  node_save($node);
  $this
    ->assertTrue($this
    ->checkLegacyValues($node, $original_values), 'Legacy field values correctly stored.');
  $node = $this
    ->nodeLoad($node->nid);
  $this
    ->assertTrue($this
    ->checkFieldValues($node, $original_values, LANGUAGE_NONE), 'Term original language correctly changed to the former translation language.');

  // Change the node language to the former translation language.
  entity_translation_get_handler('node', $node)
    ->setOriginalLanguage($translation_langcode);
  foreach ($original_values as $name => $value) {
    $field_name = $name . '_field';
    $node->{$field_name}[$translation_langcode] = $node->{$field_name}[LANGUAGE_NONE];
    $node->{$field_name}[LANGUAGE_NONE] = array();
  }
  node_save($node);
  $this
    ->assertTrue($this
    ->checkLegacyValues($node, $original_values), 'Legacy field values correctly stored.');
  $node = $this
    ->nodeLoad($node->nid, $translation_langcode);
  $this
    ->assertTrue($this
    ->checkFieldValues($node, $original_values, $translation_langcode), 'Term original language correctly changed to language neutral.');

  // Make a replacing field untranslatable and change its value.
  $field_name = 'title_field';
  $field = field_info_field($field_name);
  $field['translatable'] = FALSE;
  field_update_field($field);
  $original_values['title'] = LANGUAGE_NONE . '_' . $this
    ->randomName();
  $node->title = $original_values['title'];
  node_save($node);
  $this
    ->assertTrue($this
    ->checkLegacyValues($node, $original_values), 'Legacy field values correctly stored.');
  $node = $this
    ->nodeLoad($node->nid);
  $this
    ->assertEqual($node->{$field_name}[LANGUAGE_NONE][0]['value'], $original_values['title'], 'Untranslatable replacing field on translatable entity correctly handled.');
}