link.validate.test 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. /**
  3. * @file
  4. * Tests that exercise the validation functions in the link module.
  5. */
  6. class LinkValidateTestCase extends LinkBaseTestClass {
  7. protected function createLink($url, $title, $attributes = array()) {
  8. return array(
  9. 'url' => $url,
  10. 'title' => $title,
  11. 'attributes' => $attributes,
  12. );
  13. }
  14. /**
  15. * Takes a url, and sees if it can validate that the url is valid.
  16. */
  17. protected function link_test_validate_url($url) {
  18. $field_name = $this->createLinkField();
  19. $label = $this->randomName();
  20. $settings = array(
  21. 'title' => $label,
  22. $field_name => array(
  23. LANGUAGE_NONE=> array(
  24. array(
  25. 'title' => $label,
  26. 'url' => $url,
  27. )
  28. ),
  29. ),
  30. );
  31. $node = $this->drupalCreateNode($settings);
  32. $this->assertNotNull($node, ' has been created.', 'Node created');
  33. $this->assertEqual($url, $node->{$field_name}[LANGUAGE_NONE][0]['url']);
  34. }
  35. }
  36. class LinkValidateTest extends LinkValidateTestCase {
  37. public static function getInfo() {
  38. return array(
  39. 'name' => 'Link Validation Tests',
  40. 'description' => 'Tests the field validation.',
  41. 'group' => 'Link',
  42. );
  43. }
  44. function test_link_validate_basic_url() {
  45. $this->link_test_validate_url('http://www.example.com');
  46. }
  47. /**
  48. * Test if we're stopped from posting a bad url on default validation.
  49. */
  50. function test_link_validate_bad_url_validate_default() {
  51. $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  52. $perms = array_keys($perms[array_search('administrator', user_roles())]);
  53. $admin = $this->drupalCreateUser($perms);
  54. $this->drupalLogin($admin);
  55. // create field
  56. $name = strtolower($this->randomName());
  57. $edit = array(
  58. 'fields[_add_new_field][label]' => $name,
  59. 'fields[_add_new_field][field_name]' => $name,
  60. 'fields[_add_new_field][type]' => 'link_field',
  61. 'fields[_add_new_field][widget_type]' => 'link_field',
  62. );
  63. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  64. $this->drupalPost(NULL, array(), t('Save field settings'));
  65. $this->drupalPost(NULL, array(), t('Save settings'));
  66. // Is field created?
  67. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  68. node_types_rebuild();
  69. menu_rebuild();
  70. // create page form
  71. $this->drupalGet('node/add/page');
  72. $field_name = 'field_' . $name;
  73. $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
  74. $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
  75. $edit = array(
  76. 'title' => 'Simple Title',
  77. $field_name . '[und][0][url]' => 'edik:naw',
  78. );
  79. $this->drupalPost(NULL, $edit, t('Save'));
  80. $this->assertText(t('The value @value provided for @field is not a valid URL.', array('@value' => 'edik:naw', '@field' => $name)));
  81. }
  82. /**
  83. * Test if we're stopped from posting a bad url with validation on.
  84. */
  85. function test_link_validate_bad_url_validate_on() {
  86. $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  87. $perms = array_keys($perms[array_search('administrator', user_roles())]);
  88. $admin = $this->drupalCreateUser($perms);
  89. $this->drupalLogin($admin);
  90. // create field
  91. $name = strtolower($this->randomName());
  92. $edit = array(
  93. 'fields[_add_new_field][label]' => $name,
  94. 'fields[_add_new_field][field_name]' => $name,
  95. 'fields[_add_new_field][type]' => 'link_field',
  96. 'fields[_add_new_field][widget_type]' => 'link_field',
  97. );
  98. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  99. $this->drupalPost(NULL, array(), t('Save field settings'));
  100. $this->drupalPost(NULL, array('instance[settings][validate_url]' => TRUE), t('Save settings'));
  101. // Is field created?
  102. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  103. node_types_rebuild();
  104. menu_rebuild();
  105. // create page form
  106. $this->drupalGet('node/add/page');
  107. $field_name = 'field_' . $name;
  108. $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
  109. $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
  110. $edit = array(
  111. 'title' => 'Simple Title',
  112. $field_name . '[und][0][url]' => 'edik:naw',
  113. );
  114. $this->drupalPost(NULL, $edit, t('Save'));
  115. $this->assertText(t('The value @value provided for @field is not a valid URL.', array('@field' => $name, '@value' => 'edik:naw')));
  116. }
  117. /**
  118. * Test if we can post a bad url if the validation is expressly turned off.
  119. */
  120. function test_link_validate_bad_url_validate_off() {
  121. $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  122. $perms = array_keys($perms[array_search('administrator', user_roles())]);
  123. $admin = $this->drupalCreateUser($perms);
  124. $this->drupalLogin($admin);
  125. // create field
  126. $name = strtolower($this->randomName());
  127. $edit = array(
  128. 'fields[_add_new_field][label]' => $name,
  129. 'fields[_add_new_field][field_name]' => $name,
  130. 'fields[_add_new_field][type]' => 'link_field',
  131. 'fields[_add_new_field][widget_type]' => 'link_field',
  132. );
  133. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  134. $this->drupalPost(NULL, array(), t('Save field settings'));
  135. $this->drupalPost(NULL, array('instance[settings][validate_url]' => FALSE), t('Save settings'));
  136. /*$instance_details = db_query("SELECT * FROM {field_config_instance} WHERE field_name = :field_name AND bundle = 'page'", array(':field_name' => 'field_'. $name))->fetchObject();
  137. $this->fail('<pre>'. print_r($instance_details, TRUE) .'</pre>');
  138. $this->fail('<pre>'. print_r(unserialize($instance_details->data), TRUE) .'</pre>');*/
  139. // Is field created?
  140. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  141. node_types_rebuild();
  142. menu_rebuild();
  143. // create page form
  144. $this->drupalGet('node/add/page');
  145. $field_name = 'field_' . $name;
  146. $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
  147. $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
  148. $edit = array(
  149. 'title' => 'Simple Title',
  150. $field_name . '[und][0][url]' => 'edik:naw',
  151. );
  152. $this->drupalPost(NULL, $edit, t('Save'));
  153. $this->assertNoText(t('The value %value provided for %field is not a valid URL.', array('%field' => $name, '%value' => 'edik:naw')));
  154. }
  155. /**
  156. * Test if a bad url can sneak through un-filtered if we play with the validation...
  157. */
  158. function x_test_link_validate_switching_between_validation_status() {
  159. $this->acquireContentTypes(1);
  160. $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  161. $perms = array_keys($perms[array_search('administrator', user_roles())]);
  162. $admin = $this->drupalCreateUser($perms);
  163. $this->drupalLogin($admin);
  164. variable_set('node_options_' . $this->content_types[0]->name, array('status', 'promote'));
  165. $field_settings = array(
  166. 'type' => 'link',
  167. 'widget_type' => 'link',
  168. 'type_name' => $this->content_types[0]->name,
  169. 'attributes' => array(), // <-- This is needed or we have an error
  170. 'validate_url' => 0,
  171. );
  172. $field = $this->createField($field_settings, 0);
  173. //$this->fail('<pre>'. print_r($field, TRUE) .'</pre>');
  174. $field_db_info = content_database_info($field);
  175. $this->acquireNodes(2);
  176. $node = node_load($this->nodes[0]->nid);
  177. $this->drupalGet('node/' . $this->nodes[0]->nid);
  178. $edit = array();
  179. $title = $this->randomName();
  180. $url = 'javascript:alert("http://example.com/' . $this->randomName() . '")';
  181. $edit[$field['field_name'] . '[0][url]'] = $url;
  182. $edit[$field['field_name'] . '[0][title]'] = $title;
  183. $this->drupalPost('node/' . $this->nodes[0]->nid . '/edit', $edit, t('Save'));
  184. //$this->pass($this->content);
  185. $this->assertNoText(t('The value %value provided for %field is not a valid URL.', array('%field' => $name, '%value' => trim($url))));
  186. // Make sure we get a new version!
  187. $node = node_load($this->nodes[0]->nid, NULL, TRUE);
  188. $this->assertEqual($url, $node->{$field['field_name']}[0]['url']);
  189. $this->drupalGet('node/' . $node->nid);
  190. $this->assertNoRaw($url, 'Make sure Javascript does not display.');
  191. // Turn the array validation back _on_.
  192. $edit = array('validate_url' => TRUE);
  193. $node_type_link = str_replace('_', '-', $node->type);
  194. //$this->drupalGet('admin/content/node-type/'. $node_type_link .'/fields'); ///'. $field['field_name']);
  195. //$this->fail($this->content);
  196. $this->drupalPost('admin/content/node-type/' . $node_type_link . '/fields/' . $field['field_name'], $edit, t('Save field settings'));
  197. $this->drupalGet('node/' . $node->nid);
  198. // This actually works because the display_url goes through the core
  199. // url() function. But we should have a test that makes sure it continues
  200. // to work.
  201. $this->assertNoRaw($url, 'Make sure Javascript does not display.');
  202. //$this->fail($this->content);
  203. }
  204. // Validate that '<front>' is a valid url.
  205. function test_link_front_url() {
  206. $this->link_test_validate_url('<front>');
  207. }
  208. // Validate that an internal url would be accepted.
  209. function test_link_internal_url() {
  210. // Create the content first.
  211. $node = $this->drupalCreateNode();
  212. $link = 'node/' . $node->nid;
  213. $this->link_test_validate_url($link);
  214. $type = link_url_type($link);
  215. $this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
  216. }
  217. // Validate a simple mailto.
  218. function test_link_mailto() {
  219. $this->link_test_validate_url('mailto:jcfiala@gmail.com');
  220. }
  221. function test_link_external_https() {
  222. $this->link_test_validate_url('https://www.example.com/');
  223. }
  224. function test_link_ftp() {
  225. $this->link_test_validate_url('ftp://www.example.com/');
  226. }
  227. }
  228. class LinkValidateTestNews extends LinkValidateTestCase {
  229. public static function getInfo() {
  230. return array(
  231. 'name' => 'Link News Validation Tests',
  232. 'description' => 'Tests the field validation for usenet urls.',
  233. 'group' => 'Link',
  234. );
  235. }
  236. // Validate a news link to a message group
  237. function test_link_news() {
  238. $this->link_test_validate_url('news:comp.infosystems.www.misc');
  239. }
  240. // Validate a news link to a message id. Said ID copied off of google groups.
  241. function test_link_news_message() {
  242. $this->link_test_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
  243. }
  244. }
  245. class LinkValidateSpecificURL extends LinkValidateTestCase {
  246. public static function getInfo() {
  247. return array(
  248. 'name' => 'Link Specific URL Validation Tests',
  249. 'description' => 'Tests field validation with unusual urls',
  250. 'group' => 'Link',
  251. );
  252. }
  253. // Lets throw in a lot of umlouts for testing!
  254. function test_umlout_url() {
  255. $this->link_test_validate_url('http://üÜü.exämple.com/nöde');
  256. }
  257. function test_umlout_mailto() {
  258. $this->link_test_validate_url('mailto:Üser@exÅmple.com');
  259. }
  260. function test_german_b_url() {
  261. $this->link_test_validate_url('http://www.test.com/ßstuff');
  262. }
  263. function test_special_n_url() {
  264. $this->link_test_validate_url('http://www.testÑñ.com/');
  265. }
  266. function test_curly_brackets_in_query() {
  267. $this->link_test_validate_url('http://www.healthyteennetwork.org/index.asp?Type=B_PR&SEC={2AE1D600-4FC6-4B4D-8822-F1D5F072ED7B}&DE={235FD1E7-208D-4363-9854-4E6775EB8A4C}');
  268. }
  269. /**
  270. * Here, we're testing that a very long url is stored properly in the db.
  271. *
  272. * Basically, trying to test http://drupal.org/node/376818
  273. */
  274. function testLinkURLFieldIsBig() {
  275. $long_url = 'http://th.wikipedia.org/wiki/%E0%B9%82%E0%B8%A3%E0%B8%87%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B9%80%E0%B8%9A%E0%B8%8D%E0%B8%88%E0%B8%A1%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%B9%E0%B8%97%E0%B8%B4%E0%B8%A8_%E0%B8%99%E0%B8%84%E0%B8%A3%E0%B8%A8%E0%B8%A3%E0%B8%B5%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%A3%E0%B8%B2%E0%B8%8A';
  276. $this->link_test_validate_url($long_url);
  277. }
  278. }
  279. /**
  280. * A series of tests of links, only going against the link_validate_url function in link.module.
  281. *
  282. * Validation is guided by the rules in http://tools.ietf.org/html/rfc1738 !
  283. */
  284. class LinkValidateUrlLight extends DrupalWebTestCase {
  285. public static function getInfo() {
  286. return array(
  287. 'name' => 'Link Light Validation Tests',
  288. 'description' => 'Tests the link_validate_url() function by itself, without invoking the full drupal/cck lifecycle.',
  289. 'group' => 'Link',
  290. );
  291. }
  292. function setUp() {
  293. parent::setUp('link');
  294. }
  295. /**
  296. * Translates the LINK type constants to english for display and debugging of tests
  297. */
  298. function name_Link_Type($type) {
  299. switch ($type) {
  300. case LINK_FRONT:
  301. return "Front";
  302. case LINK_EMAIL:
  303. return "Email";
  304. case LINK_NEWS:
  305. return "Newsgroup";
  306. case LINK_INTERNAL:
  307. return "Internal Link";
  308. case LINK_EXTERNAL:
  309. return "External Link";
  310. case FALSE:
  311. return "Invalid Link";
  312. default:
  313. return "Bad Value:" . $type;
  314. }
  315. }
  316. // Make sure that a link labeled <front> works.
  317. function testValidateFrontLink() {
  318. $valid = link_validate_url('<front>');
  319. $this->assertEqual(LINK_FRONT, $valid, 'Make sure that front link is verified and identified');
  320. }
  321. function testValidateEmailLink() {
  322. $valid = link_validate_url('mailto:bob@example.com');
  323. $this->assertEqual(LINK_EMAIL, $valid, "Make sure a basic mailto is verified and identified");
  324. }
  325. function testValidateEmailLinkBad() {
  326. $valid = link_validate_url(':bob@example.com');
  327. $this->assertEqual(FALSE, $valid, 'Make sure just a bad address is correctly failed');
  328. }
  329. function testValidateNewsgroupLink() {
  330. $valid = link_validate_url('news:comp.infosystems.www.misc');
  331. $this->assertEqual(LINK_NEWS, $valid, 'Make sure link to newsgroup validates as news.');
  332. }
  333. function testValidateNewsArticleLink() {
  334. $valid = link_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
  335. $this->assertEqual(LINK_NEWS, $valid, 'Make sure link to specific article validates as news.');
  336. }
  337. function testValidateBadNewsgroupLink() {
  338. $valid = link_validate_url('news:comp.bad_name.misc');
  339. $this->assertEqual(FALSE, $valid, 'newsgroup names can\'t contain underscores, so it should come back as invalid.');
  340. }
  341. function testValidateInternalLinks() {
  342. $tempfile = drupal_tempnam('public://files', 'test');
  343. $links = array(
  344. file_uri_target($tempfile),
  345. drupal_realpath($tempfile),
  346. );
  347. foreach ($links as $link) {
  348. $type = link_url_type($link);
  349. $this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
  350. $valid = link_validate_url($link);
  351. $this->assertTrue($valid, 'Test ' . $link . ' is valid internal link.');
  352. }
  353. }
  354. function testValidateExternalLinks() {
  355. $links = array(
  356. 'http://localhost:8080/',
  357. 'www.example.com',
  358. 'www.example.com/',
  359. 'http://username:p%40ssw0rd!@www.example.com/',
  360. 'http://@www.example.com/',
  361. 'http://username:@www.example.com/',
  362. 'http://username:password@www.example.com:8080/',
  363. 'http://127.0.0.1:80/',
  364. 'http://127.173.24.255:4723/',
  365. '127.173.24.255:4723/',
  366. 'http://255.255.255.255:4823/',
  367. 'www.test-site.com',
  368. 'http://example.com/index.php?q=node/123',
  369. 'http://example.com/?first_name=Joe Bob&last_name=Smith',
  370. // Anchors
  371. 'http://www.example.com/index.php#test',
  372. 'http://www.example.com/index.php#this@that.',
  373. 'http://www.example.com/index.php#',
  374. 'http://www.cnn.com/video/#/video/politics/2008/12/09/intv.madeleine.albright.cnn',
  375. 'http://www.archive.org/stream/aesopsfables00aesorich#page/n7/mode/2up',
  376. 'http://www.example.com/blah/#this@that?',
  377. );
  378. // Test all of the protocols.
  379. $allowed_protocols = variable_get('filter_allowed_protocols', array('http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'mailto', 'irc', 'ssh', 'sftp', 'webcal'));
  380. foreach ($allowed_protocols as $protocol) {
  381. if ($protocol !== 'news' && $protocol !== 'mailto') {
  382. $links[] = $protocol . '://www.example.com';
  383. }
  384. }
  385. foreach ($links as $link) {
  386. $type = link_url_type($link);
  387. $this->assertEqual(LINK_EXTERNAL, $type, 'Testing that ' . $link . ' is an external link.');
  388. $valid = link_validate_url($link);
  389. $this->assertTrue($valid, 'Test ' . $link . ' is valid external link.');
  390. // The following two lines are commented out and only used for comparisons.
  391. //$valid2 = valid_url($link, TRUE);
  392. //$this->assertEqual(TRUE, $valid2, "Using valid_url() on $link.");
  393. }
  394. // Test if we can make a tld allowable:
  395. variable_set('link_allowed_domains', array('frog'));
  396. $valid = link_validate_url('http://www.example.frog');
  397. $this->assertEqual(LINK_EXTERNAL, $valid, "Testing that http://www.example.frog is a valid external link if we've added 'frog' to the list of valid domains.");
  398. }
  399. function testInvalidExternalLinks() {
  400. $links = array(
  401. 'http://www.ex ample.com/',
  402. 'http://25.0.0/', // bad ip!
  403. 'http://4827.0.0.2/',
  404. '//www.example.com/',
  405. 'http://www.testß.com/', // ß not allowed in domain names!
  406. //'http://www.-fudge.com/', // domains can't have sections starting with a dash.
  407. 'http://example.com/index.php?page=this\that',
  408. 'example@example.com',
  409. );
  410. foreach ($links as $link) {
  411. $valid = link_validate_url($link);
  412. $this->assertEqual(FALSE, $valid, 'Testing that ' . $link . ' is not a valid link.');
  413. }
  414. // Test if we can make a tld disallowed:
  415. variable_set('link_allowed_domains', array('toad'));
  416. $valid = link_validate_url('http://www.example.frog');
  417. $this->assertEqual(FALSE, $valid, "Testing that http://www.example.frog is an invalid external link if we've not added 'frog' to the list of valid domains.");
  418. }
  419. }