You are here

tmgmt_node.test in Translation Management Tool 7

File

sources/node/tmgmt_node.test
View source
<?php

/**
 * Basic Node Source tests.
 */
class TMGMTNodeSourceTestCase extends TMGMTEntityTestCaseUtility {
  static function getInfo() {
    return array(
      'name' => 'Node Source tests',
      'description' => 'Exporting source data from nodes and saving translations back to nodes',
      'group' => 'Translation Management',
    );
  }
  function setUp() {
    parent::setUp(array(
      'tmgmt_node',
      'translation',
    ));
    $this
      ->loginAsAdmin();
    $this
      ->setEnvironment('de');
    $this
      ->createNodeType('page', 'Basic page', TRANSLATION_ENABLED, FALSE);
    $this
      ->attachFields('node', 'page', array(
      TRUE,
      TRUE,
      FALSE,
      FALSE,
    ));
  }

  /**
   * Tests nodes field translation.
   */
  function testNodeSource() {

    // Create a translation job.
    $job = $this
      ->createJob();
    $job->translator = $this->default_translator->name;
    $job->settings = array();
    $job
      ->save();
    for ($i = 0; $i < 2; $i++) {
      $node = $this
        ->createNode('page');

      // Create a job item for this node and add it to the job.
      $item = $job
        ->addItem('node', 'node', $node->nid);
      $this
        ->assertEqual('Basic page', $item
        ->getSourceType());
    }

    // Translate the job.
    $job
      ->requestTranslation();
    foreach ($job
      ->getItems() as $item) {

      // The source is only available in en.
      $this
        ->assertJobItemLangCodes($item, 'en', array(
        'en',
      ));
      $item
        ->acceptTranslation();
      $node = node_load($item->item_id);

      // Check if the tnid attribute is bigger than 0.
      $this
        ->assertTrue($node->tnid > 0, 'The source node is part of a translation set.');

      // The translations may be statically cached, so make make sure
      // to reset the cache before loading the node translations.
      $cached_translations =& drupal_static('translation_node_get_translations', array());
      unset($cached_translations[$node->tnid]);

      // Load the translation set of the source node.
      $translations = translation_node_get_translations($node->tnid);
      $this
        ->assertNotNull($translations['de'], 'Translation found for the source node.');
      if (isset($translations['de'])) {
        $tnode = node_load($translations['de']->nid, NULL, TRUE);
        $this
          ->checkTranslatedData($tnode, $item
          ->getData(), 'de');
      }

      // The source should be now available for en and de.
      $this
        ->assertJobItemLangCodes($item, 'en', array(
        'de',
        'en',
      ));
    }
  }

  /**
   * Test if the source is able to pull content in requested language.
   */
  function testRequestDataForSpecificLanguage() {
    $this
      ->setEnvironment('sk');
    $this
      ->setEnvironment('es');
    $content_type = $this
      ->drupalCreateContentType();
    $node = $this
      ->drupalCreateNode(array(
      'title' => $this
        ->randomName(),
      'language' => 'sk',
      'body' => array(
        'sk' => array(
          array(),
        ),
      ),
      'type' => $content_type->type,
    ));
    $this
      ->drupalCreateNode(array(
      'title' => 'en translation',
      'language' => 'en',
      'tnid' => $node->nid,
      'body' => array(
        'en' => array(
          array(),
        ),
      ),
      'type' => $content_type->type,
    ));

    // Create a translation job.
    $job = $this
      ->createJob('en', 'de');
    $job
      ->save();
    $job
      ->addItem('node', 'node', $node->nid);
    $data = $job
      ->getData();
    $this
      ->assertEqual($data[1]['node_title']['#text'], 'en translation');

    // Create new job item with a source language for which the translation
    // does not exit.
    $job = $this
      ->createJob('es', 'cs');
    $job
      ->save();
    try {
      $job
        ->addItem('node', 'node', $node->nid);
      $this
        ->fail('The job item should not be added as there is no translation for language "es"');
    } catch (TMGMTException $e) {
      $languages = language_list();
      $this
        ->assertEqual(t('Unable to load %language translation for the node %title', array(
        '%language' => $languages['es']->name,
        '%title' => $node->title,
      )), $e
        ->getMessage());
    }
  }

  /**
   * Compares the data from an entity with the translated data.
   *
   * @param $node
   *  The translated node object.
   * @param $data
   *  An array with the translated data.
   * @param $langcode
   *  The code of the target language.
   */
  function checkTranslatedData($node, $data, $langcode) {
    foreach (element_children($data) as $field_name) {
      if ($field_name == 'node_title') {
        $this
          ->assertEqual($node->title, $data['node_title']['#translation']['#text'], 'The title of the translated node matches the translated data.');
        continue;
      }
      foreach (element_children($data[$field_name]) as $delta) {
        $field_langcode = field_is_translatable('node', field_info_field($field_name)) ? $langcode : LANGUAGE_NONE;
        foreach (element_children($data[$field_name][$delta]) as $column) {
          $column_value = $data[$field_name][$delta][$column];
          if (!isset($column_value['#translate']) || $column_value['#translate']) {
            $this
              ->assertEqual($node->{$field_name}[$field_langcode][$delta][$column], $column_value['#translation']['#text'], format_string('The translatable field %field:%delta has been populated with the proper translated data.', array(
              '%field' => $field_name,
              'delta' => $delta,
            )));
          }
        }
      }
    }
  }

}

Classes

Namesort descending Description
TMGMTNodeSourceTestCase Basic Node Source tests.