Projet

Général

Profil

Paste
Télécharger (30,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / pathauto / pathauto.test @ 384fc62a

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) {
59
    $uri = entity_uri($entity_type, $entity);
60
    $this->assertNoAliasExists(array('source' => $uri['path']));
61
  }
62

    
63
  function assertAlias($source, $expected_alias, $language = LANGUAGE_NONE) {
64
    drupal_clear_path_cache($source);
65
    $alias = drupal_get_path_alias($source, $language);
66
    $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)));
67
  }
68

    
69
  function assertAliasExists($conditions) {
70
    $path = path_load($conditions);
71
    $this->assertTrue($path, t('Alias with conditions @conditions found.', array('@conditions' => var_export($conditions, TRUE))));
72
    return $path;
73
  }
74

    
75
  function assertNoAliasExists($conditions) {
76
    $alias = path_load($conditions);
77
    $this->assertFalse($alias, t('Alias with conditions @conditions not found.', array('@conditions' => var_export($conditions, TRUE))));
78
  }
79

    
80
  function deleteAllAliases() {
81
    db_delete('url_alias')->execute();
82
    drupal_clear_path_cache();
83
  }
84

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

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

    
108
  function assertEntityPattern($entity_type, $bundle, $language = LANGUAGE_NONE, $expected) {
109
    drupal_static_reset('pathauto_pattern_load_by_entity');
110
    $this->refreshVariables();
111
    $pattern = pathauto_pattern_load_by_entity($entity_type, $bundle, $language);
112
    $this->assertIdentical($expected, $pattern);
113
  }
114

    
115
  function drupalGetTermByName($name, $reset = FALSE) {
116
    $terms = entity_load('taxonomy_term', array(), array('name' => $name), $reset);
117
    return !empty($terms) ? reset($terms) : FALSE;
118
  }
119
}
120

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

    
134
  function setUp(array $modules = array()) {
135
    parent::setUp($modules);
136
    module_load_include('inc', 'pathauto');
137
  }
138

    
139
  /**
140
   * Test _pathauto_get_schema_alias_maxlength().
141
   */
142
  function testGetSchemaAliasMaxLength() {
143
    $this->assertIdentical(_pathauto_get_schema_alias_maxlength(), 255);
144
  }
145

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

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

    
169
  /**
170
   * Test pathauto_cleanstring().
171
   */
172
  function testCleanString() {
173
    $tests = array();
174
    variable_set('pathauto_ignore_words', ', in, is,that, the  , this, with, ');
175
    variable_set('pathauto_max_component_length', 35);
176

    
177
    // Test the 'ignored words' removal.
178
    $tests['this'] = 'this';
179
    $tests['this with that'] = 'this-with-that';
180
    $tests['this thing with that thing'] = 'thing-thing';
181

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

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

    
189
    foreach ($tests as $input => $expected) {
190
      $output = pathauto_cleanstring($input);
191
      $this->assertEqual($output, $expected, t("pathauto_cleanstring('@input') expected '@expected', actual '@output'", array('@input' => $input, '@expected' => $expected, '@output' => $output)));
192
    }
193
  }
194

    
195
  /**
196
   * Test pathauto_path_delete_multiple().
197
   */
198
  function testPathDeleteMultiple() {
199
    $this->saveAlias('node/1', 'node-1-alias');
200
    $this->saveAlias('node/1/view', 'node-1-alias/view');
201
    $this->saveAlias('node/1', 'node-1-alias-en', 'en');
202
    $this->saveAlias('node/1', 'node-1-alias-fr', 'fr');
203
    $this->saveAlias('node/2', 'node-2-alias');
204

    
205
    pathauto_path_delete_all('node/1');
206
    $this->assertNoAliasExists(array('source' => "node/1"));
207
    $this->assertNoAliasExists(array('source' => "node/1/view"));
208
    $this->assertAliasExists(array('source' => "node/2"));
209
  }
210

    
211
  /**
212
   * Test the different update actions in pathauto_create_alias().
213
   */
214
  function testUpdateActions() {
215
    // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'insert'.
216
    variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_NO_NEW);
