pathauto.test 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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, $alias = NULL) {
  50. $uri = entity_uri($entity_type, $entity);
  51. $path = array('source' => $uri['path']);
  52. if (!empty($alias)) {
  53. $path['alias'] = $alias;
  54. }
  55. $this->assertNoAliasExists($path);
  56. }
  57. function assertAlias($source, $expected_alias, $language = LANGUAGE_NONE) {
  58. drupal_clear_path_cache($source);
  59. $alias = drupal_get_path_alias($source, $language);
  60. $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)));
  61. }
  62. function assertAliasExists($conditions) {
  63. $path = path_load($conditions);
  64. $this->assertTrue($path, t('Alias with conditions @conditions found.', array('@conditions' => var_export($conditions, TRUE))));
  65. return $path;
  66. }
  67. function assertNoAliasExists($conditions) {
  68. $alias = path_load($conditions);
  69. $this->assertFalse($alias, t('Alias with conditions @conditions not found.', array('@conditions' => var_export($conditions, TRUE))));
  70. }
  71. function deleteAllAliases() {
  72. db_delete('url_alias')->execute();
  73. drupal_clear_path_cache();
  74. }
  75. function addVocabulary(array $vocabulary = array()) {
  76. $name = drupal_strtolower($this->randomName(5));
  77. $vocabulary += array(
  78. 'name' => $name,
  79. 'machine_name' => $name,
  80. 'nodes' => array('article' => 'article'),
  81. );
  82. $vocabulary = (object) $vocabulary;
  83. taxonomy_vocabulary_save($vocabulary);
  84. return $vocabulary;
  85. }
  86. function addTerm(stdClass $vocabulary, array $term = array()) {
  87. $term += array(
  88. 'name' => drupal_strtolower($this->randomName(5)),
  89. 'vocabulary_machine_name' => $vocabulary->machine_name,
  90. 'vid' => $vocabulary->vid,
  91. );
  92. $term = (object) $term;
  93. taxonomy_term_save($term);
  94. return $term;
  95. }
  96. function assertEntityPattern($entity_type, $bundle, $language = LANGUAGE_NONE, $expected) {
  97. drupal_static_reset('pathauto_pattern_load_by_entity');
  98. $this->refreshVariables();
  99. $pattern = pathauto_pattern_load_by_entity($entity_type, $bundle, $language);
  100. $this->assertIdentical($expected, $pattern);
  101. }
  102. function drupalGetTermByName($name, $reset = FALSE) {
  103. $terms = entity_load('taxonomy_term', array(), array('name' => $name), $reset);
  104. return !empty($terms) ? reset($terms) : FALSE;
  105. }
  106. }
  107. /**
  108. * Unit tests for Pathauto functions.
  109. */
  110. class PathautoUnitTestCase extends PathautoTestHelper {
  111. public static function getInfo() {
  112. return array(
  113. 'name' => 'Pathauto unit tests',
  114. 'description' => 'Unit tests for Pathauto functions.',
  115. 'group' => 'Pathauto',
  116. 'dependencies' => array('token'),
  117. );
  118. }
  119. function setUp(array $modules = array()) {
  120. parent::setUp($modules);
  121. module_load_include('inc', 'pathauto');
  122. }
  123. /**
  124. * Test _pathauto_get_schema_alias_maxlength().
  125. */
  126. function testGetSchemaAliasMaxLength() {
  127. $this->assertIdentical(_pathauto_get_schema_alias_maxlength(), 255);
  128. }
  129. /**
  130. * Test pathauto_pattern_load_by_entity().
  131. */
  132. function testPatternLoadByEntity() {
  133. variable_set('pathauto_node_story_en_pattern', ' story/en/[node:title] ');
  134. variable_set('pathauto_node_story_pattern', 'story/[node:title]');
  135. variable_set('pathauto_node_pattern', 'content/[node:title]');
  136. variable_set('pathauto_user_pattern', 'users/[user:name]');
  137. $tests = array(
  138. array('entity' => 'node', 'bundle' => 'story', 'language' => 'fr', 'expected' => 'story/[node:title]'),
  139. array('entity' => 'node', 'bundle' => 'story', 'language' => 'en', 'expected' => 'story/en/[node:title]'),
  140. array('entity' => 'node', 'bundle' => 'story', 'language' => LANGUAGE_NONE, 'expected' => 'story/[node:title]'),
  141. array('entity' => 'node', 'bundle' => 'page', 'language' => 'en', 'expected' => 'content/[node:title]'),
  142. array('entity' => 'user', 'bundle' => 'user', 'language' => LANGUAGE_NONE, 'expected' => 'users/[user:name]'),
  143. array('entity' => 'invalid-entity', 'bundle' => '', 'language' => LANGUAGE_NONE, 'expected' => ''),
  144. );
  145. foreach ($tests as $test) {
  146. $actual = pathauto_pattern_load_by_entity($test['entity'], $test['bundle'], $test['language']);
  147. $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'])));
  148. }
  149. }
  150. /**
  151. * Test pathauto_cleanstring().
  152. */
  153. function testCleanString() {
  154. $tests = array();
  155. variable_set('pathauto_ignore_words', ', in, is,that, the , this, with, ');
  156. variable_set('pathauto_max_component_length', 35);
  157. // Test the 'ignored words' removal.
  158. $tests['this'] = 'this';
  159. $tests['this with that'] = 'this-with-that';
  160. $tests['this thing with that thing'] = 'thing-thing';
  161. // Test length truncation and duplicate separator removal.
  162. $tests[' - Pathauto is the greatest - module ever in Drupal history - '] = 'pathauto-greatest-module-ever';
  163. // Test that HTML tags are removed.
  164. $tests['This <span class="text">text</span> has <br /><a href="http://example.com"><strong>HTML tags</strong></a>.'] = 'text-has-html-tags';
  165. $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';
  166. foreach ($tests as $input => $expected) {
  167. $output = pathauto_cleanstring($input);
  168. $this->assertEqual($output, $expected, t("pathauto_cleanstring('@input') expected '@expected', actual '@output'", array('@input' => $input, '@expected' => $expected, '@output' => $output)));
  169. }
  170. }
  171. /**
  172. * Test pathauto_clean_alias().
  173. */
  174. function testCleanAlias() {
  175. $tests = array();
  176. $tests['one/two/three'] = 'one/two/three';
  177. $tests['/one/two/three/'] = 'one/two/three';
  178. $tests['one//two///three'] = 'one/two/three';
  179. $tests['one/two--three/-/--/-/--/four---five'] = 'one/two-three/four-five';
  180. $tests['one/-//three--/four'] = 'one/three/four';
  181. foreach ($tests as $input => $expected) {
  182. $output = pathauto_clean_alias($input);
  183. $this->assertEqual($output, $expected, t("pathauto_clean_alias('@input') expected '@expected', actual '@output'", array('@input' => $input, '@expected' => $expected, '@output' => $output)));
  184. }
  185. }
  186. /**
  187. * Test pathauto_path_delete_multiple().
  188. */
  189. function testPathDeleteMultiple() {
  190. $this->saveAlias('node/1', 'node-1-alias');
  191. $this->saveAlias('node/1/view', 'node-1-alias/view');
  192. $this->saveAlias('node/1', 'node-1-alias-en', 'en');
  193. $this->saveAlias('node/1', 'node-1-alias-fr', 'fr');
  194. $this->saveAlias('node/2', 'node-2-alias');
  195. pathauto_path_delete_all('node/1');
  196. $this->assertNoAliasExists(array('source' => "node/1"));
  197. $this->assertNoAliasExists(array('source' => "node/1/view"));
  198. $this->assertAliasExists(array('source' => "node/2"));
  199. }
  200. /**
  201. * Test the different update actions in pathauto_create_alias().
  202. */
  203. function testUpdateActions() {
  204. // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'insert'.
  205. variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_NO_NEW);
  206. $node = $this->drupalCreateNode(array('title' => 'First title'));
  207. $this->assertEntityAlias('node', $node, 'content/first-title');
  208. // Default action is PATHAUTO_UPDATE_ACTION_DELETE.
  209. variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE);
  210. $node->title = 'Second title';
  211. pathauto_node_update($node);
  212. $this->assertEntityAlias('node', $node, 'content/second-title');
  213. $this->assertNoAliasExists(array('alias' => 'content/first-title'));
  214. // Test PATHAUTO_UPDATE_ACTION_LEAVE
  215. variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_LEAVE);
  216. $node->title = 'Third title';
  217. pathauto_node_update($node);
  218. $this->assertEntityAlias('node', $node, 'content/third-title');
  219. $this->assertAliasExists(array('source' => "node/{$node->nid}", 'alias' => 'content/second-title'));
  220. variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE);
  221. $node->title = 'Fourth title';
  222. pathauto_node_update($node);
  223. $this->assertEntityAlias('node', $node, 'content/fourth-title');
  224. $this->assertNoAliasExists(array('alias' => 'content/third-title'));
  225. // The older second alias is not deleted yet.
  226. $older_path = $this->assertAliasExists(array('source' => "node/{$node->nid}", 'alias' => 'content/second-title'));
  227. path_delete($older_path);
  228. variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_NO_NEW);
  229. $node->title = 'Fifth title';
  230. pathauto_node_update($node);
  231. $this->assertEntityAlias('node', $node, 'content/fourth-title');
  232. $this->assertNoAliasExists(array('alias' => 'content/fifth-title'));
  233. // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'update'.
  234. $this->deleteAllAliases();
  235. pathauto_node_update($node);
  236. $this->assertEntityAlias('node', $node, 'content/fifth-title');
  237. // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'bulkupdate'.
  238. $this->deleteAllAliases();
  239. $node->title = 'Sixth title';
  240. pathauto_node_update_alias($node, 'bulkupdate');
  241. $this->assertEntityAlias('node', $node, 'content/sixth-title');
  242. }
  243. /**
  244. * Test that pathauto_create_alias() will not create an alias for a pattern
  245. * that does not get any tokens replaced.
  246. */
  247. function testNoTokensNoAlias() {
  248. $node = $this->drupalCreateNode(array('title' => ''));
  249. $this->assertNoEntityAliasExists('node', $node);
  250. $node->title = 'hello';
  251. pathauto_node_update($node);
  252. $this->assertEntityAlias('node', $node, 'content/hello');
  253. }
  254. /**
  255. * Test the handling of path vs non-path tokens in pathauto_clean_token_values().
  256. */
  257. function testPathTokens() {
  258. variable_set('pathauto_taxonomy_term_pattern', '[term:parent:url:path]/[term:name]');
  259. $vocab = $this->addVocabulary();
  260. $term1 = $this->addTerm($vocab, array('name' => 'Parent term'));
  261. $this->assertEntityAlias('taxonomy_term', $term1, 'parent-term');
  262. $term2 = $this->addTerm($vocab, array('name' => 'Child term', 'parent' => $term1->tid));
  263. $this->assertEntityAlias('taxonomy_term', $term2, 'parent-term/child-term');
  264. $this->saveEntityAlias('taxonomy_term', $term1, 'My Crazy/Alias/');
  265. pathauto_taxonomy_term_update($term2);
  266. $this->assertEntityAlias('taxonomy_term', $term2, 'My Crazy/Alias/child-term');
  267. }
  268. /**
  269. * Test using fields for path structures.
  270. */
  271. function testParentChildPathTokens() {
  272. // First create a field which will be used to create the path. It must
  273. // begin with a letter.
  274. $fieldname = 'a' . drupal_strtolower($this->randomName());
  275. field_create_field(array('field_name' => $fieldname, 'type' => 'text'));
  276. field_create_instance(array('field_name' => $fieldname, 'entity_type' => 'taxonomy_term', 'bundle' => 'tags'));
  277. // Make the path pattern of a field use the value of this field appended
  278. // to the parent taxonomy term's pattern if there is one.
  279. variable_set('pathauto_taxonomy_term_tags_pattern', '[term:parents:join-path]/[term:' . $fieldname . ']');
  280. // Start by creating a parent term.
  281. $parent = new stdClass();
  282. $parent->$fieldname = array(LANGUAGE_NONE => array(array('value' => $parent->name = $this->randomName())));
  283. $parent->vid = 1;
  284. taxonomy_term_save($parent);
  285. // Create the child term.
  286. $child = new stdClass();
  287. $child->name = $this->randomName();
  288. $child->$fieldname = array(LANGUAGE_NONE => array(array('value' => $child->name = $this->randomName())));
  289. $child->vid = 1;
  290. $child->parent = $parent->tid;
  291. taxonomy_term_save($child);
  292. $this->assertEntityAlias('taxonomy_term', $child, drupal_strtolower($parent->name . '/' . $child->name));
  293. // Re-saving the parent term should not modify the child term's alias.
  294. taxonomy_term_save($parent);
  295. $this->assertEntityAlias('taxonomy_term', $child, drupal_strtolower($parent->name . '/' . $child->name));
  296. }
  297. function testEntityBundleRenamingDeleting() {
  298. // Create a vocabulary and test that it's pattern variable works.
  299. $vocab = $this->addVocabulary(array('machine_name' => 'old_name'));
  300. variable_set('pathauto_taxonomy_term_pattern', 'base');
  301. variable_set("pathauto_taxonomy_term_old_name_pattern", 'bundle');
  302. $this->assertEntityPattern('taxonomy_term', 'old_name', LANGUAGE_NONE, 'bundle');
  303. // Rename the vocabulary's machine name, which should cause its pattern
  304. // variable to also be renamed.
  305. $vocab->machine_name = 'new_name';
  306. taxonomy_vocabulary_save($vocab);
  307. $this->assertEntityPattern('taxonomy_term', 'new_name', LANGUAGE_NONE, 'bundle');
  308. $this->assertEntityPattern('taxonomy_term', 'old_name', LANGUAGE_NONE, 'base');
  309. // Delete the vocabulary, which should cause its pattern variable to also
  310. // be deleted.
  311. taxonomy_vocabulary_delete($vocab->vid);
  312. $this->assertEntityPattern('taxonomy_term', 'new_name', LANGUAGE_NONE, 'base');
  313. }
  314. function testNoExistingPathAliases() {
  315. variable_set('pathauto_node_page_pattern', '[node:title]');
  316. variable_set('pathauto_punctuation_period', PATHAUTO_PUNCTUATION_DO_NOTHING);
  317. // Check that Pathauto does not create an alias of '/admin'.
  318. $node = $this->drupalCreateNode(array('title' => 'Admin', 'type' => 'page'));
  319. $this->assertEntityAlias('node', $node, 'admin-0');
  320. // Check that Pathauto does not create an alias of '/modules'.
  321. $node->title = 'Modules';
  322. node_save($node);
  323. $this->assertEntityAlias('node', $node, 'modules-0');
  324. // Check that Pathauto does not create an alias of '/index.php'.
  325. $node->title = 'index.php';
  326. node_save($node);
  327. $this->assertEntityAlias('node', $node, 'index.php-0');
  328. // Check that a safe value gets an automatic alias. This is also a control
  329. // to ensure the above tests work properly.
  330. $node->title = 'Safe value';
  331. node_save($node);
  332. $this->assertEntityAlias('node', $node, 'safe-value');
  333. }
  334. function testPathAliasUniquifyWordsafe() {
  335. variable_set('pathauto_max_length', 25);
  336. $node_1 = $this->drupalCreateNode(array('title' => 'thequick brownfox jumpedover thelazydog', 'type' => 'page'));
  337. $node_2 = $this->drupalCreateNode(array('title' => 'thequick brownfox jumpedover thelazydog', 'type' => 'page'));
  338. // Check that pathauto_alias_uniquify is calling truncate_utf8 with $wordsafe param set to TRUE.
  339. // If it doesn't path alias result would be content/thequick-brownf-0
  340. $this->assertEntityAlias('node', $node_1, 'content/thequick-brownfox');
  341. $this->assertEntityAlias('node', $node_2, 'content/thequick-0');
  342. }
  343. }
  344. /**
  345. * Helper test class with some added functions for testing.
  346. */
  347. class PathautoFunctionalTestHelper extends PathautoTestHelper {
  348. protected $admin_user;
  349. function setUp(array $modules = array()) {
  350. parent::setUp($modules);
  351. // Set pathauto settings we assume to be as-is in this test.
  352. variable_set('pathauto_node_page_pattern', 'content/[node:title]');
  353. // Allow other modules to add additional permissions for the admin user.
  354. $permissions = array(
  355. 'administer pathauto',
  356. 'administer url aliases',
  357. 'create url aliases',
  358. 'administer nodes',
  359. 'bypass node access',
  360. 'access content overview',
  361. 'administer taxonomy',
  362. 'administer users',
  363. );
  364. $args = func_get_args();
  365. if (isset($args[1]) && is_array($args[1])) {
  366. $permissions = array_merge($permissions, $args[1]);
  367. }
  368. $this->admin_user = $this->drupalCreateUser($permissions);
  369. $this->drupalLogin($this->admin_user);
  370. }
  371. }
  372. /**
  373. * Test basic pathauto functionality.
  374. */
  375. class PathautoFunctionalTestCase extends PathautoFunctionalTestHelper {
  376. public static function getInfo() {
  377. return array(
  378. 'name' => 'Pathauto basic tests',
  379. 'description' => 'Test basic pathauto functionality.',
  380. 'group' => 'Pathauto',
  381. 'dependencies' => array('token'),
  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. // Ensure that the Pathauto checkbox is checked by default on the node add form.
  391. $this->drupalGet('node/add/page');
  392. $this->assertFieldChecked('edit-path-pathauto');
  393. // Create node for testing by previewing and saving the node form.
  394. $title = ' Testing: node title [';
  395. $automatic_alias = 'content/testing-node-title';
  396. $this->drupalPost(NULL, array('title' => $title), 'Preview');
  397. $this->drupalPost(NULL, array(), 'Save');
  398. $node = $this->drupalGetNodeByTitle($title);
  399. // Look for alias generated in the form.
  400. $this->drupalGet("node/{$node->nid}/edit");
  401. $this->assertFieldChecked('edit-path-pathauto');
  402. $this->assertFieldByName('path[alias]', $automatic_alias, 'Generated alias visible in the path alias field.');
  403. // Check whether the alias actually works.
  404. $this->drupalGet($automatic_alias);
  405. $this->assertText($title, 'Node accessible through automatic alias.');
  406. // Disable the update action. The checkbox should not be visible.
  407. variable_set('pathauto_update_action', 0);
  408. $this->drupalGet("node/{$node->nid}/edit");
  409. $this->assertNoFieldById('edit-path-pathauto');
  410. // Reset the update action back to default. The checkbox should be visible.
  411. variable_del('pathauto_update_action');
  412. $this->drupalGet("node/{$node->nid}/edit");
  413. $this->assertFieldChecked('edit-path-pathauto');
  414. // Manually set the node's alias.
  415. $manual_alias = 'content/' . $node->nid;
  416. $edit = array(
  417. 'path[pathauto]' => FALSE,
  418. 'path[alias]' => $manual_alias,
  419. );
  420. $this->drupalPost(NULL, $edit, t('Save'));
  421. $this->assertText("Basic page $title has been updated.");
  422. // Check that the automatic alias checkbox is now unchecked by default.
  423. $this->drupalGet("node/{$node->nid}/edit");
  424. $this->assertNoFieldChecked('edit-path-pathauto');
  425. $this->assertFieldByName('path[alias]', $manual_alias);
  426. // Submit the node form with the default values.
  427. $this->drupalPost(NULL, array(), t('Save'));
  428. $this->assertText("Basic page $title has been updated.");
  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. // Now attempt to create a node that has no pattern (article content type).
  436. // The Pathauto checkbox should not exist.
  437. $this->drupalGet('node/add/article');
  438. $this->assertNoFieldById('edit-path-pathauto');
  439. $this->assertFieldByName('path[alias]', '');
  440. $edit = array();
  441. $edit['title'] = 'My test article';
  442. $this->drupalPost(NULL, $edit, t('Save'));
  443. $node = $this->drupalGetNodeByTitle($edit['title']);
  444. // Pathauto checkbox should still not exist.
  445. $this->drupalGet('node/' . $node->nid . '/edit');
  446. $this->assertNoFieldById('edit-path-pathauto');
  447. $this->assertFieldByName('path[alias]', '');
  448. $this->assertNoEntityAlias('node', $node);
  449. // Set the page pattern to use only tokens so we can test the checkbox
  450. // behavior if none of the tokens have a value currently.
  451. variable_set('pathauto_node_page_pattern', '[node:title]');
  452. // Create a node with an empty title. The Pathauto checkbox should still be
  453. // visible but unchecked.
  454. $node = $this->drupalCreateNode(array('type' => 'page', 'title' => ''));
  455. $this->drupalGet('node/' . $node->nid . '/edit');
  456. $this->assertNoFieldChecked('edit-path-pathauto');
  457. $this->assertFieldByName('path[alias]', '');
  458. $this->assertNoEntityAlias('node', $node);
  459. $edit = array();
  460. $edit['title'] = 'Valid title';
  461. $edit['path[pathauto]'] = TRUE;
  462. $this->drupalPost(NULL, $edit, t('Save'));
  463. $this->drupalGet('node/' . $node->nid . '/edit');
  464. $this->assertFieldChecked('edit-path-pathauto');
  465. $this->assertFieldByName('path[alias]', 'valid-title');
  466. }
  467. /**
  468. * Test node operations.
  469. */
  470. function testNodeOperations() {
  471. $node1 = $this->drupalCreateNode(array('title' => 'node1'));
  472. $node2 = $this->drupalCreateNode(array('title' => 'node2'));
  473. // Delete all current URL aliases.
  474. $this->deleteAllAliases();
  475. $edit = array(
  476. 'operation' => 'pathauto_update_alias',
  477. "nodes[{$node1->nid}]" => TRUE,
  478. );
  479. $this->drupalPost('admin/content', $edit, t('Update'));
  480. $this->assertText('Updated URL alias for 1 node.');
  481. $this->assertEntityAlias('node', $node1, 'content/' . $node1->title);
  482. $this->assertEntityAlias('node', $node2, 'node/' . $node2->nid);
  483. }
  484. /**
  485. * @todo Merge this with existing node test methods?
  486. */
  487. public function testNodeState() {
  488. $nodeNoAliasUser = $this->drupalCreateUser(array('bypass node access'));
  489. $nodeAliasUser = $this->drupalCreateUser(array('bypass node access', 'create url aliases'));
  490. $node = $this->drupalCreateNode(array(
  491. 'title' => 'Node version one',
  492. 'type' => 'page',
  493. 'path' => array(
  494. 'pathauto' => FALSE,
  495. ),
  496. ));
  497. $this->assertNoEntityAlias('node', $node);
  498. // Set a manual path alias for the node.
  499. $node->path['alias'] = 'test-alias';
  500. node_save($node);
  501. // Ensure that the pathauto field was saved to the database.
  502. $node = node_load($node->nid, NULL, TRUE);
  503. $this->assertFalse($node->path['pathauto']);
  504. // Ensure that the manual path alias was saved and an automatic alias was not generated.
  505. $this->assertEntityAlias('node', $node, 'test-alias');
  506. $this->assertNoEntityAliasExists('node', $node, 'content/node-version-one');
  507. // Save the node as a user who does not have access to path fieldset.
  508. $this->drupalLogin($nodeNoAliasUser);
  509. $this->drupalGet('node/' . $node->nid . '/edit');
  510. $this->assertNoFieldByName('path[pathauto]');
  511. $edit = array('title' => 'Node version two');
  512. $this->drupalPost(NULL, $edit, 'Save');
  513. $this->assertText('Basic page Node version two has been updated.');
  514. $this->assertEntityAlias('node', $node, 'test-alias');
  515. $this->assertNoEntityAliasExists('node', $node, 'content/node-version-one');
  516. $this->assertNoEntityAliasExists('node', $node, 'content/node-version-two');
  517. // Load the edit node page and check that the Pathauto checkbox is unchecked.
  518. $this->drupalLogin($nodeAliasUser);
  519. $this->drupalGet('node/' . $node->nid . '/edit');
  520. $this->assertNoFieldChecked('edit-path-pathauto');
  521. // Edit the manual alias and save the node.
  522. $edit = array(
  523. 'title' => 'Node version three',
  524. 'path[alias]' => 'manually-edited-alias',
  525. );
  526. $this->drupalPost(NULL, $edit, 'Save');
  527. $this->assertText('Basic page Node version three has been updated.');
  528. $this->assertEntityAlias('node', $node, 'manually-edited-alias');
  529. $this->assertNoEntityAliasExists('node', $node, 'test-alias');
  530. $this->assertNoEntityAliasExists('node', $node, 'content/node-version-one');
  531. $this->assertNoEntityAliasExists('node', $node, 'content/node-version-two');
  532. $this->assertNoEntityAliasExists('node', $node, 'content/node-version-three');
  533. // Programatically save the node with an automatic alias.
  534. $node = node_load($node->nid, NULL, TRUE);
  535. $node->path['pathauto'] = TRUE;
  536. node_save($node);
  537. // Ensure that the pathauto field was saved to the database.
  538. $node = node_load($node->nid, NULL, TRUE);
  539. $this->assertTrue($node->path['pathauto']);
  540. $this->assertEntityAlias('node', $node, 'content/node-version-three');
  541. $this->assertNoEntityAliasExists('node', $node, 'manually-edited-alias');
  542. $this->assertNoEntityAliasExists('node', $node, 'test-alias');
  543. $this->assertNoEntityAliasExists('node', $node, 'content/node-version-one');
  544. $this->assertNoEntityAliasExists('node', $node, 'content/node-version-two');
  545. }
  546. /**
  547. * Basic functional testing of Pathauto with taxonomy terms.
  548. */
  549. function testTermEditing() {
  550. $this->drupalGet('admin/structure');
  551. $this->drupalGet('admin/structure/taxonomy');
  552. // Create term for testing.
  553. $name = ' Testing: term name [ ';
  554. $automatic_alias = 'tags/testing-term-name';
  555. $this->drupalPost('admin/structure/taxonomy/tags/add', array('name' => $name), 'Save');
  556. $name = trim($name);
  557. $this->assertText("Created new term $name.");
  558. $term = $this->drupalGetTermByName($name);
  559. // Look for alias generated in the form.
  560. $this->drupalGet("taxonomy/term/{$term->tid}/edit");
  561. $this->assertFieldChecked('edit-path-pathauto');
  562. $this->assertFieldByName('path[alias]', $automatic_alias, 'Generated alias visible in the path alias field.');
  563. // Check whether the alias actually works.
  564. $this->drupalGet($automatic_alias);
  565. $this->assertText($name, 'Term accessible through automatic alias.');
  566. // Manually set the term's alias.
  567. $manual_alias = 'tags/' . $term->tid;
  568. $edit = array(
  569. 'path[pathauto]' => FALSE,
  570. 'path[alias]' => $manual_alias,
  571. );
  572. $this->drupalPost("taxonomy/term/{$term->tid}/edit", $edit, t('Save'));
  573. $this->assertText("Updated term $name.");
  574. // Check that the automatic alias checkbox is now unchecked by default.
  575. $this->drupalGet("taxonomy/term/{$term->tid}/edit");
  576. $this->assertNoFieldChecked('edit-path-pathauto');
  577. $this->assertFieldByName('path[alias]', $manual_alias);
  578. // Submit the term form with the default values.
  579. $this->drupalPost(NULL, array(), t('Save'));
  580. $this->assertText("Updated term $name.");
  581. // Test that the old (automatic) alias has been deleted and only accessible
  582. // through the new (manual) alias.
  583. $this->drupalGet($automatic_alias);
  584. $this->assertResponse(404, 'Term not accessible through automatic alias.');
  585. $this->drupalGet($manual_alias);
  586. $this->assertText($name, 'Term accessible through manual alias.');
  587. }
  588. /**
  589. * Basic functional testing of Pathauto with users.
  590. */
  591. function testUserEditing() {
  592. // There should be no Pathauto checkbox on user forms.
  593. $this->drupalGet('user/' . $this->admin_user->uid . '/edit');
  594. $this->assertNoFieldById('edit-path-pathauto');
  595. }
  596. /**
  597. * Test user operations.
  598. */
  599. function testUserOperations() {
  600. $account = $this->drupalCreateUser();
  601. // Delete all current URL aliases.
  602. $this->deleteAllAliases();
  603. $edit = array(
  604. 'operation' => 'pathauto_update_alias',
  605. "accounts[{$account->uid}]" => TRUE,
  606. );
  607. $this->drupalPost('admin/people', $edit, t('Update'));
  608. $this->assertText('Updated URL alias for 1 user account.');
  609. $this->assertEntityAlias('user', $account, 'users/' . drupal_strtolower($account->name));
  610. $this->assertEntityAlias('user', $this->admin_user, 'user/' . $this->admin_user->uid);
  611. }
  612. function testSettingsValidation() {
  613. $edit = array();
  614. $edit['pathauto_max_length'] = 'abc';
  615. $edit['pathauto_max_component_length'] = 'abc';
  616. $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
  617. $this->assertText('The field Maximum alias length is not a valid number.');
  618. $this->assertText('The field Maximum component length is not a valid number.');
  619. $this->assertNoText('The configuration options have been saved.');
  620. $edit['pathauto_max_length'] = '0';
  621. $edit['pathauto_max_component_length'] = '0';
  622. $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
  623. $this->assertText('The field Maximum alias length cannot be less than 1.');
  624. $this->assertText('The field Maximum component length cannot be less than 1.');
  625. $this->assertNoText('The configuration options have been saved.');
  626. $edit['pathauto_max_length'] = '999';
  627. $edit['pathauto_max_component_length'] = '999';
  628. $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
  629. $this->assertText('The field Maximum alias length cannot be greater than 255.');
  630. $this->assertText('The field Maximum component length cannot be greater than 255.');
  631. $this->assertNoText('The configuration options have been saved.');
  632. $edit['pathauto_max_length'] = '50';
  633. $edit['pathauto_max_component_length'] = '50';
  634. $this->drupalPost('admin/config/search/path/settings', $edit, 'Save configuration');
  635. $this->assertText('The configuration options have been saved.');
  636. }
  637. function testPatternsValidation() {
  638. $edit = array();
  639. $edit['pathauto_node_pattern'] = '[node:title]/[user:name]/[term:name]';
  640. $edit['pathauto_node_page_pattern'] = 'page';
  641. $this->drupalPost('admin/config/search/path/patterns', $edit, 'Save configuration');
  642. $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].');
  643. $this->assertText('The Pattern for all Basic page paths cannot contain fewer than one token.');
  644. $this->assertNoText('The configuration options have been saved.');
  645. $edit['pathauto_node_pattern'] = '[node:title]';
  646. $edit['pathauto_node_page_pattern'] = 'page/[node:title]';
  647. $edit['pathauto_node_article_pattern'] = '';
  648. $this->drupalPost('admin/config/search/path/patterns', $edit, 'Save configuration');
  649. $this->assertText('The configuration options have been saved.');
  650. }
  651. /**
  652. * Test programmatic entity creation for aliases.
  653. */
  654. function testProgrammaticEntityCreation() {
  655. $node = $this->drupalCreateNode(array('title' => 'Test node', 'path' => array('pathauto' => TRUE)));
  656. $this->assertEntityAlias('node', $node, 'content/test-node');
  657. $vocabulary = $this->addVocabulary(array('name' => 'Tags'));
  658. $term = $this->addTerm($vocabulary, array('name' => 'Test term', 'path' => array('pathauto' => TRUE)));
  659. $this->assertEntityAlias('taxonomy_term', $term, 'tags/test-term');
  660. $edit['name'] = 'Test user';
  661. $edit['mail'] = 'test-user@example.com';
  662. $edit['pass'] = user_password();
  663. $edit['path'] = array('pathauto' => TRUE);
  664. $edit['status'] = 1;
  665. $account = user_save(drupal_anonymous_user(), $edit);
  666. $this->assertEntityAlias('user', $account, 'users/test-user');
  667. }
  668. }
  669. class PathautoLocaleTestCase extends PathautoFunctionalTestHelper {
  670. public static function getInfo() {
  671. return array(
  672. 'name' => 'Pathauto localization tests',
  673. 'description' => 'Test pathauto functionality with localization and translation.',
  674. 'group' => 'Pathauto',
  675. 'dependencies' => array('token'),
  676. );
  677. }
  678. function setUp(array $modules = array()) {
  679. $modules[] = 'locale';
  680. $modules[] = 'translation';
  681. parent::setUp($modules, array('administer languages'));
  682. // Add predefined French language and reset the locale cache.
  683. require_once DRUPAL_ROOT . '/includes/locale.inc';
  684. locale_add_language('fr', NULL, NULL, LANGUAGE_LTR, '', 'fr');
  685. drupal_language_initialize();
  686. }
  687. /**
  688. * Test that when an English node is updated, its old English alias is
  689. * updated and its newer French alias is left intact.
  690. */
  691. function testLanguageAliases() {
  692. $node = array(
  693. 'title' => 'English node',
  694. 'language' => 'en',
  695. 'body' => array('en' => array(array())),
  696. 'path' => array(
  697. 'alias' => 'english-node',
  698. 'pathauto' => FALSE,
  699. ),
  700. );
  701. $node = $this->drupalCreateNode($node);
  702. $english_alias = path_load(array('alias' => 'english-node', 'language' => 'en'));
  703. $this->assertTrue($english_alias, 'Alias created with proper language.');
  704. // Also save a French alias that should not be left alone, even though
  705. // it is the newer alias.
  706. $this->saveEntityAlias('node', $node, 'french-node', 'fr');
  707. // Add an alias with the soon-to-be generated alias, causing the upcoming
  708. // alias update to generate a unique alias with the '-0' suffix.
  709. $this->saveAlias('node/invalid', 'content/english-node', LANGUAGE_NONE);
  710. // Update the node, triggering a change in the English alias.
  711. $node->path['pathauto'] = TRUE;
  712. pathauto_node_update($node);
  713. // Check that the new English alias replaced the old one.
  714. $this->assertEntityAlias('node', $node, 'content/english-node-0', 'en');
  715. $this->assertEntityAlias('node', $node, 'french-node', 'fr');
  716. $this->assertAliasExists(array('pid' => $english_alias['pid'], 'alias' => 'content/english-node-0'));
  717. // Create a new node with the same title as before but without
  718. // specifying a language.
  719. $node = $this->drupalCreateNode(array('title' => 'English node'));
  720. // Check that the new node had a unique alias generated with the '-1'
  721. // suffix.
  722. $this->assertEntityAlias('node', $node, 'content/english-node-1');
  723. }
  724. }
  725. /**
  726. * Bulk update functionality tests.
  727. */
  728. class PathautoBulkUpdateTestCase extends PathautoFunctionalTestHelper {
  729. private $nodes;
  730. public static function getInfo() {
  731. return array(
  732. 'name' => 'Pathauto bulk updating',
  733. 'description' => 'Tests bulk updating of URL aliases.',
  734. 'group' => 'Pathauto',
  735. 'dependencies' => array('token'),
  736. );
  737. }
  738. function testBulkUpdate() {
  739. // Create some nodes.
  740. $this->nodes = array();
  741. for ($i = 1; $i <= 5; $i++) {
  742. $node = $this->drupalCreateNode();
  743. $this->nodes[$node->nid] = $node;
  744. }
  745. // Clear out all aliases.
  746. $this->deleteAllAliases();
  747. // Bulk create aliases.
  748. $edit = array(
  749. 'update[node_pathauto_bulk_update_batch_process]' => TRUE,
  750. 'update[user_pathauto_bulk_update_batch_process]' => TRUE,
  751. );
  752. $this->drupalPost('admin/config/search/path/update_bulk', $edit, t('Update'));
  753. $this->assertText('Generated 7 URL aliases.'); // 5 nodes + 2 users
  754. // Check that aliases have actually been created.
  755. foreach ($this->nodes as $node) {
  756. $this->assertEntityAliasExists('node', $node);
  757. }
  758. $this->assertEntityAliasExists('user', $this->admin_user);
  759. // Add a new node.
  760. $new_node = $this->drupalCreateNode(array('path' => array('alias' => '', 'pathauto' => FALSE)));
  761. // Run the update again which should not run against any nodes.
  762. $this->drupalPost('admin/config/search/path/update_bulk', $edit, t('Update'));
  763. $this->assertText('No new URL aliases to generate.');
  764. $this->assertNoEntityAliasExists('node', $new_node);
  765. }
  766. }
  767. /**
  768. * Token functionality tests.
  769. */
  770. class PathautoTokenTestCase extends PathautoFunctionalTestHelper {
  771. public static function getInfo() {
  772. return array(
  773. 'name' => 'Pathauto tokens',
  774. 'description' => 'Tests tokens provided by Pathauto.',
  775. 'group' => 'Pathauto',
  776. 'dependencies' => array('token'),
  777. );
  778. }
  779. function testPathautoTokens() {
  780. $array = array(
  781. 'test first arg',
  782. 'The Array / value',
  783. );
  784. $tokens = array(
  785. 'join-path' => 'test-first-arg/array-value',
  786. );
  787. $data['array'] = $array;
  788. $replacements = $this->assertTokens('array', $data, $tokens);
  789. // Ensure that the pathauto_clean_token_values() function does not alter
  790. // this token value.
  791. module_load_include('inc', 'pathauto');
  792. pathauto_clean_token_values($replacements, $data, array());
  793. $this->assertEqual($replacements['[array:join-path]'], 'test-first-arg/array-value');
  794. }
  795. /**
  796. * Function copied from TokenTestHelper::assertTokens().
  797. */
  798. function assertTokens($type, array $data, array $tokens, array $options = array()) {
  799. $input = $this->mapTokenNames($type, array_keys($tokens));
  800. $replacements = token_generate($type, $input, $data, $options);
  801. foreach ($tokens as $name => $expected) {
  802. $token = $input[$name];
  803. if (!isset($expected)) {
  804. $this->assertTrue(!isset($values[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
  805. }
  806. elseif (!isset($replacements[$token])) {
  807. $this->fail(t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
  808. }
  809. elseif (!empty($options['regex'])) {
  810. $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)));
  811. }
  812. else {
  813. $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)));
  814. }
  815. }
  816. return $replacements;
  817. }
  818. function mapTokenNames($type, array $tokens = array()) {
  819. $return = array();
  820. foreach ($tokens as $token) {
  821. $return[$token] = "[$type:$token]";
  822. }
  823. return $return;
  824. }
  825. }