You are here

function i18nSyncTestCase::testIi18nSync in Internationalization 7

File

i18n_sync/i18n_sync.test, line 23
Test field synchronization

Class

i18nSyncTestCase
@file Test field synchronization

Code

function testIi18nSync() {
  drupal_static_reset('language_list');
  $language_list = language_list();
  $language_count = count($language_list);

  // Enable tags field for page content type.
  $edit = array(
    'fields[_add_existing_field][label]' => t('Tags'),
    'fields[_add_existing_field][field_name]' => 'field_tags',
    'fields[_add_existing_field][widget_type]' => 'taxonomy_autocomplete',
  );
  $this
    ->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  $this
    ->drupalPost(NULL, array(), t('Save settings'));

  // Create some content and check selection modes
  $this
    ->drupalLogin($this->content_editor);

  // variable_set('language_content_type_story', 1);
  $source = $this
    ->createNode('page', $this
    ->randomName(), $this
    ->randomString(20), language_default('language'), array(
    'field_tags[und]' => $tag_name = $this
      ->randomName(),
  ));
  $this
    ->drupalGet('node/' . $source->nid . '/translate');
  $translations = $this
    ->createNodeTranslationSet($source);
  drupal_static_reset('translation_node_get_translations');
  $this
    ->assertEqual(count(translation_node_get_translations($source->tnid)), $language_count, "Created {$language_count} {$source->type} translations.");

  // Set up fields for synchronization: promoted, field_tags
  $this
    ->drupalLogin($this->admin_user);
  $edit = array(
    'i18n_sync_node_type[sticky]' => 1,
    'i18n_sync_node_type[promote]' => 1,
    'i18n_sync_node_type[field_tags]' => 1,
  );
  $this
    ->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  $this
    ->drupalGet('admin/structure/types/manage/page');

  // Update source fields and check translations have been updated.
  $new_title = $this
    ->randomName();
  $new_tag = $this
    ->randomName();
  $edit = array(
    'promote' => 1,
    'sticky' => 1,
    'field_tags[und]' => $new_tag,
  );
  $this
    ->drupalPost('node/' . $source->nid . '/edit', $edit, t('Save'));
  $terms = taxonomy_get_term_by_name($new_tag);
  $term = reset($terms);

  // Refresh cache and load translations again.
  drupal_static_reset('translation_node_get_translations');

  // For some reason the field cache for the node translation gets outdated values.
  // This only happens when running unit tests.
  // If we don't do this, we miss the field_tags value for the second node.
  field_cache_clear();
  $translations = translation_node_get_translations($source->tnid);
  foreach ($translations as $lang => $node) {
    $node = node_load($node->nid, NULL, TRUE);
    $this
      ->assertTrue($node->promote && $node->sticky, "Translation for language {$lang} has been promoted an made sticky.");
    $this
      ->assertEqual($node->field_tags['und'][0]['tid'], $term->tid, "Tag for translation {$lang} has been properly updated.");
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertRaw($new_tag, "New tag for translation {$lang} is showing up.");
  }
}