217
    $node = $this->drupalCreateNode(array('title' => 'First title'));
218
    $this->assertEntityAlias('node', $node, 'content/first-title');
219

    
220
    // Default action is PATHAUTO_UPDATE_ACTION_DELETE.
221
    variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE);
222
    $node->title = 'Second title';
223
    pathauto_node_update($node);
224
    $this->assertEntityAlias('node', $node, 'content/second-title');
225
    $this->assertNoAliasExists(array('alias' => 'content/first-title'));
226

    
227
    // Test PATHAUTO_UPDATE_ACTION_LEAVE
228
    variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_LEAVE);
229
    $node->title = 'Third title';
230
    pathauto_node_update($node);
231
    $this->assertEntityAlias('node', $node, 'content/third-title');
232
    $this->assertAliasExists(array('source' => "node/{$node->nid}", 'alias' => 'content/second-title'));
233

    
234
    variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE);
235
    $node->title = 'Fourth title';
236
    pathauto_node_update($node);
237
    $this->assertEntityAlias('node', $node, 'content/fourth-title');
238
    $this->assertNoAliasExists(array('alias' => 'content/third-title'));
239
    // The older second alias is not deleted yet.
240
    $older_path = $this->assertAliasExists(array('source' => "node/{$node->nid}", 'alias' => 'content/second-title'));
241
    path_delete($older_path);
242

    
243
    variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_NO_NEW);
244
    $node->title = 'Fifth title';
245
    pathauto_node_update($node);
246
    $this->assertEntityAlias('node', $node, 'content/fourth-title');
247
    $this->assertNoAliasExists(array('alias' => 'content/fith-title'));
248

    
249
    // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'update'.
250
    $this->deleteAllAliases();
251
    pathauto_node_update($node);
252
    $this->assertEntityAlias('node', $node, 'content/fifth-title');
253

    
254
    // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'bulkupdate'.
255
    $this->deleteAllAliases();
256
    $node->title = 'Sixth title';
257
    pathauto_node_update_alias($node, 'bulkupdate');
258
    $this->assertEntityAlias('node', $node, 'content/sixth-title');
259
  }
260

    
261
  /**
262
   * Test that pathauto_create_alias() will not create an alias for a pattern
263
   * that does not get any tokens replaced.
264
   */
265
  function testNoTokensNoAlias() {
266
    $node = $this->drupalCreateNode(array('title' => ''));
267
    $this->assertNoEntityAliasExists('node', $node);
268

    
269
    $node->title = 'hello';
270
    pathauto_node_update($node);
271
    $this->assertEntityAlias('node', $node, 'content/hello');
272
  }
273

    
274
  /**
275
   * Test the handling of path vs non-path tokens in pathauto_clean_token_values().
276
   */
277
  function testPathTokens() {
278
    variable_set('pathauto_taxonomy_term_pattern', '[term:parent:url:path]/[term:name]');
279
    $vocab = $this->addVocabulary();
280

    
281
    $term1 = $this->addTerm($vocab, array('name' => 'Parent term'));
282
    $this->assertEntityAlias('taxonomy_term', $term1, 'parent-term');
283

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

    
287
    $this->saveEntityAlias('taxonomy_term', $term1, 'My Crazy/Alias/');
288
    pathauto_taxonomy_term_update($term2);
289
    $this->assertEntityAlias('taxonomy_term', $term2, 'My Crazy/Alias/child-term');
290
  }
