You are here

function SimpleNewsI18nTestCase::testNewsletterIssueTranslation in Simplenews 7.2

File

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

Class

SimpleNewsI18nTestCase
Tests for I18N integration.

Code

function testNewsletterIssueTranslation() {

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

  // 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(array(
    $english_node->nid,
  ));
  $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();
  debug(db_query('SELECT * FROM {simplenews_subscriber}')
    ->fetchAll());
  $this
    ->assertEqual(3, count($this
    ->drupalGetMails()));
  $newsletter = simplenews_newsletter_load($this
    ->getRandomNewsletter());
  $languages = language_list();
  foreach ($this
    ->drupalGetMails() as $mail) {
    if ($mail['to'] == $english_mail) {
      $this
        ->assertEqual('en', $mail['language']);
      $this
        ->assertEqual('[' . $newsletter->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']);

      // @todo: Verify newsletter translation once supported again.
      $this
        ->assertEqual('[' . $newsletter->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.
  entity_get_controller('node')
    ->resetCache(array(
    $english_node->nid,
  ));
  $english_node = node_load($english_node->nid);
  $this
    ->assertEqual(1, simplenews_issue_sent_count('node', $english_node), 'subscriber count is correct for english');
  entity_get_controller('node')
    ->resetCache(array(
    $spanish_node->nid,
  ));
  $spanish_node = node_load($spanish_node->nid);
  $this
    ->assertEqual(2, simplenews_issue_sent_count('node', $spanish_node), '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'));
}