path.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. /**
  3. * @file
  4. * Tests for path.inc.
  5. */
  6. /**
  7. * Unit tests for the drupal_match_path() function in path.inc.
  8. *
  9. * @see drupal_match_path().
  10. */
  11. class DrupalMatchPathTestCase extends DrupalWebTestCase {
  12. protected $front;
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Drupal match path',
  16. 'description' => 'Tests the drupal_match_path() function to make sure it works properly.',
  17. 'group' => 'Path API',
  18. );
  19. }
  20. function setUp() {
  21. // Set up the database and testing environment.
  22. parent::setUp();
  23. // Set up a random site front page to test the '<front>' placeholder.
  24. $this->front = $this->randomName();
  25. variable_set('site_frontpage', $this->front);
  26. // Refresh our static variables from the database.
  27. $this->refreshVariables();
  28. }
  29. /**
  30. * Run through our test cases, making sure each one works as expected.
  31. */
  32. function testDrupalMatchPath() {
  33. // Set up our test cases.
  34. $tests = $this->drupalMatchPathTests();
  35. foreach ($tests as $patterns => $cases) {
  36. foreach ($cases as $path => $expected_result) {
  37. $actual_result = drupal_match_path($path, $patterns);
  38. $this->assertIdentical($actual_result, $expected_result, format_string('Tried matching the path <code>@path</code> to the pattern <pre>@patterns</pre> - expected @expected, got @actual.', array('@path' => $path, '@patterns' => $patterns, '@expected' => var_export($expected_result, TRUE), '@actual' => var_export($actual_result, TRUE))));
  39. }
  40. }
  41. }
  42. /**
  43. * Helper function for testDrupalMatchPath(): set up an array of test cases.
  44. *
  45. * @return
  46. * An array of test cases to cycle through.
  47. */
  48. private function drupalMatchPathTests() {
  49. return array(
  50. // Single absolute paths.
  51. 'blog/1' => array(
  52. 'blog/1' => TRUE,
  53. 'blog/2' => FALSE,
  54. 'test' => FALSE,
  55. ),
  56. // Single paths with wildcards.
  57. 'blog/*' => array(
  58. 'blog/1' => TRUE,
  59. 'blog/2' => TRUE,
  60. 'blog/3/edit' => TRUE,
  61. 'blog/' => TRUE,
  62. 'blog' => FALSE,
  63. 'test' => FALSE,
  64. ),
  65. // Single paths with multiple wildcards.
  66. 'node/*/revisions/*' => array(
  67. 'node/1/revisions/3' => TRUE,
  68. 'node/345/revisions/test' => TRUE,
  69. 'node/23/edit' => FALSE,
  70. 'test' => FALSE,
  71. ),
  72. // Single paths with '<front>'.
  73. '<front>' => array(
  74. $this->front => TRUE,
  75. "$this->front/" => FALSE,
  76. "$this->front/edit" => FALSE,
  77. 'node' => FALSE,
  78. '' => FALSE,
  79. ),
  80. // Paths with both '<front>' and wildcards (should not work).
  81. '<front>/*' => array(
  82. $this->front => FALSE,
  83. "$this->front/" => FALSE,
  84. "$this->front/edit" => FALSE,
  85. 'node/12' => FALSE,
  86. '' => FALSE,
  87. ),
  88. // Multiple paths with the \n delimiter.
  89. "node/*\nnode/*/edit" => array(
  90. 'node/1' => TRUE,
  91. 'node/view' => TRUE,
  92. 'node/32/edit' => TRUE,
  93. 'node/delete/edit' => TRUE,
  94. 'node/50/delete' => TRUE,
  95. 'test/example' => FALSE,
  96. ),
  97. // Multiple paths with the \r delimiter.
  98. "user/*\rblog/*" => array(
  99. 'user/1' => TRUE,
  100. 'blog/1' => TRUE,
  101. 'user/1/blog/1' => TRUE,
  102. 'user/blog' => TRUE,
  103. 'test/example' => FALSE,
  104. 'user' => FALSE,
  105. 'blog' => FALSE,
  106. ),
  107. // Multiple paths with the \r\n delimiter.
  108. "test\r\n<front>" => array(
  109. 'test' => TRUE,
  110. $this->front => TRUE,
  111. 'example' => FALSE,
  112. ),
  113. // Test existing regular expressions (should be escaped).
  114. '[^/]+?/[0-9]' => array(
  115. 'test/1' => FALSE,
  116. '[^/]+?/[0-9]' => TRUE,
  117. ),
  118. );
  119. }
  120. }
  121. /**
  122. * Tests hook_url_alter functions.
  123. */
  124. class UrlAlterFunctionalTest extends DrupalWebTestCase {
  125. public static function getInfo() {
  126. return array(
  127. 'name' => t('URL altering'),
  128. 'description' => t('Tests hook_url_inbound_alter() and hook_url_outbound_alter().'),
  129. 'group' => t('Path API'),
  130. );
  131. }
  132. function setUp() {
  133. parent::setUp('path', 'forum', 'url_alter_test');
  134. }
  135. /**
  136. * Test that URL altering works and that it occurs in the correct order.
  137. */
  138. function testUrlAlter() {
  139. $account = $this->drupalCreateUser(array('administer url aliases'));
  140. $this->drupalLogin($account);
  141. $uid = $account->uid;
  142. $name = $account->name;
  143. // Test a single altered path.
  144. $this->assertUrlInboundAlter("user/$name", "user/$uid");
  145. $this->assertUrlOutboundAlter("user/$uid", "user/$name");
  146. // Test that a path always uses its alias.
  147. $path = array('source' => "user/$uid/test1", 'alias' => 'alias/test1');
  148. path_save($path);
  149. $this->assertUrlInboundAlter('alias/test1', "user/$uid/test1");
  150. $this->assertUrlOutboundAlter("user/$uid/test1", 'alias/test1');
  151. // Test that alias source paths are normalized in the interface.
  152. $edit = array('source' => "user/$name/edit", 'alias' => 'alias/test2');
  153. $this->drupalPost('admin/config/search/path/add', $edit, t('Save'));
  154. $this->assertText(t('The alias has been saved.'));
  155. // Test that a path always uses its alias.
  156. $this->assertUrlInboundAlter('alias/test2', "user/$uid/edit");
  157. $this->assertUrlOutboundAlter("user/$uid/edit", 'alias/test2');
  158. // Test a non-existent user is not altered.
  159. $uid++;
  160. $this->assertUrlInboundAlter("user/$uid", "user/$uid");
  161. $this->assertUrlOutboundAlter("user/$uid", "user/$uid");
  162. // Test that 'forum' is altered to 'community' correctly, both at the root
  163. // level and for a specific existing forum.
  164. $this->assertUrlInboundAlter('community', 'forum');
  165. $this->assertUrlOutboundAlter('forum', 'community');
  166. $forum_vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE module = 'forum'")->fetchField();
  167. $tid = db_insert('taxonomy_term_data')
  168. ->fields(array(
  169. 'name' => $this->randomName(),
  170. 'vid' => $forum_vid,
  171. ))
  172. ->execute();
  173. $this->assertUrlInboundAlter("community/$tid", "forum/$tid");
  174. $this->assertUrlOutboundAlter("forum/$tid", "community/$tid");
  175. }
  176. /**
  177. * Test current_path() and request_path().
  178. */
  179. function testCurrentUrlRequestedPath() {
  180. $this->drupalGet('url-alter-test/bar');
  181. $this->assertRaw('request_path=url-alter-test/bar', 'request_path() returns the requested path.');
  182. $this->assertRaw('current_path=url-alter-test/foo', 'current_path() returns the internal path.');
  183. }
  184. /**
  185. * Tests that $_GET['q'] is initialized when the request path is empty.
  186. */
  187. function testGetQInitialized() {
  188. $this->drupalGet('');
  189. $this->assertText("\$_GET['q'] is non-empty with an empty request path.", "\$_GET['q'] is initialized with an empty request path.");
  190. }
  191. /**
  192. * Assert that an outbound path is altered to an expected value.
  193. *
  194. * @param $original
  195. * A string with the original path that is run through url().
  196. * @param $final
  197. * A string with the expected result after url().
  198. * @return
  199. * TRUE if $original was correctly altered to $final, FALSE otherwise.
  200. */
  201. protected function assertUrlOutboundAlter($original, $final) {
  202. // Test outbound altering.
  203. $result = url($original);
  204. $base_path = base_path() . (variable_get('clean_url', '0') ? '' : '?q=');
  205. $result = substr($result, strlen($base_path));
  206. $this->assertIdentical($result, $final, format_string('Altered outbound URL %original, expected %final, and got %result.', array('%original' => $original, '%final' => $final, '%result' => $result)));
  207. }
  208. /**
  209. * Assert that a inbound path is altered to an expected value.
  210. *
  211. * @param $original
  212. * A string with the aliased or un-normal path that is run through
  213. * drupal_get_normal_path().
  214. * @param $final
  215. * A string with the expected result after url().
  216. * @return
  217. * TRUE if $original was correctly altered to $final, FALSE otherwise.
  218. */
  219. protected function assertUrlInboundAlter($original, $final) {
  220. // Test inbound altering.
  221. $result = drupal_get_normal_path($original);
  222. $this->assertIdentical($result, $final, format_string('Altered inbound URL %original, expected %final, and got %result.', array('%original' => $original, '%final' => $final, '%result' => $result)));
  223. }
  224. }
  225. /**
  226. * Unit test for drupal_lookup_path().
  227. */
  228. class PathLookupTest extends DrupalWebTestCase {
  229. public static function getInfo() {
  230. return array(
  231. 'name' => t('Path lookup'),
  232. 'description' => t('Tests that drupal_lookup_path() returns correct paths.'),
  233. 'group' => t('Path API'),
  234. );
  235. }
  236. /**
  237. * Test that drupal_lookup_path() returns the correct path.
  238. */
  239. function testDrupalLookupPath() {
  240. $account = $this->drupalCreateUser();
  241. $uid = $account->uid;
  242. $name = $account->name;
  243. // Test the situation where the source is the same for multiple aliases.
  244. // Start with a language-neutral alias, which we will override.
  245. $path = array(
  246. 'source' => "user/$uid",
  247. 'alias' => 'foo',
  248. );
  249. path_save($path);
  250. $this->assertEqual(drupal_lookup_path('alias', $path['source']), $path['alias'], 'Basic alias lookup works.');
  251. $this->assertEqual(drupal_lookup_path('source', $path['alias']), $path['source'], 'Basic source lookup works.');
  252. // Create a language specific alias for the default language (English).
  253. $path = array(
  254. 'source' => "user/$uid",
  255. 'alias' => "users/$name",
  256. 'language' => 'en',
  257. );
  258. path_save($path);
  259. $this->assertEqual(drupal_lookup_path('alias', $path['source']), $path['alias'], 'English alias overrides language-neutral alias.');
  260. $this->assertEqual(drupal_lookup_path('source', $path['alias']), $path['source'], 'English source overrides language-neutral source.');
  261. // Create a language-neutral alias for the same path, again.
  262. $path = array(
  263. 'source' => "user/$uid",
  264. 'alias' => 'bar',
  265. );
  266. path_save($path);
  267. $this->assertEqual(drupal_lookup_path('alias', $path['source']), "users/$name", 'English alias still returned after entering a language-neutral alias.');
  268. // Create a language-specific (xx-lolspeak) alias for the same path.
  269. $path = array(
  270. 'source' => "user/$uid",
  271. 'alias' => 'LOL',
  272. 'language' => 'xx-lolspeak',
  273. );
  274. path_save($path);
  275. $this->assertEqual(drupal_lookup_path('alias', $path['source']), "users/$name", 'English alias still returned after entering a LOLspeak alias.');
  276. // The LOLspeak alias should be returned if we really want LOLspeak.
  277. $this->assertEqual(drupal_lookup_path('alias', $path['source'], 'xx-lolspeak'), 'LOL', 'LOLspeak alias returned if we specify xx-lolspeak to drupal_lookup_path().');
  278. // Create a new alias for this path in English, which should override the
  279. // previous alias for "user/$uid".
  280. $path = array(
  281. 'source' => "user/$uid",
  282. 'alias' => 'users/my-new-path',
  283. 'language' => 'en',
  284. );
  285. path_save($path);
  286. $this->assertEqual(drupal_lookup_path('alias', $path['source']), $path['alias'], 'Recently created English alias returned.');
  287. $this->assertEqual(drupal_lookup_path('source', $path['alias']), $path['source'], 'Recently created English source returned.');
  288. // Remove the English aliases, which should cause a fallback to the most
  289. // recently created language-neutral alias, 'bar'.
  290. db_delete('url_alias')
  291. ->condition('language', 'en')
  292. ->execute();
  293. drupal_clear_path_cache();
  294. $this->assertEqual(drupal_lookup_path('alias', $path['source']), 'bar', 'Path lookup falls back to recently created language-neutral alias.');
  295. // Test the situation where the alias and language are the same, but
  296. // the source differs. The newer alias record should be returned.
  297. $account2 = $this->drupalCreateUser();
  298. $path = array(
  299. 'source' => 'user/' . $account2->uid,
  300. 'alias' => 'bar',
  301. );
  302. path_save($path);
  303. $this->assertEqual(drupal_lookup_path('source', $path['alias']), $path['source'], 'Newer alias record is returned when comparing two LANGUAGE_NONE paths with the same alias.');
  304. }
  305. }
  306. /**
  307. * Tests the path_save() function.
  308. */
  309. class PathSaveTest extends DrupalWebTestCase {
  310. public static function getInfo() {
  311. return array(
  312. 'name' => t('Path save'),
  313. 'description' => t('Tests that path_save() exposes the previous alias value.'),
  314. 'group' => t('Path API'),
  315. );
  316. }
  317. function setUp() {
  318. // Enable a helper module that implements hook_path_update().
  319. parent::setUp('path_test');
  320. path_test_reset();
  321. }
  322. /**
  323. * Tests that path_save() makes the original path available to modules.
  324. */
  325. function testDrupalSaveOriginalPath() {
  326. $account = $this->drupalCreateUser();
  327. $uid = $account->uid;
  328. $name = $account->name;
  329. // Create a language-neutral alias.
  330. $path = array(
  331. 'source' => "user/$uid",
  332. 'alias' => 'foo',
  333. );
  334. $path_original = $path;
  335. path_save($path);
  336. // Alter the path.
  337. $path['alias'] = 'bar';
  338. path_save($path);
  339. // Test to see if the original alias is available to modules during
  340. // hook_path_update().
  341. $results = variable_get('path_test_results', array());
  342. $this->assertIdentical($results['hook_path_update']['original']['alias'], $path_original['alias'], 'Old path alias available to modules during hook_path_update.');
  343. $this->assertIdentical($results['hook_path_update']['original']['source'], $path_original['source'], 'Old path alias available to modules during hook_path_update.');
  344. }
  345. }