291

    
292
  function testEntityBundleRenamingDeleting() {
293
    // Create a vocabulary and test that it's pattern variable works.
294
    $vocab = $this->addVocabulary(array('machine_name' => 'old_name'));
295
    variable_set('pathauto_taxonomy_term_pattern', 'base');
296
    variable_set("pathauto_taxonomy_term_old_name_pattern", 'bundle');
297
    $this->assertEntityPattern('taxonomy_term', 'old_name', LANGUAGE_NONE, 'bundle');
298

    
299
    // Rename the vocabulary's machine name, which should cause its pattern
300
    // variable to also be renamed.
301
    $vocab->machine_name = 'new_name';
302
    taxonomy_vocabulary_save($vocab);
303
    $this->assertEntityPattern('taxonomy_term', 'new_name', LANGUAGE_NONE, 'bundle');
304
    $this->assertEntityPattern('taxonomy_term', 'old_name', LANGUAGE_NONE, 'base');
305

    
306
    // Delete the vocabulary, which should cause its pattern variable to also
307
    // be deleted.
308
    taxonomy_vocabulary_delete($vocab->vid);
309
    $this->assertEntityPattern('taxonomy_term', 'new_name', LANGUAGE_NONE, 'base');
310
  }
311

    
312
  function testNoExistingPathAliases() {
313
    variable_set('pathauto_node_page_pattern', '[node:title]');
314
    variable_set('pathauto_punctuation_period', PATHAUTO_PUNCTUATION_DO_NOTHING);
315

    
316
    // Check that Pathauto does not create an alias of '/admin'.
317
    $node = $this->drupalCreateNode(array('title' => 'Admin', 'type' => 'page'));
318
    $this->assertNoEntityAlias('node', $node);
319

    
320
    // Check that Pathauto does not create an alias of '/modules'.
321
    $node->title = 'Modules';
322
    node_save($node);
323
    $this->assertNoEntityAlias('node', $node);
324

    
325
    // Check that Pathauto does not create an alias of '/index.php'.
326
    $node->title = 'index.php';
327
    node_save($node);
328
    $this->assertNoEntityAlias('node', $node);
329

    
330
    // Check that a safe value gets an automatic alias. This is also a control
331
    // to ensure the above tests work properly.
332
    $node->title = 'Safe value';
333
    node_save($node);
334
    $this->assertEntityAlias('node', $node, 'safe-value');
335
  }
336
}
337

    
338
/**
339
 * Helper test class with some added functions for testing.
340
 */
341
class PathautoFunctionalTestHelper extends PathautoTestHelper {
342
  protected $admin_user;
343

    
344
  function setUp(array $modules = array()) {
345
    parent::setUp($modules);
346

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

    
350
    // Allow other modules to add additional permissions for the admin user.
351
    $permissions = array(
352
      'administer pathauto',
353
      'administer url aliases',
354
      'create url aliases',
355
      'administer nodes',
356
      'bypass node access',
357
      'access content overview',
358
      'administer taxonomy',
359
      'administer users',
360
    );
361
    $args = func_get_args();
362
    if (isset($args[1]) && is_array($args[1])) {
363
      $permissions = array_merge($permissions, $args[1]);
364
    }
365
    $this->admin_user = $this->drupalCreateUser($permissions);
366

    
367
    $this->drupalLogin($this->admin_user);
368
  }
369
}
370

    
371
/**
372
 * Test basic pathauto functionality.
373
 */
