link.validate.test 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. <?php
  2. /**
  3. * @file
  4. * Tests that exercise the validation functions in the link module.
  5. */
  6. /**
  7. * Validate Test Case.
  8. */
  9. class LinkValidateTestCase extends LinkBaseTestClass {
  10. /**
  11. * Create Link.
  12. */
  13. protected function createLink($url, $title, $attributes = array()) {
  14. return array(
  15. 'url' => $url,
  16. 'title' => $title,
  17. 'attributes' => $attributes,
  18. );
  19. }
  20. /**
  21. * Takes a url, and sees if it can validate that the url is valid.
  22. *
  23. * @codingStandardsIgnoreStart
  24. */
  25. protected function link_test_validate_url($url) {
  26. // @codingStandardsIgnoreEnd
  27. $field_name = $this->createLinkField();
  28. $label = $this->randomName();
  29. $settings = array(
  30. 'title' => $label,
  31. $field_name => array(
  32. LANGUAGE_NONE => array(
  33. array(
  34. 'title' => $label,
  35. 'url' => $url,
  36. ),
  37. ),
  38. ),
  39. );
  40. $node = $this->drupalCreateNode($settings);
  41. $this->assertNotNull($node, ' has been created.', 'Node created');
  42. $this->assertEqual($url, $node->{$field_name}[LANGUAGE_NONE][0]['url']);
  43. }
  44. }
  45. /**
  46. * Class for Validate Test.
  47. */
  48. class LinkValidateTest extends LinkValidateTestCase {
  49. /**
  50. * Get Info.
  51. */
  52. public static function getInfo() {
  53. return array(
  54. 'name' => 'Link Validation Tests',
  55. 'description' => 'Tests the field validation.',
  56. 'group' => 'Link',
  57. );
  58. }
  59. /**
  60. * Validate basic URL.
  61. *
  62. * @codingStandardsIgnoreStart
  63. */
  64. public function test_link_validate_basic_url() {
  65. // @codingStandardsIgnoreEnd
  66. $this->link_test_validate_url('http://www.example.com');
  67. }
  68. /**
  69. * Test if we're stopped from posting a bad url on default validation.
  70. *
  71. * @codingStandardsIgnoreStart
  72. */
  73. public function test_link_validate_bad_url_validate_default() {
  74. // @codingStandardsIgnoreEnd
  75. $this->web_user = $this->drupalCreateUser(array(
  76. 'administer content types',
  77. 'administer fields',
  78. 'administer nodes',
  79. 'administer filters',
  80. 'access content',
  81. 'create page content',
  82. 'access administration pages',
  83. ));
  84. $this->drupalLogin($this->web_user);
  85. // Create field.
  86. $name = strtolower($this->randomName());
  87. $edit = array(
  88. 'fields[_add_new_field][label]' => $name,
  89. 'fields[_add_new_field][field_name]' => $name,
  90. 'fields[_add_new_field][type]' => 'link_field',
  91. 'fields[_add_new_field][widget_type]' => 'link_field',
  92. );
  93. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  94. $this->drupalPost(NULL, array(), t('Save field settings'));
  95. $this->drupalPost(NULL, array(), t('Save settings'));
  96. // Is field created?
  97. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  98. node_types_rebuild();
  99. menu_rebuild();
  100. // Create page form.
  101. $this->drupalGet('node/add/page');
  102. $field_name = 'field_' . $name;
  103. $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
  104. $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
  105. $edit = array(
  106. 'title' => 'Simple Title',
  107. $field_name . '[und][0][url]' => 'edik:naw',
  108. );
  109. $this->drupalPost(NULL, $edit, t('Save'));
  110. $this->assertText(t('The value @value provided for @field is not a valid URL.', array(
  111. '@value' => 'edik:naw',
  112. '@field' => $name,
  113. )));
  114. }
  115. /**
  116. * Test if we're stopped from posting a bad url with validation on.
  117. *
  118. * @codingStandardsIgnoreStart
  119. */
  120. public function test_link_validate_bad_url_validate_on() {
  121. // @codingStandardsIgnoreEnd
  122. $this->web_user = $this->drupalCreateUser(array(
  123. 'administer content types',
  124. 'administer fields',
  125. 'administer nodes',
  126. 'administer filters',
  127. 'access content',
  128. 'create page content',
  129. 'access administration pages',
  130. ));
  131. $this->drupalLogin($this->web_user);
  132. // Create field.
  133. $name = strtolower($this->randomName());
  134. $edit = array(
  135. 'fields[_add_new_field][label]' => $name,
  136. 'fields[_add_new_field][field_name]' => $name,
  137. 'fields[_add_new_field][type]' => 'link_field',
  138. 'fields[_add_new_field][widget_type]' => 'link_field',
  139. );
  140. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  141. $this->drupalPost(NULL, array(), t('Save field settings'));
  142. $this->drupalPost(NULL, array('instance[settings][validate_url]' => TRUE), t('Save settings'));
  143. // Is field created?
  144. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  145. node_types_rebuild();
  146. menu_rebuild();
  147. // Create page form.
  148. $this->drupalGet('node/add/page');
  149. $field_name = 'field_' . $name;
  150. $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
  151. $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
  152. $edit = array(
  153. 'title' => 'Simple Title',
  154. $field_name . '[und][0][url]' => 'edik:naw',
  155. );
  156. $this->drupalPost(NULL, $edit, t('Save'));
  157. $this->assertText(t('The value @value provided for @field is not a valid URL.', array(
  158. '@field' => $name,
  159. '@value' => 'edik:naw',
  160. )));
  161. }
  162. /**
  163. * Test if we can post a bad url if the validation is expressly turned off.
  164. *
  165. * @codingStandardsIgnoreStart
  166. */
  167. public function test_link_validate_bad_url_validate_off() {
  168. // @codingStandardsIgnoreEnd
  169. $this->web_user = $this->drupalCreateUser(array(
  170. 'administer content types',
  171. 'administer fields',
  172. 'administer nodes',
  173. 'administer filters',
  174. 'access content',
  175. 'create page content',
  176. 'access administration pages',
  177. ));
  178. $this->drupalLogin($this->web_user);
  179. // Create field.
  180. $name = strtolower($this->randomName());
  181. $edit = array(
  182. 'fields[_add_new_field][label]' => $name,
  183. 'fields[_add_new_field][field_name]' => $name,
  184. 'fields[_add_new_field][type]' => 'link_field',
  185. 'fields[_add_new_field][widget_type]' => 'link_field',
  186. );
  187. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  188. $this->drupalPost(NULL, array(), t('Save field settings'));
  189. $this->drupalPost(NULL, array('instance[settings][validate_url]' => FALSE), t('Save settings'));
  190. // @codingStandardsIgnoreLine
  191. /*$instance_details = db_query("SELECT * FROM {field_config_instance} WHERE field_name = :field_name AND bundle = 'page'", array(':field_name' => 'field_'. $name))->fetchObject();
  192. $this->fail('<pre>'. print_r($instance_details, TRUE) .'</pre>');
  193. $this->fail('<pre>'. print_r(unserialize($instance_details->data), TRUE) .'</pre>');*/
  194. // Is field created?
  195. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  196. node_types_rebuild();
  197. menu_rebuild();
  198. // Create page form.
  199. $this->drupalGet('node/add/page');
  200. $field_name = 'field_' . $name;
  201. $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
  202. $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
  203. $edit = array(
  204. 'title' => 'Simple Title',
  205. $field_name . '[und][0][url]' => 'edik:naw',
  206. );
  207. $this->drupalPost(NULL, $edit, t('Save'));
  208. $this->assertNoText(t('The value %value provided for %field is not a valid URL.', array(
  209. '%field' => $name,
  210. '%value' => 'edik:naw',
  211. )));
  212. }
  213. /**
  214. * Validate switching between validation status.
  215. *
  216. * Test if a bad url can sneak through un-filtered if we play with the
  217. * validation...
  218. *
  219. * @codingStandardsIgnoreStart
  220. */
  221. public function x_test_link_validate_switching_between_validation_status() {
  222. // @codingStandardsIgnoreEnd
  223. $this->acquireContentTypes(1);
  224. $this->web_user = $this->drupalCreateUser(array(
  225. 'administer content types',
  226. 'administer fields',
  227. 'administer nodes',
  228. 'access administration pages',
  229. 'access content',
  230. 'create ' . $this->content_types[0]->type . ' content',
  231. 'edit any ' . $this->content_types[0]->type . ' content',
  232. ));
  233. $this->drupalLogin($this->web_user);
  234. variable_set('node_options_' . $this->content_types[0]->name, array(
  235. 'status',
  236. 'promote',
  237. ));
  238. $field_settings = array(
  239. 'type' => 'link',
  240. 'widget_type' => 'link',
  241. 'type_name' => $this->content_types[0]->name,
  242. // <-- This is needed or we have an error.
  243. 'attributes' => array(),
  244. 'validate_url' => 0,
  245. );
  246. $field = $this->createField($field_settings, 0);
  247. $this->acquireNodes(2);
  248. $this->drupalGet('node/' . $this->nodes[0]->nid);
  249. $edit = array();
  250. $title = $this->randomName();
  251. $url = 'javascript:alert("http://example.com/' . $this->randomName() . '")';
  252. $edit[$field['field_name'] . '[0][url]'] = $url;
  253. $edit[$field['field_name'] . '[0][title]'] = $title;
  254. $this->drupalPost('node/' . $this->nodes[0]->nid . '/edit', $edit, t('Save'));
  255. // $this->pass($this->content);.
  256. // @codingStandardsIgnoreLine
  257. $this->assertNoText(t('The value %value provided for %field is not a valid URL.', array(
  258. // @codingStandardsIgnoreLine
  259. '%field' => $name,
  260. '%value' => trim($url),
  261. )));
  262. // Make sure we get a new version!
  263. $node = node_load($this->nodes[0]->nid, NULL, TRUE);
  264. $this->assertEqual($url, $node->{$field['field_name']}[0]['url']);
  265. $this->drupalGet('node/' . $node->nid);
  266. $this->assertNoRaw($url, 'Make sure Javascript does not display.');
  267. // Turn the array validation back _on_.
  268. $edit = array('validate_url' => TRUE);
  269. $node_type_link = str_replace('_', '-', $node->type);
  270. // @codingStandardsIgnoreLine
  271. // $this->drupalGet('admin/content/node-type/'. $node_type_link .'/fields'); ///'. $field['field_name']);
  272. // $this->fail($this->content);.
  273. $this->drupalPost('admin/content/node-type/' . $node_type_link . '/fields/' . $field['field_name'], $edit, t('Save field settings'));
  274. $this->drupalGet('node/' . $node->nid);
  275. // This actually works because the display_url goes through the core
  276. // url() function. But we should have a test that makes sure it continues
  277. // to work.
  278. $this->assertNoRaw($url, 'Make sure Javascript does not display.');
  279. // $this->fail($this->content);.
  280. }
  281. /**
  282. * Validate that '<front>' is a valid url.
  283. *
  284. * @codingStandardsIgnoreStart
  285. */
  286. public function test_link_front_url() {
  287. // @codingStandardsIgnoreEnd
  288. $this->link_test_validate_url('<front>');
  289. }
  290. /**
  291. * Validate that an internal url would be accepted.
  292. *
  293. * @codingStandardsIgnoreStart
  294. */
  295. public function test_link_internal_url() {
  296. // @codingStandardsIgnoreEnd
  297. // Create the content first.
  298. $node = $this->drupalCreateNode();
  299. $link = 'node/' . $node->nid;
  300. $this->link_test_validate_url($link);
  301. $type = link_url_type($link);
  302. $this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
  303. }
  304. /**
  305. * Validate a simple mailto.
  306. *
  307. * @codingStandardsIgnoreStart
  308. */
  309. public function test_link_mailto() {
  310. // @codingStandardsIgnoreEnd
  311. $this->link_test_validate_url('mailto:jcfiala@gmail.com');
  312. }
  313. /**
  314. * Check link external https.
  315. *
  316. * @codingStandardsIgnoreStart
  317. */
  318. public function test_link_external_https() {
  319. // @codingStandardsIgnoreEnd
  320. $this->link_test_validate_url('https://www.example.com/');
  321. }
  322. /**
  323. * Check link FTP.
  324. *
  325. * @codingStandardsIgnoreStart
  326. */
  327. public function test_link_ftp() {
  328. // @codingStandardsIgnoreEnd
  329. $this->link_test_validate_url('ftp://www.example.com/');
  330. }
  331. }
  332. /**
  333. * Validate Test News.
  334. */
  335. class LinkValidateTestNews extends LinkValidateTestCase {
  336. /**
  337. * Get Info.
  338. */
  339. public static function getInfo() {
  340. return array(
  341. 'name' => 'Link News Validation Tests',
  342. 'description' => 'Tests the field validation for usenet urls.',
  343. 'group' => 'Link',
  344. );
  345. }
  346. /**
  347. * Validate a news link to a message group.
  348. *
  349. * @codingStandardsIgnoreStart
  350. */
  351. public function test_link_news() {
  352. // @codingStandardsIgnoreEnd
  353. $this->link_test_validate_url('news:comp.infosystems.www.misc');
  354. }
  355. /**
  356. * Validate a news link to a message id. Said ID copied off of google groups.
  357. *
  358. * @codingStandardsIgnoreStart
  359. */
  360. public function test_link_news_message() {
  361. // @codingStandardsIgnoreEnd
  362. $this->link_test_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
  363. }
  364. }
  365. /**
  366. * Validate Specific URL.
  367. */
  368. class LinkValidateSpecificURL extends LinkValidateTestCase {
  369. /**
  370. * Get Info.
  371. */
  372. public static function getInfo() {
  373. return array(
  374. 'name' => 'Link Specific URL Validation Tests',
  375. 'description' => 'Tests field validation with unusual urls',
  376. 'group' => 'Link',
  377. );
  378. }
  379. /**
  380. * Lets throw in a lot of umlouts for testing!
  381. *
  382. * @codingStandardsIgnoreStart
  383. */
  384. public function test_umlout_url() {
  385. // @codingStandardsIgnoreEnd
  386. $this->link_test_validate_url('http://üÜü.exämple.com/nöde');
  387. }
  388. /**
  389. * Check umlout mailto.
  390. *
  391. * @codingStandardsIgnoreStart
  392. */
  393. public function test_umlout_mailto() {
  394. // @codingStandardsIgnoreEnd
  395. $this->link_test_validate_url('mailto:Üser@exÅmple.com');
  396. }
  397. /**
  398. * Check german b in url.
  399. *
  400. * @codingStandardsIgnoreStart
  401. */
  402. public function test_german_b_url() {
  403. // @codingStandardsIgnoreEnd
  404. $this->link_test_validate_url('http://www.test.com/ßstuff');
  405. }
  406. /**
  407. * Check Special in url.
  408. *
  409. * @codingStandardsIgnoreStart
  410. */
  411. public function test_special_n_url() {
  412. // @codingStandardsIgnoreEnd
  413. $this->link_test_validate_url('http://www.testÑñ.com/');
  414. }
  415. /**
  416. * Curly Brackets in query.
  417. *
  418. * @codingStandardsIgnoreStart
  419. */
  420. public function test_curly_brackets_in_query() {
  421. // @codingStandardsIgnoreEnd
  422. $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}');
  423. }
  424. /**
  425. * Here, we're testing that a very long url is stored properly in the db.
  426. *
  427. * Basically, trying to test http://drupal.org/node/376818
  428. *
  429. * @codingStandardsIgnoreStart
  430. */
  431. public function testLinkURLFieldIsBig() {
  432. // @codingStandardsIgnoreEnd
  433. $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';
  434. $this->link_test_validate_url($long_url);
  435. }
  436. }
  437. /**
  438. * Validate Url Light.
  439. *
  440. * A series of tests of links, only going against the link_validate_url function
  441. * in link.module.
  442. *
  443. * Validation is guided by the rules in http://tools.ietf.org/html/rfc1738 !
  444. */
  445. class LinkValidateUrlLight extends DrupalWebTestCase {
  446. /**
  447. * Get Info.
  448. */
  449. public static function getInfo() {
  450. return array(
  451. 'name' => 'Link Light Validation Tests',
  452. 'description' => 'Tests the link_validate_url() function by itself, without invoking the full drupal/cck lifecycle.',
  453. 'group' => 'Link',
  454. );
  455. }
  456. /**
  457. * Setup.
  458. */
  459. public function setUp() {
  460. parent::setUp('link');
  461. }
  462. /**
  463. * Name Link Type.
  464. *
  465. * Translates the LINK type constants to english for display and debugging of
  466. * tests.
  467. *
  468. * @codingStandardsIgnoreStart
  469. */
  470. public function name_Link_Type($type) {
  471. // @codingStandardsIgnoreEnd
  472. switch ($type) {
  473. case LINK_FRONT:
  474. return "Front";
  475. case LINK_EMAIL:
  476. return "Email";
  477. case LINK_TEL:
  478. return "Telephone";
  479. case LINK_NEWS:
  480. return "Newsgroup";
  481. case LINK_INTERNAL:
  482. return "Internal Link";
  483. case LINK_EXTERNAL:
  484. return "External Link";
  485. case FALSE:
  486. return "Invalid Link";
  487. default:
  488. return "Bad Value:" . $type;
  489. }
  490. }
  491. /**
  492. * Make sure that a link labeled <front> works.
  493. */
  494. public function testValidateFrontLink() {
  495. $valid = link_validate_url('<front>');
  496. $this->assertEqual(LINK_FRONT, $valid, 'Make sure that front link is verified and identified');
  497. }
  498. /**
  499. * Validate Email Link.
  500. */
  501. public function testValidateEmailLink() {
  502. $valid = link_validate_url('mailto:bob@example.com');
  503. $this->assertEqual(LINK_EMAIL, $valid, "Make sure a basic mailto is verified and identified");
  504. }
  505. /**
  506. * Validate Email Link Bad.
  507. */
  508. public function testValidateEmailLinkBad() {
  509. $valid = link_validate_url(':bob@example.com');
  510. $this->assertEqual(FALSE, $valid, 'Make sure just a bad address is correctly failed');
  511. }
  512. function testValidateTelLinks() {
  513. $links = array(
  514. 'tel:01',
  515. 'tel:123456789012345',
  516. 'tel:+123456789012345',
  517. );
  518. foreach ($links as $link) {
  519. $type = link_url_type($link);
  520. $this->assertEqual(LINK_TEL, $type, 'Test ' . $link . ' is a tel link.');
  521. $valid = link_validate_url($link);
  522. $this->assertTrue($valid, 'Test ' . $link . ' is valid tel link.');
  523. }
  524. }
  525. function testValidateTelLinksBad() {
  526. $links = array(
  527. 'tel:0',
  528. 'tel:1234567890123456',
  529. 'tel:+1',
  530. 'tel:+0123456789',
  531. 'tel:+1234567890123456',
  532. ':12345678',
  533. );
  534. foreach ($links as $link) {
  535. $type = link_url_type($link);
  536. $this->assertFalse($type, 'Test ' . $link . ' is not a tel link.');
  537. $valid = link_validate_url($link);
  538. $this->assertFalse($valid, 'Test ' . $link . ' is not a valid tel link.');
  539. }
  540. }
  541. /**
  542. * Validate Newsgroup Link.
  543. */
  544. public function testValidateNewsgroupLink() {
  545. $valid = link_validate_url('news:comp.infosystems.www.misc');
  546. $this->assertEqual(LINK_NEWS, $valid, 'Make sure link to newsgroup validates as news.');
  547. }
  548. /**
  549. * Validate News Article Link.
  550. */
  551. public function testValidateNewsArticleLink() {
  552. $valid = link_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
  553. $this->assertEqual(LINK_NEWS, $valid, 'Make sure link to specific article validates as news.');
  554. }
  555. /**
  556. * Validate Bad Newsgroup Link.
  557. */
  558. public function testValidateBadNewsgroupLink() {
  559. $valid = link_validate_url('news:comp.bad_name.misc');
  560. $this->assertEqual(FALSE, $valid, 'newsgroup names can\'t contain underscores, so it should come back as invalid.');
  561. }
  562. /**
  563. * Validate Internal Links.
  564. */
  565. public function testValidateInternalLinks() {
  566. $tempfile = drupal_tempnam('public://files', 'test');
  567. $links = array(
  568. 'rss.xml',
  569. file_uri_target($tempfile),
  570. drupal_realpath($tempfile),
  571. );
  572. foreach ($links as $link) {
  573. $type = link_url_type($link);
  574. $this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
  575. $valid = link_validate_url($link);
  576. $this->assertTrue($valid, 'Test ' . $link . ' is valid internal link.');
  577. }
  578. }
  579. /**
  580. * Validate External Links.
  581. */
  582. public function testValidateExternalLinks() {
  583. $links = array(
  584. 'http://localhost:8080/',
  585. 'www.example.com',
  586. 'www.example.com/',
  587. 'http://username:p%40ssw0rd!@www.example.com/',
  588. 'http://@www.example.com/',
  589. 'http://username:@www.example.com/',
  590. 'http://username:password@www.example.com:8080/',
  591. 'http://127.0.0.1:80/',
  592. 'http://127.173.24.255:4723/',
  593. '127.173.24.255:4723/',
  594. 'http://255.255.255.255:4823/',
  595. 'www.test-site.com',
  596. 'http://example.com/index.php?q=node/123',
  597. 'http://example.com/?first_name=Joe Bob&last_name=Smith',
  598. // Anchors.
  599. 'http://www.example.com/index.php#test',
  600. 'http://www.example.com/index.php#this@that.',
  601. 'http://www.example.com/index.php#',
  602. 'http://www.cnn.com/video/#/video/politics/2008/12/09/intv.madeleine.albright.cnn',
  603. 'http://www.archive.org/stream/aesopsfables00aesorich#page/n7/mode/2up',
  604. 'http://www.example.com/blah/#this@that?',
  605. );
  606. // Test all of the protocols.
  607. $allowed_protocols = variable_get('filter_allowed_protocols', array(
  608. 'http',
  609. 'https',
  610. 'ftp',
  611. 'news',
  612. 'nntp',
  613. 'telnet',
  614. 'mailto',
  615. 'irc',
  616. 'ssh',
  617. 'sftp',
  618. 'webcal',
  619. ));
  620. foreach ($allowed_protocols as $protocol) {
  621. if ($protocol !== 'news' && $protocol !== 'mailto') {
  622. $links[] = $protocol . '://www.example.com';
  623. }
  624. }
  625. foreach ($links as $link) {
  626. $type = link_url_type($link);
  627. $this->assertEqual(LINK_EXTERNAL, $type, 'Testing that ' . $link . ' is an external link.');
  628. $valid = link_validate_url($link);
  629. $this->assertTrue($valid, 'Test ' . $link . ' is valid external link.');
  630. // The following two lines are commented out and only used for
  631. // comparisons.
  632. // $valid2 = valid_url($link, TRUE);
  633. // $this->assertEqual(TRUE, $valid2, "Using valid_url() on $link.");.
  634. }
  635. }
  636. /**
  637. * Check Invalid External Links.
  638. */
  639. public function testInvalidExternalLinks() {
  640. $links = array(
  641. 'http://www.ex ample.com/',
  642. // Bad ip!
  643. 'http://25.0.0/',
  644. 'http://4827.0.0.2/',
  645. // ß not allowed in domain names!
  646. 'http://www.testß.com/',
  647. // Bad TLD.
  648. 'http://.www.foo.bar./',
  649. // Domains can't have sections starting with a dash.
  650. // 'http://www.-fudge.com/',
  651. 'http://example.com/index.php?page=this\that',
  652. 'example@example.com',
  653. );
  654. foreach ($links as $link) {
  655. $valid = link_validate_url($link);
  656. $this->assertEqual(FALSE, $valid, 'Testing that ' . $link . ' is not a valid link.');
  657. }
  658. }
  659. }