View source
<?php
namespace Drupal\simplenews\Tests;
use Drupal\field\Entity\FieldConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\Node;
use Drupal\simplenews\Entity\Newsletter;
class SimplenewsI18nTest extends SimplenewsTestBase {
public static $modules = [
'locale',
'config_translation',
'content_translation',
];
protected $adminUser;
protected $defaultLanguage;
protected $secondaryLanguage;
function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser(array(
'bypass node access',
'administer nodes',
'administer languages',
'administer content types',
'access administration pages',
'administer filters',
'translate interface',
'subscribe to newsletters',
'administer site configuration',
'translate any entity',
'administer content translation',
'administer simplenews subscriptions',
'send newsletter',
'create content translations',
));
$this
->drupalLogin($this->adminUser);
$this
->setUpLanguages();
}
function setUpLanguages() {
$this->defaultLanguage = 'en';
$this->secondaryLanguage = 'es';
$this
->addLanguage($this->secondaryLanguage);
$config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'simplenews_issue');
$config
->setLanguageAlterable(TRUE);
$config
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'simplenews_issue', TRUE);
drupal_static_reset();
\Drupal::entityTypeManager()
->clearCachedDefinitions();
\Drupal::service('router.builder')
->rebuild();
$field = FieldConfig::loadByName('node', 'simplenews_issue', 'body');
$field
->setTranslatable(TRUE);
$field
->save();
$this
->rebuildContainer();
}
function addLanguage($language_code) {
$language = ConfigurableLanguage::createFromLangcode($language_code);
$language
->save();
}
function testNewsletterIssueTranslation() {
$english_mail = $this
->randomEmail();
$spanish_mail = $this
->randomEmail();
$spanish_mail2 = $this
->randomEmail();
$newsletter_id = $this
->getRandomNewsletter();
$subscription_manager = \Drupal::service('simplenews.subscription_manager');
$subscription_manager
->subscribe($english_mail, $newsletter_id, FALSE, 'english', 'en');
$subscription_manager
->subscribe($spanish_mail, $newsletter_id, FALSE, 'spanish', 'es');
$subscription_manager
->subscribe($spanish_mail2, $newsletter_id, FALSE, 'spanish', 'es');
$edit = array(
'language_configuration[content_translation]' => TRUE,
);
$this
->drupalPostForm('admin/structure/types/manage/simplenews_issue', $edit, t('Save content type'));
$newsletter_id = $this
->getRandomNewsletter();
$english = array(
'title[0][value]' => $this
->randomMachineName(),
'simplenews_issue' => $newsletter_id,
'body[0][value]' => 'Link to node: [node:url]',
);
$this
->drupalPostForm('node/add/simplenews_issue', $english, 'Save');
$this
->assertTrue(preg_match('|node/(\\d+)$|', $this
->getUrl(), $matches), 'Node created');
$node = Node::load($matches[1]);
$this
->clickLink(t('Translate'));
$this
->clickLink(t('Add'));
$spanish = array(
'title[0][value]' => $this
->randomMachineName(),
'body[0][value]' => 'Link to node: [node:url] ES',
);
$this
->drupalPostForm(NULL, $spanish, t('Save (this translation)'));
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache(array(
$node
->id(),
));
$node = Node::load($node
->id());
$translation = $node
->getTranslation($this->secondaryLanguage);
$this
->clickLink(t('Newsletter'));
$this
->drupalPostForm(NULL, array(), t('Send now'));
$this
->cronRun();
$this
->assertEqual(3, count($this
->drupalGetMails()));
$newsletter = Newsletter::load($newsletter_id);
foreach ($this
->drupalGetMails() as $mail) {
if ($mail['to'] == $english_mail) {
$this
->assertEqual('en', $mail['langcode']);
$this
->assertEqual('[' . $newsletter
->label() . '] ' . $node
->getTitle(), $mail['subject']);
$node_url = $node
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$title = $node
->getTitle();
}
elseif ($mail['to'] == $spanish_mail || $mail['to'] == $spanish_mail2) {
$this
->assertEqual('es', $mail['langcode']);
$this
->assertEqual('[' . $newsletter->name . '] ' . $translation
->label(), $mail['subject']);
$node_url = $translation
->toUrl('canonical', [
'absolute' => TRUE,
'language' => $translation
->language(),
])
->toString();
$title = $translation
->getTitle();
}
else {
$this
->fail(t('Mail not sent to expected recipient'));
}
$this
->assertTrue(strpos($mail['body'], $node_url) !== FALSE);
$this
->assertTrue(strpos($mail['body'], mb_strtoupper($title)) !== FALSE);
}
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache(array(
$node
->id(),
));
$node = Node::load($node
->id());
$translation = $node
->getTranslation($this->secondaryLanguage);
$this
->assertEqual(1, $node->simplenews_issue->sent_count, 'subscriber count is correct for english');
$this
->assertEqual(2, $translation->simplenews_issue->sent_count, 'subscriber count is correct for spanish');
$english = array(
'title[0][value]' => $this
->randomMachineName(),
'langcode[0][value]' => 'en',
'body[0][value]' => 'Link to node: [node:url]',
);
$this
->drupalPostForm('node/add/simplenews_issue', $english, 'Save');
$this
->clickLink(t('Edit'));
$edit = array(
'langcode[0][value]' => 'es',
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
}
}