374
class PathautoFunctionalTestCase extends PathautoFunctionalTestHelper {
375
  public static function getInfo() {
376
    return array(
377
      'name' => 'Pathauto basic tests',
378
      'description' => 'Test basic pathauto functionality.',
379
      'group' => 'Pathauto',
380
      'dependencies' => array('token'),
381
    );
382
  }
383

    
384
  /**
385
   * Basic functional testing of Pathauto.
386
   */
387
  function testNodeEditing() {
388
    // Delete the default node pattern. Only the page content type will have a pattern.
389
    variable_del('pathauto_node_pattern');
390

    
391
    // Ensure that the Pathauto checkbox is checked by default on the node add form.
392
    $this->drupalGet('node/add/page');
393
    $this->assertFieldChecked('edit-path-pathauto');
394

    
395
    // Create node for testing by previewing and saving the node form.
396
    $title = ' Testing: node title [';
397
    $automatic_alias = 'content/testing-node-title';
398
    $this->drupalPost(NULL, array('title' => $title), 'Preview');
399
    $this->drupalPost(NULL, array(), 'Save');
400
    $node = $this->drupalGetNodeByTitle($title);
401

    
402
    // Look for alias generated in the form.
403
    $this->drupalGet("node/{$node->nid}/edit");
404
    $this->assertFieldChecked('edit-path-pathauto');
405
    $this->assertFieldByName('path[alias]', $automatic_alias, 'Generated alias visible in the path alias field.');
406

    
407
    // Check whether the alias actually works.
408
    $this->drupalGet($automatic_alias);
409
    $this->assertText($title, 'Node accessible through automatic alias.');
410

    
411
    // Manually set the node's alias.
412
    $manual_alias = 'content/' . $node->nid;
413
    $edit = array(
414
      'path[pathauto]' => FALSE,
415
      'path[alias]' => $manual_alias,
416
    );
417
    $this->drupalPost("node/{$node->nid}/edit", $edit, t('Save'));
418
    $this->assertText("Basic page $title has been updated.");
419

    
420
    // Check that the automatic alias checkbox is now unchecked by default.
421
    $this->drupalGet("node/{$node->nid}/edit");
422
    $this->assertNoFieldChecked('edit-path-pathauto');
423
    $this->assertFieldByName('path[alias]', $manual_alias);
424

    
425
    // Submit the node form with the default values.
426
    $this->drupalPost(NULL, array(), t('Save'));
427
    $this->assertText("Basic page $title has been updated.");
428

    
429
    // Test that the old (automatic) alias has been deleted and only accessible
430
    // through the new (manual) alias.
431
    $this->drupalGet($automatic_alias);
432
    $this->assertResponse(404, 'Node not accessible through automatic alias.');
433
    $this->drupalGet($manual_alias);
434
    $this->assertText($title, 'Node accessible through manual alias.');
435

    
436
    // Now attempt to create a node that has no pattern (article content type).
437
    // The Pathauto checkbox should not exist.
438
    $this->drupalGet('node/add/article');
439
    $this->assertNoFieldById('edit-path-pathauto');
440
    $this->assertFieldByName('path[alias]', '');
441

    
442
    $edit = array();
443
    $edit['title'] = 'My test article';
444
    $this->drupalPost(NULL, $edit, t('Save'));
445
    $node = $this->drupalGetNodeByTitle($edit['title']);
446

    
447
    // Pathauto checkbox should still not exist.
448
    $this->drupalGet('node/' . $node->nid . '/edit');
449
    $this->assertNoFieldById('edit-path-pathauto');
450
    $this->assertFieldByName('path[alias]', '');
451
    $this->assertNoEntityAlias('node', $node);
452
  }
453

    
454
  /**
455
   * Test node operations.
456
   */
457
  function testNodeOperations() {
458
    $node1 = $this->drupalCreateNode(array('title' => 'node1'));
459
    $node2 = $this->drupalCreateNode(array('title' => 'node2'));
460

    
461
    // Delete all current URL aliases.
462
    $this->deleteAllAliases();
463

    
464
    $edit = array(
465
      'operation' => 'pathauto_update_alias',
466
      "nodes[{$node1->nid}]" => TRUE,
467
    );
468
    $this->drupalPost('admin/content', $edit, t('Update'));
469
    $this->assertText('Updated URL alias for 1 node.');
470

    
471
    $this->assertEntityAlias('node', $node1, 'content/' . $node1->title);
472
    $this->assertEntityAlias('node', $node2, 'node/' . $node2->nid);
473
  }
474

    
475
  /**
476
   * Basic functional testing of Pathauto with taxonomy terms.
477
   */
478
  function testTermEditing() {
479
    $this->drupalGet('admin/structure');
480
    $this->drupalGet('admin/structure/taxonomy');
481

    
482
    // Create term for testing.
483
    $name = ' Testing: term name [ ';
484
    $automatic_alias = 'tags/testing-term-name';
485
    $this->drupalPost('admin/structure/taxonomy/tags/add', array('name' => $name), 'Save');
486
    $name = trim($name);
487
    $this->assertText("Created new term $name.");
488
    $term = $this->drupalGetTermByName($name);
489

    
490
    // Look for alias generated in the form.
491
    $this->drupalGet("taxonomy/term/{$term->tid}/edit");
492
    $this->assertFieldChecked('edit-path-pathauto');
493
    $this->assertFieldByName('path[alias]', $automatic_alias, 'Generated alias visible in the path alias field.');
494

    
495
    // Check whether the alias actually works.
496
    $this->drupalGet($automatic_alias);
497
    $this->assertText($name, 'Term accessible through automatic alias.');
498

    
499
    // Manually set the term's alias.
500
    $manual_alias = 'tags/' . $term->tid;
501
    $edit = array(
502
      'path[pathauto]' => FALSE,
503
      'path[alias]' => $manual_alias,
504
    );
505
    $this->drupalPost("taxonomy/term/{$term->tid}/edit", $edit, t('Save'));
506
    $this->assertText("Updated term $name.");
507

    
508
    // Check that the automatic alias checkbox is now unchecked by default.
509
    $this->drupalGet("taxonomy/term/{$term->tid}/edit");
510
    $this->assertNoFieldChecked('edit-path-pathauto');
511
    $this->assertFieldByName('path[alias]', $manual_alias);
512

    
513
    // Submit the term form with the default values.
514
    $this->drupalPost(NULL, array(), t('Save'));
515
    $this->assertText("Updated term $name.");
516

    
517
    // Test that the old (automatic) alias has been deleted and only accessible
518
    // through the new (manual) alias.
519
    $this->drupalGet($automatic_alias);
520
    $this->assertResponse(404, 'Term not accessible through automatic alias.');
521
    $this->drupalGet($manual_alias);
522
    $this->assertText($name, 'Term accessible through manual alias.');
523
  }
524

    
525
  /**
526
   * Basic functional testing of Pathauto with users.
527
   */
528
  function testUserEditing() {
529
    // There should be no Pathauto checkbox on user forms.
530
    $this->drupalGet('user/' . $this->admin_user->uid . '/edit');
531
    $this->assertNoFieldById('edit-path-pathauto');
532
  }
533

    
534
  /**
535
   * Test user operations.
536
   */
537
  function testUserOperations() {
538
    $account = $this->drupalCreateUser();
539

    
540
    // Delete all current URL aliases.
541
    $this->deleteAllAliases();
542

    
543
    $edit = array(
544
      'operation' => 'pathauto_update_alias',
545
      "accounts[{$account->uid}]" => TRUE,
546
    );
547
    $this->drupalPost('admin/people', $edit, t('Update'));
548
    $this->assertText('Updated URL alias for 1 user account.');
549

    
550
    $this->assertEntityAlias('user', $account, 'users/' . drupal_strtolower($account->name));
551
    $this->assertEntityAlias('user', $this->admin_user, 'user/' . $this->admin_user->uid);
552
  }
553

    
554
  function testSettingsValidation() {
555
    $edit = array();
556
    $edit['pathauto_max_length'] = 'abc';
557
    $edit['pathauto_max_component_length'] = 'abc';
558
    $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
559
    $this->assertText('The field Maximum alias length is not a valid number.');
560
    $this->assertText('The field Maximum component length is not a valid number.');
561
    $this->assertNoText('The configuration options have been saved.');
562

    
563
    $edit['pathauto_max_length'] = '0';
564
    $edit['pathauto_max_component_length'] = '0';
565
    $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
566
    $this->assertText('The field Maximum alias length cannot be less than 1.');
567
    $this->assertText('The field Maximum component length cannot be less than 1.');
568
    $this->assertNoText('The configuration options have been saved.');
569

    
570
    $edit['pathauto_max_length'] = '999';
571
    $edit['pathauto_max_component_length'] = '999';
572
    $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
573
    $this->assertText('The field Maximum alias length cannot be greater than 255.');
574
    $this->assertText('The field Maximum component length cannot be greater than 255.');
575
    $this->assertNoText('The configuration options have been saved.');
576

    
577
    $edit['pathauto_max_length'] = '50';
578
    $edit['pathauto_max_component_length'] = '50';
579
    $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
580
    $this->assertText('The configuration options have been saved.');
581
  }
582

    
583
  function testPatternsValidation() {
584
    $edit = array();
585
    $edit['pathauto_node_pattern'] = '[node:title]/[user:name]/[term:name]';
586
    $edit['pathauto_node_page_pattern'] = 'page';
587
    $this->drupalPost('admin/config/search/path/patterns', $edit, 'Save configuration');
588
    $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].');
589
    $this->assertText('The Pattern for all Basic page paths cannot contain fewer than one token.');
590
    $this->assertNoText('The configuration options have been saved.');
591

    
592
    $edit['pathauto_node_pattern'] = '[node:title]';
593
    $edit['pathauto_node_page_pattern'] = 'page/[node:title]';
594
    $edit['pathauto_node_article_pattern'] = '';
595
    $this->drupalPost('admin/config/search/path/patterns', $edit, 'Save configuration');
596
    $this->assertText('The configuration options have been saved.');
597
  }
