View source
<?php
namespace Drupal\FunctionalTests\Routing;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\link\LinkItemInterface;
use Drupal\Tests\BrowserTestBase;
class RouteCachingLanguageTest extends BrowserTestBase {
protected static $modules = [
'path',
'node',
'content_translation',
'link',
'block',
];
protected $defaultTheme = 'stark';
protected $webUser;
protected function setUp() : void {
parent::setUp();
$this
->createContentType([
'type' => 'page',
]);
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
$permissions = [
'access administration pages',
'administer content translation',
'administer content types',
'administer languages',
'administer url aliases',
'create content translations',
'create page content',
'create url aliases',
'edit any page content',
'translate any entity',
];
$this->webUser = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->webUser);
ConfigurableLanguage::createFromLangcode('fr')
->save();
$edit = [
'entity_types[node]' => 1,
'settings[node][page][translatable]' => 1,
'settings[node][page][fields][path]' => 1,
'settings[node][page][fields][body]' => 1,
'settings[node][page][settings][language][language_alterable]' => 1,
];
$this
->drupalGet('admin/config/regional/content-language');
$this
->submitForm($edit, 'Save configuration');
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_link',
'entity_type' => 'node',
'type' => 'link',
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'page',
'settings' => [
'title' => DRUPAL_OPTIONAL,
'link_type' => LinkItemInterface::LINK_GENERIC,
],
]);
$field
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('node', 'page', 'default')
->setComponent('field_link', [
'type' => 'link_default',
])
->save();
\Drupal::service('entity_display.repository')
->getViewDisplay('node', 'page', 'full')
->setComponent('field_link', [
'type' => 'link',
])
->save();
$edit = [
'language_interface[enabled][language-url]' => 1,
];
$this
->drupalGet('admin/config/regional/language/detection');
$this
->submitForm($edit, 'Save settings');
$edit = [
'prefix[en]' => 'en',
];
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->submitForm($edit, 'Save configuration');
$this
->resetAll();
$definitions = \Drupal::service('entity_field.manager')
->getFieldDefinitions('node', 'page');
$this
->assertTrue($definitions['path']
->isTranslatable(), 'Node path is translatable.');
$this
->assertTrue($definitions['body']
->isTranslatable(), 'Node body is translatable.');
}
public function testLinkTranslationWithAlias($source_langcode) {
$source_url_options = [
'language' => ConfigurableLanguage::load($source_langcode),
];
$edit = [
'langcode[0][value]' => $source_langcode,
'title[0][value]' => 'Target page',
'path[0][alias]' => '/target-page',
];
$this
->drupalGet('node/add/page', $source_url_options);
$this
->submitForm($edit, 'Save');
$assert_session = $this
->assertSession();
$assert_session
->addressEquals($source_langcode . '/target-page');
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextContains('Target page');
$edit = [
'langcode[0][value]' => $source_langcode,
'title[0][value]' => 'Link page',
'field_link[0][uri]' => '/target-page',
'field_link[0][title]' => 'Target page',
'path[0][alias]' => '/link-page',
];
$this
->drupalGet('node/add/page', $source_url_options);
$this
->submitForm($edit, 'Save');
$assert_session
->pageTextContains('Link page');
$this
->clickLink('Target page');
$assert_session
->addressEquals($source_langcode . '/target-page');
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextContains('Target page');
$this
->resetAll();
$this
->drupalGet('link-page', $source_url_options);
$this
->clickLink('Translate');
$this
->clickLink('Add');
$edit = [
'title[0][value]' => 'Translated link page',
'path[0][alias]' => '/translated-link-page',
];
$this
->submitForm($edit, 'Save (this translation)');
$assert_session
->pageTextContains('Translated link page');
$this
->drupalGet('target-page', $source_url_options);
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextContains('Target page');
}
public function providerLanguage() {
return [
[
'en',
],
[
'fr',
],
];
}
}