pathauto.test 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. <?php
  2. /**
  3. * @file
  4. * Functionality tests for Pathauto.
  5. *
  6. * @ingroup pathauto
  7. */
  8. /**
  9. * Helper test class with some added functions for testing.
  10. */
  11. class PathautoTestHelper extends DrupalWebTestCase {
  12. function setUp(array $modules = array()) {
  13. $modules[] = 'path';
  14. $modules[] = 'token';
  15. $modules[] = 'pathauto';
  16. $modules[] = 'taxonomy';
  17. parent::setUp($modules);
  18. }
  19. function assertToken($type, $object, $token, $expected) {
  20. $tokens = token_generate($type, array($token => $token), array($type => $object));
  21. $tokens += array($token => '');
  22. $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)));
  23. }
  24. function saveAlias($source, $alias, $language = LANGUAGE_NONE) {
  25. $alias = array(
  26. 'source' => $source,
  27. 'alias' => $alias,
  28. 'language' => $language,
  29. );
  30. path_save($alias);
  31. return $alias;
  32. }
  33. function saveEntityAlias($entity_type, $entity, $alias, $language = LANGUAGE_NONE) {
  34. $uri = entity_uri($entity_type, $entity);
  35. return $this->saveAlias($uri['path'], $alias, $language);
  36. }
  37. function assertEntityAlias($entity_type, $entity, $expected_alias, $language = LANGUAGE_NONE) {
  38. $uri = entity_uri($entity_type, $entity);
  39. $this->assertAlias($uri['path'], $expected_alias, $language);
  40. }
  41. function assertEntityAliasExists($entity_type, $entity) {
  42. $uri = entity_uri($entity_type, $entity);
  43. return $this->assertAliasExists(array('source' => $uri['path']));
  44. }
  45. function assertNoEntityAlias($entity_type, $entity, $language = LANGUAGE_NONE) {
  46. $uri = entity_uri($entity_type, $entity);
  47. $this->assertEntityAlias($entity_type, $entity, $uri['path'], $language);
  48. }
  49. function assertNoEntityAliasExists($entity_type, $entity) {
  50. $uri = entity_uri($entity_type, $entity);
  51. $this->assertNoAliasExists(array('source' => $uri['path']));
  52. }
  53. function assertAlias($source, $expected_alias, $language = LANGUAGE_NONE) {
  54. drupal_clear_path_cache($source);
  55. $alias = drupal_get_path_alias($source, $language);
  56. $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)));
  57. }
  58. function assertAliasExists($conditions) {
  59. $path = path_load($conditions);
  60. $this->assertTrue($path, t('Alias with conditions @conditions found.', array('@conditions' => var_export($conditions, TRUE))));
  61. return $path;
  62. }
  63. function assertNoAliasExists($conditions) {
  64. $alias = path_load($conditions);
  65. $this->assertFalse($alias, t('Alias with conditions @conditions not found.', array('@conditions' => var_export($conditions, TRUE))));
  66. }
  67. function deleteAllAliases() {
  68. db_delete('url_alias')->execute();
  69. drupal_clear_path_cache();
  70. }
  71. function addVocabulary(array $vocabulary = array()) {
  72. $name = drupal_strtolower($this->randomName(5));
  73. $vocabulary += array(
  74. 'name' => $name,
  75. 'machine_name' => $name,
  76. 'nodes' => array('article' => 'article'),
  77. );
  78. $vocabulary = (object) $vocabulary;
  79. taxonomy_vocabulary_save($vocabulary);
  80. return $vocabulary;
  81. }
  82. function addTerm(stdClass $vocabulary, array $term = array()) {
  83. $term += array(
  84. 'name' => drupal_strtolower($this->randomName(5)),
  85. 'vocabulary_machine_name' => $vocabulary->machine_name,
  86. 'vid' => $vocabulary->vid,
  87. );
  88. $term = (object) $term;
  89. taxonomy_term_save($term);
  90. return $term;
  91. }
  92. function assertEntityPattern($entity_type, $bundle, $language = LANGUAGE_NONE, $expected) {
  93. drupal_static_reset('pathauto_pattern_load_by_entity');
  94. $this->refreshVariables();
  95. $pattern = pathauto_pattern_load_by_entity($entity_type, $bundle, $language);
  96. $this->assertIdentical($expected, $pattern);
  97. }
  98. function drupalGetTermByName($name, $reset = FALSE) {
  99. $terms = entity_load('taxonomy_term', array(), array('name' => $name), $reset);
  100. return !empty($terms) ? reset($terms) : FALSE;
  101. }
  102. }
  103. /**
  104. * Unit tests for Pathauto functions.
  105. */
  106. class PathautoUnitTestCase extends PathautoTestHelper {
  107. public static function getInfo() {
  108. return array(
  109. 'name' => 'Pathauto unit tests',
  110. 'description' => 'Unit tests for Pathauto functions.',
  111. 'group' => 'Pathauto',
  112. 'dependencies' => array('token'),
  113. );
  114. }
  115. function setUp(array $modules = array()) {
  116. parent::setUp($modules);
  117. module_load_include('inc', 'pathauto');
  118. }
  119. /**
  120. * Test _pathauto_get_schema_alias_maxlength().
  121. */
  122. function testGetSchemaAliasMaxLength() {
  123. $this->assertIdentical(_pathauto_get_schema_alias_maxlength(), 255);
  124. }
  125. /**
  126. * Test pathauto_pattern_load_by_entity().
  127. */
  128. function testPatternLoadByEntity() {
  129. variable_set('pathauto_node_story_en_pattern', ' story/en/[node:title] ');
  130. variable_set('pathauto_node_story_pattern', 'story/[node:title]');
  131. variable_set('pathauto_node_pattern', 'content/[node:title]');
  132. variable_set('pathauto_user_pattern', 'users/[user:name]');
  133. $tests = array(
  134. array('entity' => 'node', 'bundle' => 'story', 'language' => 'fr', 'expected' => 'story/[node:title]'),
  135. array('entity' => 'node', 'bundle' => 'story', 'language' => 'en', 'expected' => 'story/en/[node:title]'),
  136. array('entity' => 'node', 'bundle' => 'story', 'language' => LANGUAGE_NONE, 'expected' => 'story/[node:title]'),
  137. array('entity' => 'node', 'bundle' => 'page', 'language' => 'en', 'expected' => 'content/[node:title]'),
  138. array('entity' => 'user', 'bundle' => 'user', 'language' => LANGUAGE_NONE, 'expected' => 'users/[user:name]'),
  139. array('entity' => 'invalid-entity', 'bundle' => '', 'language' => LANGUAGE_NONE, 'expected' => ''),
  140. );
  141. foreach ($tests as $test) {
  142. $actual = pathauto_pattern_load_by_entity($test['entity'], $test['bundle'], $test['language']);
  143. $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'])));
  144. }
  145. }
  146. /**
  147. * Test pathauto_cleanstring().
  148. */
  149. function testCleanString() {
  150. $tests = array();
  151. variable_set('pathauto_ignore_words', ', in, is,that, the , this, with, ');
  152. variable_set('pathauto_max_component_length', 35);
  153. // Test the 'ignored words' removal.
  154. $tests['this'] = 'this';
  155. $tests['this with that'] = 'this-with-that';
  156. $tests['this thing with that thing'] = 'thing-thing';
  157. // Test length truncation and duplicate separator removal.
  158. $tests[' - Pathauto is the greatest - module ever in Drupal history - '] = 'pathauto-greatest-module-ever';
  159. // Test that HTML tags are removed.
  160. $tests['This <span class="text">text</span> has <br /><a href="http://example.com"><strong>HTML tags</strong></a>.'] = 'text-has-html-tags';
  161. $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';
  162. foreach ($tests as $input => $expected) {
  163. $output = pathauto_cleanstring($input);
  164. $this->assertEqual($output, $expected, t("pathauto_cleanstring('@input') expected '@expected', actual '@output'", array('@input' => $input, '@expected' => $expected, '@output' => $output)));
  165. }
  166. }
  167. /**
  168. * Test pathauto_path_delete_multiple().
  169. */
  170. function testPathDeleteMultiple() {
  171. $this->saveAlias('node/1', 'node-1-alias');
  172. $this->saveAlias('node/1/view', 'node-1-alias/view');
  173. $this->saveAlias('node/1', 'node-1-alias-en', 'en');
  174. $this->saveAlias('node/1', 'node-1-alias-fr', 'fr');
  175. $this->saveAlias('node/2', 'node-2-alias');
  176. pathauto_path_delete_all('node/1');
  177. $this->assertNoAliasExists(array('source' => "node/1"));
  178. $this->assertNoAliasExists(array('source' => "node/1/view"));
  179. $this->assertAliasExists(array('source' => "node/2"));
  180. }
  181. /**
  182. * Test the different update actions in pathauto_create_alias().
  183. */
  184. function testUpdateActions() {
  185. // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'insert'.
  186. variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_NO_NEW);
  187. $node = $this->drupalCreateNode(array('title' => 'First title'));
  188. $this->assertEntityAlias('node', $node, 'content/first-title');
  189. // Default action is PATHAUTO_UPDATE_ACTION_DELETE.
  190. variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE);
  191. $node->title = 'Second title';
  192. pathauto_node_update($node);
  193. $this->assertEntityAlias('node', $node, 'content/second-title');
  194. $this->assertNoAliasExists(array('alias' => 'content/first-title'));
  195. // Test PATHAUTO_UPDATE_ACTION_LEAVE
  196. variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_LEAVE);
  197. $node->title = 'Third title';
  198. pathauto_node_update($node);
  199. $this->assertEntityAlias('node', $node, 'content/third-title');
  200. $this->assertAliasExists(array('source' => "node/{$node->nid}", 'alias' => 'content/second-title'));
  201. variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE);
  202. $node->title = 'Fourth title';
  203. pathauto_node_update($node);
  204. $this->assertEntityAlias('node', $node, 'content/fourth-title');
  205. $this->assertNoAliasExists(array('alias' => 'content/third-title'));
  206. // The older second alias is not deleted yet.
  207. $older_path = $this->assertAliasExists(array('source' => "node/{$node->nid}", 'alias' => 'content/second-title'));
  208. path_delete($older_path);
  209. variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_NO_NEW);
  210. $node->title = 'Fifth title';
  211. pathauto_node_update($node);
  212. $this->assertEntityAlias('node', $node, 'content/fourth-title');
  213. $this->assertNoAliasExists(array('alias' => 'content/fith-title'));
  214. // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'update'.
  215. $this->deleteAllAliases();
  216. pathauto_node_update($node);
  217. $this->assertEntityAlias('node', $node, 'content/fifth-title');
  218. // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'bulkupdate'.
  219. $this->deleteAllAliases();
  220. $node->title = 'Sixth title';
  221. pathauto_node_update_alias($node, 'bulkupdate');
  222. $this->assertEntityAlias('node', $node, 'content/sixth-title');
  223. }
  224. /**
  225. * Test that pathauto_create_alias() will not create an alias for a pattern
  226. * that does not get any tokens replaced.
  227. */
  228. function testNoTokensNoAlias() {
  229. $node = $this->drupalCreateNode(array('title' => ''));
  230. $this->assertNoEntityAliasExists('node', $node);
  231. $node->title = 'hello';
  232. pathauto_node_update($node);
  233. $this->assertEntityAlias('node', $node, 'content/hello');
  234. }
  235. /**
  236. * Test the handling of path vs non-path tokens in pathauto_clean_token_values().
  237. */
  238. function testPathTokens() {
  239. variable_set('pathauto_taxonomy_term_pattern', '[term:parent:url:path]/[term:name]');
  240. $vocab = $this->addVocabulary();
  241. $term1 = $this->addTerm($vocab, array('name' => 'Parent term'));
  242. $this->assertEntityAlias('taxonomy_term', $term1, 'parent-term');
  243. $term2 = $this->addTerm($vocab, array('name' => 'Child term', 'parent' => $term1->tid));
  244. $this->assertEntityAlias('taxonomy_term', $term2, 'parent-term/child-term');
  245. $this->saveEntityAlias('taxonomy_term', $term1, 'My Crazy/Alias/');
  246. pathauto_taxonomy_term_update($term2);
  247. $this->assertEntityAlias('taxonomy_term', $term2, 'My Crazy/Alias/child-term');
  248. }
  249. function testEntityBundleRenamingDeleting() {
  250. // Create a vocabulary and test that it's pattern variable works.
  251. $vocab = $this->addVocabulary(array('machine_name' => 'old_name'));
  252. variable_set('pathauto_taxonomy_term_pattern', 'base');
  253. variable_set("pathauto_taxonomy_term_old_name_pattern", 'bundle');
  254. $this->assertEntityPattern('taxonomy_term', 'old_name', LANGUAGE_NONE, 'bundle');
  255. // Rename the vocabulary's machine name, which should cause its pattern
  256. // variable to also be renamed.
  257. $vocab->machine_name = 'new_name';
  258. taxonomy_vocabulary_save($vocab);
  259. $this->assertEntityPattern('taxonomy_term', 'new_name', LANGUAGE_NONE, 'bundle');
  260. $this->assertEntityPattern('taxonomy_term', 'old_name', LANGUAGE_NONE, 'base');
  261. // Delete the vocabulary, which should cause its pattern variable to also
  262. // be deleted.
  263. taxonomy_vocabulary_delete($vocab->vid);
  264. $this->assertEntityPattern('taxonomy_term', 'new_name', LANGUAGE_NONE, 'base');
  265. }
  266. function testNoExistingPathAliases() {
  267. variable_set('pathauto_node_page_pattern', '[node:title]');
  268. variable_set('pathauto_punctuation_period', PATHAUTO_PUNCTUATION_DO_NOTHING);
  269. // Check that Pathauto does not create an alias of '/admin'.
  270. $node = $this->drupalCreateNode(array('title' => 'Admin', 'type' => 'page'));
  271. $this->assertNoEntityAlias('node', $node);
  272. // Check that Pathauto does not create an alias of '/modules'.
  273. $node->title = 'Modules';
  274. node_save($node);
  275. $this->assertNoEntityAlias('node', $node);
  276. // Check that Pathauto does not create an alias of '/index.php'.
  277. $node->title = 'index.php';
  278. node_save($node);
  279. $this->assertNoEntityAlias('node', $node);
  280. // Check that a safe value gets an automatic alias. This is also a control
  281. // to ensure the above tests work properly.
  282. $node->title = 'Safe value';
  283. node_save($node);
  284. $this->assertEntityAlias('node', $node, 'safe-value');
  285. }
  286. }
  287. /**
  288. * Helper test class with some added functions for testing.
  289. */
  290. class PathautoFunctionalTestHelper extends PathautoTestHelper {
  291. protected $admin_user;
  292. function setUp(array $modules = array()) {
  293. parent::setUp($modules);
  294. // Set pathauto settings we assume to be as-is in this test.
  295. variable_set('pathauto_node_page_pattern', 'content/[node:title]');
  296. // Allow other modules to add additional permissions for the admin user.
  297. $permissions = array(
  298. 'administer pathauto',
  299. 'administer url aliases',
  300. 'create url aliases',
  301. 'administer nodes',
  302. 'bypass node access',
  303. 'access content overview',
  304. 'administer taxonomy',
  305. 'administer users',
  306. );
  307. $args = func_get_args();
  308. if (isset($args[1]) && is_array($args[1])) {
  309. $permissions = array_merge($permissions, $args[1]);
  310. }
  311. $this->admin_user = $this->drupalCreateUser($permissions);
  312. $this->drupalLogin($this->admin_user);
  313. }
  314. }
  315. /**
  316. * Test basic pathauto functionality.
  317. */
  318. class PathautoFunctionalTestCase extends PathautoFunctionalTestHelper {
  319. public static function getInfo() {
  320. return array(
  321. 'name' => 'Pathauto basic tests',
  322. 'description' => 'Test basic pathauto functionality.',
  323. 'group' => 'Pathauto',
  324. 'dependencies' => array('token'),
  325. );
  326. }
  327. /**
  328. * Basic functional testing of Pathauto.
  329. */
  330. function testNodeEditing() {
  331. // Delete the default node pattern. Only the page content type will have a pattern.
  332. variable_del('pathauto_node_pattern');
  333. // Ensure that the Pathauto checkbox is checked by default on the node add form.
  334. $this->drupalGet('node/add/page');
  335. $this->assertFieldChecked('edit-path-pathauto');
  336. // Create node for testing by previewing and saving the node form.
  337. $title = ' Testing: node title [';
  338. $automatic_alias = 'content/testing-node-title';
  339. $this->drupalPost(NULL, array('title' => $title), 'Preview');
  340. $this->drupalPost(NULL, array(), 'Save');
  341. $node = $this->drupalGetNodeByTitle($title);
  342. // Look for alias generated in the form.
  343. $this->drupalGet("node/{$node->nid}/edit");
  344. $this->assertFieldChecked('edit-path-pathauto');
  345. $this->assertFieldByName('path[alias]', $automatic_alias, 'Generated alias visible in the path alias field.');
  346. // Check whether the alias actually works.
  347. $this->drupalGet($automatic_alias);
  348. $this->assertText($title, 'Node accessible through automatic alias.');
  349. // Manually set the node's alias.
  350. $manual_alias = 'content/' . $node->nid;
  351. $edit = array(
  352. 'path[pathauto]' => FALSE,
  353. 'path[alias]' => $manual_alias,
  354. );
  355. $this->drupalPost("node/{$node->nid}/edit", $edit, t('Save'));
  356. $this->assertText("Basic page $title has been updated.");
  357. // Check that the automatic alias checkbox is now unchecked by default.
  358. $this->drupalGet("node/{$node->nid}/edit");
  359. $this->assertNoFieldChecked('edit-path-pathauto');
  360. $this->assertFieldByName('path[alias]', $manual_alias);
  361. // Submit the node form with the default values.
  362. $this->drupalPost(NULL, array(), t('Save'));
  363. $this->assertText("Basic page $title has been updated.");
  364. // Test that the old (automatic) alias has been deleted and only accessible
  365. // through the new (manual) alias.
  366. $this->drupalGet($automatic_alias);
  367. $this->assertResponse(404, 'Node not accessible through automatic alias.');
  368. $this->drupalGet($manual_alias);
  369. $this->assertText($title, 'Node accessible through manual alias.');
  370. // Now attempt to create a node that has no pattern (article content type).
  371. // The Pathauto checkbox should not exist.
  372. $this->drupalGet('node/add/article');
  373. $this->assertNoFieldById('edit-path-pathauto');
  374. $this->assertFieldByName('path[alias]', '');
  375. $edit = array();
  376. $edit['title'] = 'My test article';
  377. $this->drupalPost(NULL, $edit, t('Save'));
  378. $node = $this->drupalGetNodeByTitle($edit['title']);
  379. // Pathauto checkbox should still not exist.
  380. $this->drupalGet('node/' . $node->nid . '/edit');
  381. $this->assertNoFieldById('edit-path-pathauto');
  382. $this->assertFieldByName('path[alias]', '');
  383. $this->assertNoEntityAlias('node', $node);
  384. }
  385. /**
  386. * Test node operations.
  387. */
  388. function testNodeOperations() {
  389. $node1 = $this->drupalCreateNode(array('title' => 'node1'));
  390. $node2 = $this->drupalCreateNode(array('title' => 'node2'));
  391. // Delete all current URL aliases.
  392. $this->deleteAllAliases();
  393. $edit = array(
  394. 'operation' => 'pathauto_update_alias',
  395. "nodes[{$node1->nid}]" => TRUE,
  396. );
  397. $this->drupalPost('admin/content', $edit, t('Update'));
  398. $this->assertText('Updated URL alias for 1 node.');
  399. $this->assertEntityAlias('node', $node1, 'content/' . $node1->title);
  400. $this->assertEntityAlias('node', $node2, 'node/' . $node2->nid);
  401. }
  402. /**
  403. * Basic functional testing of Pathauto with taxonomy terms.
  404. */
  405. function testTermEditing() {
  406. $this->drupalGet('admin/structure');
  407. $this->drupalGet('admin/structure/taxonomy');
  408. // Create term for testing.
  409. $name = ' Testing: term name [ ';
  410. $automatic_alias = 'tags/testing-term-name';
  411. $this->drupalPost('admin/structure/taxonomy/tags/add', array('name' => $name), 'Save');
  412. $name = trim($name);
  413. $this->assertText("Created new term $name.");
  414. $term = $this->drupalGetTermByName($name);
  415. // Look for alias generated in the form.
  416. $this->drupalGet("taxonomy/term/{$term->tid}/edit");
  417. $this->assertFieldChecked('edit-path-pathauto');
  418. $this->assertFieldByName('path[alias]', $automatic_alias, 'Generated alias visible in the path alias field.');
  419. // Check whether the alias actually works.
  420. $this->drupalGet($automatic_alias);
  421. $this->assertText($name, 'Term accessible through automatic alias.');
  422. // Manually set the term's alias.
  423. $manual_alias = 'tags/' . $term->tid;
  424. $edit = array(
  425. 'path[pathauto]' => FALSE,
  426. 'path[alias]' => $manual_alias,
  427. );
  428. $this->drupalPost("taxonomy/term/{$term->tid}/edit", $edit, t('Save'));
  429. $this->assertText("Updated term $name.");
  430. // Check that the automatic alias checkbox is now unchecked by default.
  431. $this->drupalGet("taxonomy/term/{$term->tid}/edit");
  432. $this->assertNoFieldChecked('edit-path-pathauto');
  433. $this->assertFieldByName('path[alias]', $manual_alias);
  434. // Submit the term form with the default values.
  435. $this->drupalPost(NULL, array(), t('Save'));
  436. $this->assertText("Updated term $name.");
  437. // Test that the old (automatic) alias has been deleted and only accessible
  438. // through the new (manual) alias.
  439. $this->drupalGet($automatic_alias);
  440. $this->assertResponse(404, 'Term not accessible through automatic alias.');
  441. $this->drupalGet($manual_alias);
  442. $this->assertText($name, 'Term accessible through manual alias.');
  443. }
  444. /**
  445. * Basic functional testing of Pathauto with users.
  446. */
  447. function testUserEditing() {
  448. // There should be no Pathauto checkbox on user forms.
  449. $this->drupalGet('user/' . $this->admin_user->uid . '/edit');
  450. $this->assertNoFieldById('edit-path-pathauto');
  451. }
  452. /**
  453. * Test user operations.
  454. */
  455. function testUserOperations() {
  456. $account = $this->drupalCreateUser();
  457. // Delete all current URL aliases.
  458. $this->deleteAllAliases();
  459. $edit = array(
  460. 'operation' => 'pathauto_update_alias',
  461. "accounts[{$account->uid}]" => TRUE,
  462. );
  463. $this->drupalPost('admin/people', $edit, t('Update'));
  464. $this->assertText('Updated URL alias for 1 user account.');
  465. $this->assertEntityAlias('user', $account, 'users/' . drupal_strtolower($account->name));
  466. $this->assertEntityAlias('user', $this->admin_user, 'user/' . $this->admin_user->uid);
  467. }
  468. function testSettingsValidation() {
  469. $edit = array();
  470. $edit['pathauto_max_length'] = 'abc';
  471. $edit['pathauto_max_component_length'] = 'abc';
  472. $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
  473. $this->assertText('The field Maximum alias length is not a valid number.');
  474. $this->assertText('The field Maximum component length is not a valid number.');
  475. $this->assertNoText('The configuration options have been saved.');
  476. $edit['pathauto_max_length'] = '0';
  477. $edit['pathauto_max_component_length'] = '0';
  478. $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
  479. $this->assertText('The field Maximum alias length cannot be less than 1.');
  480. $this->assertText('The field Maximum component length cannot be less than 1.');
  481. $this->assertNoText('The configuration options have been saved.');
  482. $edit['pathauto_max_length'] = '999';
  483. $edit['pathauto_max_component_length'] = '999';
  484. $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
  485. $this->assertText('The field Maximum alias length cannot be greater than 255.');
  486. $this->assertText('The field Maximum component length cannot be greater than 255.');
  487. $this->assertNoText('The configuration options have been saved.');
  488. $edit['pathauto_max_length'] = '50';
  489. $edit['pathauto_max_component_length'] = '50';
  490. $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
  491. $this->assertText('The configuration options have been saved.');
  492. }
  493. function testPatternsValidation() {
  494. $edit = array();
  495. $edit['pathauto_node_pattern'] = '[node:title]/[user:name]/[term:name]';
  496. $edit['pathauto_node_page_pattern'] = 'page';
  497. $this->drupalPost('admin/config/search/path/patterns', $edit, 'Save configuration');
  498. $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].');
  499. $this->assertText('The Pattern for all Basic page paths cannot contain fewer than one token.');
  500. $this->assertNoText('The configuration options have been saved.');
  501. $edit['pathauto_node_pattern'] = '[node:title]';
  502. $edit['pathauto_node_page_pattern'] = 'page/[node:title]';
  503. $edit['pathauto_node_article_pattern'] = '';
  504. $this->drupalPost('admin/config/search/path/patterns', $edit, 'Save configuration');
  505. $this->assertText('The configuration options have been saved.');
  506. }
  507. /**
  508. * Test programmatic entity creation for aliases.
  509. */
  510. function testProgrammaticEntityCreation() {
  511. $node = $this->drupalCreateNode(array('title' => 'Test node', 'path' => array('pathauto' => TRUE)));
  512. $this->assertEntityAlias('node', $node, 'content/test-node');
  513. $vocabulary = $this->addVocabulary(array('name' => 'Tags'));
  514. $term = $this->addTerm($vocabulary, array('name' => 'Test term', 'path' => array('pathauto' => TRUE)));
  515. $this->assertEntityAlias('taxonomy_term', $term, 'tags/test-term');
  516. $edit['name'] = 'Test user';
  517. $edit['mail'] = 'test-user@example.com';
  518. $edit['pass'] = user_password();
  519. $edit['path'] = array('pathauto' => TRUE);
  520. $edit['status'] = 1;
  521. $account = user_save(drupal_anonymous_user(), $edit);
  522. $this->assertEntityAlias('user', $account, 'users/test-user');
  523. }
  524. }
  525. class PathautoLocaleTestCase extends PathautoFunctionalTestHelper {
  526. public static function getInfo() {
  527. return array(
  528. 'name' => 'Pathauto localization tests',
  529. 'description' => 'Test pathauto functionality with localization and translation.',
  530. 'group' => 'Pathauto',
  531. 'dependencies' => array('token'),
  532. );
  533. }
  534. function setUp(array $modules = array()) {
  535. $modules[] = 'locale';
  536. $modules[] = 'translation';
  537. parent::setUp($modules, array('administer languages'));
  538. // Add predefined French language and reset the locale cache.
  539. require_once DRUPAL_ROOT . '/includes/locale.inc';
  540. locale_add_language('fr', NULL, NULL, LANGUAGE_LTR, '', 'fr');
  541. drupal_language_initialize();
  542. }
  543. /**
  544. * Test that when an English node is updated, its old English alias is
  545. * updated and its newer French alias is left intact.
  546. */
  547. function testLanguageAliases() {
  548. $node = array(
  549. 'title' => 'English node',
  550. 'language' => 'en',
  551. 'body' => array('en' => array(array())),
  552. 'path' => array(
  553. 'alias' => 'english-node',
  554. 'pathauto' => FALSE,
  555. ),
  556. );
  557. $node = $this->drupalCreateNode($node);
  558. $english_alias = path_load(array('alias' => 'english-node', 'language' => 'en'));
  559. $this->assertTrue($english_alias, 'Alias created with proper language.');
  560. // Also save a French alias that should not be left alone, even though
  561. // it is the newer alias.
  562. $this->saveEntityAlias('node', $node, 'french-node', 'fr');
  563. // Add an alias with the soon-to-be generated alias, causing the upcoming
  564. // alias update to generate a unique alias with the '-0' suffix.
  565. $this->saveAlias('node/invalid', 'content/english-node', LANGUAGE_NONE);
  566. // Update the node, triggering a change in the English alias.
  567. $node->path['pathauto'] = TRUE;
  568. pathauto_node_update($node);
  569. // Check that the new English alias replaced the old one.
  570. $this->assertEntityAlias('node', $node, 'content/english-node-0', 'en');
  571. $this->assertEntityAlias('node', $node, 'french-node', 'fr');
  572. $this->assertAliasExists(array('pid' => $english_alias['pid'], 'alias' => 'content/english-node-0'));
  573. }
  574. }
  575. /**
  576. * Bulk update functionality tests.
  577. */
  578. class PathautoBulkUpdateTestCase extends PathautoFunctionalTestHelper {
  579. private $nodes;
  580. public static function getInfo() {
  581. return array(
  582. 'name' => 'Pathauto bulk updating',
  583. 'description' => 'Tests bulk updating of URL aliases.',
  584. 'group' => 'Pathauto',
  585. 'dependencies' => array('token'),
  586. );
  587. }
  588. function testBulkUpdate() {
  589. // Create some nodes.
  590. $this->nodes = array();
  591. for ($i = 1; $i <= 5; $i++) {
  592. $node = $this->drupalCreateNode();
  593. $this->nodes[$node->nid] = $node;
  594. }
  595. // Clear out all aliases.
  596. $this->deleteAllAliases();
  597. // Bulk create aliases.
  598. $edit = array(
  599. 'update[node_pathauto_bulk_update_batch_process]' => TRUE,
  600. 'update[user_pathauto_bulk_update_batch_process]' => TRUE,
  601. );
  602. $this->drupalPost('admin/config/search/path/update_bulk', $edit, t('Update'));
  603. $this->assertText('Generated 7 URL aliases.'); // 5 nodes + 2 users
  604. // Check that aliases have actually been created.
  605. foreach ($this->nodes as $node) {
  606. $this->assertEntityAliasExists('node', $node);
  607. }
  608. $this->assertEntityAliasExists('user', $this->admin_user);
  609. // Add a new node.
  610. $new_node = $this->drupalCreateNode(array('path' => array('alias' => '', 'pathauto' => FALSE)));
  611. // Run the update again which should only run against the new node.
  612. $this->drupalPost('admin/config/search/path/update_bulk', $edit, t('Update'));
  613. $this->assertText('Generated 1 URL alias.'); // 1 node + 0 users
  614. $this->assertEntityAliasExists('node', $new_node);
  615. }
  616. }
  617. /**
  618. * Token functionality tests.
  619. */
  620. class PathautoTokenTestCase extends PathautoFunctionalTestHelper {
  621. public static function getInfo() {
  622. return array(
  623. 'name' => 'Pathauto tokens',
  624. 'description' => 'Tests tokens provided by Pathauto.',
  625. 'group' => 'Pathauto',
  626. 'dependencies' => array('token'),
  627. );
  628. }
  629. function testPathautoTokens() {
  630. $array = array(
  631. 'test first arg',
  632. 'The Array / value',
  633. );
  634. $tokens = array(
  635. 'join-path' => 'test-first-arg/array-value',
  636. );
  637. $data['array'] = $array;
  638. $replacements = $this->assertTokens('array', $data, $tokens);
  639. // Ensure that the pathauto_clean_token_values() function does not alter
  640. // this token value.
  641. module_load_include('inc', 'pathauto');
  642. pathauto_clean_token_values($replacements, $data, array());
  643. $this->assertEqual($replacements['[array:join-path]'], 'test-first-arg/array-value');
  644. }
  645. /**
  646. * Function copied from TokenTestHelper::assertTokens().
  647. */
  648. function assertTokens($type, array $data, array $tokens, array $options = array()) {
  649. $input = $this->mapTokenNames($type, array_keys($tokens));
  650. $replacements = token_generate($type, $input, $data, $options);
  651. foreach ($tokens as $name => $expected) {
  652. $token = $input[$name];
  653. if (!isset($expected)) {
  654. $this->assertTrue(!isset($values[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
  655. }
  656. elseif (!isset($replacements[$token])) {
  657. $this->fail(t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
  658. }
  659. elseif (!empty($options['regex'])) {
  660. $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)));
  661. }
  662. else {
  663. $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)));
  664. }
  665. }
  666. return $replacements;
  667. }
  668. function mapTokenNames($type, array $tokens = array()) {
  669. $return = array();
  670. foreach ($tokens as $token) {
  671. $return[$token] = "[$type:$token]";
  672. }
  673. return $return;
  674. }
  675. }