598

    
599
  /**
600
   * Test programmatic entity creation for aliases.
601
   */
602
  function testProgrammaticEntityCreation() {
603
    $node = $this->drupalCreateNode(array('title' => 'Test node', 'path' => array('pathauto' => TRUE)));
604
    $this->assertEntityAlias('node', $node, 'content/test-node');
605

    
606
    $vocabulary = $this->addVocabulary(array('name' => 'Tags'));
607
    $term = $this->addTerm($vocabulary, array('name' => 'Test term', 'path' => array('pathauto' => TRUE)));
608
    $this->assertEntityAlias('taxonomy_term', $term, 'tags/test-term');
609

    
610
    $edit['name'] = 'Test user';
611
    $edit['mail'] = 'test-user@example.com';
612
    $edit['pass']   = user_password();
613
    $edit['path'] = array('pathauto' => TRUE);
614
    $edit['status'] = 1;
615
    $account = user_save(drupal_anonymous_user(), $edit);
616
    $this->assertEntityAlias('user', $account, 'users/test-user');
617
  }
618
}
619

    
620
class PathautoLocaleTestCase extends PathautoFunctionalTestHelper {
621
  public static function getInfo() {
622
    return array(
623
      'name' => 'Pathauto localization tests',
624
      'description' => 'Test pathauto functionality with localization and translation.',
625
      'group' => 'Pathauto',
626
      'dependencies' => array('token'),
627
    );
628
  }
629

    
630
  function setUp(array $modules = array()) {
631
    $modules[] = 'locale';
632
    $modules[] = 'translation';
633
    parent::setUp($modules, array('administer languages'));
634

    
635
    // Add predefined French language and reset the locale cache.
636
    require_once DRUPAL_ROOT . '/includes/locale.inc';
637
    locale_add_language('fr', NULL, NULL, LANGUAGE_LTR, '', 'fr');
638
    drupal_language_initialize();
639
  }
640

    
641
  /**
642
   * Test that when an English node is updated, its old English alias is
643
   * updated and its newer French alias is left intact.
644
   */
645
  function testLanguageAliases() {
646
    $node = array(
647
      'title' => 'English node',
648
      'language' => 'en',
649
      'body' => array('en' => array(array())),
650
      'path' => array(
651
        'alias' => 'english-node',
652
        'pathauto' => FALSE,
653
      ),
654
    );
655
    $node = $this->drupalCreateNode($node);
656
    $english_alias = path_load(array('alias' => 'english-node', 'language' => 'en'));
657
    $this->assertTrue($english_alias, 'Alias created with proper language.');
658

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

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

    
667
    // Update the node, triggering a change in the English alias.
668
    $node->path['pathauto'] = TRUE;
669
    pathauto_node_update($node);
670

    
671
    // Check that the new English alias replaced the old one.
672
    $this->assertEntityAlias('node', $node, 'content/english-node-0', 'en');
673
    $this->assertEntityAlias('node', $node, 'french-node', 'fr');
674
    $this->assertAliasExists(array('pid' => $english_alias['pid'], 'alias' => 'content/english-node-0'));
675
  }
676
}
677

    
678
/**
679
 * Bulk update functionality tests.
680
 */
