You are here

public function Drupali18nSyncNodeReferenceTestCase::testI18nSyncNodeReference in i18n node reference synch 7

Full test for node synchronization.

File

./i18n_node_reference.test, line 92
Test node reference field synchronization.

Class

Drupali18nSyncNodeReferenceTestCase
Unit test for node reference field synchronisation.

Code

public function testI18nSyncNodeReference() {

  // Log in as the content editor.
  $this
    ->drupalLogin($this->content_editor);

  // Create translatable articles for reference.
  $articles = array();
  $article_translations = array();
  $articles['en'] = $this
    ->createNode('article', 'English only article', $this
    ->randomString(20), language_default('language'));
  $articles['en_nl'] = $this
    ->createNode('article', 'English and Dutch article', $this
    ->randomString(20), 'en');
  $this
    ->drupalGet('node/' . $articles['en_nl']->nid . '/translate');
  $article_translations['en_nl'] = $this
    ->createNodeTranslationSet($articles['en_nl']);
  $articles['en_nl_2'] = $this
    ->createNode('article', 'English and Dutch 2 article', $this
    ->randomString(20), 'en');
  $this
    ->drupalGet('node/' . $articles['en_nl_2']->nid . '/translate');
  $article_translations['en_nl_2'] = $this
    ->createNodeTranslationSet($articles['en_nl_2']);
  $articles['und'] = $this
    ->createNode('article', 'Language independent', $this
    ->randomString(20), LANGUAGE_NONE);

  // Create untranslatable references.
  $untranslated = array();
  $untranslated['a'] = $this
    ->createNode('untranslated', 'A', $this
    ->randomString(20), 'en');
  $untranslated['b'] = $this
    ->createNode('untranslated', 'B', $this
    ->randomString(20), 'nl');
  $untranslated['c'] = $this
    ->createNode('untranslated', 'C', $this
    ->randomString(20), LANGUAGE_NONE);
  $pages = array();
  $page_translations = array();
  $pages['en'] = $this
    ->createNode('page', 'English only page', $this
    ->randomString(20), language_default('language'));
  $pages['en_nl'] = $this
    ->createNode('page', 'English and Dutch page', $this
    ->randomString(20), 'en', array(
    'title' => 'English and Dutch',
  ));
  $this
    ->drupalGet('node/' . $pages['en_nl']->nid . '/translate');
  $page_translations['en_nl'] = $this
    ->createNodeTranslationSet($pages['en_nl']);
  $pages['und'] = $this
    ->createNode('page', 'Untranslatable page', $this
    ->randomString(20), LANGUAGE_NONE);

  // Test if article references are saved.
  $nids = array(
    $articles['en']->nid,
    $article_translations['en_nl']['en']->nid,
    $articles['und']->nid,
    $untranslated['a']->nid,
    $untranslated['c']->nid,
  );
  $edit = array();
  foreach ($nids as $nid) {
    $edit["field_article[und][{$nid}]"] = $nid;
  }
  $this
    ->drupalPost('node/' . $pages['en']->nid . '/edit', $edit, t('Save'));
  $this
    ->assertPageReferencesNodes($pages['en']->nid, $nids, t("Test setting node references for page without translations"));

  // Test simple synchronization.
  $this
    ->drupalPost('node/' . $page_translations['en_nl']['en']->nid . '/edit', $edit, t('Save'));
  $this
    ->assertPageReferencesNodes($page_translations['en_nl']['en']->nid, $nids, t("Test setting node references for page with translations"));
  $trans_nids = array(
    $article_translations['en_nl']['nl']->nid,
    $articles['und']->nid,
    $untranslated['a']->nid,
    $untranslated['c']->nid,
  );
  $this
    ->assertPageReferencesNodes($page_translations['en_nl']['nl']->nid, $trans_nids, t("Test translated nids."));

  // Test if removed translations are really removed.
  $edit = array();
  foreach ($trans_nids as $nid) {
    $edit["field_article[und][{$nid}]"] = FALSE;
  }
  $this
    ->drupalPost('node/' . $page_translations['en_nl']['nl']->nid . '/edit', $edit, t('Save'));
  $this
    ->assertPageReferencesNodes($page_translations['en_nl']['nl']->nid, array(), t("Test removing node references in Dutch page"));
  $this
    ->assertPageReferencesNodes($page_translations['en_nl']['en']->nid, array(
    $articles['en']->nid,
  ), t("Test removed nids in the English version. Only the English-only article should remain"));

  // Test node with language 'undefined'.
  $nids = array(
    $article_translations['en_nl']['en']->nid,
    $articles['und']->nid,
    $untranslated['a']->nid,
    $untranslated['b']->nid,
    $untranslated['c']->nid,
  );
  $edit = array();
  foreach ($nids as $nid) {
    $edit["field_article[und][{$nid}]"] = $nid;
  }
  $this
    ->drupalPost('node/' . $pages['und']->nid . '/edit', $edit, t('Save'));
  $this
    ->assertPageReferencesNodes($pages['und']->nid, $nids, t("Test setting node references for untranslatable page"));
}