[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/pathauto/ -> pathauto.test (source)

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7