Projet

Général

Profil

Paste
Télécharger (38,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / pathauto / pathauto.test @ d719f12f

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

    
22
  function assertToken($type, $object, $token, $expected) {
23
    $tokens = token_generate($type, array($token => $token), array($type => $object));
24
    $tokens += array($token => '');
25
    $this->assertIdentical($tokens[$token], $expected, t("Token value for [@type:@token] was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $tokens[$token], '@expected' => $expected)));
26
  }
27

    
28
  function saveAlias($source, $alias, $language = LANGUAGE_NONE) {
29
    $alias = array(
30
      'source' => $source,
31
      'alias' => $alias,
32
      'language' => $language,
33
    );
34
    path_save($alias);
35
    return $alias;
36
  }
37

    
38
  function saveEntityAlias($entity_type, $entity, $alias, $language = LANGUAGE_NONE) {
39
    $uri = entity_uri($entity_type, $entity);
40
    return $this->saveAlias($uri['path'], $alias, $language);
41
  }
42

    
43
  function assertEntityAlias($entity_type, $entity, $expected_alias, $language = LANGUAGE_NONE) {
44
    $uri = entity_uri($entity_type, $entity);
45
    $this->assertAlias($uri['path'], $expected_alias, $language);
46
  }
47

    
48
  function assertEntityAliasExists($entity_type, $entity) {
49
    $uri = entity_uri($entity_type, $entity);
50
    return $this->assertAliasExists(array('source' => $uri['path']));
51
  }
52

    
53
  function assertNoEntityAlias($entity_type, $entity, $language = LANGUAGE_NONE) {
54
    $uri = entity_uri($entity_type, $entity);
55
    $this->assertEntityAlias($entity_type, $entity, $uri['path'], $language);
56
  }
57

    
58
  function assertNoEntityAliasExists($entity_type, $entity, $alias = NULL) {
59
    $uri = entity_uri($entity_type, $entity);
60
    $path = array('source' => $uri['path']);
61
    if (!empty($alias)) {
62
      $path['alias'] = $alias;
63
    }
64
    $this->assertNoAliasExists($path);
65
  }
66

    
67
  function assertAlias($source, $expected_alias, $language = LANGUAGE_NONE) {
68
    drupal_clear_path_cache($source);
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 = 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 = path_load($conditions);
81
    $this->assertFalse($alias, t('Alias with conditions @conditions not found.', array('@conditions' => var_export($conditions, TRUE))));
82
  }
83

    
84
  function deleteAllAliases() {
85
    db_delete('url_alias')->execute();
86
    drupal_clear_path_cache();
87
  }
88

    
89
  function addVocabulary(array $vocabulary = array()) {
90
    $name = drupal_strtolower($this->randomName(5));
91
    $vocabulary += array(
92
      'name' => $name,
93
      'machine_name' => $name,
94
      'nodes' => array('article' => 'article'),
95
    );
96
    $vocabulary = (object) $vocabulary;
97
    taxonomy_vocabulary_save($vocabulary);
98
    return $vocabulary;
99
  }
100

    
101
  function addTerm(stdClass $vocabulary, array $term = array()) {
102
    $term += array(
103
      'name' => drupal_strtolower($this->randomName(5)),
104
      'vocabulary_machine_name' => $vocabulary->machine_name,
105
      'vid' => $vocabulary->vid,
106
    );
107
    $term = (object) $term;
108
    taxonomy_term_save($term);
109
    return $term;
110
  }
111

    
112
  function assertEntityPattern($entity_type, $bundle, $language = LANGUAGE_NONE, $expected) {
113
    drupal_static_reset('pathauto_pattern_load_by_entity');
114
    $this->refreshVariables();
115
    $pattern = pathauto_pattern_load_by_entity($entity_type, $bundle, $language);
116
    $this->assertIdentical($expected, $pattern);
117
  }
118

    
119
  function drupalGetTermByName($name, $reset = FALSE) {
120
    $terms = entity_load('taxonomy_term', array(), array('name' => $name), $reset);
121
    return !empty($terms) ? reset($terms) : FALSE;
122
  }
123
}
124

    
125
/**
126
 * Unit tests for Pathauto functions.
127
 */
128
class PathautoUnitTestCase extends PathautoTestHelper {
129
  public static function getInfo() {
130
    return array(
131
      'name' => 'Pathauto unit tests',
132
      'description' => 'Unit tests for Pathauto functions.',
133
      'group' => 'Pathauto',
134
      'dependencies' => array('token'),
135
    );
136
  }
137

    
138
  function setUp(array $modules = array()) {
139
    parent::setUp($modules);
140
    module_load_include('inc', 'pathauto');
141
  }
142

    
143
  /**
144
   * Test _pathauto_get_schema_alias_maxlength().
145
   */
146
  function testGetSchemaAliasMaxLength() {
147
    $this->assertIdentical(_pathauto_get_schema_alias_maxlength(), 255);
148
  }
149

    
150
  /**
151
   * Test pathauto_pattern_load_by_entity().
152
   */
153
  function testPatternLoadByEntity() {
154
    variable_set('pathauto_node_story_en_pattern', ' story/en/[node:title] ');
155
    variable_set('pathauto_node_story_pattern', 'story/[node:title]');
156
    variable_set('pathauto_node_pattern', 'content/[node:title]');
157
    variable_set('pathauto_user_pattern', 'users/[user:name]');
158

    
159
    $tests = array(
160
      array('entity' => 'node', 'bundle' => 'story', 'language' => 'fr', 'expected' => 'story/[node:title]'),
161
      array('entity' => 'node', 'bundle' => 'story', 'language' => 'en', 'expected' => 'story/en/[node:title]'),
162
      array('entity' => 'node', 'bundle' => 'story', 'language' => LANGUAGE_NONE, 'expected' => 'story/[node:title]'),
163
      array('entity' => 'node', 'bundle' => 'page', 'language' => 'en', 'expected' => 'content/[node:title]'),
164
      array('entity' => 'user', 'bundle' => 'user', 'language' => LANGUAGE_NONE, 'expected' => 'users/[user:name]'),
165
      array('entity' => 'invalid-entity', 'bundle' => '', 'language' => LANGUAGE_NONE, 'expected' => ''),
166
    );
167
    foreach ($tests as $test) {
168
      $actual = pathauto_pattern_load_by_entity($test['entity'], $test['bundle'], $test['language']);
169
      $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'])));
170
    }
171
  }
172

    
173
  /**
174
   * Test pathauto_cleanstring().
175
   */
176
  function testCleanString() {
177
    $tests = array();
178
    variable_set('pathauto_ignore_words', ', in, is,that, the  , this, with, ');
179
    variable_set('pathauto_max_component_length', 35);
180

    
181
    // Test the 'ignored words' removal.
182
    $tests['this'] = 'this';
183
    $tests['this with that'] = 'this-with-that';
184
    $tests['this thing with that thing'] = 'thing-thing';
185

    
186
    // Test length truncation and duplicate separator removal.
187
    $tests[' - Pathauto is the greatest - module ever in Drupal history - '] = 'pathauto-greatest-module-ever';
188

    
189
    // Test that HTML tags are removed.
190
    $tests['This <span class="text">text</span> has <br /><a href="http://example.com"><strong>HTML tags</strong></a>.'] = 'text-has-html-tags';
191
    $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';
192

    
193
    foreach ($tests as $input => $expected) {
194
      $output = pathauto_cleanstring($input);
195
      $this->assertEqual($output, $expected, t("pathauto_cleanstring('@input') expected '@expected', actual '@output'", array('@input' => $input, '@expected' => $expected, '@output' => $output)));
196
    }
197
  }
198

    
199
  /**
200
   * Test pathauto_clean_alias().
201
   */
202
  function testCleanAlias() {
203
    $tests = array();
204
    $tests['one/two/three'] = 'one/two/three';
205
    $tests['/one/two/three/'] = 'one/two/three';
206
    $tests['one//two///three'] = 'one/two/three';
207
    $tests['one/two--three/-/--/-/--/four---five'] = 'one/two-three/four-five';
208
    $tests['one/-//three--/four'] = 'one/three/four';
209
    foreach ($tests as $input => $expected) {
210
      $output = pathauto_clean_alias($input);
211
      $this->assertEqual($output, $expected, t("pathauto_clean_alias('@input') expected '@expected', actual '@output'", array('@input' => $input, '@expected' => $expected, '@output' => $output)));
212
    }
213

    
214
  }
215

    
216
  /**
217
   * Test pathauto_path_delete_multiple().
218
   */
219
  function testPathDeleteMultiple() {
220
    $this->saveAlias('node/1', 'node-1-alias');
221
    $this->saveAlias('node/1/view', 'node-1-alias/view');
222
    $this->saveAlias('node/1', 'node-1-alias-en', 'en');
223
    $this->saveAlias('node/1', 'node-1-alias-fr', 'fr');
224
    $this->saveAlias('node/2', 'node-2-alias');
225

    
226
    pathauto_path_delete_all('node/1');
227
    $this->assertNoAliasExists(array('source' => "node/1"));
228
    $this->assertNoAliasExists(array('source' => "node/1/view"));
229
    $this->assertAliasExists(array('source' => "node/2"));
230
  }
231

    
232
  /**
233
   * Test the different update actions in pathauto_create_alias().
234
   */
235
  function testUpdateActions() {
236
    // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'insert'.
237
    variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_NO_NEW);
238
    $node = $this->drupalCreateNode(array('title' => 'First title'));
239
    $this->assertEntityAlias('node', $node, 'content/first-title');
240

    
241
    // Default action is PATHAUTO_UPDATE_ACTION_DELETE.
242
    variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE);
243
    $node->title = 'Second title';
244
    pathauto_node_update($node);
245
    $this->assertEntityAlias('node', $node, 'content/second-title');
246
    $this->assertNoAliasExists(array('alias' => 'content/first-title'));
247

    
248
    // Test PATHAUTO_UPDATE_ACTION_LEAVE
249
    variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_LEAVE);
250
    $node->title = 'Third title';
251
    pathauto_node_update($node);
252
    $this->assertEntityAlias('node', $node, 'content/third-title');
253
    $this->assertAliasExists(array('source' => "node/{$node->nid}", 'alias' => 'content/second-title'));
254

    
255
    variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE);
