link.validate.test 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. '%field' => $name,
  259. '%value' => trim($url),
  260. )));
  261. // Make sure we get a new version!
  262. $node = node_load($this->nodes[0]->nid, NULL, TRUE);
  263. $this->assertEqual($url, $node->{$field['field_name']}[0]['url']);
  264. $this->drupalGet('node/' . $node->nid);
  265. $this->assertNoRaw($url, 'Make sure Javascript does not display.');
  266. // Turn the array validation back _on_.
  267. $edit = array('validate_url' => TRUE);
  268. $node_type_link = str_replace('_', '-', $node->type);
  269. // @codingStandardsIgnoreLine
  270. // $this->drupalGet('admin/content/node-type/'. $node_type_link .'/fields'); ///'. $field['field_name']);
  271. // $this->fail($this->content);.
  272. $this->drupalPost('admin/content/node-type/' . $node_type_link . '/fields/' . $field['field_name'], $edit, t('Save field settings'));
  273. $this->drupalGet('node/' . $node->nid);
  274. // This actually works because the display_url goes through the core
  275. // url() function. But we should have a test that makes sure it continues
  276. // to work.
  277. $this->assertNoRaw($url, 'Make sure Javascript does not display.');
  278. // $this->fail($this->content);.
  279. }
  280. /**
  281. * Validate that '<front>' is a valid url.
  282. *
  283. * @codingStandardsIgnoreStart
  284. */
  285. public function test_link_front_url() {
  286. // @codingStandardsIgnoreEnd
  287. $this->link_test_validate_url('<front>');
  288. }
  289. /**
  290. * Validate that an internal url would be accepted.
  291. *
  292. * @codingStandardsIgnoreStart
  293. */
  294. public function test_link_internal_url() {
  295. // @codingStandardsIgnoreEnd
  296. // Create the content first.
  297. $node = $this->drupalCreateNode();
  298. $link = 'node/' . $node->nid;
  299. $this->link_test_validate_url($link);
  300. $type = link_url_type($link);
  301. $this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
  302. }
  303. /**
  304. * Validate a simple mailto.
  305. *
  306. * @codingStandardsIgnoreStart
  307. */
  308. public function test_link_mailto() {
  309. // @codingStandardsIgnoreEnd
  310. $this->link_test_validate_url('mailto:jcfiala@gmail.com');
  311. }
  312. /**
  313. * Check link external https.
  314. *
  315. * @codingStandardsIgnoreStart
  316. */
  317. public function test_link_external_https() {
  318. // @codingStandardsIgnoreEnd
  319. $this->link_test_validate_url('https://www.example.com/');
  320. }
  321. /**
  322. * Check link FTP.
  323. *
  324. * @codingStandardsIgnoreStart
  325. */
  326. public function test_link_ftp() {
  327. // @codingStandardsIgnoreEnd
  328. $this->link_test_validate_url('ftp://www.example.com/');
  329. }
  330. }
  331. /**
  332. * Validate Test News.
  333. */
  334. class LinkValidateTestNews extends LinkValidateTestCase {
  335. /**
  336. * Get Info.
  337. */
  338. public static function getInfo() {
  339. return array(
  340. 'name' => 'Link News Validation Tests',
  341. 'description' => 'Tests the field validation for usenet urls.',
  342. 'group' => 'Link',
  343. );
  344. }
  345. /**
  346. * Validate a news link to a message group.
  347. *
  348. * @codingStandardsIgnoreStart
  349. */
  350. public function test_link_news() {
  351. // @codingStandardsIgnoreEnd
  352. $this->link_test_validate_url('news:comp.infosystems.www.misc');
  353. }
  354. /**
  355. * Validate a news link to a message id. Said ID copied off of google groups.
  356. *
  357. * @codingStandardsIgnoreStart
  358. */
  359. public function test_link_news_message() {
  360. // @codingStandardsIgnoreEnd
  361. $this->link_test_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
  362. }
  363. }
  364. /**
  365. * Validate Specific URL.
  366. */
  367. class LinkValidateSpecificURL extends LinkValidateTestCase {
  368. /**
  369. * Get Info.
  370. */
  371. public static function getInfo() {
  372. return array(
  373. 'name' => 'Link Specific URL Validation Tests',
  374. 'description' => 'Tests field validation with unusual urls',
  375. 'group' => 'Link',
  376. );
  377. }
  378. /**
  379. * Lets throw in a lot of umlouts for testing!
  380. *
  381. * @codingStandardsIgnoreStart
  382. */
  383. public function test_umlout_url() {
  384. // @codingStandardsIgnoreEnd
  385. $this->link_test_validate_url('http://üÜü.exämple.com/nöde');
  386. }
  387. /**
  388. * Check umlout mailto.
  389. *
  390. * @codingStandardsIgnoreStart
  391. */
  392. public function test_umlout_mailto() {
  393. // @codingStandardsIgnoreEnd
  394. $this->link_test_validate_url('mailto:Üser@exÅmple.com');
  395. }
  396. /**
  397. * Check german b in url.
  398. *
  399. * @codingStandardsIgnoreStart
  400. */
  401. public function test_german_b_url() {
  402. // @codingStandardsIgnoreEnd
  403. $this->link_test_validate_url('http://www.test.com/ßstuff');
  404. }
  405. /**
  406. * Check Special in url.
  407. *
  408. * @codingStandardsIgnoreStart
  409. */
  410. public function test_special_n_url() {
  411. // @codingStandardsIgnoreEnd
  412. $this->link_test_validate_url('http://www.testÑñ.com/');
  413. }
  414. /**
  415. * Curly Brackets in query.
  416. *
  417. * @codingStandardsIgnoreStart
  418. */
  419. public function test_curly_brackets_in_query() {
  420. // @codingStandardsIgnoreEnd
  421. $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}');
  422. }
  423. /**
  424. * Here, we're testing that a very long url is stored properly in the db.
  425. *
  426. * Basically, trying to test http://drupal.org/node/376818
  427. *
  428. * @codingStandardsIgnoreStart
  429. */
  430. public function testLinkURLFieldIsBig() {
  431. // @codingStandardsIgnoreEnd
  432. $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';
  433. $this->link_test_validate_url($long_url);
  434. }
  435. }
  436. /**
  437. * Validate Url Light.
  438. *
  439. * A series of tests of links, only going against the link_validate_url function
  440. * in link.module.
  441. *
  442. * Validation is guided by the rules in http://tools.ietf.org/html/rfc1738 !
  443. */
  444. class LinkValidateUrlLight extends DrupalWebTestCase {
  445. /**
  446. * Get Info.
  447. */
  448. public static function getInfo() {
  449. return array(
  450. 'name' => 'Link Light Validation Tests',
  451. 'description' => 'Tests the link_validate_url() function by itself, without invoking the full drupal/cck lifecycle.',
  452. 'group' => 'Link',
  453. );
  454. }
  455. /**
  456. * Setup.
  457. */
  458. public function setUp() {
  459. parent::setUp('link');
  460. }
  461. /**
  462. * Name Link Type.
  463. *
  464. * Translates the LINK type constants to english for display and debugging of
  465. * tests.
  466. *
  467. * @codingStandardsIgnoreStart
  468. */
  469. public function name_Link_Type($type) {
  470. // @codingStandardsIgnoreEnd
  471. switch ($type) {
  472. case LINK_FRONT:
  473. return "Front";
  474. case LINK_EMAIL:
  475. return "Email";
  476. case LINK_NEWS:
  477. return "Newsgroup";
  478. case LINK_INTERNAL:
  479. return "Internal Link";
  480. case LINK_EXTERNAL:
  481. return "External Link";
  482. case FALSE:
  483. return "Invalid Link";
  484. default:
  485. return "Bad Value:" . $type;
  486. }
  487. }
  488. /**
  489. * Make sure that a link labeled <front> works.
  490. */
  491. public function testValidateFrontLink() {
  492. $valid = link_validate_url('<front>');
  493. $this->assertEqual(LINK_FRONT, $valid, 'Make sure that front link is verified and identified');
  494. }
  495. /**
  496. * Validate Email Link.
  497. */
  498. public function testValidateEmailLink() {
  499. $valid = link_validate_url('mailto:bob@example.com');
  500. $this->assertEqual(LINK_EMAIL, $valid, "Make sure a basic mailto is verified and identified");
  501. }
  502. /**
  503. * Validate Email Link Bad.
  504. */
  505. public function testValidateEmailLinkBad() {
  506. $valid = link_validate_url(':bob@example.com');
  507. $this->assertEqual(FALSE, $valid, 'Make sure just a bad address is correctly failed');
  508. }
  509. /**
  510. * Validate Newsgroup Link.
  511. */
  512. public function testValidateNewsgroupLink() {
  513. $valid = link_validate_url('news:comp.infosystems.www.misc');
  514. $this->assertEqual(LINK_NEWS, $valid, 'Make sure link to newsgroup validates as news.');
  515. }
  516. /**
  517. * Validate News Article Link.
  518. */
  519. public function testValidateNewsArticleLink() {
  520. $valid = link_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
  521. $this->assertEqual(LINK_NEWS, $valid, 'Make sure link to specific article validates as news.');
  522. }
  523. /**
  524. * Validate Bad Newsgroup Link.
  525. */
  526. public function testValidateBadNewsgroupLink() {
  527. $valid = link_validate_url('news:comp.bad_name.misc');
  528. $this->assertEqual(FALSE, $valid, 'newsgroup names can\'t contain underscores, so it should come back as invalid.');
  529. }
  530. /**
  531. * Validate Internal Links.
  532. */
  533. public function testValidateInternalLinks() {
  534. $tempfile = drupal_tempnam('public://files', 'test');
  535. $links = array(
  536. 'rss.xml',
  537. file_uri_target($tempfile),
  538. drupal_realpath($tempfile),
  539. );
  540. foreach ($links as $link) {
  541. $type = link_url_type($link);
  542. $this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
  543. $valid = link_validate_url($link);
  544. $this->assertTrue($valid, 'Test ' . $link . ' is valid internal link.');
  545. }
  546. }
  547. /**
  548. * Validate External Links.
  549. */
  550. public function testValidateExternalLinks() {
  551. $links = array(
  552. 'http://localhost:8080/',
  553. 'www.example.com',
  554. 'www.example.com/',
  555. 'http://username:p%40ssw0rd!@www.example.com/',
  556. 'http://@www.example.com/',
  557. 'http://username:@www.example.com/',
  558. 'http://username:password@www.example.com:8080/',
  559. 'http://127.0.0.1:80/',
  560. 'http://127.173.24.255:4723/',
  561. '127.173.24.255:4723/',
  562. 'http://255.255.255.255:4823/',
  563. 'www.test-site.com',
  564. 'http://example.com/index.php?q=node/123',
  565. 'http://example.com/?first_name=Joe Bob&last_name=Smith',
  566. // Anchors.
  567. 'http://www.example.com/index.php#test',
  568. 'http://www.example.com/index.php#this@that.',
  569. 'http://www.example.com/index.php#',
  570. 'http://www.cnn.com/video/#/video/politics/2008/12/09/intv.madeleine.albright.cnn',
  571. 'http://www.archive.org/stream/aesopsfables00aesorich#page/n7/mode/2up',
  572. 'http://www.example.com/blah/#this@that?',
  573. );
  574. // Test all of the protocols.
  575. $allowed_protocols = variable_get('filter_allowed_protocols', array(
  576. 'http',
  577. 'https',
  578. 'ftp',
  579. 'news',
  580. 'nntp',
  581. 'telnet',
  582. 'mailto',
  583. 'irc',
  584. 'ssh',
  585. 'sftp',
  586. 'webcal',
  587. ));
  588. foreach ($allowed_protocols as $protocol) {
  589. if ($protocol !== 'news' && $protocol !== 'mailto') {
  590. $links[] = $protocol . '://www.example.com';
  591. }
  592. }
  593. foreach ($links as $link) {
  594. $type = link_url_type($link);
  595. $this->assertEqual(LINK_EXTERNAL, $type, 'Testing that ' . $link . ' is an external link.');
  596. $valid = link_validate_url($link);
  597. $this->assertTrue($valid, 'Test ' . $link . ' is valid external link.');
  598. // The following two lines are commented out and only used for
  599. // comparisons.
  600. // $valid2 = valid_url($link, TRUE);
  601. // $this->assertEqual(TRUE, $valid2, "Using valid_url() on $link.");.
  602. }
  603. }
  604. /**
  605. * Check Invalid External Links.
  606. */
  607. public function testInvalidExternalLinks() {
  608. $links = array(
  609. 'http://www.ex ample.com/',
  610. // Bad ip!
  611. 'http://25.0.0/',
  612. 'http://4827.0.0.2/',
  613. // ß not allowed in domain names!
  614. 'http://www.testß.com/',
  615. // Bad TLD.
  616. 'http://.www.foo.bar./',
  617. // Domains can't have sections starting with a dash.
  618. // 'http://www.-fudge.com/',
  619. 'http://example.com/index.php?page=this\that',
  620. 'example@example.com',
  621. );
  622. foreach ($links as $link) {
  623. $valid = link_validate_url($link);
  624. $this->assertEqual(FALSE, $valid, 'Testing that ' . $link . ' is not a valid link.');
  625. }
  626. }
  627. }