link.validate.test 18 KB

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