3 namespace Drupal\Tests\locale\Functional;
5 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests you can configure a language for individual URL aliases.
14 class LocalePathTest extends BrowserTestBase {
21 public static $modules = ['node', 'locale', 'path', 'views'];
26 protected function setUp() {
29 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
30 $this->config('system.site')->set('page.front', '/node')->save();
34 * Test if a language can be associated with a path alias.
36 public function testPathLanguageConfiguration() {
37 // User to add and remove language.
38 $admin_user = $this->drupalCreateUser(['administer languages', 'create page content', 'administer url aliases', 'create url aliases', 'access administration pages', 'access content overview']);
40 // Add custom language.
41 $this->drupalLogin($admin_user);
42 // Code for the language.
44 // The English name for the language.
45 $name = $this->randomMachineName(16);
49 'predefined_langcode' => 'custom',
50 'langcode' => $langcode,
52 'direction' => LanguageInterface::DIRECTION_LTR,
54 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
57 $edit = ["prefix[$langcode]" => $prefix];
58 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
60 // Check that the "xx" front page is readily available because path prefix
61 // negotiation is pre-configured.
62 $this->drupalGet($prefix);
63 $this->assertText(t('Welcome to Drupal'), 'The "xx" front page is readibly available.');
66 $node = $this->drupalCreateNode(['type' => 'page']);
68 // Create a path alias in default language (English).
69 $path = 'admin/config/search/path/add';
70 $english_path = $this->randomMachineName(8);
72 'source' => '/node/' . $node->id(),
73 'alias' => '/' . $english_path,
76 $this->drupalPostForm($path, $edit, t('Save'));
78 // Create a path alias in new custom language.
79 $custom_language_path = $this->randomMachineName(8);
81 'source' => '/node/' . $node->id(),
82 'alias' => '/' . $custom_language_path,
83 'langcode' => $langcode,
85 $this->drupalPostForm($path, $edit, t('Save'));
87 // Confirm English language path alias works.
88 $this->drupalGet($english_path);
89 $this->assertText($node->label(), 'English alias works.');
91 // Confirm custom language path alias works.
92 $this->drupalGet($prefix . '/' . $custom_language_path);
93 $this->assertText($node->label(), 'Custom language alias works.');
95 // Create a custom path.
96 $custom_path = $this->randomMachineName(8);
98 // Check priority of language for alias by source path.
100 'source' => '/node/' . $node->id(),
101 'alias' => '/' . $custom_path,
102 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
104 $this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']);
105 $lookup_path = $this->container->get('path.alias_manager')->getAliasByPath('/node/' . $node->id(), 'en');
106 $this->assertEqual('/' . $english_path, $lookup_path, 'English language alias has priority.');
107 // Same check for language 'xx'.
108 $lookup_path = $this->container->get('path.alias_manager')->getAliasByPath('/node/' . $node->id(), $prefix);
109 $this->assertEqual('/' . $custom_language_path, $lookup_path, 'Custom language alias has priority.');
110 $this->container->get('path.alias_storage')->delete($edit);
112 // Create language nodes to check priority of aliases.
113 $first_node = $this->drupalCreateNode(['type' => 'page', 'promote' => 1, 'langcode' => 'en']);
114 $second_node = $this->drupalCreateNode(['type' => 'page', 'promote' => 1, 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]);
116 // Assign a custom path alias to the first node with the English language.
118 'source' => '/node/' . $first_node->id(),
119 'alias' => '/' . $custom_path,
120 'langcode' => $first_node->language()->getId(),
122 $this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']);
124 // Assign a custom path alias to second node with
125 // LanguageInterface::LANGCODE_NOT_SPECIFIED.
127 'source' => '/node/' . $second_node->id(),
128 'alias' => '/' . $custom_path,
129 'langcode' => $second_node->language()->getId(),
131 $this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']);
133 // Test that both node titles link to our path alias.
134 $this->drupalGet('admin/content');
135 $custom_path_url = Url::fromUserInput('/' . $custom_path)->toString();
136 $elements = $this->xpath('//a[@href=:href and normalize-space(text())=:title]', [':href' => $custom_path_url, ':title' => $first_node->label()]);
137 $this->assertTrue(!empty($elements), 'First node links to the path alias.');
138 $elements = $this->xpath('//a[@href=:href and normalize-space(text())=:title]', [':href' => $custom_path_url, ':title' => $second_node->label()]);
139 $this->assertTrue(!empty($elements), 'Second node links to the path alias.');
141 // Confirm that the custom path leads to the first node.
142 $this->drupalGet($custom_path);
143 $this->assertText($first_node->label(), 'Custom alias returns first node.');
145 // Confirm that the custom path with prefix leads to the second node.
146 $this->drupalGet($prefix . '/' . $custom_path);
147 $this->assertText($second_node->label(), 'Custom alias with prefix returns second node.');