256
    $node->title = 'Fourth title';
257
    pathauto_node_update($node);
258
    $this->assertEntityAlias('node', $node, 'content/fourth-title');
259
    $this->assertNoAliasExists(array('alias' => 'content/third-title'));
260
    // The older second alias is not deleted yet.
261
    $older_path = $this->assertAliasExists(array('source' => "node/{$node->nid}", 'alias' => 'content/second-title'));
262
    path_delete($older_path);
263

    
264
    variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_NO_NEW);
265
    $node->title = 'Fifth title';
266
    pathauto_node_update($node);
267
    $this->assertEntityAlias('node', $node, 'content/fourth-title');
268
    $this->assertNoAliasExists(array('alias' => 'content/fifth-title'));
269

    
270
    // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'update'.
271
    $this->deleteAllAliases();
272
    pathauto_node_update($node);
273
    $this->assertEntityAlias('node', $node, 'content/fifth-title');
274

    
275
    // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'bulkupdate'.
276
    $this->deleteAllAliases();
277
    $node->title = 'Sixth title';
278
    pathauto_node_update_alias($node, 'bulkupdate');
279
    $this->assertEntityAlias('node', $node, 'content/sixth-title');
280
  }
281

    
282
  /**
283
   * Test that pathauto_create_alias() will not create an alias for a pattern
284
   * that does not get any tokens replaced.
285
   */
