View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
class NodeTypeTranslationTest extends BrowserTestBase {
public static $modules = [
'block',
'config_translation',
'field_ui',
'node',
];
protected $defaultTheme = 'stark';
protected $defaultLangcode = 'fr';
protected $additionalLangcodes = [
'es',
];
protected $adminUser;
protected function setUp() {
parent::setUp();
$admin_permissions = [
'administer content types',
'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
->drupalPostForm("admin/structure/types/manage/{$type}/translate/{$langcode}/add", $edit, t('Save translation'));
$this
->drupalPostForm('admin/appearance', [
'use_admin_theme' => '0',
], t('Save configuration'));
$this
->drupalGet("{$langcode}/node/add/{$type}");
$this
->assertRaw(t('Create @name', [
'@name' => $translated_name,
], [
'langcode' => $langcode,
]));
$this
->drupalPostForm('admin/appearance', [
'use_admin_theme' => '1',
], t('Save configuration'));
$this
->drupalGet("{$langcode}/node/add/{$type}");
$this
->assertRaw(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
->drupalPostForm("admin/structure/types/manage/{$type}", [
'title_label' => 'Edited title',
], t('Save content type'));
$this
->drupalGet("admin/structure/types/manage/{$type}/translate/{$langcode}/add");
$this
->assertText('Edited title');
$this
->drupalPostForm(NULL, [
"translation[config_names][core.base_field_override.node.{$type}.title][label]" => 'Translated title',
], t('Save translation'));
$this
->drupalGet("node/add/{$type}");
$this
->assertText('Edited title');
$this
->drupalGet("{$langcode}/node/add/{$type}");
$this
->assertText('Translated title');
$this
->drupalPostForm("admin/structure/types/manage/{$type}/fields/add-field", [
'new_storage_type' => 'email',
'label' => 'Email',
'field_name' => 'email',
], 'Save and continue');
$this
->drupalPostForm(NULL, [], 'Save field settings');
$this
->drupalPostForm(NULL, [], '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
->drupalPostForm('admin/config/regional/language', [
'site_default_language' => 'es',
], 'Save configuration');
$this
->drupalGet("es/admin/structure/types/manage/{$type}/fields/add-field");
$this
->drupalPostForm(NULL, [
'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
->assertText("The configuration objects have different language codes so they cannot be translated");
}
}