View source
<?php
namespace Drupal\Tests\language\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
class ConfigurableLanguageManagerTest extends BrowserTestBase {
public static $modules = [
'language',
'content_translation',
'node',
'locale',
'block',
'system',
'user',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$user = $this
->createUser([], '', TRUE);
$this
->drupalLogin($user);
ConfigurableLanguage::createFromLangcode('es')
->save();
NodeType::create([
'type' => 'page',
'name' => t('Page'),
])
->save();
$config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'page');
$config
->setDefaultLangcode('en')
->setLanguageAlterable(TRUE)
->save();
$node = Node::create([
'type' => 'page',
'title' => 'English',
]);
$node
->save();
$node
->addTranslation('es', [
'title' => 'Español',
]);
$node
->save();
\Drupal::getContainer()
->get('language_negotiator')
->updateConfiguration([
'language_interface',
'language_content',
]);
$user
->set('preferred_admin_langcode', 'en')
->save();
$this
->config('node.settings')
->set('use_admin_theme', '1')
->save();
$this->container
->get('router.builder')
->rebuild();
$this
->placeBlock('system_powered_by_block', [
'region' => 'content',
]);
$this
->drupalGet('/es/node/1');
$string_storage = \Drupal::getContainer()
->get('locale.storage');
$source = $string_storage
->findString([
'source' => 'Powered by <a href=":poweredby">Drupal</a>',
]);
$string_storage
->createTranslation([
'lid' => $source->lid,
'language' => 'es',
'translation' => 'Funciona con ...',
])
->save();
Cache::invalidateTags([
'rendered',
'locale',
]);
}
public function testUrlContentTranslationWithPreferredAdminLanguage() {
$assert_session = $this
->assertSession();
$language_negotiator = \Drupal::getContainer()
->get('language_negotiator');
$language_negotiator
->saveConfiguration('language_interface', [
'language-user-admin' => 1,
'language-url' => 2,
'language-selected' => 3,
]);
$language_negotiator
->saveConfiguration('language_content', [
'language-url' => 4,
'language-selected' => 5,
]);
$this
->drupalGet('/node/1');
$assert_session
->pageTextContains('English');
$assert_session
->pageTextContains('Powered by');
$this
->drupalGet('/es/node/1');
$assert_session
->pageTextContains('Español');
$assert_session
->pageTextContains('Funciona con');
$assert_session
->pageTextNotContains('Powered by');
$this
->drupalGet('/es/node/1/edit');
$assert_session
->pageTextContains('Español');
$assert_session
->pageTextContains('Powered by');
$assert_session
->pageTextNotContains('Funciona con');
}
public function testUrlContentTranslationWithSessionLanguage() {
$assert_session = $this
->assertSession();
$language_negotiator = \Drupal::getContainer()
->get('language_negotiator');
$language_negotiator
->saveConfiguration('language_interface', [
'language-session' => 1,
'language-url' => 2,
'language-selected' => 3,
]);
$language_negotiator
->saveConfiguration('language_content', [
'language-url' => 4,
'language-selected' => 5,
]);
$this
->drupalGet('/node/1');
$assert_session
->pageTextContains('English');
$assert_session
->pageTextContains('Powered by');
$this
->drupalGet('/es/node/1');
$assert_session
->pageTextContains('Español');
$assert_session
->pageTextNotContains('Powered by');
$assert_session
->pageTextContains('Funciona con');
$this
->drupalGet('/node/1', [
'query' => [
'language' => 'es',
],
]);
$assert_session
->pageTextContains('English');
$assert_session
->pageTextNotContains('Español');
$assert_session
->pageTextContains('Funciona con');
$assert_session
->pageTextNotContains('Powered by');
$this
->drupalGet('/es/node/1', [
'query' => [
'language' => 'en',
],
]);
$assert_session
->pageTextNotContains('English');
$assert_session
->pageTextContains('Español');
$assert_session
->pageTextNotContains('Funciona con');
$assert_session
->pageTextContains('Powered by');
}
public function testUserProfileTranslationWithPreferredAdminLanguage() {
$assert_session = $this
->assertSession();
$language_negotiator = \Drupal::getContainer()
->get('language_negotiator');
$language_negotiator
->saveConfiguration('language_interface', [
'language-user-admin' => 1,
'language-selected' => 2,
]);
$field_name = mb_strtolower($this
->randomMachineName());
$label = mb_strtolower($this
->randomMachineName());
$field_label_en = "English {$label}";
$field_label_es = "Español {$label}";
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'user',
'type' => 'string',
]);
$field_storage
->save();
$instance = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'user',
'label' => $field_label_en,
]);
$instance
->save();
\Drupal::languageManager()
->getLanguageConfigOverride('es', "field.field.user.user.{$field_name}")
->set('label', $field_label_es)
->save();
EntityFormDisplay::create([
'targetEntityType' => 'user',
'bundle' => 'user',
'mode' => 'default',
'status' => TRUE,
])
->setComponent($field_name, [
'type' => 'string_textfield',
])
->save();
$user_id = \Drupal::currentUser()
->id();
$this
->drupalGet("/user/{$user_id}/edit");
$assert_session
->pageTextContains($field_label_en);
$assert_session
->pageTextNotContains($field_label_es);
$this
->drupalPostForm(NULL, [
'edit-preferred-admin-langcode' => 'es',
], 'edit-submit');
$assert_session
->pageTextContains($field_label_es);
$assert_session
->pageTextNotContains($field_label_en);
$this
->drupalPostForm(NULL, [
'edit-preferred-admin-langcode' => 'en',
], 'edit-submit');
$assert_session
->pageTextContains($field_label_en);
$assert_session
->pageTextNotContains($field_label_es);
}
}