286
  function testNoTokensNoAlias() {
287
    $node = $this->drupalCreateNode(array('title' => ''));
288
    $this->assertNoEntityAliasExists('node', $node);
289

    
290
    $node->title = 'hello';
291
    pathauto_node_update($node);
292
    $this->assertEntityAlias('node', $node, 'content/hello');
293
  }
294

    
295
  /**
296
   * Test the handling of path vs non-path tokens in pathauto_clean_token_values().
297
   */
298
  function testPathTokens() {
299
    variable_set('pathauto_taxonomy_term_pattern', '[term:parent:url:path]/[term:name]');
300
    $vocab = $this->addVocabulary();
301

    
302
    $term1 = $this->addTerm($vocab, array('name' => 'Parent term'));
303
    $this->assertEntityAlias('taxonomy_term', $term1, 'parent-term');
304

    
305
    $term2 = $this->addTerm($vocab, array('name' => 'Child term', 'parent' => $term1->tid));
306
    $this->assertEntityAlias('taxonomy_term', $term2, 'parent-term/child-term');
307

    
308
    $this->saveEntityAlias('taxonomy_term', $term1, 'My Crazy/Alias/');
309
    pathauto_taxonomy_term_update($term2);
310
    $this->assertEntityAlias('taxonomy_term', $term2, 'My Crazy/Alias/child-term');
311
  }
312

    
313
  /**
314
   * Test using fields for path structures.
315
   */
316
  function testParentChildPathTokens() {
317
    // First create a field which will be used to create the path. It must
318
    // begin with a letter.
319
    $fieldname = 'a' . drupal_strtolower($this->randomName());
320
    field_create_field(array('field_name' => $fieldname, 'type' => 'text'));
321
    field_create_instance(array('field_name' => $fieldname, 'entity_type' => 'taxonomy_term', 'bundle' => 'tags'));
322

    
323
    // Make the path pattern of a field use the value of this field appended
324
    // to the parent taxonomy term's pattern if there is one.
325
    variable_set('pathauto_taxonomy_term_tags_pattern', '[term:parents:join-path]/[term:' . $fieldname . ']');
326

    
327
    // Start by creating a parent term.
328
    $parent = new stdClass();
329
    $parent->$fieldname = array(LANGUAGE_NONE => array(array('value' => $parent->name = $this->randomName())));
330
    $parent->vid = 1;
331
    taxonomy_term_save($parent);
332

    
333
    // Create the child term.
334
    $child = new stdClass();
335
    $child->name = $this->randomName();
336
    $child->$fieldname = array(LANGUAGE_NONE => array(array('value' => $child->name = $this->randomName())));
337
    $child->vid = 1;
338
    $child->parent = $parent->tid;
339
    taxonomy_term_save($child);
340
    $this->assertEntityAlias('taxonomy_term', $child, drupal_strtolower($parent->name . '/' . $child->name));
341

    
342
    // Re-saving the parent term should not modify the child term's alias.
343
    taxonomy_term_save($parent);
344
    $this->assertEntityAlias('taxonomy_term', $child, drupal_strtolower($parent->name . '/' . $child->name));
345
  }
346

    
347
  function testEntityBundleRenamingDeleting() {
348
    // Create a vocabulary and test that it's pattern variable works.
349
    $vocab = $this->addVocabulary(array('machine_name' => 'old_name'));
350
    variable_set('pathauto_taxonomy_term_pattern', 'base');
351
    variable_set("pathauto_taxonomy_term_old_name_pattern", 'bundle');
352
    $this->assertEntityPattern('taxonomy_term', 'old_name', LANGUAGE_NONE, 'bundle');
353

    
354
    // Rename the vocabulary's machine name, which should cause its pattern
355
    // variable to also be renamed.
356
    $vocab->machine_name = 'new_name';
357
    taxonomy_vocabulary_save($vocab);
358
    $this->assertEntityPattern('taxonomy_term', 'new_name', LANGUAGE_NONE, 'bundle');
359
    $this->assertEntityPattern('taxonomy_term', 'old_name', LANGUAGE_NONE, 'base');
360

    
361
    // Delete the vocabulary, which should cause its pattern variable to also
362
    // be deleted.
363
    taxonomy_vocabulary_delete($vocab->vid);
364
    $this->assertEntityPattern('taxonomy_term', 'new_name', LANGUAGE_NONE, 'base');
365
  }
