3 namespace Drupal\pathauto\Tests;
5 use Drupal\Core\Language\Language;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\pathauto\PathautoState;
9 use Drupal\simpletest\WebTestBase;
12 * Test pathauto functionality with localization and translation.
16 class PathautoLocaleTest extends WebTestBase {
18 use PathautoTestHelperTrait;
25 public static $modules = array('node', 'pathauto', 'locale', 'content_translation');
30 protected function setUp() {
33 // Create Article node type.
34 $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
38 * Test that when an English node is updated, its old English alias is
39 * updated and its newer French alias is left intact.
41 function testLanguageAliases() {
43 $this->createPattern('node', '/content/[node:title]');
45 // Add predefined French language.
46 ConfigurableLanguage::createFromLangcode('fr')->save();
49 'title' => 'English node',
51 'path' => array(array(
52 'alias' => '/english-node',
56 $node = $this->drupalCreateNode($node);
57 $english_alias = \Drupal::service('path.alias_storage')->load(array('alias' => '/english-node', 'langcode' => 'en'));
58 $this->assertTrue($english_alias, 'Alias created with proper language.');
60 // Also save a French alias that should not be left alone, even though
61 // it is the newer alias.
62 $this->saveEntityAlias($node, '/french-node', 'fr');
64 // Add an alias with the soon-to-be generated alias, causing the upcoming
65 // alias update to generate a unique alias with the '-0' suffix.
66 $this->saveAlias('/node/invalid', '/content/english-node', Language::LANGCODE_NOT_SPECIFIED);
68 // Update the node, triggering a change in the English alias.
69 $node->path->pathauto = PathautoState::CREATE;
72 // Check that the new English alias replaced the old one.
73 $this->assertEntityAlias($node, '/content/english-node-0', 'en');
74 $this->assertEntityAlias($node, '/french-node', 'fr');
75 $this->assertAliasExists(array('pid' => $english_alias['pid'], 'alias' => '/content/english-node-0'));
77 // Create a new node with the same title as before but without
78 // specifying a language.
79 $node = $this->drupalCreateNode(array('title' => 'English node', 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
81 // Check that the new node had a unique alias generated with the '-0'
83 $this->assertEntityAlias($node, '/content/english-node-0', LanguageInterface::LANGCODE_NOT_SPECIFIED);
87 * Test that patterns work on multilingual content.
89 function testLanguagePatterns() {
90 $this->drupalLogin($this->rootUser);
92 // Add French language.
94 'predefined_langcode' => 'fr',
96 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
98 $this->enableArticleTranslation();
100 // Create a pattern for English articles.
101 $this->drupalGet('admin/config/search/path/patterns/add');
103 'type' => 'canonical_entities:node',
105 $this->drupalPostAjaxForm(NULL, $edit, 'type');
107 'pattern' => '/the-articles/[node:title]',
108 'label' => 'English articles',
109 'id' => 'english_articles',
110 'bundles[article]' => TRUE,
111 'languages[en]' => TRUE,
113 $this->drupalPostForm(NULL, $edit, 'Save');
114 $this->assertText('Pattern English articles saved.');
116 // Create a pattern for French articles.
117 $this->drupalGet('admin/config/search/path/patterns/add');
119 'type' => 'canonical_entities:node',
121 $this->drupalPostAjaxForm(NULL, $edit, 'type');
123 'pattern' => '/les-articles/[node:title]',
124 'label' => 'French articles',
125 'id' => 'french_articles',
126 'bundles[article]' => TRUE,
127 'languages[fr]' => TRUE,
129 $this->drupalPostForm(NULL, $edit, 'Save');
130 $this->assertText('Pattern French articles saved.');
132 // Create a node and its translation. Assert aliases.
134 'title[0][value]' => 'English node',
135 'langcode[0][value]' => 'en',
137 $this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
138 $english_node = $this->drupalGetNodeByTitle('English node');
139 $this->assertAlias('/node/' . $english_node->id(), '/the-articles/english-node', 'en');
141 $this->drupalGet('node/' . $english_node->id() . '/translations');
142 $this->clickLink(t('Add'));
144 'title[0][value]' => 'French node',
146 $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
147 $this->rebuildContainer();
148 $english_node = $this->drupalGetNodeByTitle('English node');
149 $french_node = $english_node->getTranslation('fr');
150 $this->assertAlias('/node/' . $french_node->id(), '/les-articles/french-node', 'fr');
152 // Bulk delete and Bulk generate patterns. Assert aliases.
153 $this->deleteAllAliases();
154 // Bulk create aliases.
156 'update[canonical_entities:node]' => TRUE,
158 $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
159 $this->assertText(t('Generated 2 URL aliases.'));
160 $this->assertAlias('/node/' . $english_node->id(), '/the-articles/english-node', 'en');
161 $this->assertAlias('/node/' . $french_node->id(), '/les-articles/french-node', 'fr');
165 * Tests the alias created for a node with language Not Applicable.
167 public function testLanguageNotApplicable() {
168 $this->drupalLogin($this->rootUser);
169 $this->enableArticleTranslation();
171 // Create a pattern for nodes.
172 $pattern = $this->createPattern('node', '/content/[node:title]', -1);
175 // Create a node with language Not Applicable.
176 $node = $this->createNode(['type' => 'article', 'title' => 'Test node', 'langcode' => LanguageInterface::LANGCODE_NOT_APPLICABLE]);
178 // Check that the generated alias has language Not Specified.
179 $alias = \Drupal::service('pathauto.alias_storage_helper')->loadBySource('/node/' . $node->id());
180 $this->assertEqual($alias['langcode'], LanguageInterface::LANGCODE_NOT_SPECIFIED, 'PathautoGenerator::createEntityAlias() adjusts the alias langcode from Not Applicable to Not Specified.');
182 // Check that the alias works.
183 $this->drupalGet('content/test-node');
184 $this->assertResponse(200);
188 * Enables content translation on articles.
190 protected function enableArticleTranslation() {
191 // Enable content translation on articles.
192 $this->drupalGet('admin/config/regional/content-language');
194 'entity_types[node]' => TRUE,
195 'settings[node][article][translatable]' => TRUE,
196 'settings[node][article][settings][language][language_alterable]' => TRUE,
198 $this->drupalPostForm(NULL, $edit, t('Save configuration'));