comparison core/tests/Drupal/FunctionalTests/Routing/RouteCachingLanguageTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\FunctionalTests\Routing;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\link\LinkItemInterface;
9 use Drupal\Tests\BrowserTestBase;
10
11 /**
12 * Tests that route lookup is cached by the current language.
13 *
14 * @group routing
15 */
16 class RouteCachingLanguageTest extends BrowserTestBase {
17
18 /**
19 * Modules to enable.
20 *
21 * @var array
22 */
23 public static $modules = ['path', 'node', 'content_translation', 'link', 'block'];
24
25 /**
26 * An user with permissions to administer content types.
27 *
28 * @var \Drupal\user\UserInterface
29 */
30 protected $webUser;
31
32 protected function setUp() {
33 parent::setUp();
34
35 $this->createContentType(['type' => 'page']);
36
37 $this->drupalPlaceBlock('local_tasks_block');
38 $this->drupalPlaceBlock('page_title_block');
39
40 $permissions = [
41 'access administration pages',
42 'administer content translation',
43 'administer content types',
44 'administer languages',
45 'administer url aliases',
46 'create content translations',
47 'create page content',
48 'create url aliases',
49 'edit any page content',
50 'translate any entity',
51 ];
52 // Create and log in user.
53 $this->webUser = $this->drupalCreateUser($permissions);
54 $this->drupalLogin($this->webUser);
55
56 // Enable French language.
57 ConfigurableLanguage::createFromLangcode('fr')->save();
58
59 // Enable translation for page node.
60 $edit = [
61 'entity_types[node]' => 1,
62 'settings[node][page][translatable]' => 1,
63 'settings[node][page][fields][path]' => 1,
64 'settings[node][page][fields][body]' => 1,
65 'settings[node][page][settings][language][language_alterable]' => 1,
66 ];
67 $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
68
69 // Create a field with settings to validate.
70 $field_storage = FieldStorageConfig::create([
71 'field_name' => 'field_link',
72 'entity_type' => 'node',
73 'type' => 'link',
74 ]);
75 $field_storage->save();
76 $field = FieldConfig::create([
77 'field_storage' => $field_storage,
78 'bundle' => 'page',
79 'settings' => [
80 'title' => DRUPAL_OPTIONAL,
81 'link_type' => LinkItemInterface::LINK_GENERIC,
82 ],
83 ]);
84 $field->save();
85
86 entity_get_form_display('node', 'page', 'default')
87 ->setComponent('field_link', [
88 'type' => 'link_default',
89 ])
90 ->save();
91 entity_get_display('node', 'page', 'full')
92 ->setComponent('field_link', [
93 'type' => 'link',
94 ])
95 ->save();
96
97 // Enable URL language detection and selection and set a prefix for both
98 // languages.
99 $edit = ['language_interface[enabled][language-url]' => 1];
100 $this->drupalPostForm('admin/config/regional/language/detection', $edit, 'Save settings');
101 $edit = ['prefix[en]' => 'en'];
102 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, 'Save configuration');
103
104 // Reset the cache after changing the negotiation settings as that changes
105 // how links are built.
106 $this->resetAll();
107
108 $definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'page');
109 $this->assertTrue($definitions['path']->isTranslatable(), 'Node path is translatable.');
110 $this->assertTrue($definitions['body']->isTranslatable(), 'Node body is translatable.');
111 }
112
113 /**
114 * Creates content with a link field pointing to an alias of another language.
115 *
116 * @dataProvider providerLanguage
117 */
118 public function testLinkTranslationWithAlias($source_langcode) {
119 $source_url_options = [
120 'language' => ConfigurableLanguage::load($source_langcode),
121 ];
122
123 // Create a target node in the source language that is the link target.
124 $edit = [
125 'langcode[0][value]' => $source_langcode,
126 'title[0][value]' => 'Target page',
127 'path[0][alias]' => '/target-page',
128 ];
129 $this->drupalPostForm('node/add/page', $edit, t('Save'), $source_url_options);
130
131 // Confirm that the alias works.
132 $assert_session = $this->assertSession();
133 $assert_session->addressEquals($source_langcode . '/target-page');
134 $assert_session->statusCodeEquals(200);
135 $assert_session->pageTextContains('Target page');
136
137 // Create a second node that links to the first through the link field.
138 $edit = [
139 'langcode[0][value]' => $source_langcode,
140 'title[0][value]' => 'Link page',
141 'field_link[0][uri]' => '/target-page',
142 'field_link[0][title]' => 'Target page',
143 'path[0][alias]' => '/link-page',
144 ];
145 $this->drupalPostForm('node/add/page', $edit, t('Save'), $source_url_options);
146
147 // Make sure the link node is displayed with a working link.
148 $assert_session->pageTextContains('Link page');
149 $this->clickLink('Target page');
150 $assert_session->addressEquals($source_langcode . '/target-page');
151 $assert_session->statusCodeEquals(200);
152 $assert_session->pageTextContains('Target page');
153
154 // Clear all caches, then add a translation for the link node.
155 $this->resetAll();
156
157 $this->drupalGet('link-page', $source_url_options);
158 $this->clickLink('Translate');
159 $this->clickLink(t('Add'));
160
161 // Do not change the link field.
162 $edit = [
163 'title[0][value]' => 'Translated link page',
164 'path[0][alias]' => '/translated-link-page',
165 ];
166 $this->drupalPostForm(NULL, $edit, 'Save (this translation)');
167
168 $assert_session->pageTextContains('Translated link page');
169
170 // @todo Clicking on the link does not include the language prefix.
171 $this->drupalGet('target-page', $source_url_options);
172 $assert_session->statusCodeEquals(200);
173 $assert_session->pageTextContains('Target page');
174 }
175
176 /**
177 * Data provider for testFromUri().
178 */
179 public function providerLanguage() {
180 return [
181 ['en'],
182 ['fr'],
183 ];
184 }
185
186 }