366

    
367
  function testNoExistingPathAliases() {
368
    variable_set('pathauto_node_page_pattern', '[node:title]');
369
    variable_set('pathauto_punctuation_period', PATHAUTO_PUNCTUATION_DO_NOTHING);
370

    
371
    // Check that Pathauto does not create an alias of '/admin'.
372
    $node = $this->drupalCreateNode(array('title' => 'Admin', 'type' => 'page'));
373
    $this->assertEntityAlias('node', $node, 'admin-0');
374

    
375
    // Check that Pathauto does not create an alias of '/modules'.
376
    $node->title = 'Modules';
377
    node_save($node);
378
    $this->assertEntityAlias('node', $node, 'modules-0');
379

    
380
    // Check that Pathauto does not create an alias of '/index.php'.
381
    $node->title = 'index.php';
382
    node_save($node);
383
    $this->assertEntityAlias('node', $node, 'index.php-0');
384

    
385
    // Check that a safe value gets an automatic alias. This is also a control
386
    // to ensure the above tests work properly.
387
    $node->title = 'Safe value';
388
    node_save($node);
389
    $this->assertEntityAlias('node', $node, 'safe-value');
390
  }
391

    
392
  function testPathAliasUniquifyWordsafe() {
393
    variable_set('pathauto_max_length', 25);
394

    
395
    $node_1 = $this->drupalCreateNode(array('title' => 'thequick brownfox jumpedover thelazydog', 'type' => 'page'));
396
    $node_2 = $this->drupalCreateNode(array('title' => 'thequick brownfox jumpedover thelazydog', 'type' => 'page'));
397

    
398
    // Check that pathauto_alias_uniquify is calling truncate_utf8 with $wordsafe param set to TRUE.
399
    // If it doesn't path alias result would be content/thequick-brownf-0
400
    $this->assertEntityAlias('node', $node_1, 'content/thequick-brownfox');
401
    $this->assertEntityAlias('node', $node_2, 'content/thequick-0');
402
  }
403
}
404

    
405
/**
406
 * Helper test class with some added functions for testing.
407
 */
408
class PathautoFunctionalTestHelper extends PathautoTestHelper {
409
  protected $admin_user;
410

    
411
  function setUp(array $modules = array()) {
412
    parent::setUp($modules);
413

    
414
    // Set pathauto settings we assume to be as-is in this test.
415
    variable_set('pathauto_node_page_pattern', 'content/[node:title]');
416

    
417
    // Allow other modules to add additional permissions for the admin user.
418
    $permissions = array(
419
      'administer pathauto',
420
      'administer url aliases',
421
      'create url aliases',
422
      'administer nodes',
423
      'bypass node access',
424
      'access content overview',
425
      'administer taxonomy',
426
      'administer users',
427
    );
428
    $args = func_get_args();
429
    if (isset($args[1]) && is_array($args[1])) {
430
      $permissions = array_merge($permissions, $args[1]);
431
    }
432
    $this->admin_user = $this->drupalCreateUser($permissions);
433

    
434
    $this->drupalLogin($this->admin_user);
435
  }
436
}
437

    
438
/**
439
 * Test basic pathauto functionality.
440
 */
