Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / pathauto / src / Tests / PathautoSettingsFormWebTest.php
1 <?php
2
3 namespace Drupal\pathauto\Tests;
4
5 use Drupal\pathauto\PathautoGeneratorInterface;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests pathauto settings form.
10  *
11  * @group pathauto
12  */
13 class PathautoSettingsFormWebTest extends WebTestBase {
14
15   use PathautoTestHelperTrait;
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = array('node', 'pathauto');
23
24   /**
25    * Admin user.
26    *
27    * @var \Drupal\user\UserInterface
28    */
29   protected $adminUser;
30
31   /**
32    * Form values that are set by default.
33    *
34    * @var array
35    */
36   protected $defaultFormValues = array(
37     'verbose' => FALSE,
38     'separator' => '-',
39     'case' => '1',
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',
46   );
47
48   /**
49    * Punctuation form items with default values.
50    *
51    * @var array
52    */
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',
85   );
86
87   /**
88    * {@inheritdoc}
89    */
90   function setUp() {
91     parent::setUp();
92
93     $this->drupalCreateContentType(array('type' => 'article'));
94
95     $permissions = array(
96       'administer pathauto',
97       'notify of path changes',
98       'administer url aliases',
99       'create url aliases',
100       'bypass node access',
101     );
102     $this->adminUser = $this->drupalCreateUser($permissions);
103     $this->drupalLogin($this->adminUser);
104     $this->createPattern('node', '/content/[node:title]');
105   }
106
107   /**
108    * Test if the default values are shown correctly in the form.
109    */
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']);
121   }
122
123   /**
124    * Test the verbose option.
125    */
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');
131
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');
137
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.');
142   }
143
144   /**
145    * Tests generating aliases with different settings.
146    */
147   function testSettingsForm() {
148     // Ensure the separator settings apply correctly.
149     $this->checkAlias('My awesome content', '/content/my.awesome.content', array('separator' => '.'));
150
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'));
155
156     // Ensure the maximum alias length is working.
157     $this->checkAlias('My awesome Content', '/content/my-awesome', array('max_length' => '23'));
158
159     // Ensure the maximum component length is working.
160     $this->checkAlias('My awesome Content', '/content/my', array('max_component_length' => '2'));
161
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));
165
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));
168   }
169
170   /**
171    * Test the punctuation setting form items.
172    */
173   function testPunctuationSettings() {
174     // Test the replacement of punctuations.
175     $settings = [];
176     foreach ($this->defaultPunctuations as $key => $punctuation) {
177       $settings[$key] = PathautoGeneratorInterface::PUNCTUATION_REPLACE;
178     }
179
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);
183
184     // Test the removal of punctuations.
185     $settings = [];
186     foreach ($this->defaultPunctuations as $key => $punctuation) {
187       $settings[$key] = PathautoGeneratorInterface::PUNCTUATION_REMOVE;
188     }
189
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);
193
194     // Keep all punctuations in alias.
195     $settings = [];
196     foreach ($this->defaultPunctuations as $key => $punctuation) {
197       $settings[$key] = PathautoGeneratorInterface::PUNCTUATION_DO_NOTHING;
198     }
199
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);
203   }
204
205   /**
206    * Helper method to check the an aliases.
207    *
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.
214    */
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.'));
220
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();
225
226     // Create a node and check if the settings applied.
227     $node = $this->createNode(
228       array(
229         'title' => $title,
230         'type' => 'article',
231       )
232     );
233
234     $this->drupalGet($alias);
235     $this->assertResponse(200);
236     $this->assertEntityAlias($node, $alias);
237   }
238
239 }