1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Functionality tests for Pathauto. |
---|
6 | * |
---|
7 | * @ingroup pathauto |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Helper test class with some added functions for testing. |
---|
12 | */ |
---|
13 | class PathautoTestHelper extends DrupalWebTestCase { |
---|
14 | function setUp(array $modules = array()) { |
---|
15 | $modules[] = 'path'; |
---|
16 | $modules[] = 'token'; |
---|
17 | $modules[] = 'pathauto'; |
---|
18 | $modules[] = 'taxonomy'; |
---|
19 | parent::setUp($modules); |
---|
20 | |
---|
21 | // Clear static caches. |
---|
22 | pathauto_static_reset(); |
---|
23 | } |
---|
24 | |
---|
25 | function assertToken($type, $object, $token, $expected) { |
---|
26 | $this->assertTokens($type, $object, array($token => $expected)); |
---|
27 | } |
---|
28 | |
---|
29 | function assertTokens($type, $object, array $tokens) { |
---|
30 | $values = pathauto_get_placeholders($type, $object); |
---|
31 | $values = $values['values']; |
---|
32 | foreach ($tokens as $token => $expected) { |
---|
33 | $this->assertIdentical($values[$token], $expected, t("Token value for [@token] was '@actual', expected value '@expected'.", array('@token' => $token, '@actual' => $values[$token], '@expected' => $expected))); |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | function saveAlias($source, $alias, $language = '') { |
---|
38 | path_set_alias($source, $alias, NULL, $language); |
---|
39 | return db_fetch_array(db_query_range("SELECT * FROM {url_alias} WHERE src = '%s' AND dst = '%s' AND language = '%s' ORDER BY pid DESC", $source, $alias, $language, 0, 1)); |
---|
40 | } |
---|
41 | |
---|
42 | function saveEntityAlias($entity_type, $entity, $alias, $language = '') { |
---|
43 | $uri = $this->entity_uri($entity_type, $entity); |
---|
44 | return $this->saveAlias($uri['path'], $alias, $language); |
---|
45 | } |
---|
46 | |
---|
47 | function assertEntityAlias($entity_type, $entity, $expected_alias, $language = '') { |
---|
48 | $uri = $this->entity_uri($entity_type, $entity); |
---|
49 | $this->assertAlias($uri['path'], $expected_alias, $language); |
---|
50 | } |
---|
51 | |
---|
52 | function assertEntityAliasExists($entity_type, $entity) { |
---|
53 | $uri = $this->entity_uri($entity_type, $entity); |
---|
54 | return $this->assertAliasExists(array('source' => $uri['path'])); |
---|
55 | } |
---|
56 | |
---|
57 | function assertNoEntityAlias($entity_type, $entity, $language = '') { |
---|
58 | $uri = $this->entity_uri($entity_type, $entity); |
---|
59 | $this->assertEntityAlias($entity_type, $entity, $uri['path'], $language); |
---|
60 | } |
---|
61 | |
---|
62 | function assertNoEntityAliasExists($entity_type, $entity) { |
---|
63 | $uri = $this->entity_uri($entity_type, $entity); |
---|
64 | $this->assertNoAliasExists(array('source' => $uri['path'])); |
---|
65 | } |
---|
66 | |
---|
67 | function assertAlias($source, $expected_alias, $language = '') { |
---|
68 | drupal_clear_path_cache(); |
---|
69 | $alias = drupal_get_path_alias($source, $language); |
---|
70 | $this->assertIdentical($alias, $expected_alias, t("Alias for %source with language '@language' was %actual, expected %expected.", array('%source' => $source, '%actual' => $alias, '%expected' => $expected_alias, '@language' => $language))); |
---|
71 | } |
---|
72 | |
---|
73 | function assertAliasExists($conditions) { |
---|
74 | $path = $this->path_load($conditions); |
---|
75 | $this->assertTrue($path, t('Alias with conditions @conditions found.', array('@conditions' => var_export($conditions, TRUE)))); |
---|
76 | return $path; |
---|
77 | } |
---|
78 | |
---|
79 | function assertNoAliasExists($conditions) { |
---|
80 | $alias = $this->path_load($conditions); |
---|
81 | $this->assertFalse($alias, t('Alias with conditions @conditions not found.', array('@conditions' => var_export($conditions, TRUE)))); |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Backport of Drupal 7's entity_uri() function. |
---|
86 | */ |
---|
87 | protected function entity_uri($entity_type, $entity) { |
---|
88 | $uri = array(); |
---|
89 | |
---|
90 | switch ($entity_type) { |
---|
91 | case 'node': |
---|
92 | $uri['path'] = 'node/' . $entity->nid; |
---|
93 | break; |
---|
94 | case 'taxonomy_term': |
---|
95 | $uri['path'] = taxonomy_term_path($entity); |
---|
96 | break; |
---|
97 | case 'user': |
---|
98 | $uri['path'] = 'user/' . $entity->uid; |
---|
99 | break; |
---|
100 | default: |
---|
101 | return $this->fail(t('Unknown entity @type.', array('@type' => $entity_type))); |
---|
102 | } |
---|
103 | |
---|
104 | return $uri; |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Backport of Drupal 7's path_load() function. |
---|
109 | */ |
---|
110 | protected function path_load($conditions) { |
---|
111 | if (is_numeric($conditions)) { |
---|
112 | $conditions = array('pid' => $conditions); |
---|
113 | } |
---|
114 | elseif (is_string($conditions)) { |
---|
115 | $conditions = array('src' => $conditions); |
---|
116 | } |
---|
117 | |
---|
118 | // Adjust for some D7 {url_alias} column name changes so we can keep |
---|
119 | // the test files in sync. |
---|
120 | if (isset($conditions['source'])) { |
---|
121 | $conditions['src'] = $conditions['source']; |
---|
122 | unset($conditions['source']); |
---|
123 | } |
---|
124 | if (isset($conditions['alias'])) { |
---|
125 | $conditions['dst'] = $conditions['alias']; |
---|
126 | unset($conditions['alias']); |
---|
127 | } |
---|
128 | |
---|
129 | $args = array(); |
---|
130 | $schema = drupal_get_schema_unprocessed('system', 'url_alias'); |
---|
131 | foreach ($conditions as $field => $value) { |
---|
132 | $field_type = $schema['fields'][$field]['type']; |
---|
133 | if (is_array($value)) { |
---|
134 | $conditions[$field] = "$field = " . db_placeholders($value, $field_type); |
---|
135 | $args = array_merge($args, $value); |
---|
136 | } |
---|
137 | else { |
---|
138 | $placeholder = db_type_placeholder($field_type); |
---|
139 | $conditions[$field] = "$field = $placeholder"; |
---|
140 | $args[] = $value; |
---|
141 | } |
---|
142 | |
---|
143 | } |
---|
144 | |
---|
145 | $sql = "SELECT * FROM {url_alias} WHERE " . implode(' AND ', $conditions); |
---|
146 | return db_fetch_array(db_query_range($sql, $args, 0, 1)); |
---|
147 | } |
---|
148 | |
---|
149 | function deleteAllAliases() { |
---|
150 | db_query("DELETE FROM {url_alias}"); |
---|
151 | drupal_clear_path_cache(); |
---|
152 | } |
---|
153 | |
---|
154 | function addVocabulary(array $vocabulary = array()) { |
---|
155 | $vocabulary += array( |
---|
156 | 'name' => drupal_strtolower($this->randomName(5)), |
---|
157 | 'nodes' => array('story' => 'story'), |
---|
158 | ); |
---|
159 | taxonomy_save_vocabulary($vocabulary); |
---|
160 | return (object) $vocabulary; |
---|
161 | } |
---|
162 | |
---|
163 | function addTerm(stdClass $vocabulary, array $term = array()) { |
---|
164 | $term += array( |
---|
165 | 'name' => drupal_strtolower($this->randomName(5)), |
---|
166 | 'vid' => $vocabulary->vid, |
---|
167 | ); |
---|
168 | taxonomy_save_term($term); |
---|
169 | return (object) $term; |
---|
170 | } |
---|
171 | |
---|
172 | function addNodeType(array $type) { |
---|
173 | if (!isset($type['name'])) { |
---|
174 | $type['name'] = $this->randomName(8); |
---|
175 | } |
---|
176 | |
---|
177 | $type += array( |
---|
178 | 'type' => drupal_strtolower($type['name']), |
---|
179 | 'module' => 'node', |
---|
180 | 'description' => $this->randomName(40), |
---|
181 | 'custom' => TRUE, |
---|
182 | 'modified' => TRUE, |
---|
183 | 'locked' => FALSE, |
---|
184 | 'help' => '', |
---|
185 | 'min_word_count' => '', |
---|
186 | ); |
---|
187 | |
---|
188 | $type = (object) _node_type_set_defaults($type); |
---|
189 | node_type_save($type); |
---|
190 | node_types_rebuild(); |
---|
191 | return $type; |
---|
192 | } |
---|
193 | |
---|
194 | function assertEntityPattern($entity_type, $bundle, $language = '', $expected) { |
---|
195 | pathauto_static_reset('pathauto_pattern_load_by_entity'); |
---|
196 | $this->refreshVariables(); |
---|
197 | $pattern = pathauto_pattern_load_by_entity($entity_type, $bundle, $language); |
---|
198 | $this->assertIdentical($expected, $pattern); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * Unit tests for Pathauto functions. |
---|
204 | */ |
---|
205 | class PathautoUnitTestCase extends PathautoTestHelper { |
---|
206 | public static function getInfo() { |
---|
207 | return array( |
---|
208 | 'name' => 'Pathauto unit tests', |
---|
209 | 'description' => 'Unit tests for Pathauto functions.', |
---|
210 | 'group' => 'Pathauto', |
---|
211 | 'dependencies' => array('token'), |
---|
212 | ); |
---|
213 | } |
---|
214 | |
---|
215 | function setUp(array $modules = array()) { |
---|
216 | parent::setUp($modules); |
---|
217 | module_load_include('inc', 'pathauto'); |
---|
218 | } |
---|
219 | |
---|
220 | /** |
---|
221 | * Test _pathauto_get_schema_alias_maxlength(). |
---|
222 | */ |
---|
223 | function testGetSchemaAliasMaxLength() { |
---|
224 | $this->assertIdentical(_pathauto_get_schema_alias_maxlength(), 128); |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | * Test pathauto_pattern_load_by_entity(). |
---|
229 | */ |
---|
230 | function testPatternLoadByEntity() { |
---|
231 | variable_set('pathauto_node_story_en_pattern', ' story/en/[node:title] '); |
---|
232 | variable_set('pathauto_node_story_pattern', 'story/[node:title]'); |
---|
233 | variable_set('pathauto_node_pattern', 'content/[node:title]'); |
---|
234 | variable_set('pathauto_user_pattern', 'users/[user:name]'); |
---|
235 | |
---|
236 | $tests = array( |
---|
237 | array('entity' => 'node', 'bundle' => 'story', 'language' => 'fr', 'expected' => 'story/[node:title]'), |
---|
238 | array('entity' => 'node', 'bundle' => 'story', 'language' => 'en', 'expected' => 'story/en/[node:title]'), |
---|
239 | array('entity' => 'node', 'bundle' => 'story', 'language' => '', 'expected' => 'story/[node:title]'), |
---|
240 | array('entity' => 'node', 'bundle' => 'page', 'language' => 'en', 'expected' => 'content/[node:title]'), |
---|
241 | array('entity' => 'user', 'bundle' => 'user', 'language' => '', 'expected' => 'users/[user:name]'), |
---|
242 | array('entity' => 'invalid-entity', 'bundle' => '', 'language' => '', 'expected' => ''), |
---|
243 | ); |
---|
244 | foreach ($tests as $test) { |
---|
245 | $actual = pathauto_pattern_load_by_entity($test['entity'], $test['bundle'], $test['language']); |
---|
246 | $this->assertIdentical($actual, $test['expected'], t("pathauto_pattern_load_by_entity('@entity', '@bundle', '@language') returned '@actual', expected '@expected'", array('@entity' => $test['entity'], '@bundle' => $test['bundle'], '@language' => $test['language'], '@actual' => $actual, '@expected' => $test['expected']))); |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | * Test pathauto_cleanstring(). |
---|
252 | */ |
---|
253 | function testCleanString() { |
---|
254 | $tests = array(); |
---|
255 | variable_set('pathauto_ignore_words', ', in, is,that, the , this, with, '); |
---|
256 | variable_set('pathauto_max_component_length', 35); |
---|
257 | |
---|
258 | // Test the 'ignored words' removal. |
---|
259 | $tests['this'] = 'this'; |
---|
260 | $tests['this with that'] = 'this-with-that'; |
---|
261 | $tests['this thing with that thing'] = 'thing-thing'; |
---|
262 | |
---|
263 | // Test length truncation and duplicate separator removal. |
---|
264 | $tests[' - Pathauto is the greatest - module ever in Drupal history - '] = 'pathauto-greatest-module-ever'; |
---|
265 | |
---|
266 | // Test that HTML tags are removed. |
---|
267 | $tests['This <span class="text">text</span> has <br /><a href="http://example.com"><strong>HTML tags</strong></a>.'] = 'text-has-html-tags'; |
---|
268 | $tests[check_plain('This <span class="text">text</span> has <br /><a href="http://example.com"><strong>HTML tags</strong></a>.')] = 'text-has-html-tags'; |
---|
269 | |
---|
270 | foreach ($tests as $input => $expected) { |
---|
271 | $output = pathauto_cleanstring($input); |
---|
272 | $this->assertEqual($output, $expected, t("pathauto_cleanstring('@input') expected '@expected', actual '@output'", array('@input' => $input, '@expected' => $expected, '@output' => $output))); |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * Test pathauto_path_delete_multiple(). |
---|
278 | */ |
---|
279 | function testPathDeleteMultiple() { |
---|
280 | $this->saveAlias('node/1', 'node-1-alias'); |
---|
281 | $this->saveAlias('node/1/view', 'node-1-alias/view'); |
---|
282 | $this->saveAlias('node/1', 'node-1-alias-en', 'en'); |
---|
283 | $this->saveAlias('node/1', 'node-1-alias-fr', 'fr'); |
---|
284 | $this->saveAlias('node/2', 'node-2-alias'); |
---|
285 | |
---|
286 | pathauto_path_delete_all('node/1'); |
---|
287 | $this->assertNoAliasExists(array('source' => "node/1")); |
---|
288 | $this->assertNoAliasExists(array('source' => "node/1/view")); |
---|
289 | $this->assertAliasExists(array('source' => "node/2")); |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | * Test _pathauto_get_raw_tokens(). |
---|
294 | */ |
---|
295 | function testGetRawTokens() { |
---|
296 | $raw_tokens = _pathauto_get_raw_tokens(); |
---|
297 | $this->assertFalse(in_array('node-path', $raw_tokens), 'Non-raw tokens not included.'); |
---|
298 | $this->assertTrue(in_array('node-path-raw', $raw_tokens), 'Token [catpath] has a matching raw token.'); |
---|
299 | $this->assertFalse(in_array('node-url-raw', $raw_tokens), 'Token [catalias] does not have a matching raw token.'); |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * Test the different update actions in pathauto_create_alias(). |
---|
304 | */ |
---|
305 | function testUpdateActions() { |
---|
306 | // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'insert'. |
---|
307 | variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_NO_NEW); |
---|
308 | $node = $this->drupalCreateNode(array('title' => 'First title')); |
---|
309 | $this->assertEntityAlias('node', $node, 'content/first-title'); |
---|
310 | |
---|
311 | // Default action is PATHAUTO_UPDATE_ACTION_DELETE. |
---|
312 | variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE); |
---|
313 | $node->title = 'Second title'; |
---|
314 | pathauto_nodeapi($node, 'update'); |
---|
315 | $this->assertEntityAlias('node', $node, 'content/second-title'); |
---|
316 | $this->assertNoAliasExists(array('alias' => 'content/first-title')); |
---|
317 | |
---|
318 | // Test PATHAUTO_UPDATE_ACTION_LEAVE |
---|
319 | variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_LEAVE); |
---|
320 | $node->title = 'Third title'; |
---|
321 | pathauto_nodeapi($node, 'update'); |
---|
322 | $this->assertEntityAlias('node', $node, 'content/third-title'); |
---|
323 | $this->assertAliasExists(array('source' => "node/{$node->nid}", 'alias' => 'content/second-title')); |
---|
324 | |
---|
325 | variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE); |
---|
326 | $node->title = 'Fourth title'; |
---|
327 | pathauto_nodeapi($node, 'update'); |
---|
328 | $this->assertEntityAlias('node', $node, 'content/fourth-title'); |
---|
329 | $this->assertNoAliasExists(array('alias' => 'content/third-title')); |
---|
330 | // The older second alias is not deleted yet. |
---|
331 | $older_path = $this->assertAliasExists(array('source' => "node/{$node->nid}", 'alias' => 'content/second-title')); |
---|
332 | path_set_alias(NULL, NULL, $older_path['pid']); |
---|
333 | |
---|
334 | variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_NO_NEW); |
---|
335 | $node->title = 'Fifth title'; |
---|
336 | pathauto_nodeapi($node, 'update'); |
---|
337 | $this->assertEntityAlias('node', $node, 'content/fourth-title'); |
---|
338 | $this->assertNoAliasExists(array('alias' => 'content/fith-title')); |
---|
339 | |
---|
340 | // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'update'. |
---|
341 | $this->deleteAllAliases(); |
---|
342 | pathauto_nodeapi($node, 'update'); |
---|
343 | $this->assertEntityAlias('node', $node, 'content/fifth-title'); |
---|
344 | |
---|
345 | // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'bulkupdate'. |
---|
346 | $this->deleteAllAliases(); |
---|
347 | $node->title = 'Sixth title'; |
---|
348 | pathauto_node_update_alias($node, 'bulkupdate'); |
---|
349 | $this->assertEntityAlias('node', $node, 'content/sixth-title'); |
---|
350 | } |
---|
351 | |
---|
352 | /** |
---|
353 | * Test that pathauto_create_alias() will not create an alias for a pattern |
---|
354 | * that does not get any tokens replaced. |
---|
355 | */ |
---|
356 | function testNoTokensNoAlias() { |
---|
357 | $node = $this->drupalCreateNode(array('title' => '')); |
---|
358 | $this->assertNoEntityAlias('node', $node); |
---|
359 | |
---|
360 | $node->title = 'hello'; |
---|
361 | pathauto_nodeapi($node, 'update'); |
---|
362 | $this->assertEntityAlias('node', $node, 'content/hello'); |
---|
363 | } |
---|
364 | |
---|
365 | /** |
---|
366 | * Test the handling of path vs non-path tokens in pathauto_clean_token_values(). |
---|
367 | * |
---|
368 | * @see PathautoBookTokenTestCase::testBookPathAlias() |
---|
369 | */ |
---|
370 | //function testPathTokens() { |
---|
371 | //} |
---|
372 | |
---|
373 | function testEntityBundleRenamingDeleting() { |
---|
374 | // Create a vocabulary type. |
---|
375 | $vocabulary = $this->addVocabulary(); |
---|
376 | variable_set('pathauto_taxonomy_pattern', 'base'); |
---|
377 | variable_set('pathauto_taxonomy_' . $vocabulary->vid . '_pattern', 'bundle'); |
---|
378 | $this->assertEntityPattern('taxonomy', $vocabulary->vid, '', 'bundle'); |
---|
379 | |
---|
380 | // Delete the vocabulary, which should cause its pattern variable to also |
---|
381 | // be deleted. |
---|
382 | taxonomy_del_vocabulary($vocabulary->vid); |
---|
383 | $this->assertEntityPattern('taxonomy', $vocabulary->vid, '', 'base'); |
---|
384 | |
---|
385 | // Create a node type and test that it's pattern variable works. |
---|
386 | $type = $this->addNodeType(array('type' => 'old_name')); |
---|
387 | variable_set('pathauto_node_pattern', 'base'); |
---|
388 | variable_set("pathauto_node_old_name_pattern", 'bundle'); |
---|
389 | $this->assertEntityPattern('node', 'old_name', '', 'bundle'); |
---|
390 | |
---|
391 | // Rename the node type's machine name, which should cause its pattern |
---|
392 | // variable to also be renamed. |
---|
393 | $type->type = 'new_name'; |
---|
394 | $type->old_type = 'old_name'; |
---|
395 | node_type_save($type); |
---|
396 | node_types_rebuild(); |
---|
397 | $this->assertEntityPattern('node', 'new_name', '', 'bundle'); |
---|
398 | $this->assertEntityPattern('node', 'old_name', '', 'base'); |
---|
399 | |
---|
400 | // Delete the node type, which should cause its pattern variable to also |
---|
401 | // be deleted. |
---|
402 | node_type_delete($type->type); |
---|
403 | $this->assertEntityPattern('node', 'new_name', '', 'base'); |
---|
404 | } |
---|
405 | |
---|
406 | function testNoExistingPathAliases() { |
---|
407 | variable_set('pathauto_node_page_pattern', '[title-raw]'); |
---|
408 | variable_set('pathauto_punctuation_period', PATHAUTO_PUNCTUATION_DO_NOTHING); |
---|
409 | |
---|
410 | // Check that Pathauto does not create an alias of '/admin'. |
---|
411 | $node = $this->drupalCreateNode(array('title' => 'Admin', 'type' => 'page')); |
---|
412 | $this->assertNoEntityAlias('node', $node); |
---|
413 | |
---|
414 | // Check that Pathauto does not create an alias of '/modules'. |
---|
415 | $node->title = 'Modules'; |
---|
416 | node_save($node); |
---|
417 | $this->assertNoEntityAlias('node', $node); |
---|
418 | |
---|
419 | // Check that Pathauto does not create an alias of '/index.php'. |
---|
420 | $node->title = 'index.php'; |
---|
421 | node_save($node); |
---|
422 | $this->assertNoEntityAlias('node', $node); |
---|
423 | |
---|
424 | // Check that a safe value gets an automatic alias. This is also a control |
---|
425 | // to ensure the above tests work properly. |
---|
426 | $node->title = 'Safe value'; |
---|
427 | node_save($node); |
---|
428 | $this->assertEntityAlias('node', $node, 'safe-value'); |
---|
429 | } |
---|
430 | |
---|
431 | /** |
---|
432 | * Test pathauto_clean_alias(). |
---|
433 | */ |
---|
434 | function testCleanAlias() { |
---|
435 | $tests = array(); |
---|
436 | $tests['one/two/three'] = 'one/two/three'; |
---|
437 | $tests['/one/two/three/'] = 'one/two/three'; |
---|
438 | $tests['one//two///three'] = 'one/two/three'; |
---|
439 | $tests['one/two--three/-/--/-/--/four---five'] = 'one/two-three/four-five'; |
---|
440 | $tests['one/-//three--/four'] = 'one/three/four'; |
---|
441 | foreach ($tests as $input => $expected) { |
---|
442 | $output = pathauto_clean_alias($input); |
---|
443 | $this->assertEqual($output, $expected, t("pathauto_clean_alias('@input') expected '@expected', actual '@output'", array('@input' => $input, '@expected' => $expected, '@output' => $output))); |
---|
444 | } |
---|
445 | } |
---|
446 | |
---|
447 | function testPathAliasUniquifyWordsafe() { |
---|
448 | variable_set('pathauto_max_length', 25); |
---|
449 | |
---|
450 | $node_1 = $this->drupalCreateNode(array('title' => 'thequick brownfox jumpedover thelazydog', 'type' => 'page')); |
---|
451 | $node_2 = $this->drupalCreateNode(array('title' => 'thequick brownfox jumpedover thelazydog', 'type' => 'page')); |
---|
452 | |
---|
453 | // Check that pathauto_alias_uniquify is calling truncate_utf8 with $wordsafe param set to TRUE. |
---|
454 | // If it doesn't path alias result would be content/thequick-brownf-0 |
---|
455 | $this->assertEntityAlias('node', $node_1, 'content/thequick-brownfox'); |
---|
456 | $this->assertEntityAlias('node', $node_2, 'content/thequick-0'); |
---|
457 | } |
---|
458 | } |
---|
459 | |
---|
460 | /** |
---|
461 | * Helper test class with some added functions for testing. |
---|
462 | */ |
---|
463 | class PathautoFunctionalTestHelper extends PathautoTestHelper { |
---|
464 | protected $admin_user; |
---|
465 | |
---|
466 | function setUp(array $modules = array()) { |
---|
467 | parent::setUp($modules); |
---|
468 | |
---|
469 | // Set pathauto settings we assume to be as-is in this test. |
---|
470 | variable_set('pathauto_node_page_pattern', 'content/[title-raw]'); |
---|
471 | |
---|
472 | // Allow other modules to add additional permissions for the admin user. |
---|
473 | $permissions = array( |
---|
474 | 'administer pathauto', |
---|
475 | 'administer url aliases', |
---|
476 | 'create url aliases', |
---|
477 | 'administer nodes', |
---|
478 | 'administer users', |
---|
479 | ); |
---|
480 | $args = func_get_args(); |
---|
481 | if (isset($args[1]) && is_array($args[1])) { |
---|
482 | $permissions = array_merge($permissions, $args[1]); |
---|
483 | } |
---|
484 | $this->admin_user = $this->drupalCreateUser($permissions); |
---|
485 | |
---|
486 | $this->drupalLogin($this->admin_user); |
---|
487 | } |
---|
488 | } |
---|
489 | |
---|
490 | /** |
---|
491 | * Test basic pathauto functionality. |
---|
492 | */ |
---|
493 | class PathautoFunctionalTestCase extends PathautoFunctionalTestHelper { |
---|
494 | public static function getInfo() { |
---|
495 | return array( |
---|
496 | 'name' => 'Pathauto basic tests', |
---|
497 | 'description' => 'Test basic pathauto functionality.', |
---|
498 | 'group' => 'Pathauto', |
---|
499 | 'dependencies' => array('token'), |
---|
500 | ); |
---|
501 | } |
---|
502 | |
---|
503 | /** |
---|
504 | * Basic functional testing of Pathauto. |
---|
505 | */ |
---|
506 | function testNodeEditing() { |
---|
507 | // Create node for testing. |
---|
508 | $random_title = $this->randomName(10); |
---|
509 | $title = ' Simpletest title ' . $random_title . ' ['; |
---|
510 | $automatic_alias = 'content/simpletest-title-' . strtolower($random_title); |
---|
511 | $node = $this->drupalCreateNode(array('title' => $title, 'type' => 'page')); |
---|
512 | |
---|
513 | // Look for alias generated in the form. |
---|
514 | $this->drupalGet('node/' . $node->nid . '/edit'); |
---|
515 | $this->assertFieldChecked('edit-pathauto-perform-alias'); |
---|
516 | $this->assertFieldByName('path', $automatic_alias, 'Proper automated alias generated.'); |
---|
517 | |
---|
518 | // Check whether the alias actually works. |
---|
519 | $this->drupalGet($automatic_alias); |
---|
520 | $this->assertText($title, 'Node accessible through automatic alias.'); |
---|
521 | |
---|
522 | // Manually set the node's alias. |
---|
523 | $manual_alias = 'content/' . $node->nid; |
---|
524 | $edit = array( |
---|
525 | 'pathauto_perform_alias' => FALSE, |
---|
526 | 'path' => $manual_alias, |
---|
527 | ); |
---|
528 | $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); |
---|
529 | $this->assertText(t('@type @title has been updated', array('@type' => 'Page', '@title' => $title))); |
---|
530 | |
---|
531 | // Check that the automatic alias checkbox is now unchecked by default. |
---|
532 | $this->drupalGet('node/' . $node->nid . '/edit'); |
---|
533 | $this->assertNoFieldChecked('edit-pathauto-perform-alias'); |
---|
534 | $this->assertFieldByName('path', $manual_alias); |
---|
535 | |
---|
536 | // Submit the node form with the default values. |
---|
537 | $this->drupalPost(NULL, array(), t('Save')); |
---|
538 | $this->assertText(t('@type @title has been updated', array('@type' => 'Page', '@title' => $title))); |
---|
539 | |
---|
540 | // Test that the old (automatic) alias has been deleted and only accessible |
---|
541 | // through the new (manual) alias. |
---|
542 | $this->drupalGet($automatic_alias); |
---|
543 | $this->assertResponse(404, 'Node not accessible through automatic alias.'); |
---|
544 | $this->drupalGet($manual_alias); |
---|
545 | $this->assertText($title, 'Node accessible through manual alias.'); |
---|
546 | } |
---|
547 | |
---|
548 | /** |
---|
549 | * Test node operations. |
---|
550 | */ |
---|
551 | function testNodeOperations() { |
---|
552 | $node1 = $this->drupalCreateNode(array('title' => 'node1')); |
---|
553 | $node2 = $this->drupalCreateNode(array('title' => 'node2')); |
---|
554 | |
---|
555 | // Delete all current URL aliases. |
---|
556 | $this->deleteAllAliases(); |
---|
557 | |
---|
558 | $edit = array( |
---|
559 | 'operation' => 'pathauto_update_alias', |
---|
560 | "nodes[{$node1->nid}]" => TRUE, |
---|
561 | ); |
---|
562 | $this->drupalPost('admin/content/node', $edit, t('Update')); |
---|
563 | $this->assertText('Updated URL alias for 1 node.'); |
---|
564 | |
---|
565 | $this->assertEntityAlias('node', $node1, 'content/' . $node1->title); |
---|
566 | $this->assertEntityAlias('node', $node2, 'node/' . $node2->nid); |
---|
567 | } |
---|
568 | |
---|
569 | /** |
---|
570 | * Test user operations. |
---|
571 | */ |
---|
572 | function testUserOperations() { |
---|
573 | $account = $this->drupalCreateUser(); |
---|
574 | |
---|
575 | // Delete all current URL aliases. |
---|
576 | $this->deleteAllAliases(); |
---|
577 | |
---|
578 | $edit = array( |
---|
579 | 'operation' => 'pathauto_update_alias', |
---|
580 | "accounts[{$account->uid}]" => TRUE, |
---|
581 | ); |
---|
582 | $this->drupalPost('admin/user/user', $edit, t('Update')); |
---|
583 | $this->assertText('Updated URL alias for 1 user account.'); |
---|
584 | |
---|
585 | $this->assertEntityAlias('user', $account, 'users/' . drupal_strtolower($account->name)); |
---|
586 | $this->assertEntityAlias('user', $this->admin_user, 'user/' . $this->admin_user->uid); |
---|
587 | } |
---|
588 | |
---|
589 | function testSettingsValidation() { |
---|
590 | $edit = array(); |
---|
591 | $edit['pathauto_max_length'] = 'abc'; |
---|
592 | $edit['pathauto_max_component_length'] = 'abc'; |
---|
593 | $this->drupalPost('admin/build/path/settings', $edit, 'Save configuration'); |
---|
594 | $this->assertText('The field Maximum alias length is not a valid number.'); |
---|
595 | $this->assertText('The field Maximum component length is not a valid number.'); |
---|
596 | $this->assertNoText('The configuration options have been saved.'); |
---|
597 | |
---|
598 | $edit['pathauto_max_length'] = '0'; |
---|
599 | $edit['pathauto_max_component_length'] = '0'; |
---|
600 | $this->drupalPost('admin/build/path/settings', $edit, 'Save configuration'); |
---|
601 | $this->assertText('The field Maximum alias length cannot be less than 1.'); |
---|
602 | $this->assertText('The field Maximum component length cannot be less than 1.'); |
---|
603 | $this->assertNoText('The configuration options have been saved.'); |
---|
604 | |
---|
605 | $edit['pathauto_max_length'] = '999'; |
---|
606 | $edit['pathauto_max_component_length'] = '999'; |
---|
607 | $this->drupalPost('admin/build/path/settings', $edit, 'Save configuration'); |
---|
608 | $this->assertText('The field Maximum alias length cannot be greater than 128.'); |
---|
609 | $this->assertText('The field Maximum component length cannot be greater than 128.'); |
---|
610 | $this->assertNoText('The configuration options have been saved.'); |
---|
611 | |
---|
612 | $edit['pathauto_max_length'] = '50'; |
---|
613 | $edit['pathauto_max_component_length'] = '50'; |
---|
614 | $this->drupalPost('admin/build/path/settings', $edit, 'Save configuration'); |
---|
615 | $this->assertText('The configuration options have been saved.'); |
---|
616 | } |
---|
617 | |
---|
618 | function testPatternsValidation() { |
---|
619 | $edit = array(); |
---|
620 | $edit['pathauto_node_pattern'] = '[title-raw]/[user-created-small]/[cat]/[term]'; |
---|
621 | $edit['pathauto_node_page_pattern'] = 'page'; |
---|
622 | $this->drupalPost('admin/build/path/patterns', $edit, 'Save configuration'); |
---|
623 | $this->assertText('The Default path pattern (applies to all node types with blank patterns below) is using the following invalid tokens: [user-created-small], [cat].'); |
---|
624 | $this->assertText('The Pattern for all Page paths cannot contain fewer than one token.'); |
---|
625 | $this->assertNoText('The configuration options have been saved.'); |
---|
626 | |
---|
627 | $edit['pathauto_node_pattern'] = '[title-raw]'; |
---|
628 | $edit['pathauto_node_page_pattern'] = 'page/[title-raw]'; |
---|
629 | $edit['pathauto_node_story_pattern'] = ''; |
---|
630 | $this->drupalPost('admin/build/path/patterns', $edit, 'Save configuration'); |
---|
631 | $this->assertText('The configuration options have been saved.'); |
---|
632 | } |
---|
633 | } |
---|
634 | |
---|
635 | class PathautoLocaleTestCase extends PathautoFunctionalTestHelper { |
---|
636 | public static function getInfo() { |
---|
637 | return array( |
---|
638 | 'name' => 'Pathauto localization tests', |
---|
639 | 'description' => 'Test pathauto functionality with localization and translation.', |
---|
640 | 'group' => 'Pathauto', |
---|
641 | 'dependencies' => array('token'), |
---|
642 | ); |
---|
643 | } |
---|
644 | |
---|
645 | function setUp(array $modules = array()) { |
---|
646 | $modules[] = 'locale'; |
---|
647 | $modules[] = 'translation'; |
---|
648 | parent::setUp($modules, array('administer languages')); |
---|
649 | |
---|
650 | // Add predefined French language and reset the locale cache. |
---|
651 | require_once './includes/locale.inc'; |
---|
652 | locale_add_language('fr', NULL, NULL, LANGUAGE_LTR, '', 'fr'); |
---|
653 | language_list('language', TRUE); |
---|
654 | drupal_init_language(); |
---|
655 | } |
---|
656 | |
---|
657 | /** |
---|
658 | * Test that when an English node is updated, its old English alias is |
---|
659 | * updated and its newer French alias is left intact. |
---|
660 | */ |
---|
661 | function testLanguageAliases() { |
---|
662 | $node = array( |
---|
663 | 'title' => 'English node', |
---|
664 | 'language' => 'en', |
---|
665 | 'path' => 'english-node', |
---|
666 | 'pathauto_perform_alias' => FALSE, |
---|
667 | ); |
---|
668 | $node = $this->drupalCreateNode($node); |
---|
669 | $english_alias = $this->path_load(array('alias' => 'english-node')); |
---|
670 | $this->assertTrue($english_alias, 'Alias created with proper language.'); |
---|
671 | |
---|
672 | // Also save a French alias that should not be left alone, even though |
---|
673 | // it is the newer alias. |
---|
674 | $this->saveEntityAlias('node', $node, 'french-node', 'fr'); |
---|
675 | |
---|
676 | // Add an alias with the soon-to-be generated alias, causing the upcoming |
---|
677 | // alias update to generate a unique alias with the '-0' suffix. |
---|
678 | $this->saveAlias('node/invalid', 'content/english-node', ''); |
---|
679 | |
---|
680 | // Update the node, triggering a change in the English alias. |
---|
681 | $node->pathauto_perform_alias = TRUE; |
---|
682 | pathauto_nodeapi($node, 'update'); |
---|
683 | |
---|
684 | // Check that the new English alias replaced the old one. |
---|
685 | $this->assertEntityAlias('node', $node, 'content/english-node-0', 'en'); |
---|
686 | $this->assertEntityAlias('node', $node, 'french-node', 'fr'); |
---|
687 | $this->assertAliasExists(array('pid' => $english_alias['pid'], 'alias' => 'content/english-node-0')); |
---|
688 | } |
---|
689 | } |
---|
690 | |
---|
691 | /** |
---|
692 | * Bulk update functionality tests. |
---|
693 | */ |
---|
694 | class PathautoBulkUpdateTestCase extends PathautoFunctionalTestHelper { |
---|
695 | private $nodes; |
---|
696 | |
---|
697 | public static function getInfo() { |
---|
698 | return array( |
---|
699 | 'name' => 'Pathauto bulk updating', |
---|
700 | 'description' => 'Tests bulk updating of URL aliases.', |
---|
701 | 'group' => 'Pathauto', |
---|
702 | 'dependencies' => array('token'), |
---|
703 | ); |
---|
704 | } |
---|
705 | |
---|
706 | function testBulkUpdate() { |
---|
707 | // Create some nodes. |
---|
708 | $this->nodes = array(); |
---|
709 | for ($i = 1; $i <= 5; $i++) { |
---|
710 | $node = $this->drupalCreateNode(); |
---|
711 | $this->nodes[$node->nid] = $node; |
---|
712 | } |
---|
713 | |
---|
714 | // Clear out all aliases. |
---|
715 | $this->deleteAllAliases(); |
---|
716 | |
---|
717 | // Bulk create aliases. |
---|
718 | $edit = array( |
---|
719 | 'update[node_pathauto_bulk_update_batch_process]' => TRUE, |
---|
720 | 'update[user_pathauto_bulk_update_batch_process]' => TRUE, |
---|
721 | ); |
---|
722 | $this->drupalPost('admin/build/path/update_bulk', $edit, t('Update')); |
---|
723 | $this->assertText('Generated 7 URL aliases.'); // 5 nodes + 2 users |
---|
724 | |
---|
725 | // Check that aliases have actually been created. |
---|
726 | foreach ($this->nodes as $node) { |
---|
727 | $this->assertEntityAliasExists('node', $node); |
---|
728 | } |
---|
729 | $this->assertEntityAliasExists('user', $this->admin_user); |
---|
730 | |
---|
731 | // Add a new node. |
---|
732 | $new_node = $this->drupalCreateNode(array('alias' => '', 'pathauto_perform_alias' => FALSE)); |
---|
733 | |
---|
734 | // Run the update again which should only run against the new node. |
---|
735 | $this->drupalPost('admin/build/path/update_bulk', $edit, t('Update')); |
---|
736 | $this->assertText('Generated 1 URL alias.'); // 1 node + 0 users |
---|
737 | |
---|
738 | $this->assertEntityAliasExists('node', $new_node); |
---|
739 | } |
---|
740 | } |
---|
741 | |
---|
742 | /* |
---|
743 | * Unit tests for the book tokens provided by Pathauto. |
---|
744 | */ |
---|
745 | class PathautoBookTokenTestCase extends PathautoTestHelper { |
---|
746 | public static function getInfo() { |
---|
747 | return array( |
---|
748 | 'name' => 'Pathauto book tokens', |
---|
749 | 'description' => 'Unit tests for the book tokens provided by Pathauto.', |
---|
750 | 'group' => 'Pathauto', |
---|
751 | 'dependencies' => array('token'), |
---|
752 | ); |
---|
753 | } |
---|
754 | |
---|
755 | function setUp(array $modules = array()) { |
---|
756 | $modules[] = 'book'; |
---|
757 | parent::setUp($modules); |
---|
758 | |
---|
759 | variable_set('book_allowed_types', array('book', 'page')); |
---|
760 | variable_set('pathauto_node_book_pattern', '[bookpathalias]/[title-raw]'); |
---|
761 | } |
---|
762 | |
---|
763 | function testBookPathAlias() { |
---|
764 | // Add a non-book node. |
---|
765 | $non_book_node = $this->drupalCreateNode(array('type' => 'book')); |
---|
766 | $this->assertToken('node', $non_book_node, 'bookpathalias', ''); |
---|
767 | |
---|
768 | // Add a root book page. |
---|
769 | $parent_node = $this->drupalCreateNode(array( |
---|
770 | 'type' => 'book', |
---|
771 | 'title' => 'Root', |
---|
772 | 'book' => array('bid' => 'new') + _book_link_defaults('new'), |
---|
773 | )); |
---|
774 | $tokens = array( |
---|
775 | 'bookpathalias' => '', |
---|
776 | ); |
---|
777 | $this->assertTokens('node', $parent_node, $tokens); |
---|
778 | |
---|
779 | // Add a first child page. |
---|
780 | $child_node1 = $this->drupalCreateNode(array( |
---|
781 | 'type' => 'book', |
---|
782 | 'title' => 'Sub page1', |
---|
783 | 'book' => array( |
---|
784 | 'bid' => $parent_node->book['bid'], |
---|
785 | 'plid' => $parent_node->book['mlid'], |
---|
786 | ) + _book_link_defaults('new'), |
---|
787 | )); |
---|
788 | $tokens = array( |
---|
789 | 'bookpathalias' => 'root', |
---|
790 | ); |
---|
791 | $this->assertTokens('node', $child_node1, $tokens); |
---|
792 | |
---|
793 | // Add a second child page. |
---|
794 | $child_node2 = $this->drupalCreateNode(array( |
---|
795 | 'type' => 'book', |
---|
796 | 'title' => 'Sub page2', |
---|
797 | 'book' => array( |
---|
798 | 'bid' => $parent_node->book['bid'], |
---|
799 | 'plid' => $parent_node->book['mlid'], |
---|
800 | ) + _book_link_defaults('new'), |
---|
801 | )); |
---|
802 | $tokens = array( |
---|
803 | 'bookpathalias' => 'root', |
---|
804 | ); |
---|
805 | $this->assertTokens('node', $child_node2, $tokens); |
---|
806 | |
---|
807 | // Add a child page on an existing child page. |
---|
808 | $sub_child_node1 = $this->drupalCreateNode(array( |
---|
809 | 'type' => 'book', |
---|
810 | 'title' => 'Sub-sub Page1', |
---|
811 | 'book' => array( |
---|
812 | 'bid' => $parent_node->book['bid'], |
---|
813 | 'plid' => $child_node1->book['mlid'], |
---|
814 | ) + _book_link_defaults('new'), |
---|
815 | )); |
---|
816 | $tokens = array( |
---|
817 | 'bookpathalias' => 'root/sub-page1', |
---|
818 | ); |
---|
819 | $this->assertTokens('node', $sub_child_node1, $tokens); |
---|
820 | |
---|
821 | // Test that path tokens should not be altered. |
---|
822 | $this->saveEntityAlias('node', $child_node1, 'My Crazy/Alias/'); |
---|
823 | pathauto_nodeapi($sub_child_node1, 'update'); |
---|
824 | $this->assertEntityAlias('node', $sub_child_node1, 'My Crazy/Alias/sub-sub-page1'); |
---|
825 | } |
---|
826 | } |
---|
827 | |
---|
828 | /* |
---|
829 | * Unit tests for the taxonomy tokens provided by Pathauto. |
---|
830 | */ |
---|
831 | class PathautoTaxonomyTokenTestCase extends PathautoFunctionalTestHelper { |
---|
832 | protected $vocab; |
---|
833 | |
---|
834 | public static function getInfo() { |
---|
835 | return array( |
---|
836 | 'name' => 'Pathauto taxonomy tokens', |
---|
837 | 'description' => 'Unit tests for the taxonomy tokens provided by Pathauto.', |
---|
838 | 'group' => 'Pathauto', |
---|
839 | 'dependencies' => array('token'), |
---|
840 | ); |
---|
841 | } |
---|
842 | |
---|
843 | function setUp(array $modules = array()) { |
---|
844 | $modules[] = 'taxonomy'; |
---|
845 | parent::setUp($modules); |
---|
846 | |
---|
847 | variable_set('pathauto_taxonomy_pattern', 'category/[vocab-raw]/[cat-raw]'); |
---|
848 | // Reset the static taxonomy.module caches. |
---|
849 | taxonomy_vocabulary_load(0, TRUE); |
---|
850 | taxonomy_get_term(0, TRUE); |
---|
851 | $this->vocab = $this->addVocabulary(); |
---|
852 | } |
---|
853 | |
---|
854 | /** |
---|
855 | * Test the [catpath] and [catalias] tokens. |
---|
856 | */ |
---|
857 | function testCatTokens() { |
---|
858 | $term1 = $this->addTerm($this->vocab); |
---|
859 | $tokens = array( |
---|
860 | 'catpath' => $term1->name, |
---|
861 | 'catalias' => "category/{$this->vocab->name}/{$term1->name}", |
---|
862 | ); |
---|
863 | $this->assertTokens('taxonomy', $term1, $tokens); |
---|
864 | |
---|
865 | // Change the term name to check that the alias is also changed. |
---|
866 | // Regression test for http://drupal.org/node/822174. |
---|
867 | $term1->oldname = $term1->name; |
---|
868 | $term1->name = drupal_strtolower($this->randomName()); |
---|
869 | $form_values = (array) $term1; |
---|
870 | taxonomy_save_term($form_values); |
---|
871 | $tokens = array( |
---|
872 | 'catpath' => $term1->name, |
---|
873 | ); |
---|
874 | $this->assertTokens('taxonomy', $term1, $tokens); |
---|
875 | |
---|
876 | $term2 = $this->addTerm($this->vocab, array('parent' => $term1->tid)); |
---|
877 | $tokens = array( |
---|
878 | 'catpath' => "{$term1->name}/{$term2->name}", |
---|
879 | 'catalias' => "category/{$this->vocab->name}/{$term2->name}", |
---|
880 | ); |
---|
881 | $this->assertTokens('taxonomy', $term2, $tokens); |
---|
882 | |
---|
883 | $term3 = $this->addTerm($this->vocab, array('parent' => $term2->tid, 'name' => ' foo/bar fer|zle ')); |
---|
884 | $tokens = array( |
---|
885 | 'catpath' => "{$term1->name}/{$term2->name}/foobar-ferzle", |
---|
886 | 'catalias' => "category/{$this->vocab->name}/foobar-ferzle", |
---|
887 | ); |
---|
888 | $this->assertTokens('taxonomy', $term3, $tokens); |
---|
889 | } |
---|
890 | |
---|
891 | /** |
---|
892 | * Test the [termpath] token. |
---|
893 | */ |
---|
894 | function testTermTokens() { |
---|
895 | $term1 = $this->addTerm($this->vocab, array('weight' => 5)); |
---|
896 | $term2 = $this->addTerm($this->vocab, array('weight' => -5)); |
---|
897 | $term3 = $this->addTerm($this->vocab, array('weight' => 0)); |
---|
898 | |
---|
899 | $node = $this->drupalCreateNode(array( |
---|
900 | 'type' => 'story', |
---|
901 | 'taxonomy' => array($term1->tid, $term2->tid, $term3->tid), |
---|
902 | )); |
---|
903 | $tokens = array( |
---|
904 | 'termpath' => $term2->name, |
---|
905 | 'termalias' => "category/{$this->vocab->name}/{$term2->name}", |
---|
906 | ); |
---|
907 | $this->assertTokens('node', $node, $tokens); |
---|
908 | $this->assertToken('node', $node, 'termpath', $term2->name); |
---|
909 | $this->assertToken('node', $node, 'termalias', "category/{$this->vocab->name}/{$term2->name}"); |
---|
910 | |
---|
911 | $non_term_node = $this->drupalCreateNode(array('type' => 'story')); |
---|
912 | $tokens = array( |
---|
913 | 'termpath' => '', |
---|
914 | 'termalias' => '', |
---|
915 | ); |
---|
916 | $this->assertTokens('node', $non_term_node, $tokens); |
---|
917 | } |
---|
918 | } |
---|