441
class PathautoFunctionalTestCase extends PathautoFunctionalTestHelper {
442
  public static function getInfo() {
443
    return array(
444
      'name' => 'Pathauto basic tests',
445
      'description' => 'Test basic pathauto functionality.',
446
      'group' => 'Pathauto',
447
      'dependencies' => array('token'),
448
    );
449
  }
450

    
451
  /**
452
   * Basic functional testing of Pathauto.
453
   */
454
  function testNodeEditing() {
455
    // Delete the default node pattern. Only the page content type will have a pattern.
456
    variable_del('pathauto_node_pattern');
457

    
458
    // Ensure that the Pathauto checkbox is checked by default on the node add form.
459
    $this->drupalGet('node/add/page');
460
    $this->assertFieldChecked('edit-path-pathauto');
461

    
462
    // Create node for testing by previewing and saving the node form.
463
    $title = ' Testing: node title [';
464
    $automatic_alias = 'content/testing-node-title';
465
    $this->drupalPost(NULL, array('title' => $title), 'Preview');
466
    $this->drupalPost(NULL, array(), 'Save');
467
    $node = $this->drupalGetNodeByTitle($title);
468

    
469
    // Look for alias generated in the form.
470
    $this->drupalGet("node/{$node->nid}/edit");
471
    $this->assertFieldChecked('edit-path-pathauto');
472
    $this->assertFieldByName('path[alias]', $automatic_alias, 'Generated alias visible in the path alias field.');
473

    
474
    // Check whether the alias actually works.
475
    $this->drupalGet($automatic_alias);
476
    $this->assertText($title, 'Node accessible through automatic alias.');
477

    
478
    // Disable the update action. The checkbox should not be visible.
479
    variable_set('pathauto_update_action', 0);
480
    $this->drupalGet("node/{$node->nid}/edit");
481
    $this->assertNoFieldById('edit-path-pathauto');
482

    
483
    // Reset the update action back to default. The checkbox should be visible.
484
    variable_del('pathauto_update_action');
485
    $this->drupalGet("node/{$node->nid}/edit");
486
    $this->assertFieldChecked('edit-path-pathauto');
487

    
488
    // Manually set the node's alias.
489
    $manual_alias = 'content/' . $node->nid;
490
    $edit = array(
491
      'path[pathauto]' => FALSE,
492
      'path[alias]' => $manual_alias,
493
    );
494
    $this->drupalPost(NULL, $edit, t('Save'));
495
    $this->assertText("Basic page $title has been updated.");
496

    
497
    // Check that the automatic alias checkbox is now unchecked by default.
498
    $this->drupalGet("node/{$node->nid}/edit");
499
    $this->assertNoFieldChecked('edit-path-pathauto');
500
    $this->assertFieldByName('path[alias]', $manual_alias);
501

    
502
    // Submit the node form with the default values.
503
    $this->drupalPost(NULL, array(), t('Save'));
504
    $this->assertText("Basic page $title has been updated.");
505

    
506
    // Test that the old (automatic) alias has been deleted and only accessible
507
    // through the new (manual) alias.
508
    $this->drupalGet($automatic_alias);
509
    $this->assertResponse(404, 'Node not accessible through automatic alias.');
510
    $this->drupalGet($manual_alias);
511
    $this->assertText($title, 'Node accessible through manual alias.');
512

    
513
    // Now attempt to create a node that has no pattern (article content type).
514
    // The Pathauto checkbox should not exist.
515
    $this->drupalGet('node/add/article');
516
    $this->assertNoFieldById('edit-path-pathauto');
517
    $this->assertFieldByName('path[alias]', '');
518

    
519
    $edit = array();
520
    $edit['title'] = 'My test article';
521
    $this->drupalPost(NULL, $edit, t('Save'));
522
    $node = $this->drupalGetNodeByTitle($edit['title']);
523

    
524
    // Pathauto checkbox should still not exist.
525
    $this->drupalGet('node/' . $node->nid . '/edit');
526
    $this->assertNoFieldById('edit-path-pathauto');
527
    $this->assertFieldByName('path[alias]', '');
528
    $this->assertNoEntityAlias('node', $node);
529

    
530
    // Set the page pattern to use only tokens so we can test the checkbox
531
    // behavior if none of the tokens have a value currently.
532
    variable_set('pathauto_node_page_pattern', '[node:title]');
533

    
534
    // Create a node with an empty title. The Pathauto checkbox should still be
535
    // visible but unchecked.
536
    $node = $this->drupalCreateNode(array('type' => 'page', 'title' => ''));
537
    $this->drupalGet('node/' . $node->nid . '/edit');
538
    $this->assertNoFieldChecked('edit-path-pathauto');
539
    $this->assertFieldByName('path[alias]', '');
540
    $this->assertNoEntityAlias('node', $node);
541

    
542
    $edit = array();
543
    $edit['title'] = 'Valid title';
544
    $edit['path[pathauto]'] = TRUE;
545
    $this->drupalPost(NULL, $edit, t('Save'));
546
    $this->drupalGet('node/' . $node->nid . '/edit');
547
    $this->assertFieldChecked('edit-path-pathauto');
548
    $this->assertFieldByName('path[alias]', 'valid-title');
549
  }
550

    
551
  /**
552
   * Test node operations.
553
   */
554
  function testNodeOperations() {
555
    $node1 = $this->drupalCreateNode(array('title' => 'node1'));
556
    $node2 = $this->drupalCreateNode(array('title' => 'node2'));
557

    
558
    // Delete all current URL aliases.
559
    $this->deleteAllAliases();
560

    
561
    $edit = array(
562
      'operation' => 'pathauto_update_alias',
563
      "nodes[{$node1->nid}]" => TRUE,
564
    );
565
    $this->drupalPost('admin/content', $edit, t('Update'));
566
    $this->assertText('Updated URL alias for 1 node.');
567

    
568
    $this->assertEntityAlias('node', $node1, 'content/' . $node1->title);
569
    $this->assertEntityAlias('node', $node2, 'node/' . $node2->nid);
570
  }
571

    
572
  /**
573
   * @todo Merge this with existing node test methods?
574
   */
575
  public function testNodeState() {
576
    $nodeNoAliasUser = $this->drupalCreateUser(array('bypass node access'));
577
    $nodeAliasUser = $this->drupalCreateUser(array('bypass node access', 'create url aliases'));
578

    
579
    $node = $this->drupalCreateNode(array(
580
      'title' => 'Node version one',
581
      'type' => 'page',
582
      'path' => array(
583
        'pathauto' => FALSE,
584
      ),
585
    ));
586

    
587
    $this->assertNoEntityAlias('node', $node);
588

    
589
    // Set a manual path alias for the node.
590
    $node->path['alias'] = 'test-alias';
591
    node_save($node);
592

    
593
    // Ensure that the pathauto field was saved to the database.
594
    $node = node_load($node->nid, NULL, TRUE);
595
    $this->assertFalse($node->path['pathauto']);
596

    
597
    // Ensure that the manual path alias was saved and an automatic alias was not generated.
598
    $this->assertEntityAlias('node', $node, 'test-alias');
599
    $this->assertNoEntityAliasExists('node', $node, 'content/node-version-one');
600

    
601
    // Save the node as a user who does not have access to path fieldset.
602
    $this->drupalLogin($nodeNoAliasUser);
603
    $this->drupalGet('node/' . $node->nid . '/edit');
604
    $this->assertNoFieldByName('path[pathauto]');
605

    
606
    $edit = array('title' => 'Node version two');
607
    $this->drupalPost(NULL, $edit, 'Save');
608
    $this->assertText('Basic page Node version two has been updated.');
609

    
610
    $this->assertEntityAlias('node', $node, 'test-alias');
611
    $this->assertNoEntityAliasExists('node', $node, 'content/node-version-one');
612
    $this->assertNoEntityAliasExists('node', $node, 'content/node-version-two');
613

    
614
    // Load the edit node page and check that the Pathauto checkbox is unchecked.
615
    $this->drupalLogin($nodeAliasUser);
616
    $this->drupalGet('node/' . $node->nid . '/edit');
617
    $this->assertNoFieldChecked('edit-path-pathauto');
618

    
619
    // Edit the manual alias and save the node.
620
    $edit = array(
621
      'title' => 'Node version three',
622
      'path[alias]' => 'manually-edited-alias',
623
    );
624
    $this->drupalPost(NULL, $edit, 'Save');
625
    $this->assertText('Basic page Node version three has been updated.');
626

    
627
    $this->assertEntityAlias('node', $node, 'manually-edited-alias');
628
    $this->assertNoEntityAliasExists('node', $node, 'test-alias');
629
    $this->assertNoEntityAliasExists('node', $node, 'content/node-version-one');
630
    $this->assertNoEntityAliasExists('node', $node, 'content/node-version-two');
631
    $this->assertNoEntityAliasExists('node', $node, 'content/node-version-three');
632

    
633
    // Programatically save the node with an automatic alias.
634
    $node = node_load($node->nid, NULL, TRUE);
635
    $node->path['pathauto'] = TRUE;
636
    node_save($node);
637

    
638
    // Ensure that the pathauto field was saved to the database.
639
    $node = node_load($node->nid, NULL, TRUE);
640
    $this->assertTrue($node->path['pathauto']);
641

    
642
    $this->assertEntityAlias('node', $node, 'content/node-version-three');
643
    $this->assertNoEntityAliasExists('node', $node, 'manually-edited-alias');
644
    $this->assertNoEntityAliasExists('node', $node, 'test-alias');
645
    $this->assertNoEntityAliasExists('node', $node, 'content/node-version-one');
646
    $this->assertNoEntityAliasExists('node', $node, 'content/node-version-two');
647
  }
648

    
649
  /**
650
   * Basic functional testing of Pathauto with taxonomy terms.
651
   */
652
  function testTermEditing() {
653
    $this->drupalGet('admin/structure');
654
    $this->drupalGet('admin/structure/taxonomy');
655

    
656
    // Create term for testing.
657
    $name = ' Testing: term name [ ';
658
    $automatic_alias = 'tags/testing-term-name';
659
    $this->drupalPost('admin/structure/taxonomy/tags/add', array('name' => $name), 'Save');
660
    $name = trim($name);
661
    $this->assertText("Created new term $name.");
662
    $term = $this->drupalGetTermByName($name);
663

    
664
    // Look for alias generated in the form.
665
    $this->drupalGet("taxonomy/term/{$term->tid}/edit");
666
    $this->assertFieldChecked('edit-path-pathauto');
667
    $this->assertFieldByName('path[alias]', $automatic_alias, 'Generated alias visible in the path alias field.');
668

    
669
    // Check whether the alias actually works.
670
    $this->drupalGet($automatic_alias);
671
    $this->assertText($name, 'Term accessible through automatic alias.');
672

    
673
    // Manually set the term's alias.
674
    $manual_alias = 'tags/' . $term->tid;
675
    $edit = array(
676
      'path[pathauto]' => FALSE,
677
      'path[alias]' => $manual_alias,
678
    );
679
    $this->drupalPost("taxonomy/term/{$term->tid}/edit", $edit, t('Save'));
680
    $this->assertText("Updated term $name.");
681

    
682
    // Check that the automatic alias checkbox is now unchecked by default.
683
    $this->drupalGet("taxonomy/term/{$term->tid}/edit");
684
    $this->assertNoFieldChecked('edit-path-pathauto');
685
    $this->assertFieldByName('path[alias]', $manual_alias);
686

    
687
    // Submit the term form with the default values.
688
    $this->drupalPost(NULL, array(), t('Save'));
689
    $this->assertText("Updated term $name.");
690

    
691
    // Test that the old (automatic) alias has been deleted and only accessible
692
    // through the new (manual) alias.
693
    $this->drupalGet($automatic_alias);
694
    $this->assertResponse(404, 'Term not accessible through automatic alias.');
695
    $this->drupalGet($manual_alias);
696
    $this->assertText($name, 'Term accessible through manual alias.');
697
  }
698

    
699
  /**
700
   * Basic functional testing of Pathauto with users.
701
   */
702
  function testUserEditing() {
703
    // There should be no Pathauto checkbox on user forms.
704
    $this->drupalGet('user/' . $this->admin_user->uid . '/edit');
705
    $this->assertNoFieldById('edit-path-pathauto');
706
  }
707

    
708
  /**
709
   * Test user operations.
710
   */
711
  function testUserOperations() {
712
    $account = $this->drupalCreateUser();
713

    
714
    // Delete all current URL aliases.
715
    $this->deleteAllAliases();
716

    
717
    $edit = array(
718
      'operation' => 'pathauto_update_alias',
719
      "accounts[{$account->uid}]" => TRUE,
720
    );
721
    $this->drupalPost('admin/people', $edit, t('Update'));
722
    $this->assertText('Updated URL alias for 1 user account.');
723

    
724
    $this->assertEntityAlias('user', $account, 'users/' . drupal_strtolower($account->name));
725
    $this->assertEntityAlias('user', $this->admin_user, 'user/' . $this->admin_user->uid);
726
  }
727

    
728
  function testSettingsValidation() {
729
    $edit = array();
730
    $edit['pathauto_max_length'] = 'abc';
731
    $edit['pathauto_max_component_length'] = 'abc';
732
    $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
733
    $this->assertText('The field Maximum alias length is not a valid number.');
734
    $this->assertText('The field Maximum component length is not a valid number.');
735
    $this->assertNoText('The configuration options have been saved.');
736

    
737
    $edit['pathauto_max_length'] = '0';
738
    $edit['pathauto_max_component_length'] = '0';
739
    $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
740
    $this->assertText('The field Maximum alias length cannot be less than 1.');
741
    $this->assertText('The field Maximum component length cannot be less than 1.');
742
    $this->assertNoText('The configuration options have been saved.');
743

    
744
    $edit['pathauto_max_length'] = '999';
745
    $edit['pathauto_max_component_length'] = '999';
746
    $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
747
    $this->assertText('The field Maximum alias length cannot be greater than 255.');
748
    $this->assertText('The field Maximum component length cannot be greater than 255.');
749
    $this->assertNoText('The configuration options have been saved.');
750

    
751
    $edit['pathauto_max_length'] = '50';
752
    $edit['pathauto_max_component_length'] = '50';
753
    $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
754
    $this->assertText('The configuration options have been saved.');
755
  }
756

    
757
  function testPatternsValidation() {
758
    $edit = array();
759
    $edit['pathauto_node_pattern'] = '[node:title]/[user:name]/[term:name]';
760
    $edit['pathauto_node_page_pattern'] = 'page';
761
    $this->drupalPost('admin/config/search/path/patterns', $edit, 'Save configuration');
762
    $this->assertText('The Default path pattern (applies to all content types with blank patterns below) is using the following invalid tokens: [user:name], [term:name].');
763
    $this->assertText('The Pattern for all Basic page paths cannot contain fewer than one token.');
764
    $this->assertNoText('The configuration options have been saved.');
765

    
766
    $edit['pathauto_node_pattern'] = '[node:title]';
767
    $edit['pathauto_node_page_pattern'] = 'page/[node:title]';
768
    $edit['pathauto_node_article_pattern'] = '';
769
    $this->drupalPost('admin/config/search/path/patterns', $edit, 'Save configuration');
770
    $this->assertText('The configuration options have been saved.');
771
  }
772

    
773
  /**
774
   * Test programmatic entity creation for aliases.
775
   */
776
  function testProgrammaticEntityCreation() {
777
    $node = $this->drupalCreateNode(array('title' => 'Test node', 'path' => array('pathauto' => TRUE)));
778
    $this->assertEntityAlias('node', $node, 'content/test-node');
779

    
780
    $vocabulary = $this->addVocabulary(array('name' => 'Tags'));
781
    $term = $this->addTerm($vocabulary, array('name' => 'Test term', 'path' => array('pathauto' => TRUE)));
782
    $this->assertEntityAlias('taxonomy_term', $term, 'tags/test-term');
783

    
784
    $edit['name'] = 'Test user';
785
    $edit['mail'] = 'test-user@example.com';
786
    $edit['pass']   = user_password();
787
    $edit['path'] = array('pathauto' => TRUE);
788
    $edit['status'] = 1;
789
    $account = user_save(drupal_anonymous_user(), $edit);
790
    $this->assertEntityAlias('user', $account, 'users/test-user');
791
  }
792
}
793

    
794
class PathautoLocaleTestCase extends PathautoFunctionalTestHelper {
795
  public static function getInfo() {
796
    return array(
797
      'name' => 'Pathauto localization tests',
798
      'description' => 'Test pathauto functionality with localization and translation.',
799
      'group' => 'Pathauto',
800
      'dependencies' => array('token'),
801
    );
802
  }
803

    
804
  function setUp(array $modules = array()) {
805
    $modules[] = 'locale';
806
    $modules[] = 'translation';
807
    parent::setUp($modules, array('administer languages'));
808

    
809
    // Add predefined French language and reset the locale cache.
810
    require_once DRUPAL_ROOT . '/includes/locale.inc';
811
    locale_add_language('fr', NULL, NULL, LANGUAGE_LTR, '', 'fr');
812
    drupal_language_initialize();
813
  }
814

    
815
  /**
816
   * Test that when an English node is updated, its old English alias is
817
   * updated and its newer French alias is left intact.
818
   */
819
  function testLanguageAliases() {
820
    $node = array(
821
      'title' => 'English node',
822
      'language' => 'en',
823
      'body' => array('en' => array(array())),
824
      'path' => array(
825
        'alias' => 'english-node',
826
        'pathauto' => FALSE,
827
      ),
828
    );
829
    $node = $this->drupalCreateNode($node);
830
    $english_alias = path_load(array('alias' => 'english-node', 'language' => 'en'));
831
    $this->assertTrue($english_alias, 'Alias created with proper language.');
832

    
833
    // Also save a French alias that should not be left alone, even though
834
    // it is the newer alias.
835
    $this->saveEntityAlias('node', $node, 'french-node', 'fr');
836

    
837
    // Add an alias with the soon-to-be generated alias, causing the upcoming
838
    // alias update to generate a unique alias with the '-0' suffix.
839
    $this->saveAlias('node/invalid', 'content/english-node', LANGUAGE_NONE);
840

    
841
    // Update the node, triggering a change in the English alias.
842
    $node->path['pathauto'] = TRUE;
843
    pathauto_node_update($node);
844

    
845
    // Check that the new English alias replaced the old one.
846
    $this->assertEntityAlias('node', $node, 'content/english-node-0', 'en');
847
    $this->assertEntityAlias('node', $node, 'french-node', 'fr');
848
    $this->assertAliasExists(array('pid' => $english_alias['pid'], 'alias' => 'content/english-node-0'));
849

    
850
    // Create a new node with the same title as before but without
851
    // specifying a language.
852
    $node = $this->drupalCreateNode(array('title' => 'English node'));
853

    
854
    // Check that the new node had a unique alias generated with the '-1'
855
    // suffix.
856
    $this->assertEntityAlias('node', $node, 'content/english-node-1');
857
  }
858
}
859

    
860
/**
861
 * Bulk update functionality tests.
862
 */
