View source
<?php
namespace Drupal\Tests\content_translation\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\node\Functional\NodeTestBase;
use Drupal\Tests\TestFileCreationTrait;
class ContentTranslationLanguageChangeTest extends NodeTestBase {
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
}
protected static $modules = [
'language',
'content_translation',
'content_translation_test',
'node',
'block',
'field_ui',
'image',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$langcodes = [
'de',
'fr',
];
foreach ($langcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
$this
->drupalPlaceBlock('local_tasks_block');
$user = $this
->drupalCreateUser([
'administer site configuration',
'administer nodes',
'create article content',
'edit any article content',
'delete any article content',
'administer content translation',
'translate any entity',
'create content translations',
'administer languages',
'administer content types',
'administer node fields',
]);
$this
->drupalLogin($user);
$edit = [
'entity_types[node]' => TRUE,
'settings[node][article][translatable]' => TRUE,
'settings[node][article][settings][language][language_alterable]' => TRUE,
];
$this
->drupalGet('admin/config/regional/content-language');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('admin/structure/types/manage/article/fields/add-field');
$edit = [
'new_storage_type' => 'image',
'field_name' => 'image_field',
'label' => 'image_field',
];
$this
->submitForm($edit, 'Save and continue');
$this
->submitForm([], 'Save field settings');
$this
->submitForm([], 'Save settings');
}
public function testLanguageChange() {
$this
->drupalGet('node/add/article');
$edit = [
'title[0][value]' => 'english_title',
];
$this
->submitForm($edit, 'Save');
$this
->clickLink('Translate');
$this
->clickLink('Add');
$this
->submitForm([], 'Save (this translation)');
$this
->clickLink('Translate');
$this
->clickLink('Edit');
$images = $this
->drupalGetTestFiles('image')[1];
$edit = [
'langcode[0][value]' => 'de',
'files[field_image_field_0]' => $images->uri,
];
$this
->submitForm($edit, 'Upload');
$this
->submitForm([
'field_image_field[0][alt]' => 'alternative_text',
], 'Save (this translation)');
$node = $this
->getNodeByTitle('english_title');
$translation_languages = $node
->getTranslationLanguages();
$this
->assertArrayHasKey('fr', $translation_languages);
$this
->assertArrayHasKey('de', $translation_languages);
}
public function testTitleDoesNotChangesOnChangingLanguageWidgetAndTriggeringAjaxCall() {
$this
->drupalGet('node/add/article', [
'query' => [
'test_field_only_en_fr' => 1,
],
]);
$edit = [
'title[0][value]' => 'english_title',
'test_field_only_en_fr' => 'node created',
];
$this
->submitForm($edit, 'Save');
$this
->assertEquals('node created', \Drupal::state()
->get('test_field_only_en_fr'));
$this
->clickLink('Translate');
$this
->clickLink('Add');
$this
->submitForm([], 'Save (this translation)');
$this
->clickLink('Translate');
$node = $this
->getNodeByTitle('english_title');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->titleEquals('Edit Article english_title | Drupal');
$images = $this
->drupalGetTestFiles('image')[1];
$edit = [
'langcode[0][value]' => 'de',
'files[field_image_field_0]' => $images->uri,
];
$this
->submitForm($edit, 'Upload');
$this
->assertSession()
->titleEquals('Edit Article english_title | Drupal');
$edit = [
'langcode[0][value]' => 'en',
'field_image_field[0][alt]' => 'alternative_text',
];
$this
->submitForm($edit, 'Save (this translation)');
$node = $this
->getNodeByTitle('english_title');
$translation_languages = $node
->getTranslationLanguages();
$this
->assertArrayHasKey('fr', $translation_languages);
$this
->assertArrayNotHasKey('de', $translation_languages);
}
}