comparison core/modules/locale/tests/src/Functional/LocalePathTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\locale\Functional;
4
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\Core\Url;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10 * Tests you can configure a language for individual URL aliases.
11 *
12 * @group locale
13 */
14 class LocalePathTest extends BrowserTestBase {
15
16 /**
17 * Modules to enable.
18 *
19 * @var array
20 */
21 public static $modules = ['node', 'locale', 'path', 'views'];
22
23 /**
24 * {@inheritdoc}
25 */
26 protected function setUp() {
27 parent::setUp();
28
29 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
30 $this->config('system.site')->set('page.front', '/node')->save();
31 }
32
33 /**
34 * Test if a language can be associated with a path alias.
35 */
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']);
39
40 // Add custom language.
41 $this->drupalLogin($admin_user);
42 // Code for the language.
43 $langcode = 'xx';
44 // The English name for the language.
45 $name = $this->randomMachineName(16);
46 // The domain prefix.
47 $prefix = $langcode;
48 $edit = [
49 'predefined_langcode' => 'custom',
50 'langcode' => $langcode,
51 'label' => $name,
52 'direction' => LanguageInterface::DIRECTION_LTR,
53 ];
54 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
55
56 // Set path prefix.
57 $edit = ["prefix[$langcode]" => $prefix];
58 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
59
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.');
64
65 // Create a node.
66 $node = $this->drupalCreateNode(['type' => 'page']);
67
68 // Create a path alias in default language (English).
69 $path = 'admin/config/search/path/add';
70 $english_path = $this->randomMachineName(8);
71 $edit = [
72 'source' => '/node/' . $node->id(),
73 'alias' => '/' . $english_path,
74 'langcode' => 'en',
75 ];
76 $this->drupalPostForm($path, $edit, t('Save'));
77
78 // Create a path alias in new custom language.
79 $custom_language_path = $this->randomMachineName(8);
80 $edit = [
81 'source' => '/node/' . $node->id(),
82 'alias' => '/' . $custom_language_path,
83 'langcode' => $langcode,
84 ];
85 $this->drupalPostForm($path, $edit, t('Save'));
86
87 // Confirm English language path alias works.
88 $this->drupalGet($english_path);
89 $this->assertText($node->label(), 'English alias works.');
90
91 // Confirm custom language path alias works.
92 $this->drupalGet($prefix . '/' . $custom_language_path);
93 $this->assertText($node->label(), 'Custom language alias works.');
94
95 // Create a custom path.
96 $custom_path = $this->randomMachineName(8);
97
98 // Check priority of language for alias by source path.
99 $edit = [
100 'source' => '/node/' . $node->id(),
101 'alias' => '/' . $custom_path,
102 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
103 ];
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);
111
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]);
115
116 // Assign a custom path alias to the first node with the English language.
117 $edit = [
118 'source' => '/node/' . $first_node->id(),
119 'alias' => '/' . $custom_path,
120 'langcode' => $first_node->language()->getId(),
121 ];
122 $this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']);
123
124 // Assign a custom path alias to second node with
125 // LanguageInterface::LANGCODE_NOT_SPECIFIED.
126 $edit = [
127 'source' => '/node/' . $second_node->id(),
128 'alias' => '/' . $custom_path,
129 'langcode' => $second_node->language()->getId(),
130 ];
131 $this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']);
132
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.');
140
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.');
144
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.');
148
149 }
150
151 }