863
class PathautoBulkUpdateTestCase extends PathautoFunctionalTestHelper {
864
  private $nodes;
865

    
866
  public static function getInfo() {
867
    return array(
868
      'name' => 'Pathauto bulk updating',
869
      'description' => 'Tests bulk updating of URL aliases.',
870
      'group' => 'Pathauto',
871
      'dependencies' => array('token'),
872
    );
873
  }
874

    
875
  function testBulkUpdate() {
876
    // Create some nodes.
877
    $this->nodes = array();
878
    for ($i = 1; $i <= 5; $i++) {
879
      $node = $this->drupalCreateNode();
880
      $this->nodes[$node->nid] = $node;
881
    }
882

    
883
    // Clear out all aliases.
884
    $this->deleteAllAliases();
885

    
886
    // Bulk create aliases.
887
    $edit = array(
888
      'update[node_pathauto_bulk_update_batch_process]' => TRUE,
889
      'update[user_pathauto_bulk_update_batch_process]' => TRUE,
890
    );
891
    $this->drupalPost('admin/config/search/path/update_bulk', $edit, t('Update'));
892
    $this->assertText('Generated 7 URL aliases.'); // 5 nodes + 2 users
893

    
894
    // Check that aliases have actually been created.
895
    foreach ($this->nodes as $node) {
896
      $this->assertEntityAliasExists('node', $node);
897
    }
898
    $this->assertEntityAliasExists('user', $this->admin_user);
899

    
900
    // Add a new node.
901
    $new_node = $this->drupalCreateNode(array('path' => array('alias' => '', 'pathauto' => FALSE)));
902

    
903
    // Run the update again which should not run against any nodes.
904
    $this->drupalPost('admin/config/search/path/update_bulk', $edit, t('Update'));
905
    $this->assertText('No new URL aliases to generate.');
906

    
907
    $this->assertNoEntityAliasExists('node', $new_node);
908
  }
909
}
910

    
911
/**
912
 * Token functionality tests.
913
 */