681
class PathautoBulkUpdateTestCase extends PathautoFunctionalTestHelper {
682
  private $nodes;
683

    
684
  public static function getInfo() {
685
    return array(
686
      'name' => 'Pathauto bulk updating',
687
      'description' => 'Tests bulk updating of URL aliases.',
688
      'group' => 'Pathauto',
689
      'dependencies' => array('token'),
690
    );
691
  }
692

    
693
  function testBulkUpdate() {
694
    // Create some nodes.
695
    $this->nodes = array();
696
    for ($i = 1; $i <= 5; $i++) {
697
      $node = $this->drupalCreateNode();
698
      $this->nodes[$node->nid] = $node;
699
    }
700

    
701
    // Clear out all aliases.
702
    $this->deleteAllAliases();
703

    
704
    // Bulk create aliases.
705
    $edit = array(
706
      'update[node_pathauto_bulk_update_batch_process]' => TRUE,
707
      'update[user_pathauto_bulk_update_batch_process]' => TRUE,
708
    );
709
    $this->drupalPost('admin/config/search/path/update_bulk', $edit, t('Update'));
710
    $this->assertText('Generated 7 URL aliases.'); // 5 nodes + 2 users
711

    
712
    // Check that aliases have actually been created.
713
    foreach ($this->nodes as $node) {
714
      $this->assertEntityAliasExists('node', $node);
715
    }
716
    $this->assertEntityAliasExists('user', $this->admin_user);
717

    
718
    // Add a new node.
719
    $new_node = $this->drupalCreateNode(array('path' => array('alias' => '', 'pathauto' => FALSE)));
720

    
721
    // Run the update again which should only run against the new node.
722
    $this->drupalPost('admin/config/search/path/update_bulk', $edit, t('Update'));
723
    $this->assertText('Generated 1 URL alias.'); // 1 node + 0 users
724

    
725
    $this->assertEntityAliasExists('node', $new_node);
726
  }
727
}
728

    
729
/**
730
 * Token functionality tests.
731
 */
