3 namespace Drupal\pathauto\Tests;
5 use Drupal\pathauto\PathautoGeneratorInterface;
6 use Drupal\simpletest\WebTestBase;
9 * Tests pathauto settings form.
13 class PathautoSettingsFormWebTest extends WebTestBase {
15 use PathautoTestHelperTrait;
22 public static $modules = array('node', 'pathauto');
27 * @var \Drupal\user\UserInterface
32 * Form values that are set by default.
36 protected $defaultFormValues = array(
40 'max_length' => '100',
41 'max_component_length' => '100',
42 'update_action' => '2',
43 'transliterate' => '1',
44 'reduce_ascii' => FALSE,
45 'ignore_words' => 'a, an, as, at, before, but, by, for, from, is, in, into, like, of, off, on, onto, per, since, than, the, this, that, to, up, via, with',
49 * Punctuation form items with default values.
53 protected $defaultPunctuations = array(
54 'punctuation[double_quotes]' => '0',
55 'punctuation[quotes]' => '0',
56 'punctuation[backtick]' => '0',
57 'punctuation[comma]' => '0',
58 'punctuation[period]' => '0',
59 'punctuation[hyphen]' => '1',
60 'punctuation[underscore]' => '0',
61 'punctuation[colon]' => '0',
62 'punctuation[semicolon]' => '0',
63 'punctuation[pipe]' => '0',
64 'punctuation[left_curly]' => '0',
65 'punctuation[left_square]' => '0',
66 'punctuation[right_curly]' => '0',
67 'punctuation[right_square]' => '0',
68 'punctuation[plus]' => '0',
69 'punctuation[equal]' => '0',
70 'punctuation[asterisk]' => '0',
71 'punctuation[ampersand]' => '0',
72 'punctuation[percent]' => '0',
73 'punctuation[caret]' => '0',
74 'punctuation[dollar]' => '0',
75 'punctuation[hash]' => '0',
76 'punctuation[exclamation]' => '0',
77 'punctuation[tilde]' => '0',
78 'punctuation[left_parenthesis]' => '0',
79 'punctuation[right_parenthesis]' => '0',
80 'punctuation[question_mark]' => '0',
81 'punctuation[less_than]' => '0',
82 'punctuation[greater_than]' => '0',
83 'punctuation[slash]' => '0',
84 'punctuation[back_slash]' => '0',
93 $this->drupalCreateContentType(array('type' => 'article'));
96 'administer pathauto',
97 'notify of path changes',
98 'administer url aliases',
101 'bypass node access',
103 $this->adminUser = $this->drupalCreateUser($permissions);
104 $this->drupalLogin($this->adminUser);
105 $this->createPattern('node', '/content/[node:title]');
109 * Test if the default values are shown correctly in the form.
111 function testDefaultFormValues() {
112 $this->drupalGet('/admin/config/search/path/settings');
113 $this->assertNoFieldChecked('edit-verbose');
114 $this->assertField('edit-separator', $this->defaultFormValues['separator']);
115 $this->assertFieldChecked('edit-case');
116 $this->assertField('edit-max-length', $this->defaultFormValues['max_length']);
117 $this->assertField('edit-max-component-length', $this->defaultFormValues['max_component_length']);
118 $this->assertFieldChecked('edit-update-action-2');
119 $this->assertFieldChecked('edit-transliterate');
120 $this->assertNoFieldChecked('edit-reduce-ascii');
121 $this->assertField('edit-ignore-words', $this->defaultFormValues['ignore_words']);
125 * Test the verbose option.
127 function testVerboseOption() {
128 $edit = array('verbose' => '1');
129 $this->drupalPostForm('/admin/config/search/path/settings', $edit, t('Save configuration'));
130 $this->assertText(t('The configuration options have been saved.'));
131 $this->assertFieldChecked('edit-verbose');
133 $title = 'Verbose settings test';
134 $this->drupalGet('/node/add/article');
135 $this->assertFieldChecked('edit-path-0-pathauto');
136 $this->drupalPostForm(NULL, array('title[0][value]' => $title), t('Save and publish'));
137 $this->assertText('Created new alias /content/verbose-settings-test for');
139 $node = $this->drupalGetNodeByTitle($title);
140 $this->drupalPostForm('/node/' . $node->id() . '/edit', array('title[0][value]' => 'Updated title'), t('Save and keep published'));
141 $this->assertText('Created new alias /content/updated-title for');
142 $this->assertText('replacing /content/verbose-settings-test.');
146 * Tests generating aliases with different settings.
148 function testSettingsForm() {
149 // Ensure the separator settings apply correctly.
150 $this->checkAlias('My awesome content', '/content/my.awesome.content', array('separator' => '.'));
152 // Ensure the character case setting works correctly.
153 // Leave case the same as source token values.
154 $this->checkAlias('My awesome Content', '/content/My-awesome-Content', array('case' => FALSE));
155 $this->checkAlias('Change Lower', '/content/change-lower', array('case' => '1'));
157 // Ensure the maximum alias length is working.
158 $this->checkAlias('My awesome Content', '/content/my-awesome', array('max_length' => '23'));
160 // Ensure the maximum component length is working.
161 $this->checkAlias('My awesome Content', '/content/my', array('max_component_length' => '2'));
163 // Ensure transliteration option is working.
164 $this->checkAlias('è é àl ö äl ü', '/content/e-e-al-o-al-u', array('transliterate' => '1'));
165 $this->checkAlias('è é àl äl ö ü', '/content/è-é-àl-äl-ö-ü', array('transliterate' => FALSE));
167 $ignore_words = 'a, new, very, should';
168 $this->checkAlias('a very new alias to test', '/content/alias-to-test', array('ignore_words' => $ignore_words));
172 * Test the punctuation setting form items.
174 function testPunctuationSettings() {
175 // Test the replacement of punctuations.
177 foreach ($this->defaultPunctuations as $key => $punctuation) {
178 $settings[$key] = PathautoGeneratorInterface::PUNCTUATION_REPLACE;
181 $title = 'aa"b`c,d.e-f_g:h;i|j{k[l}m]n+o=p*q%r^s$t#u!v~w(x)y?z>1/2\3';
182 $alias = '/content/aa-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z-1-2-3';
183 $this->checkAlias($title, $alias, $settings);
185 // Test the removal of punctuations.
187 foreach ($this->defaultPunctuations as $key => $punctuation) {
188 $settings[$key] = PathautoGeneratorInterface::PUNCTUATION_REMOVE;
191 $title = 'a"b`c,d.e-f_g:h;i|j{k[l}m]n+o=p*q%r^s$t#u!v~w(x)y?z>1/2\3';
192 $alias = '/content/abcdefghijklmnopqrstuvwxyz123';
193 $this->checkAlias($title, $alias, $settings);
195 // Keep all punctuations in alias.
197 foreach ($this->defaultPunctuations as $key => $punctuation) {
198 $settings[$key] = PathautoGeneratorInterface::PUNCTUATION_DO_NOTHING;
201 $title = 'al"b`c,d.e-f_g:h;i|j{k[l}m]n+o=p*q%r^s$t#u!v~w(x)y?z>1/2\3';
202 $alias = '/content/al"b`c,d.e-f_g:h;i|j{k[l}m]n+o=p*q%r^s$t#u!v~w(x)y?z>1/2\3';
203 $this->checkAlias($title, $alias, $settings);
207 * Helper method to check the an aliases.
209 * @param string $title
210 * The node title to build the aliases from.
211 * @param string $alias
212 * The expected alias.
213 * @param array $settings
214 * The form values the alias should be generated with.
216 protected function checkAlias($title, $alias, $settings = array()) {
217 // Submit the settings form.
218 $edit = array_merge($this->defaultFormValues + $this->defaultPunctuations, $settings);
219 $this->drupalPostForm('/admin/config/search/path/settings', $edit, t('Save configuration'));
220 $this->assertText(t('The configuration options have been saved.'));
222 // If we do not clear the caches here, AliasCleaner will use its
223 // cleanStringCache instance variable. Due to that the creation of aliases
224 // with $this->createNode() will only work correctly on the first call.
225 \Drupal::service('pathauto.generator')->resetCaches();
227 // Create a node and check if the settings applied.
228 $node = $this->createNode(
235 $this->drupalGet($alias);
236 $this->assertResponse(200);
237 $this->assertEntityAlias($node, $alias);