dd271ef59b216c8c958a6040c860601fe890824b
[yaffs-website] / web / modules / contrib / pathauto / src / Tests / PathautoLocaleTest.php
1 <?php
2
3 namespace Drupal\pathauto\Tests;
4
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;
10
11 /**
12  * Test pathauto functionality with localization and translation.
13  *
14  * @group pathauto
15  */
16 class PathautoLocaleTest extends WebTestBase {
17
18   use PathautoTestHelperTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = array('node', 'pathauto', 'locale', 'content_translation');
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32
33     // Create Article node type.
34     $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
35   }
36
37   /**
38    * Test that when an English node is updated, its old English alias is
39    * updated and its newer French alias is left intact.
40    */
41   function testLanguageAliases() {
42
43     $this->createPattern('node', '/content/[node:title]');
44
45     // Add predefined French language.
46     ConfigurableLanguage::createFromLangcode('fr')->save();
47
48     $node = array(
49       'title' => 'English node',
50       'langcode' => 'en',
51       'path' => array(array(
52         'alias' => '/english-node',
53         'pathauto' => FALSE,
54       )),
55     );
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.');
59
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');
63
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);
67
68     // Update the node, triggering a change in the English alias.
69     $node->path->pathauto = PathautoState::CREATE;
70     $node->save();
71
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'));
76
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));
80
81     // Check that the new node had a unique alias generated with the '-0'
82     // suffix.
83     $this->assertEntityAlias($node, '/content/english-node-0', LanguageInterface::LANGCODE_NOT_SPECIFIED);
84   }
85
86   /**
87    * Test that patterns work on multilingual content.
88    */
89   function testLanguagePatterns() {
90     $this->drupalLogin($this->rootUser);
91
92     // Add French language.
93     $edit = array(
94       'predefined_langcode' => 'fr',
95     );
96     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
97
98     $this->enableArticleTranslation();
99
100     // Create a pattern for English articles.
101     $this->drupalGet('admin/config/search/path/patterns/add');
102     $edit = array(
103       'type' => 'canonical_entities:node',
104     );
105     $this->drupalPostAjaxForm(NULL, $edit, 'type');
106     $edit += array(
107       'pattern' => '/the-articles/[node:title]',
108       'label' => 'English articles',
109       'id' => 'english_articles',
110       'bundles[article]' => TRUE,
111       'languages[en]' => TRUE,
112     );
113     $this->drupalPostForm(NULL, $edit, 'Save');
114     $this->assertText('Pattern English articles saved.');
115
116     // Create a pattern for French articles.
117     $this->drupalGet('admin/config/search/path/patterns/add');
118     $edit = array(
119       'type' => 'canonical_entities:node',
120     );
121     $this->drupalPostAjaxForm(NULL, $edit, 'type');
122     $edit += array(
123       'pattern' => '/les-articles/[node:title]',
124       'label' => 'French articles',
125       'id' => 'french_articles',
126       'bundles[article]' => TRUE,
127       'languages[fr]' => TRUE,
128     );
129     $this->drupalPostForm(NULL, $edit, 'Save');
130     $this->assertText('Pattern French articles saved.');
131
132     // Create a node and its translation. Assert aliases.
133     $edit = array(
134       'title[0][value]' => 'English node',
135       'langcode[0][value]' => 'en',
136     );
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');
140
141     $this->drupalGet('node/' . $english_node->id() . '/translations');
142     $this->clickLink(t('Add'));
143     $edit = array(
144       'title[0][value]' => 'French node',
145     );
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');
151
152     // Bulk delete and Bulk generate patterns. Assert aliases.
153     $this->deleteAllAliases();
154     // Bulk create aliases.
155     $edit = array(
156       'update[canonical_entities:node]' => TRUE,
157     );
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');
162   }
163
164   /**
165    * Tests the alias created for a node with language Not Applicable.
166    */
167   public function testLanguageNotApplicable() {
168     $this->drupalLogin($this->rootUser);
169     $this->enableArticleTranslation();
170
171     // Create a pattern for nodes.
172     $pattern = $this->createPattern('node', '/content/[node:title]', -1);
173     $pattern->save();
174
175     // Create a node with language Not Applicable.
176     $node = $this->createNode(['type' => 'article', 'title' => 'Test node', 'langcode' => LanguageInterface::LANGCODE_NOT_APPLICABLE]);
177
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.');
181
182     // Check that the alias works.
183     $this->drupalGet('content/test-node');
184     $this->assertResponse(200);
185   }
186
187   /**
188    * Enables content translation on articles.
189    */
190   protected function enableArticleTranslation() {
191     // Enable content translation on articles.
192     $this->drupalGet('admin/config/regional/content-language');
193     $edit = array(
194       'entity_types[node]' => TRUE,
195       'settings[node][article][translatable]' => TRUE,
196       'settings[node][article][settings][language][language_alterable]' => TRUE,
197     );
198     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
199   }
200
201 }