You are here

function SimpleNewsI18nTestCase::testContentTranslation in Simplenews 7

File

tests/simplenews.test, line 1725
Simplenews test functions.

Class

SimpleNewsI18nTestCase
Tests for I18N integration.

Code

function testContentTranslation() {

  // Sign up two users, one in english, another in spanish.
  $english_mail = $this
    ->randomEmail();
  $spanish_mail = $this
    ->randomEmail();
  $spanish_mail2 = $this
    ->randomEmail();
  $tid = $this
    ->getRandomNewsletter();
  simplenews_subscribe_user($english_mail, $tid, FALSE, 'english', 'en');
  simplenews_subscribe_user($spanish_mail, $tid, FALSE, 'spanish', 'es');
  simplenews_subscribe_user($spanish_mail2, $tid, FALSE, 'spanish', 'es');

  // Translate category.
  $vocabulary = taxonomy_vocabulary_machine_name_load('newsletter');
  $vocabulary->i18n_mode = I18N_MODE_LOCALIZE;
  taxonomy_vocabulary_save($vocabulary);
  drupal_static_reset('i18n_taxonomy_vocabulary_mode');

  // Refresh strings.
  // @todo: simplenews_category_save() should probably take care of this.
  // Currently done separately in simplenews_admin_category_form_submit()
  // for the admin UI.
  $tid = $this
    ->getRandomNewsletter();
  $term = taxonomy_term_load($tid);
  taxonomy_term_save($term);

  // Translate term to spanish.
  list($textgroup, $context) = i18n_string_context(array(
    'taxonomy',
    'term',
    $tid,
    'name',
  ));
  i18n_string_textgroup($textgroup)
    ->update_translation($context, 'es', $es_name = $this
    ->randomName());

  // Enable translation for newsletters.
  $edit = array(
    'language_content_type' => TRANSLATION_ENABLED,
  );
  $this
    ->drupalPost('admin/structure/types/manage/simplenews', $edit, t('Save content type'));

  // Create a Newsletter including a translation.
  $english = array(
    'title' => $this
      ->randomName(),
    'language' => 'en',
    'body[und][0][value]' => 'Link to node: [node:url]',
  );
  $this
    ->drupalPost('node/add/simplenews', $english, 'Save');
  $this
    ->assertTrue(preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches), 'Node created');
  $english_node = node_load($matches[1]);
  $this
    ->clickLink(t('Translate'));
  $this
    ->clickLink(t('add translation'));
  $spanish = array(
    'title' => $this
      ->randomName(),
  );
  $this
    ->drupalPost(NULL, $spanish, 'Save');
  $this
    ->assertTrue(preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches), 'Node created');
  $spanish_node = node_load($matches[1]);
  entity_get_controller('node')
    ->resetCache();
  $english_node = node_load($english_node->nid);

  // Verify redirecation to source.
  $this
    ->clickLink(t('Newsletter'));
  $this
    ->assertText(t('This newsletter issue is part of a translation set. Sending this set is controlled from the translation source newsletter.'));
  $this
    ->clickLink(t('translation source newsletter'));
  $this
    ->assertText($english_node->title);

  // Send newsletter.
  $this
    ->clickLink(t('Newsletter'));
  $edit = array(
    'simplenews[send]' => SIMPLENEWS_COMMAND_SEND_NOW,
  );
  $this
    ->drupalPost(NULL, $edit, t('Submit'));
  simplenews_cron();
  $this
    ->assertEqual(3, count($this
    ->drupalGetMails()));
  $category = simplenews_category_load($this
    ->getRandomNewsletter());
  $languages = language_list();
  foreach ($this
    ->drupalGetMails() as $mail) {
    if ($mail['to'] == $english_mail) {
      $this
        ->assertEqual('en', $mail['language']);
      $this
        ->assertEqual('[' . $category->name . '] ' . $english_node->title, $mail['subject']);
      $node_url = url('node/' . $english_node->nid, array(
        'language' => $languages['en'],
        'absolute' => TRUE,
      ));
    }
    elseif ($mail['to'] == $spanish_mail || $mail['to'] == $spanish_mail2) {
      $this
        ->assertEqual('es', $mail['language']);
      $this
        ->assertEqual('[' . $es_name . '] ' . $spanish_node->title, $mail['subject']);
      $node_url = url('node/' . $spanish_node->nid, array(
        'language' => $languages['es'],
        'absolute' => TRUE,
      ));
    }
    else {
      $this
        ->fail(t('Mail not sent to expected recipient'));
    }

    // Verify that the link is in the correct language.
    $this
      ->assertTrue(strpos($mail['body'], $node_url) !== FALSE);
  }

  // Verify sent subscriber count for each node.
  $newsletter = simplenews_newsletter_load($english_node->nid);
  $this
    ->assertEqual(1, $newsletter->sent_subscriber_count, 'subscriber count is correct for english');
  $newsletter = simplenews_newsletter_load($spanish_node->nid);
  $this
    ->assertEqual(2, $newsletter->sent_subscriber_count, 'subscriber count is correct for spanish');

  // Make sure the language of a node can be changed.
  $english = array(
    'title' => $this
      ->randomName(),
    'language' => 'en',
    'body[und][0][value]' => 'Link to node: [node:url]',
  );
  $this
    ->drupalPost('node/add/simplenews', $english, 'Save');
  $this
    ->clickLink(t('Edit'));
  $edit = array(
    'language' => 'es',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
}