link.validate.test 18 KB

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