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',
100 'bypass node access',
102 $this->adminUser = $this->drupalCreateUser($permissions);
103 $this->drupalLogin($this->adminUser);
104 $this->createPattern('node', '/content/[node:title]');
108 * Test if the default values are shown correctly in the form.
110 function testDefaultFormValues() {
111 $this->drupalGet('/admin/config/search/path/settings');
112 $this->assertNoFieldChecked('edit-verbose');
113 $this->assertField('edit-separator', $this->defaultFormValues['separator']);
114 $this->assertFieldChecked('edit-case');
115 $this->assertField('edit-max-length', $this->defaultFormValues['max_length']);
116 $this->assertField('edit-max-component-length', $this->defaultFormValues['max_component_length']);
117 $this->assertFieldChecked('edit-update-action-2');
118 $this->assertFieldChecked('edit-transliterate');
119 $this->assertNoFieldChecked('edit-reduce-ascii');
120 $this->assertField('edit-ignore-words', $this->defaultFormValues['ignore_words']);
124 * Test the verbose option.
126 function testVerboseOption() {
127 $edit = array('verbose' => '1');
128 $this->drupalPostForm('/admin/config/search/path/settings', $edit, t('Save configuration'));
129 $this->assertText(t('The configuration options have been saved.'));
130 $this->assertFieldChecked('edit-verbose');
132 $title = 'Verbose settings test';
133 $this->drupalGet('/node/add/article');
134 $this->assertFieldChecked('edit-path-0-pathauto');
135 $this->drupalPostForm(NULL, array('title[0][value]' => $title), t('Save'));
136 $this->assertText('Created new alias /content/verbose-settings-test for');
138 $node = $this->drupalGetNodeByTitle($title);
139 $this->drupalPostForm('/node/' . $node->id() . '/edit', array('title[0][value]' => 'Updated title'), t('Save'));
140 $this->assertText('Created new alias /content/updated-title for');
141 $this->assertText('replacing /content/verbose-settings-test.');
145 * Tests generating aliases with different settings.
147 function testSettingsForm() {
148 // Ensure the separator settings apply correctly.
149 $this->checkAlias('My awesome content', '/content/my.awesome.content', array('separator' => '.'));
151 // Ensure the character case setting works correctly.
152 // Leave case the same as source token values.
153 $this->checkAlias('My awesome Content', '/content/My-awesome-Content', array('case' => FALSE));
154 $this->checkAlias('Change Lower', '/content/change-lower', array('case' => '1'));
156 // Ensure the maximum alias length is working.
157 $this->checkAlias('My awesome Content', '/content/my-awesome', array('max_length' => '23'));
159 // Ensure the maximum component length is working.
160 $this->checkAlias('My awesome Content', '/content/my', array('max_component_length' => '2'));
162 // Ensure transliteration option is working.
163 $this->checkAlias('è é àl ö äl ü', '/content/e-e-al-o-al-u', array('transliterate' => '1'));
164 $this->checkAlias('è é àl äl ö ü', '/content/è-é-àl-äl-ö-ü', array('transliterate' => FALSE));
166 $ignore_words = 'a, new, very, should';
167 $this->checkAlias('a very new alias to test', '/content/alias-to-test', array('ignore_words' => $ignore_words));
171 * Test the punctuation setting form items.
173 function testPunctuationSettings() {
174 // Test the replacement of punctuations.
176 foreach ($this->defaultPunctuations as $key => $punctuation) {
177 $settings[$key] = PathautoGeneratorInterface::PUNCTUATION_REPLACE;
180 $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';
181 $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';
182 $this->checkAlias($title, $alias, $settings);
184 // Test the removal of punctuations.
186 foreach ($this->defaultPunctuations as $key => $punctuation) {
187 $settings[$key] = PathautoGeneratorInterface::PUNCTUATION_REMOVE;
190 $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';
191 $alias = '/content/abcdefghijklmnopqrstuvwxyz123';
192 $this->checkAlias($title, $alias, $settings);
194 // Keep all punctuations in alias.
196 foreach ($this->defaultPunctuations as $key => $punctuation) {
197 $settings[$key] = PathautoGeneratorInterface::PUNCTUATION_DO_NOTHING;
200 $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';
201 $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';
202 $this->checkAlias($title, $alias, $settings);
206 * Helper method to check the an aliases.
208 * @param string $title
209 * The node title to build the aliases from.
210 * @param string $alias
211 * The expected alias.
212 * @param array $settings
213 * The form values the alias should be generated with.
215 protected function checkAlias($title, $alias, $settings = array()) {
216 // Submit the settings form.
217 $edit = array_merge($this->defaultFormValues + $this->defaultPunctuations, $settings);
218 $this->drupalPostForm('/admin/config/search/path/settings', $edit, t('Save configuration'));
219 $this->assertText(t('The configuration options have been saved.'));
221 // If we do not clear the caches here, AliasCleaner will use its
222 // cleanStringCache instance variable. Due to that the creation of aliases
223 // with $this->createNode() will only work correctly on the first call.
224 \Drupal::service('pathauto.generator')->resetCaches();
226 // Create a node and check if the settings applied.
227 $node = $this->createNode(
234 $this->drupalGet($alias);
235 $this->assertResponse(200);
236 $this->assertEntityAlias($node, $alias);