View source
<?php
namespace Drupal\Tests\path\Functional;
class PathLanguageTest extends PathTestBase {
protected static $modules = [
'path',
'locale',
'locale_test',
'content_translation',
];
protected $defaultTheme = 'stark';
protected $webUser;
protected function setUp() : void {
parent::setUp();
$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);
$edit = [];
$edit['predefined_langcode'] = 'fr';
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$edit = [
'language_interface[enabled][language-url]' => 1,
];
$this
->drupalGet('admin/config/regional/language/detection');
$this
->submitForm($edit, 'Save settings');
$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');
$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 testAliasTranslation() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$english_node = $this
->drupalCreateNode([
'type' => 'page',
'langcode' => 'en',
]);
$english_alias = $this
->randomMachineName();
$edit = [];
$edit['path[0][alias]'] = '/' . $english_alias;
$this
->drupalGet('node/' . $english_node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->drupalGet($english_alias);
$this
->assertSession()
->pageTextContains($english_node->body->value);
$this
->drupalGet('node/' . $english_node
->id() . '/translations');
$this
->clickLink('Add');
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName();
$edit['body[0][value]'] = $this
->randomMachineName();
$french_alias = $this
->randomMachineName();
$edit['path[0][alias]'] = '/' . $french_alias;
$this
->submitForm($edit, 'Save (this translation)');
$this->container
->get('path_alias.manager')
->cacheClear();
$this->container
->get('language_manager')
->reset();
$this
->rebuildContainer();
$languages = $this->container
->get('language_manager')
->getLanguages();
$node_storage
->resetCache([
$english_node
->id(),
]);
$english_node = $node_storage
->load($english_node
->id());
$english_node_french_translation = $english_node
->getTranslation('fr');
$this
->assertTrue($english_node
->hasTranslation('fr'), 'Node found in database.');
$this
->drupalGet('fr' . $edit['path[0][alias]']);
$this
->assertSession()
->pageTextContains($english_node_french_translation->body->value);
$this->container
->get('language_manager')
->reset();
$languages = $this->container
->get('language_manager')
->getLanguages();
$url = $english_node_french_translation
->toUrl('canonical', [
'language' => $languages['fr'],
])
->toString();
$this
->assertStringContainsString($edit['path[0][alias]'], $url, 'URL contains the path alias.');
$edit = [
'language_interface[enabled][language-user]' => 1,
'language_interface[weight][language-user]' => -9,
'language_interface[enabled][language-url]' => 1,
'language_interface[weight][language-url]' => -8,
];
$this
->drupalGet('admin/config/regional/language/detection');
$this
->submitForm($edit, 'Save settings');
$edit = [
'preferred_langcode' => 'fr',
];
$this
->drupalGet("user/" . $this->webUser
->id() . "/edit");
$this
->submitForm($edit, 'Save');
$this
->drupalGet($english_alias);
$this
->assertSession()
->pageTextContains($english_node_french_translation->body->value);
$this
->drupalGet("fr/{$french_alias}");
$this
->assertSession()
->pageTextContains($english_node_french_translation->body->value);
$edit = [
'language_interface[enabled][language-url]' => FALSE,
];
$this
->drupalGet('admin/config/regional/language/detection');
$this
->submitForm($edit, 'Save settings');
$this
->drupalGet($english_alias);
$this
->assertSession()
->pageTextContains($english_node_french_translation->body->value);
$this
->drupalGet($french_alias);
$this
->assertSession()
->statusCodeEquals(404);
$this->container
->get('path_alias.manager')
->cacheClear();
$french_node_path = $this->container
->get('path_alias.manager')
->getPathByAlias('/' . $french_alias, 'fr');
$this
->assertEquals('/node/' . $english_node_french_translation
->id(), $french_node_path, 'Normal path works.');
$french_node_path = $this->container
->get('path_alias.manager')
->getPathByAlias('/' . $french_alias, 'fr');
$this
->assertEquals('/node/' . $english_node_french_translation
->id(), $french_node_path, 'Normal path is the same.');
$french_node_alias = $this->container
->get('path_alias.manager')
->getAliasByPath('/node/' . $english_node_french_translation
->id(), 'fr');
$this
->assertEquals('/' . $french_alias, $french_node_alias, 'Alias works.');
$french_node_alias = $this->container
->get('path_alias.manager')
->getAliasByPath('/node/' . $english_node_french_translation
->id(), 'fr');
$this
->assertEquals('/' . $french_alias, $french_node_alias, 'Alias is the same.');
$english_node
->removeTranslation('fr');
$english_node
->save();
$this
->assertPathAliasNotExists('/' . $french_alias, 'fr', NULL, 'Alias for French translation is removed when translation is deleted.');
$this
->drupalGet($english_alias);
$this
->assertPathAliasExists('/' . $english_alias, 'en', NULL, 'English alias is not deleted when French translation is removed.');
$this
->assertSession()
->pageTextContains($english_node->body->value);
}
}