link.validate.test 18 KB

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