path.test 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Path module.
  5. */
  6. /**
  7. * Provides a base class for testing the Path module.
  8. */
  9. class PathTestCase extends DrupalWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Path alias functionality',
  13. 'description' => 'Add, edit, delete, and change alias and verify its consistency in the database.',
  14. 'group' => 'Path',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp('path');
  19. // Create test user and login.
  20. $web_user = $this->drupalCreateUser(array('create page content', 'edit own page content', 'administer url aliases', 'create url aliases', 'access content overview'));
  21. $this->drupalLogin($web_user);
  22. }
  23. /**
  24. * Tests the path cache.
  25. */
  26. function testPathCache() {
  27. // Create test node.
  28. $node1 = $this->drupalCreateNode();
  29. // Create alias.
  30. $edit = array();
  31. $edit['source'] = 'node/' . $node1->nid;
  32. $edit['alias'] = $this->randomName(8);
  33. $this->drupalPost('admin/config/search/path/add', $edit, t('Save'));
  34. // Visit the system path for the node and confirm a cache entry is
  35. // created.
  36. cache_clear_all('*', 'cache_path', TRUE);
  37. $this->drupalGet($edit['source']);
  38. $this->assertTrue(cache_get($edit['source'], 'cache_path'), 'Cache entry was created.');
  39. // Visit the alias for the node and confirm a cache entry is created.
  40. cache_clear_all('*', 'cache_path', TRUE);
  41. $this->drupalGet($edit['alias']);
  42. $this->assertTrue(cache_get($edit['source'], 'cache_path'), 'Cache entry was created.');
  43. }
  44. /**
  45. * Tests alias functionality through the admin interfaces.
  46. */
  47. function testAdminAlias() {
  48. // Create test node.
  49. $node1 = $this->drupalCreateNode();
  50. // Create alias.
  51. $edit = array();
  52. $edit['source'] = 'node/' . $node1->nid;
  53. $edit['alias'] = $this->randomName(8);
  54. $this->drupalPost('admin/config/search/path/add', $edit, t('Save'));
  55. // Confirm that the alias works.
  56. $this->drupalGet($edit['alias']);
  57. $this->assertText($node1->title, 'Alias works.');
  58. $this->assertResponse(200);
  59. // Change alias to one containing "exotic" characters.
  60. $pid = $this->getPID($edit['alias']);
  61. $previous = $edit['alias'];
  62. $edit['alias'] = "- ._~!$'\"()*@[]?&+%#,;=:" . // "Special" ASCII characters.
  63. "%23%25%26%2B%2F%3F" . // Characters that look like a percent-escaped string.
  64. "éøïвβ中國書۞"; // Characters from various non-ASCII alphabets.
  65. $this->drupalPost('admin/config/search/path/edit/' . $pid, $edit, t('Save'));
  66. // Confirm that the alias works.
  67. $this->drupalGet($edit['alias']);
  68. $this->assertText($node1->title, 'Changed alias works.');
  69. $this->assertResponse(200);
  70. drupal_static_reset('drupal_lookup_path');
  71. // Confirm that previous alias no longer works.
  72. $this->drupalGet($previous);
  73. $this->assertNoText($node1->title, 'Previous alias no longer works.');
  74. $this->assertResponse(404);
  75. // Create second test node.
  76. $node2 = $this->drupalCreateNode();
  77. // Set alias to second test node.
  78. $edit['source'] = 'node/' . $node2->nid;
  79. // leave $edit['alias'] the same
  80. $this->drupalPost('admin/config/search/path/add', $edit, t('Save'));
  81. // Confirm no duplicate was created.
  82. $this->assertRaw(t('The alias %alias is already in use in this language.', array('%alias' => $edit['alias'])), 'Attempt to move alias was rejected.');
  83. // Delete alias.
  84. $this->drupalPost('admin/config/search/path/edit/' . $pid, array(), t('Delete'));
  85. $this->drupalPost(NULL, array(), t('Confirm'));
  86. // Confirm that the alias no longer works.
  87. $this->drupalGet($edit['alias']);
  88. $this->assertNoText($node1->title, 'Alias was successfully deleted.');
  89. $this->assertResponse(404);
  90. }
  91. /**
  92. * Tests alias functionality through the node interfaces.
  93. */
  94. function testNodeAlias() {
  95. // Create test node.
  96. $node1 = $this->drupalCreateNode();
  97. // Create alias.
  98. $edit = array();
  99. $edit['path[alias]'] = $this->randomName(8);
  100. $this->drupalPost('node/' . $node1->nid . '/edit', $edit, t('Save'));
  101. // Confirm that the alias works.
  102. $this->drupalGet($edit['path[alias]']);
  103. $this->assertText($node1->title, 'Alias works.');
  104. $this->assertResponse(200);
  105. // Change alias to one containing "exotic" characters.
  106. $previous = $edit['path[alias]'];
  107. $edit['path[alias]'] = "- ._~!$'\"()*@[]?&+%#,;=:" . // "Special" ASCII characters.
  108. "%23%25%26%2B%2F%3F" . // Characters that look like a percent-escaped string.
  109. "éøïвβ中國書۞"; // Characters from various non-ASCII alphabets.
  110. $this->drupalPost('node/' . $node1->nid . '/edit', $edit, t('Save'));
  111. // Confirm that the alias works.
  112. $this->drupalGet($edit['path[alias]']);
  113. $this->assertText($node1->title, 'Changed alias works.');
  114. $this->assertResponse(200);
  115. // Make sure that previous alias no longer works.
  116. $this->drupalGet($previous);
  117. $this->assertNoText($node1->title, 'Previous alias no longer works.');
  118. $this->assertResponse(404);
  119. // Create second test node.
  120. $node2 = $this->drupalCreateNode();
  121. // Set alias to second test node.
  122. // Leave $edit['path[alias]'] the same.
  123. $this->drupalPost('node/' . $node2->nid . '/edit', $edit, t('Save'));
  124. // Confirm that the alias didn't make a duplicate.
  125. $this->assertText(t('The alias is already in use.'), 'Attempt to moved alias was rejected.');
  126. // Delete alias.
  127. $this->drupalPost('node/' . $node1->nid . '/edit', array('path[alias]' => ''), t('Save'));
  128. // Confirm that the alias no longer works.
  129. $this->drupalGet($edit['path[alias]']);
  130. $this->assertNoText($node1->title, 'Alias was successfully deleted.');
  131. $this->assertResponse(404);
  132. // Create third test node.
  133. $node3 = $this->drupalCreateNode();
  134. // Create an invalid alias with a leading slash and verify that the slash
  135. // is removed when the link is generated. This ensures that URL aliases
  136. // cannot be used to inject external URLs.
  137. // @todo The user interface should either display an error message or
  138. // automatically trim these invalid aliases, rather than allowing them to
  139. // be silently created, at which point the functional aspects of this
  140. // test will need to be moved elsewhere and switch to using a
  141. // programmatically-created alias instead.
  142. $alias = $this->randomName(8);
  143. $edit = array('path[alias]' => '/' . $alias);
  144. $this->drupalPost('node/' . $node3->nid . '/edit', $edit, t('Save'));
  145. $this->drupalGet('admin/content');
  146. // This checks the link href before clicking it, rather than using
  147. // DrupalWebTestCase::assertUrl() after clicking it, because the test
  148. // browser does not always preserve the correct number of slashes in the
  149. // URL when it visits internal links; using DrupalWebTestCase::assertUrl()
  150. // would actually make the test pass unconditionally on the testbot (or
  151. // anywhere else where Drupal is installed in a subdirectory).
  152. $link_xpath = $this->xpath('//a[normalize-space(text())=:label]', array(':label' => $node3->title));
  153. $link_href = (string) $link_xpath[0]['href'];
  154. $link_prefix = base_path() . (variable_get('clean_url', 0) ? '' : '?q=');
  155. $this->assertEqual($link_href, $link_prefix . $alias);
  156. $this->clickLink($node3->title);
  157. $this->assertResponse(404);
  158. }
  159. /**
  160. * Returns the path ID.
  161. *
  162. * @param $alias
  163. * A string containing an aliased path.
  164. *
  165. * @return int
  166. * Integer representing the path ID.
  167. */
  168. function getPID($alias) {
  169. return db_query("SELECT pid FROM {url_alias} WHERE alias = :alias", array(':alias' => $alias))->fetchField();
  170. }
  171. /**
  172. * Tests that duplicate aliases fail validation.
  173. */
  174. function testDuplicateNodeAlias() {
  175. // Create one node with a random alias.
  176. $node_one = $this->drupalCreateNode();
  177. $edit = array();
  178. $edit['path[alias]'] = $this->randomName();
  179. $this->drupalPost('node/' . $node_one->nid . '/edit', $edit, t('Save'));
  180. // Now create another node and try to set the same alias.
  181. $node_two = $this->drupalCreateNode();
  182. $this->drupalPost('node/' . $node_two->nid . '/edit', $edit, t('Save'));
  183. $this->assertText(t('The alias is already in use.'));
  184. $this->assertFieldByXPath("//input[@name='path[alias]' and contains(@class, 'error')]", $edit['path[alias]'], 'Textfield exists and has the error class.');
  185. }
  186. }
  187. /**
  188. * Tests URL aliases for taxonomy terms.
  189. */
  190. class PathTaxonomyTermTestCase extends DrupalWebTestCase {
  191. public static function getInfo() {
  192. return array(
  193. 'name' => 'Taxonomy term URL aliases',
  194. 'description' => 'Tests URL aliases for taxonomy terms.',
  195. 'group' => 'Path',
  196. );
  197. }
  198. function setUp() {
  199. parent::setUp('path', 'taxonomy');
  200. // Create and login user.
  201. $web_user = $this->drupalCreateUser(array('administer url aliases', 'administer taxonomy', 'access administration pages'));
  202. $this->drupalLogin($web_user);
  203. }
  204. /**
  205. * Tests alias functionality through the admin interfaces.
  206. */
  207. function testTermAlias() {
  208. // Create a term in the default 'Tags' vocabulary with URL alias.
  209. $vocabulary = taxonomy_vocabulary_load(1);
  210. $description = $this->randomName();;
  211. $edit = array();
  212. $edit['name'] = $this->randomName();
  213. $edit['description[value]'] = $description;
  214. $edit['path[alias]'] = $this->randomName();
  215. $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add', $edit, t('Save'));
  216. // Confirm that the alias works.
  217. $this->drupalGet($edit['path[alias]']);
  218. $this->assertText($description, 'Term can be accessed on URL alias.');
  219. // Change the term's URL alias.
  220. $tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name", array(':name' => $edit['name']))->fetchField();
  221. $edit2 = array();
  222. $edit2['path[alias]'] = $this->randomName();
  223. $this->drupalPost('taxonomy/term/' . $tid . '/edit', $edit2, t('Save'));
  224. // Confirm that the changed alias works.
  225. $this->drupalGet($edit2['path[alias]']);
  226. $this->assertText($description, 'Term can be accessed on changed URL alias.');
  227. // Confirm that the old alias no longer works.
  228. $this->drupalGet($edit['path[alias]']);
  229. $this->assertNoText($description, 'Old URL alias has been removed after altering.');
  230. $this->assertResponse(404, 'Old URL alias returns 404.');
  231. // Remove the term's URL alias.
  232. $edit3 = array();
  233. $edit3['path[alias]'] = '';
  234. $this->drupalPost('taxonomy/term/' . $tid . '/edit', $edit3, t('Save'));
  235. // Confirm that the alias no longer works.
  236. $this->drupalGet($edit2['path[alias]']);
  237. $this->assertNoText($description, 'Old URL alias has been removed after altering.');
  238. $this->assertResponse(404, 'Old URL alias returns 404.');
  239. }
  240. }
  241. /**
  242. * Tests URL aliases for translated nodes.
  243. */
  244. class PathLanguageTestCase extends DrupalWebTestCase {
  245. public static function getInfo() {
  246. return array(
  247. 'name' => 'Path aliases with translated nodes',
  248. 'description' => 'Confirm that paths work with translated nodes',
  249. 'group' => 'Path',
  250. );
  251. }
  252. function setUp() {
  253. parent::setUp('path', 'locale', 'translation');
  254. // Create and login user.
  255. $this->web_user = $this->drupalCreateUser(array('edit any page content', 'create page content', 'administer url aliases', 'create url aliases', 'administer languages', 'translate content', 'access administration pages'));
  256. $this->drupalLogin($this->web_user);
  257. // Enable French language.
  258. $edit = array();
  259. $edit['langcode'] = 'fr';
  260. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  261. // Enable URL language detection and selection.
  262. $edit = array('language[enabled][locale-url]' => 1);
  263. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  264. }
  265. /**
  266. * Test alias functionality through the admin interfaces.
  267. */
  268. function testAliasTranslation() {
  269. // Set 'page' content type to enable translation.
  270. variable_set('language_content_type_page', 2);
  271. $english_node = $this->drupalCreateNode(array('type' => 'page'));
  272. $english_alias = $this->randomName();
  273. // Edit the node to set language and path.
  274. $edit = array();
  275. $edit['language'] = 'en';
  276. $edit['path[alias]'] = $english_alias;
  277. $this->drupalPost('node/' . $english_node->nid . '/edit', $edit, t('Save'));
  278. // Confirm that the alias works.
  279. $this->drupalGet($english_alias);
  280. $this->assertText($english_node->title, 'Alias works.');
  281. // Translate the node into French.
  282. $this->drupalGet('node/' . $english_node->nid . '/translate');
  283. $this->clickLink(t('add translation'));
  284. $edit = array();
  285. $langcode = LANGUAGE_NONE;
  286. $edit["title"] = $this->randomName();
  287. $edit["body[$langcode][0][value]"] = $this->randomName();
  288. $french_alias = $this->randomName();
  289. $edit['path[alias]'] = $french_alias;
  290. $this->drupalPost(NULL, $edit, t('Save'));
  291. // Clear the path lookup cache.
  292. drupal_lookup_path('wipe');
  293. // Ensure the node was created.
  294. $french_node = $this->drupalGetNodeByTitle($edit["title"]);
  295. $this->assertTrue(($french_node), 'Node found in database.');
  296. // Confirm that the alias works.
  297. $this->drupalGet('fr/' . $edit['path[alias]']);
  298. $this->assertText($french_node->title, 'Alias for French translation works.');
  299. // Confirm that the alias is returned by url().
  300. drupal_static_reset('language_list');
  301. drupal_static_reset('locale_url_outbound_alter');
  302. $languages = language_list();
  303. $url = url('node/' . $french_node->nid, array('language' => $languages[$french_node->language]));
  304. $this->assertTrue(strpos($url, $edit['path[alias]']), 'URL contains the path alias.');
  305. // Confirm that the alias works even when changing language negotiation
  306. // options. Enable User language detection and selection over URL one.
  307. $edit = array(
  308. 'language[enabled][locale-user]' => 1,
  309. 'language[weight][locale-user]' => -9,
  310. 'language[enabled][locale-url]' => 1,
  311. 'language[weight][locale-url]' => -8,
  312. );
  313. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  314. // Change user language preference.
  315. $edit = array('language' => 'fr');
  316. $this->drupalPost("user/{$this->web_user->uid}/edit", $edit, t('Save'));
  317. // Check that the English alias works. In this situation French is the
  318. // current UI and content language, while URL language is English (since we
  319. // do not have a path prefix we fall back to the site's default language).
  320. // We need to ensure that the user language preference is not taken into
  321. // account while determining the path alias language, because if this
  322. // happens we have no way to check that the path alias is valid: there is no
  323. // path alias for French matching the english alias. So drupal_lookup_path()
  324. // needs to use the URL language to check whether the alias is valid.
  325. $this->drupalGet($english_alias);
  326. $this->assertText($english_node->title, 'Alias for English translation works.');
  327. // Check that the French alias works.
  328. $this->drupalGet("fr/$french_alias");
  329. $this->assertText($french_node->title, 'Alias for French translation works.');
  330. // Disable URL language negotiation.
  331. $edit = array('language[enabled][locale-url]' => FALSE);
  332. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  333. // Check that the English alias still works.
  334. $this->drupalGet($english_alias);
  335. $this->assertText($english_node->title, 'Alias for English translation works.');
  336. // Check that the French alias is not available. We check the unprefixed
  337. // alias because we disabled URL language negotiation above. In this
  338. // situation only aliases in the default language and language neutral ones
  339. // should keep working.
  340. $this->drupalGet($french_alias);
  341. $this->assertResponse(404, 'Alias for French translation is unavailable when URL language negotiation is disabled.');
  342. // drupal_lookup_path() has an internal static cache. Check to see that
  343. // it has the appropriate contents at this point.
  344. drupal_lookup_path('wipe');
  345. $french_node_path = drupal_lookup_path('source', $french_alias, $french_node->language);
  346. $this->assertEqual($french_node_path, 'node/' . $french_node->nid, 'Normal path works.');
  347. // Second call should return the same path.
  348. $french_node_path = drupal_lookup_path('source', $french_alias, $french_node->language);
  349. $this->assertEqual($french_node_path, 'node/' . $french_node->nid, 'Normal path is the same.');
  350. // Confirm that the alias works.
  351. $french_node_alias = drupal_lookup_path('alias', 'node/' . $french_node->nid, $french_node->language);
  352. $this->assertEqual($french_node_alias, $french_alias, 'Alias works.');
  353. // Second call should return the same alias.
  354. $french_node_alias = drupal_lookup_path('alias', 'node/' . $french_node->nid, $french_node->language);
  355. $this->assertEqual($french_node_alias, $french_alias, 'Alias is the same.');
  356. }
  357. }
  358. /**
  359. * Tests the user interface for creating path aliases, with languages.
  360. */
  361. class PathLanguageUITestCase extends DrupalWebTestCase {
  362. public static function getInfo() {
  363. return array(
  364. 'name' => 'Path aliases with languages',
  365. 'description' => 'Confirm that the Path module user interface works with languages.',
  366. 'group' => 'Path',
  367. );
  368. }
  369. function setUp() {
  370. parent::setUp('path', 'locale');
  371. // Create and login user.
  372. $web_user = $this->drupalCreateUser(array('edit any page content', 'create page content', 'administer url aliases', 'create url aliases', 'administer languages', 'access administration pages'));
  373. $this->drupalLogin($web_user);
  374. // Enable French language.
  375. $edit = array();
  376. $edit['langcode'] = 'fr';
  377. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  378. // Enable URL language detection and selection.
  379. $edit = array('language[enabled][locale-url]' => 1);
  380. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  381. }
  382. /**
  383. * Tests that a language-neutral URL alias works.
  384. */
  385. function testLanguageNeutralURLs() {
  386. $name = $this->randomName(8);
  387. $edit = array();
  388. $edit['source'] = 'admin/config/search/path';
  389. $edit['alias'] = $name;
  390. $this->drupalPost('admin/config/search/path/add', $edit, t('Save'));
  391. $this->drupalGet($name);
  392. $this->assertText(t('Filter aliases'), 'Language-neutral URL alias works');
  393. }
  394. /**
  395. * Tests that a default language URL alias works.
  396. */
  397. function testDefaultLanguageURLs() {
  398. $name = $this->randomName(8);
  399. $edit = array();
  400. $edit['source'] = 'admin/config/search/path';
  401. $edit['alias'] = $name;
  402. $edit['language'] = 'en';
  403. $this->drupalPost('admin/config/search/path/add', $edit, t('Save'));
  404. $this->drupalGet($name);
  405. $this->assertText(t('Filter aliases'), 'English URL alias works');
  406. }
  407. /**
  408. * Tests that a non-default language URL alias works.
  409. */
  410. function testNonDefaultURLs() {
  411. $name = $this->randomName(8);
  412. $edit = array();
  413. $edit['source'] = 'admin/config/search/path';
  414. $edit['alias'] = $name;
  415. $edit['language'] = 'fr';
  416. $this->drupalPost('admin/config/search/path/add', $edit, t('Save'));
  417. $this->drupalGet('fr/' . $name);
  418. $this->assertText(t('Filter aliases'), 'Foreign URL alias works');
  419. }
  420. }
  421. /**
  422. * Tests that paths are not prefixed on a monolingual site.
  423. */
  424. class PathMonolingualTestCase extends DrupalWebTestCase {
  425. public static function getInfo() {
  426. return array(
  427. 'name' => 'Paths on non-English monolingual sites',
  428. 'description' => 'Confirm that paths are not changed on monolingual non-English sites',
  429. 'group' => 'Path',
  430. );
  431. }
  432. function setUp() {
  433. global $language;
  434. parent::setUp('path', 'locale', 'translation');
  435. // Create and login user.
  436. $web_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
  437. $this->drupalLogin($web_user);
  438. // Enable French language.
  439. $edit = array();
  440. $edit['langcode'] = 'fr';
  441. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  442. // Make French the default language.
  443. $edit = array('site_default' => 'fr');
  444. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  445. // Disable English.
  446. $edit = array('enabled[en]' => FALSE);
  447. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  448. // Verify that French is the only language.
  449. $this->assertFalse(drupal_multilingual(), 'Site is mono-lingual');
  450. $this->assertEqual(language_default('language'), 'fr', 'French is the default language');
  451. // Set language detection to URL.
  452. $edit = array('language[enabled][locale-url]' => TRUE);
  453. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  454. // Force languages to be initialized.
  455. drupal_language_initialize();
  456. }
  457. /**
  458. * Verifies that links do not have language prefixes in them.
  459. */
  460. function testPageLinks() {
  461. // Navigate to 'admin/config' path.
  462. $this->drupalGet('admin/config');
  463. // Verify that links in this page do not have a 'fr/' prefix.
  464. $this->assertNoLinkByHref('/fr/', 'Links do not contain language prefix');
  465. // Verify that links in this page can be followed and work.
  466. $this->clickLink(t('Languages'));
  467. $this->assertResponse(200, 'Clicked link results in a valid page');
  468. $this->assertText(t('Add language'), 'Page contains the add language text');
  469. }
  470. }