path.test 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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'));
  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. }
  133. /**
  134. * Returns the path ID.
  135. *
  136. * @param $alias
  137. * A string containing an aliased path.
  138. *
  139. * @return int
  140. * Integer representing the path ID.
  141. */
  142. function getPID($alias) {
  143. return db_query("SELECT pid FROM {url_alias} WHERE alias = :alias", array(':alias' => $alias))->fetchField();
  144. }
  145. /**
  146. * Tests that duplicate aliases fail validation.
  147. */
  148. function testDuplicateNodeAlias() {
  149. // Create one node with a random alias.
  150. $node_one = $this->drupalCreateNode();
  151. $edit = array();
  152. $edit['path[alias]'] = $this->randomName();
  153. $this->drupalPost('node/' . $node_one->nid . '/edit', $edit, t('Save'));
  154. // Now create another node and try to set the same alias.
  155. $node_two = $this->drupalCreateNode();
  156. $this->drupalPost('node/' . $node_two->nid . '/edit', $edit, t('Save'));
  157. $this->assertText(t('The alias is already in use.'));
  158. $this->assertFieldByXPath("//input[@name='path[alias]' and contains(@class, 'error')]", $edit['path[alias]'], 'Textfield exists and has the error class.');
  159. }
  160. }
  161. /**
  162. * Tests URL aliases for taxonomy terms.
  163. */
  164. class PathTaxonomyTermTestCase extends DrupalWebTestCase {
  165. public static function getInfo() {
  166. return array(
  167. 'name' => 'Taxonomy term URL aliases',
  168. 'description' => 'Tests URL aliases for taxonomy terms.',
  169. 'group' => 'Path',
  170. );
  171. }
  172. function setUp() {
  173. parent::setUp('path', 'taxonomy');
  174. // Create and login user.
  175. $web_user = $this->drupalCreateUser(array('administer url aliases', 'administer taxonomy', 'access administration pages'));
  176. $this->drupalLogin($web_user);
  177. }
  178. /**
  179. * Tests alias functionality through the admin interfaces.
  180. */
  181. function testTermAlias() {
  182. // Create a term in the default 'Tags' vocabulary with URL alias.
  183. $vocabulary = taxonomy_vocabulary_load(1);
  184. $description = $this->randomName();;
  185. $edit = array();
  186. $edit['name'] = $this->randomName();
  187. $edit['description[value]'] = $description;
  188. $edit['path[alias]'] = $this->randomName();
  189. $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add', $edit, t('Save'));
  190. // Confirm that the alias works.
  191. $this->drupalGet($edit['path[alias]']);
  192. $this->assertText($description, 'Term can be accessed on URL alias.');
  193. // Change the term's URL alias.
  194. $tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name", array(':name' => $edit['name']))->fetchField();
  195. $edit2 = array();
  196. $edit2['path[alias]'] = $this->randomName();
  197. $this->drupalPost('taxonomy/term/' . $tid . '/edit', $edit2, t('Save'));
  198. // Confirm that the changed alias works.
  199. $this->drupalGet($edit2['path[alias]']);
  200. $this->assertText($description, 'Term can be accessed on changed URL alias.');
  201. // Confirm that the old alias no longer works.
  202. $this->drupalGet($edit['path[alias]']);
  203. $this->assertNoText($description, 'Old URL alias has been removed after altering.');
  204. $this->assertResponse(404, 'Old URL alias returns 404.');
  205. // Remove the term's URL alias.
  206. $edit3 = array();
  207. $edit3['path[alias]'] = '';
  208. $this->drupalPost('taxonomy/term/' . $tid . '/edit', $edit3, t('Save'));
  209. // Confirm that the alias no longer works.
  210. $this->drupalGet($edit2['path[alias]']);
  211. $this->assertNoText($description, 'Old URL alias has been removed after altering.');
  212. $this->assertResponse(404, 'Old URL alias returns 404.');
  213. }
  214. }
  215. /**
  216. * Tests URL aliases for translated nodes.
  217. */
  218. class PathLanguageTestCase extends DrupalWebTestCase {
  219. public static function getInfo() {
  220. return array(
  221. 'name' => 'Path aliases with translated nodes',
  222. 'description' => 'Confirm that paths work with translated nodes',
  223. 'group' => 'Path',
  224. );
  225. }
  226. function setUp() {
  227. parent::setUp('path', 'locale', 'translation');
  228. // Create and login user.
  229. $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'));
  230. $this->drupalLogin($this->web_user);
  231. // Enable French language.
  232. $edit = array();
  233. $edit['langcode'] = 'fr';
  234. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  235. // Enable URL language detection and selection.
  236. $edit = array('language[enabled][locale-url]' => 1);
  237. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  238. }
  239. /**
  240. * Test alias functionality through the admin interfaces.
  241. */
  242. function testAliasTranslation() {
  243. // Set 'page' content type to enable translation.
  244. variable_set('language_content_type_page', 2);
  245. $english_node = $this->drupalCreateNode(array('type' => 'page'));
  246. $english_alias = $this->randomName();
  247. // Edit the node to set language and path.
  248. $edit = array();
  249. $edit['language'] = 'en';
  250. $edit['path[alias]'] = $english_alias;
  251. $this->drupalPost('node/' . $english_node->nid . '/edit', $edit, t('Save'));
  252. // Confirm that the alias works.
  253. $this->drupalGet($english_alias);
  254. $this->assertText($english_node->title, 'Alias works.');
  255. // Translate the node into French.
  256. $this->drupalGet('node/' . $english_node->nid . '/translate');
  257. $this->clickLink(t('add translation'));
  258. $edit = array();
  259. $langcode = LANGUAGE_NONE;
  260. $edit["title"] = $this->randomName();
  261. $edit["body[$langcode][0][value]"] = $this->randomName();
  262. $french_alias = $this->randomName();
  263. $edit['path[alias]'] = $french_alias;
  264. $this->drupalPost(NULL, $edit, t('Save'));
  265. // Clear the path lookup cache.
  266. drupal_lookup_path('wipe');
  267. // Ensure the node was created.
  268. $french_node = $this->drupalGetNodeByTitle($edit["title"]);
  269. $this->assertTrue(($french_node), 'Node found in database.');
  270. // Confirm that the alias works.
  271. $this->drupalGet('fr/' . $edit['path[alias]']);
  272. $this->assertText($french_node->title, 'Alias for French translation works.');
  273. // Confirm that the alias is returned by url().
  274. drupal_static_reset('language_list');
  275. drupal_static_reset('locale_url_outbound_alter');
  276. $languages = language_list();
  277. $url = url('node/' . $french_node->nid, array('language' => $languages[$french_node->language]));
  278. $this->assertTrue(strpos($url, $edit['path[alias]']), 'URL contains the path alias.');
  279. // Confirm that the alias works even when changing language negotiation
  280. // options. Enable User language detection and selection over URL one.
  281. $edit = array(
  282. 'language[enabled][locale-user]' => 1,
  283. 'language[weight][locale-user]' => -9,
  284. 'language[enabled][locale-url]' => 1,
  285. 'language[weight][locale-url]' => -8,
  286. );
  287. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  288. // Change user language preference.
  289. $edit = array('language' => 'fr');
  290. $this->drupalPost("user/{$this->web_user->uid}/edit", $edit, t('Save'));
  291. // Check that the English alias works. In this situation French is the
  292. // current UI and content language, while URL language is English (since we
  293. // do not have a path prefix we fall back to the site's default language).
  294. // We need to ensure that the user language preference is not taken into
  295. // account while determining the path alias language, because if this
  296. // happens we have no way to check that the path alias is valid: there is no
  297. // path alias for French matching the english alias. So drupal_lookup_path()
  298. // needs to use the URL language to check whether the alias is valid.
  299. $this->drupalGet($english_alias);
  300. $this->assertText($english_node->title, 'Alias for English translation works.');
  301. // Check that the French alias works.
  302. $this->drupalGet("fr/$french_alias");
  303. $this->assertText($french_node->title, 'Alias for French translation works.');
  304. // Disable URL language negotiation.
  305. $edit = array('language[enabled][locale-url]' => FALSE);
  306. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  307. // Check that the English alias still works.
  308. $this->drupalGet($english_alias);
  309. $this->assertText($english_node->title, 'Alias for English translation works.');
  310. // Check that the French alias is not available. We check the unprefixed
  311. // alias because we disabled URL language negotiation above. In this
  312. // situation only aliases in the default language and language neutral ones
  313. // should keep working.
  314. $this->drupalGet($french_alias);
  315. $this->assertResponse(404, 'Alias for French translation is unavailable when URL language negotiation is disabled.');
  316. // drupal_lookup_path() has an internal static cache. Check to see that
  317. // it has the appropriate contents at this point.
  318. drupal_lookup_path('wipe');
  319. $french_node_path = drupal_lookup_path('source', $french_alias, $french_node->language);
  320. $this->assertEqual($french_node_path, 'node/' . $french_node->nid, 'Normal path works.');
  321. // Second call should return the same path.
  322. $french_node_path = drupal_lookup_path('source', $french_alias, $french_node->language);
  323. $this->assertEqual($french_node_path, 'node/' . $french_node->nid, 'Normal path is the same.');
  324. // Confirm that the alias works.
  325. $french_node_alias = drupal_lookup_path('alias', 'node/' . $french_node->nid, $french_node->language);
  326. $this->assertEqual($french_node_alias, $french_alias, 'Alias works.');
  327. // Second call should return the same alias.
  328. $french_node_alias = drupal_lookup_path('alias', 'node/' . $french_node->nid, $french_node->language);
  329. $this->assertEqual($french_node_alias, $french_alias, 'Alias is the same.');
  330. }
  331. }
  332. /**
  333. * Tests the user interface for creating path aliases, with languages.
  334. */
  335. class PathLanguageUITestCase extends DrupalWebTestCase {
  336. public static function getInfo() {
  337. return array(
  338. 'name' => 'Path aliases with languages',
  339. 'description' => 'Confirm that the Path module user interface works with languages.',
  340. 'group' => 'Path',
  341. );
  342. }
  343. function setUp() {
  344. parent::setUp('path', 'locale');
  345. // Create and login user.
  346. $web_user = $this->drupalCreateUser(array('edit any page content', 'create page content', 'administer url aliases', 'create url aliases', 'administer languages', 'access administration pages'));
  347. $this->drupalLogin($web_user);
  348. // Enable French language.
  349. $edit = array();
  350. $edit['langcode'] = 'fr';
  351. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  352. // Enable URL language detection and selection.
  353. $edit = array('language[enabled][locale-url]' => 1);
  354. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  355. }
  356. /**
  357. * Tests that a language-neutral URL alias works.
  358. */
  359. function testLanguageNeutralURLs() {
  360. $name = $this->randomName(8);
  361. $edit = array();
  362. $edit['source'] = 'admin/config/search/path';
  363. $edit['alias'] = $name;
  364. $this->drupalPost('admin/config/search/path/add', $edit, t('Save'));
  365. $this->drupalGet($name);
  366. $this->assertText(t('Filter aliases'), 'Language-neutral URL alias works');
  367. }
  368. /**
  369. * Tests that a default language URL alias works.
  370. */
  371. function testDefaultLanguageURLs() {
  372. $name = $this->randomName(8);
  373. $edit = array();
  374. $edit['source'] = 'admin/config/search/path';
  375. $edit['alias'] = $name;
  376. $edit['language'] = 'en';
  377. $this->drupalPost('admin/config/search/path/add', $edit, t('Save'));
  378. $this->drupalGet($name);
  379. $this->assertText(t('Filter aliases'), 'English URL alias works');
  380. }
  381. /**
  382. * Tests that a non-default language URL alias works.
  383. */
  384. function testNonDefaultURLs() {
  385. $name = $this->randomName(8);
  386. $edit = array();
  387. $edit['source'] = 'admin/config/search/path';
  388. $edit['alias'] = $name;
  389. $edit['language'] = 'fr';
  390. $this->drupalPost('admin/config/search/path/add', $edit, t('Save'));
  391. $this->drupalGet('fr/' . $name);
  392. $this->assertText(t('Filter aliases'), 'Foreign URL alias works');
  393. }
  394. }
  395. /**
  396. * Tests that paths are not prefixed on a monolingual site.
  397. */
  398. class PathMonolingualTestCase extends DrupalWebTestCase {
  399. public static function getInfo() {
  400. return array(
  401. 'name' => 'Paths on non-English monolingual sites',
  402. 'description' => 'Confirm that paths are not changed on monolingual non-English sites',
  403. 'group' => 'Path',
  404. );
  405. }
  406. function setUp() {
  407. global $language;
  408. parent::setUp('path', 'locale', 'translation');
  409. // Create and login user.
  410. $web_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
  411. $this->drupalLogin($web_user);
  412. // Enable French language.
  413. $edit = array();
  414. $edit['langcode'] = 'fr';
  415. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  416. // Make French the default language.
  417. $edit = array('site_default' => 'fr');
  418. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  419. // Disable English.
  420. $edit = array('enabled[en]' => FALSE);
  421. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  422. // Verify that French is the only language.
  423. $this->assertFalse(drupal_multilingual(), 'Site is mono-lingual');
  424. $this->assertEqual(language_default('language'), 'fr', 'French is the default language');
  425. // Set language detection to URL.
  426. $edit = array('language[enabled][locale-url]' => TRUE);
  427. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  428. // Force languages to be initialized.
  429. drupal_language_initialize();
  430. }
  431. /**
  432. * Verifies that links do not have language prefixes in them.
  433. */
  434. function testPageLinks() {
  435. // Navigate to 'admin/config' path.
  436. $this->drupalGet('admin/config');
  437. // Verify that links in this page do not have a 'fr/' prefix.
  438. $this->assertNoLinkByHref('/fr/', 'Links do not contain language prefix');
  439. // Verify that links in this page can be followed and work.
  440. $this->clickLink(t('Languages'));
  441. $this->assertResponse(200, 'Clicked link results in a valid page');
  442. $this->assertText(t('Add language'), 'Page contains the add language text');
  443. }
  444. }