732
class PathautoTokenTestCase extends PathautoFunctionalTestHelper {
733
  public static function getInfo() {
734
    return array(
735
      'name' => 'Pathauto tokens',
736
      'description' => 'Tests tokens provided by Pathauto.',
737
      'group' => 'Pathauto',
738
      'dependencies' => array('token'),
739
    );
740
  }
741

    
742
  function testPathautoTokens() {
743
    $array = array(
744
      'test first arg',
745
      'The Array / value',
746
    );
747

    
748
    $tokens = array(
749
      'join-path' => 'test-first-arg/array-value',
750
    );
751
    $data['array'] = $array;
752
    $replacements = $this->assertTokens('array', $data, $tokens);
753

    
754
    // Ensure that the pathauto_clean_token_values() function does not alter
755
    // this token value.
756
    module_load_include('inc', 'pathauto');
757
    pathauto_clean_token_values($replacements, $data, array());
758
    $this->assertEqual($replacements['[array:join-path]'], 'test-first-arg/array-value');
759
  }
760

    
761
  /**
762
   * Function copied from TokenTestHelper::assertTokens().
763
   */
764
  function assertTokens($type, array $data, array $tokens, array $options = array()) {
765
    $input = $this->mapTokenNames($type, array_keys($tokens));
766
    $replacements = token_generate($type, $input, $data, $options);
767
    foreach ($tokens as $name => $expected) {
768
      $token = $input[$name];
769
      if (!isset($expected)) {
770
        $this->assertTrue(!isset($values[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
771
      }
772
      elseif (!isset($replacements[$token])) {
773
        $this->fail(t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
774
      }
775
      elseif (!empty($options['regex'])) {
776
        $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)));
777
      }
778
      else {
779
        $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)));
780
      }
781
    }
782

    
783
    return $replacements;
784
  }
785

    
786
  function mapTokenNames($type, array $tokens = array()) {
787
    $return = array();
788
    foreach ($tokens as $token) {
789
      $return[$token] = "[$type:$token]";
790
    }
791
    return $return;
792
  }
793
}