914
class PathautoTokenTestCase extends PathautoFunctionalTestHelper {
915
  public static function getInfo() {
916
    return array(
917
      'name' => 'Pathauto tokens',
918
      'description' => 'Tests tokens provided by Pathauto.',
919
      'group' => 'Pathauto',
920
      'dependencies' => array('token'),
921
    );
922
  }
923

    
924
  function testPathautoTokens() {
925
    $array = array(
926
      'test first arg',
927
      'The Array / value',
928
    );
929

    
930
    $tokens = array(
931
      'join-path' => 'test-first-arg/array-value',
932
    );
933
    $data['array'] = $array;
934
    $replacements = $this->assertTokens('array', $data, $tokens);
935

    
936
    // Ensure that the pathauto_clean_token_values() function does not alter
937
    // this token value.
938
    module_load_include('inc', 'pathauto');
939
    pathauto_clean_token_values($replacements, $data, array());
940
    $this->assertEqual($replacements['[array:join-path]'], 'test-first-arg/array-value');
941
  }
942

    
943
  /**
944
   * Function copied from TokenTestHelper::assertTokens().
945
   */
946
  function assertTokens($type, array $data, array $tokens, array $options = array()) {
947
    $input = $this->mapTokenNames($type, array_keys($tokens));
948
    $replacements = token_generate($type, $input, $data, $options);
949
    foreach ($tokens as $name => $expected) {
950
      $token = $input[$name];
951
      if (!isset($expected)) {
952
        $this->assertTrue(!isset($values[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
953
      }
954
      elseif (!isset($replacements[$token])) {
955
        $this->fail(t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
956
      }
957
      elseif (!empty($options['regex'])) {
958
        $this->assertTrue(preg_match('/^' . $expected . '$/', $replacements[$token]), t("Token value for @token was '@actual', matching regular expression pattern '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected)));
959
      }
960
      else {
961
        $this->assertIdentical($replacements[$token], $expected, t("Token value for @token was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected)));
962
      }
963
    }
964

    
965
    return $replacements;
966
  }
967

    
968
  function mapTokenNames($type, array $tokens = array()) {
969
    $return = array();
970
    foreach ($tokens as $token) {
971
      $return[$token] = "[$type:$token]";
972
    }
973
    return $return;
974
  }
975
}