View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
class NodeTypeTranslationTest extends BrowserTestBase {
protected static $modules = [
'block',
'config_translation',
'field_ui',
'node',
];
protected $defaultTheme = 'stark';
protected $defaultLangcode = 'fr';
protected $additionalLangcodes = [
'es',
];
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$admin_permissions = [
'administer content types',
'bypass node access',
'administer node fields',
'administer languages',
'administer site configuration',
'administer themes',
'translate configuration',
];
$this->adminUser = $this
->drupalCreateUser($admin_permissions);
foreach ($this->additionalLangcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
}
protected function installParameters() {
$parameters = parent::installParameters();
$parameters['parameters']['langcode'] = $this->defaultLangcode;
\Drupal::service('file_system')
->mkdir($this->publicFilesDirectory . '/translations', NULL, TRUE);
file_put_contents($this->publicFilesDirectory . "/translations/drupal-8.0.0.{$this->defaultLangcode}.po", '');
return $parameters;
}
public function testNodeTypeTranslation() {
$type = mb_strtolower($this
->randomMachineName(16));
$name = $this
->randomString();
$this
->drupalLogin($this->adminUser);
$this
->drupalCreateContentType([
'type' => $type,
'name' => $name,
]);
$langcode = $this->additionalLangcodes[0];
$translated_name = $langcode . '-' . $name;
$edit = [
"translation[config_names][node.type.{$type}][name]" => $translated_name,
];
$this
->drupalGet("admin/structure/types/manage/{$type}/translate/{$langcode}/add");
$this
->submitForm($edit, 'Save translation');
$this
->drupalGet('admin/appearance');
$this
->submitForm([
'use_admin_theme' => '0',
], 'Save configuration');
$this
->drupalGet("{$langcode}/node/add/{$type}");
$this
->assertSession()
->responseContains(t('Create @name', [
'@name' => $translated_name,
], [
'langcode' => $langcode,
]));
$this
->drupalGet('admin/appearance');
$this
->submitForm([
'use_admin_theme' => '1',
], 'Save configuration');
$this
->drupalGet("{$langcode}/node/add/{$type}");
$this
->assertSession()
->responseContains(t('Create @name', [
'@name' => $translated_name,
], [
'langcode' => $langcode,
]));
}
public function testNodeTypeTitleLabelTranslation() {
$type = mb_strtolower($this
->randomMachineName(16));
$name = $this
->randomString();
$this
->drupalLogin($this->adminUser);
$this
->drupalCreateContentType([
'type' => $type,
'name' => $name,
]);
$langcode = $this->additionalLangcodes[0];
$this
->drupalGet("admin/structure/types/manage/{$type}");
$this
->submitForm([
'title_label' => 'Edited title',
], 'Save content type');
$this
->drupalGet("admin/structure/types/manage/{$type}/translate/{$langcode}/add");
$this
->assertSession()
->pageTextContains('Edited title');
$this
->submitForm([
"translation[config_names][core.base_field_override.node.{$type}.title][label]" => 'Translated title',
], 'Save translation');
$this
->drupalGet("node/add/{$type}");
$this
->assertSession()
->pageTextContains('Edited title');
$this
->drupalGet("{$langcode}/node/add/{$type}");
$this
->assertSession()
->pageTextContains('Translated title');
$this
->drupalGet("admin/structure/types/manage/{$type}/fields/add-field");
$this
->submitForm([
'new_storage_type' => 'email',
'label' => 'Email',
'field_name' => 'email',
], 'Save and continue');
$this
->submitForm([], 'Save field settings');
$this
->submitForm([], 'Save settings');
$type = mb_strtolower($this
->randomMachineName(16));
$name = $this
->randomString();
$this
->drupalCreateContentType([
'type' => $type,
'name' => $name,
]);
$this
->drupalPlaceBlock('local_tasks_block', [
'primary' => TRUE,
]);
$this
->drupalGet('admin/config/regional/language');
$this
->submitForm([
'site_default_language' => 'es',
], 'Save configuration');
$this
->drupalGet("es/admin/structure/types/manage/{$type}/fields/add-field");
$this
->submitForm([
'existing_storage_name' => 'field_email',
'existing_storage_label' => 'Email',
], 'Save and continue');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet("es/admin/structure/types/manage/{$type}/fields/node.{$type}.field_email/translate");
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains("The configuration objects have different language codes so they cannot be translated");
}
}