9a4a690928e9e0f52600bdef2f5a1e3e45f54283
[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       'administer nodes',
101       'bypass node access',
102     );
103     $this->adminUser = $this->drupalCreateUser($permissions);
104     $this->drupalLogin($this->adminUser);
105     $this->createPattern('node', '/content/[node:title]');
106   }
107
108   /**
109    * Test if the default values are shown correctly in the form.
110    */
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']);
122   }
123
124   /**
125    * Test the verbose option.
126    */
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');
132
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');
138
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.');
143   }
144
145   /**
146    * Tests generating aliases with different settings.
147    */
148   function testSettingsForm() {
149     // Ensure the separator settings apply correctly.
150     $this->checkAlias('My awesome content', '/content/my.awesome.content', array('separator' => '.'));
151
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'));
156
157     // Ensure the maximum alias length is working.
158     $this->checkAlias('My awesome Content', '/content/my-awesome', array('max_length' => '23'));
159
160     // Ensure the maximum component length is working.
161     $this->checkAlias('My awesome Content', '/content/my', array('max_component_length' => '2'));
162
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));
166
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));
169   }
170
171   /**
172    * Test the punctuation setting form items.
173    */
174   function testPunctuationSettings() {
175     // Test the replacement of punctuations.
176     $settings = [];
177     foreach ($this->defaultPunctuations as $key => $punctuation) {
178       $settings[$key] = PathautoGeneratorInterface::PUNCTUATION_REPLACE;
179     }
180
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);
184
185     // Test the removal of punctuations.
186     $settings = [];
187     foreach ($this->defaultPunctuations as $key => $punctuation) {
188       $settings[$key] = PathautoGeneratorInterface::PUNCTUATION_REMOVE;
189     }
190
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);
194
195     // Keep all punctuations in alias.
196     $settings = [];
197     foreach ($this->defaultPunctuations as $key => $punctuation) {
198       $settings[$key] = PathautoGeneratorInterface::PUNCTUATION_DO_NOTHING;
199     }
200
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);
204   }
205
206   /**
207    * Helper method to check the an aliases.
208    *
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.
215    */
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.'));
221
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();
226
227     // Create a node and check if the settings applied.
228     $node = $this->createNode(
229       array(
230         'title' => $title,
231         'type' => 'article',
232       )
233     );
234
235     $this->drupalGet($alias);
236     $this->assertResponse(200);
237     $this->assertEntityAlias($node, $alias);
238   }
239
240 }