View source
<?php
namespace Drupal\Tests\bamboo_twig\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
class BambooTwigI18nTest extends BambooTwigTestBase {
public static $modules = [
'locale',
'language',
'node',
'taxonomy',
'bamboo_twig',
'bamboo_twig_i18n',
'bamboo_twig_test',
];
protected $articles;
protected $tags;
public function setUp() {
parent::setUp();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this
->setUpLanguages();
$this
->setUpArticles();
$this
->setUpTags();
$this
->setUpTranslations();
}
protected function setUpLanguages() {
ConfigurableLanguage::createFromLangcode('fr')
->save();
ConfigurableLanguage::createFromLangcode('de')
->save();
}
protected function setUpArticles() {
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this->articles = [];
$article = $this->entityTypeManager
->getStorage('node')
->create([
'type' => 'article',
'title' => 'News N°1',
]);
$article
->save();
$this->articles[] = $article;
$article = $this->entityTypeManager
->getStorage('node')
->create([
'type' => 'article',
'title' => 'News N°2',
]);
$article
->save();
$article_translation = $article
->addTranslation('fr', $article
->toArray());
$article_translation->title = 'Article N°2';
$article_translation
->save();
$this->articles[] = $article;
$article = $this->entityTypeManager
->getStorage('node')
->create([
'type' => 'article',
'title' => 'News N°3',
]);
$article
->save();
$article_translation = $article
->addTranslation('fr', $article
->toArray());
$article_translation->title = 'Article N°3';
$article_translation
->save();
$article_translation = $article
->addTranslation('de', $article
->toArray());
$article_translation->title = 'Artikel N°3';
$article_translation
->save();
$this->articles[] = $article;
}
protected function setUpTags() {
$this->entityTypeManager
->getStorage('taxonomy_vocabulary')
->create([
'vid' => 'tags',
'name' => 'Tags',
])
->save();
$this->tags = [];
$tag = $this->entityTypeManager
->getStorage('taxonomy_term')
->create([
'name' => 'Tags N°1',
'vid' => 'tags',
]);
$tag
->save();
$this->tags[] = $tag;
$tag = $this->entityTypeManager
->getStorage('taxonomy_term')
->create([
'name' => 'Tags N°2',
'vid' => 'tags',
]);
$tag
->save();
$tag_translation = $tag
->addTranslation('fr', $tag
->toArray());
$tag_translation->title = 'Mot clé N°2';
$tag_translation
->save();
$this->tags[] = $tag;
$tag = $this->entityTypeManager
->getStorage('taxonomy_term')
->create([
'name' => 'Tags N°3',
'vid' => 'tags',
]);
$tag
->save();
$tag_translation = $tag
->addTranslation('fr', $tag
->toArray());
$tag_translation->title = 'Mot clé N°3';
$tag_translation
->save();
$tag_translation = $tag
->addTranslation('de', $tag
->toArray());
$tag_translation->title = 'Stichworte N°3';
$tag_translation
->save();
$this->tags[] = $tag;
}
protected function setUpTranslations() {
$localStorage = $this->container
->get('locale.storage');
$thursday = $localStorage
->createString([
'source' => 'Thursday',
]);
$thursday
->save();
$this->translationsStrings[] = $localStorage
->createTranslation([
'lid' => $thursday->lid,
'language' => 'fr',
'translation' => 'Jeudi',
])
->save();
$thu = $localStorage
->createString([
'source' => 'Thu',
]);
$thu
->save();
$this->translationsStrings[] = $localStorage
->createTranslation([
'lid' => $thu->lid,
'language' => 'fr',
'translation' => 'Jeu',
])
->save();
$july = $localStorage
->createString([
'source' => 'July',
'context' => 'Long month name',
]);
$july
->save();
$this->translationsStrings[] = $localStorage
->createTranslation([
'lid' => $july->lid,
'language' => 'fr',
'translation' => 'Juillet',
])
->save();
}
public function testCurrentLang() {
$this
->drupalGet('/bamboo-twig-i18n');
$this
->assertElementPresent('.test-i18n div.i18n-current-lang');
$this
->assertElementContains('.test-i18n div.i18n-current-lang', 'en');
$this
->drupalGet('/fr/bamboo-twig-i18n');
$this
->assertElementPresent('.test-i18n div.i18n-current-lang');
$this
->assertElementContains('.test-i18n div.i18n-current-lang', 'fr');
}
public function testFormatDate() {
$this
->drupalGet('/bamboo-twig-i18n');
$this
->assertElementPresent('.test-i18n div.i18n-format-date-string');
$this
->assertElementContains('.test-i18n div.i18n-format-date-string', 'Thursday 24th July 2014');
$this
->assertElementPresent('.test-i18n div.i18n-format-date-timestamp');
$this
->assertElementContains('.test-i18n div.i18n-format-date-timestamp', 'Thursday 24th July 2014');
$this
->assertElementPresent('.test-i18n div.i18n-format-date-datetime');
$this
->assertElementContains('.test-i18n div.i18n-format-date-datetime', 'Thursday 24th July 2014');
$this
->assertElementPresent('.test-i18n div.i18n-format-date-datetimeplus');
$this
->assertElementContains('.test-i18n div.i18n-format-date-datetimeplus', 'Thursday 24th July 2014');
$this
->assertElementPresent('.test-i18n div.i18n-format-date-drupaldatetime');
$this
->assertElementContains('.test-i18n div.i18n-format-date-drupaldatetime', 'Thursday 24th July 2014');
$this
->assertElementPresent('.test-i18n div.i18n-format-date-datetime-medium');
$this
->assertElementContains('.test-i18n div.i18n-format-date-datetime-medium', 'Thu, 07/24/2014');
$this
->drupalGet('/fr/bamboo-twig-i18n');
$this
->assertElementPresent('.test-i18n div.i18n-format-date-string');
$this
->assertElementContains('.test-i18n div.i18n-format-date-string', 'Jeudi 24th Juillet 2014');
$this
->assertElementPresent('.test-i18n div.i18n-format-date-timestamp');
$this
->assertElementContains('.test-i18n div.i18n-format-date-timestamp', 'Jeudi 24th Juillet 2014');
$this
->assertElementPresent('.test-i18n div.i18n-format-date-datetime');
$this
->assertElementContains('.test-i18n div.i18n-format-date-datetime', 'Jeudi 24th Juillet 2014');
$this
->assertElementPresent('.test-i18n div.i18n-format-date-datetimeplus');
$this
->assertElementContains('.test-i18n div.i18n-format-date-datetimeplus', 'Jeudi 24th Juillet 2014');
$this
->assertElementPresent('.test-i18n div.i18n-format-date-drupaldatetime');
$this
->assertElementContains('.test-i18n div.i18n-format-date-drupaldatetime', 'Jeudi 24th Juillet 2014');
$this
->assertElementPresent('.test-i18n div.i18n-format-date-datetime-medium');
$this
->assertElementContains('.test-i18n div.i18n-format-date-datetime-medium', 'Jeu, 07/24/2014');
}
public function testGetTranslation() {
$this
->drupalGet('/bamboo-twig-i18n');
$this
->assertElementPresent('.test-i18n div.i18n-get-translation-node-1-ru');
$this
->assertElementEmpty('.test-i18n div.i18n-get-translation-node-1-ru');
$this
->assertElementPresent('.test-i18n div.i18n-get-translation-node-1-en');
$this
->assertElementContains('.test-i18n div.i18n-get-translation-node-1-en', 'News N°1');
$this
->assertElementPresent('.test-i18n div.i18n-get-translation-node-1-de');
$this
->assertElementEmpty('.test-i18n div.i18n-get-translation-node-1-de');
$this
->assertElementPresent('.test-i18n div.i18n-get-translation-node-1-fr');
$this
->assertElementEmpty('.test-i18n div.i18n-get-translation-node-1-fr');
$this
->assertElementPresent('.test-i18n div.i18n-get-translation-node-2-en');
$this
->assertElementContains('.test-i18n div.i18n-get-translation-node-2-en', 'News N°2');
$this
->assertElementPresent('.test-i18n div.i18n-get-translation-node-2-de');
$this
->assertElementEmpty('.test-i18n div.i18n-get-translation-node-2-de');
$this
->assertElementPresent('.test-i18n div.i18n-get-translation-node-2-fr');
$this
->assertElementContains('.test-i18n div.i18n-get-translation-node-2-fr', 'Article N°2');
$this
->assertElementPresent('.test-i18n div.i18n-get-translation-node-3-en');
$this
->assertElementContains('.test-i18n div.i18n-get-translation-node-3-en', 'News N°3');
$this
->assertElementPresent('.test-i18n div.i18n-get-translation-node-3-de');
$this
->assertElementContains('.test-i18n div.i18n-get-translation-node-3-de', 'Artikel N°3');
$this
->assertElementPresent('.test-i18n div.i18n-get-translation-node-3-fr');
$this
->assertElementContains('.test-i18n div.i18n-get-translation-node-3-fr', 'Article N°3');
$this
->drupalGet('/bamboo-twig-i18n');
$this
->assertElementContains('.test-i18n div.i18n-get-translation-node-1', 'News N°1');
$this
->assertElementContains('.test-i18n div.i18n-get-translation-node-2', 'News N°2');
$this
->assertElementContains('.test-i18n div.i18n-get-translation-node-3', 'News N°3');
$this
->drupalGet('/fr/bamboo-twig-i18n');
$this
->assertElementEmpty('.test-i18n div.i18n-get-translation-node-1');
$this
->assertElementContains('.test-i18n div.i18n-get-translation-node-2', 'Article N°2');
$this
->assertElementContains('.test-i18n div.i18n-get-translation-node-3', 'Article N°3');
$this
->drupalGet('/de/bamboo-twig-i18n');
$this
->assertElementEmpty('.test-i18n div.i18n-get-translation-node-1');
$this
->assertElementEmpty('.test-i18n div.i18n-get-translation-node-2');
$this
->assertElementContains('.test-i18n div.i18n-get-translation-node-3', 'Artikel N°3');
}
}