link-validate_url-2299657-46.patch 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. From f8ea05469f22005733e7d030f45ff85771924f14 Mon Sep 17 00:00:00 2001
  2. From: Jaleel Carter <Jaleel.Carter@acquia.com>
  3. Date: Thu, 14 Jul 2016 13:29:03 -0400
  4. Subject: [PATCH] 2299657-45
  5. 2299657-45-1
  6. ---
  7. link.install | 14 +++++++++++
  8. link.module | 16 +++++++-----
  9. tests/link.attribute.test | 6 +++--
  10. tests/link.crud.test | 6 +++--
  11. tests/link.crud_browser.test | 46 ++++++++++++++++++++---------------
  12. tests/link.test | 8 +++---
  13. tests/link.validate.test | 58 ++++++++++++++++++--------------------------
  14. 7 files changed, 87 insertions(+), 67 deletions(-)
  15. diff --git a/link.install b/link.install
  16. index 14e745d..e1820f7 100644
  17. --- a/link.install
  18. +++ b/link.install
  19. @@ -115,3 +115,17 @@ function link_update_7001() {
  20. }
  21. }
  22. }
  23. +
  24. +/**
  25. + * Removes unused link_extra_domains variable.
  26. + */
  27. +function link_update_7002() {
  28. + variable_del('link_extra_domains');
  29. +}
  30. +
  31. +/**
  32. + * Implements hook_uninstall().
  33. + */
  34. +function link_uninstall() {
  35. + variable_del('link_allowed_domains');
  36. +}
  37. diff --git a/link.module b/link.module
  38. index 1e9d088..aece66d 100644
  39. --- a/link.module
  40. +++ b/link.module
  41. @@ -10,7 +10,6 @@ define('LINK_INTERNAL', 'internal');
  42. define('LINK_FRONT', 'front');
  43. define('LINK_EMAIL', 'email');
  44. define('LINK_NEWS', 'news');
  45. -define('LINK_DOMAINS', 'aero|arpa|asia|biz|build|com|cat|ceo|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|post|pro|tel|travel|mobi|local|xxx');
  46. define('LINK_TARGET_DEFAULT', 'default');
  47. define('LINK_TARGET_NEW_WINDOW', '_blank');
  48. @@ -1261,7 +1260,7 @@ function link_validate_url($text, $langcode = NULL) {
  49. *
  50. * @param string $text
  51. * Url to be checked.
  52. - *
  53. + *
  54. * @return mixed
  55. * Returns boolean FALSE if the URL is not valid. On success, returns one of
  56. * the LINK_(linktype) constants.
  57. @@ -1383,11 +1382,16 @@ function link_url_type($text) {
  58. }
  59. /**
  60. - * Returns the list of allowed domains, including domains added by admins via variable_set/$config.
  61. + * Returns the list of allowed domains.
  62. + *
  63. + * If the variable link_allowed_domains is set, restrict allowed domains to the
  64. + * strings in that array. If the variable link_allowed_domains is not set, allow
  65. + * all domains between 2 and 63 characters in length.
  66. + * See https://tools.ietf.org/html/rfc1034.
  67. */
  68. function _link_domains() {
  69. - $link_extra_domains = variable_get('link_extra_domains', array());
  70. - return empty($link_extra_domains) ? LINK_DOMAINS : LINK_DOMAINS . '|' . implode('|', $link_extra_domains);
  71. + $link_allowed_domains = variable_get('link_allowed_domains', array());
  72. + return empty($link_allowed_domains) ? '[a-z][a-z0-9-]{1,62}' : implode('|', $link_allowed_domains);
  73. }
  74. /**
  75. @@ -1439,7 +1443,7 @@ function link_field_settings_form() {
  76. /**
  77. * Additional callback to adapt the property info of link fields.
  78. - *
  79. + *
  80. * @see entity_metadata_field_entity_property_info()
  81. */
  82. function link_field_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
  83. diff --git a/tests/link.attribute.test b/tests/link.attribute.test
  84. index 36e6be5..4765814 100644
  85. --- a/tests/link.attribute.test
  86. +++ b/tests/link.attribute.test
  87. @@ -31,8 +31,10 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
  88. parent::setup('field_ui', 'link');
  89. $this->zebra = 0;
  90. // Create and login user.
  91. - $this->web_user = $this->drupalCreateUser(array('administer content types'));
  92. - $this->drupalLogin($this->web_user);
  93. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  94. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  95. + $admin = $this->drupalCreateUser($perms);
  96. + $this->drupalLogin($admin);
  97. }
  98. protected function createLink($url, $title, $attributes = array()) {
  99. diff --git a/tests/link.crud.test b/tests/link.crud.test
  100. index e9b7db4..5f12840 100644
  101. --- a/tests/link.crud.test
  102. +++ b/tests/link.crud.test
  103. @@ -29,8 +29,10 @@ class LinkContentCrudTest extends DrupalWebTestCase {
  104. $title = $this->randomName(20);
  105. // Create and login user.
  106. - $this->web_user = $this->drupalCreateUser(array('administer content types'));
  107. - $this->drupalLogin($this->web_user);
  108. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  109. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  110. + $admin = $this->drupalCreateUser($perms);
  111. + $this->drupalLogin($admin);
  112. $this->drupalGet('admin/structure/types');
  113. diff --git a/tests/link.crud_browser.test b/tests/link.crud_browser.test
  114. index be04260..803169b 100644
  115. --- a/tests/link.crud_browser.test
  116. +++ b/tests/link.crud_browser.test
  117. @@ -42,15 +42,11 @@ class LinkUITest extends DrupalWebTestcase {
  118. */
  119. function testLinkCreate() {
  120. //libxml_use_internal_errors(true);
  121. - $this->web_user = $this->drupalCreateUser(array(
  122. - 'administer content types',
  123. - 'administer nodes',
  124. - 'administer filters',
  125. - 'access content',
  126. - 'create page content',
  127. - 'access administration pages'
  128. - ));
  129. - $this->drupalLogin($this->web_user);
  130. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  131. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  132. + $admin = $this->drupalCreateUser($perms);
  133. + $this->drupalLogin($admin);
  134. +
  135. // create field
  136. $name = strtolower($this->randomName());
  137. @@ -133,7 +129,7 @@ class LinkUITest extends DrupalWebTestcase {
  138. $input_test_cases[] = $test_case;
  139. foreach ($input_test_cases as $input) {
  140. - $this->drupalLogin($this->web_user);
  141. + $this->drupalLogin($admin);
  142. $this->drupalGet('node/add/page');
  143. $edit = array(
  144. @@ -179,8 +175,10 @@ class LinkUITest extends DrupalWebTestcase {
  145. * title actually displays <strong>.
  146. */
  147. function testStaticLinkCreate() {
  148. - $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
  149. - $this->drupalLogin($this->web_user);
  150. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  151. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  152. + $admin = $this->drupalCreateUser($perms);
  153. + $this->drupalLogin($admin);
  154. // create field
  155. $name = strtolower($this->randomName());
  156. @@ -227,8 +225,10 @@ class LinkUITest extends DrupalWebTestcase {
  157. * Testing that if you have the title but no url, the title is not sanitized twice.
  158. */
  159. function testCRUDTitleOnlyTitleNoLink() {
  160. - $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
  161. - $this->drupalLogin($this->web_user);
  162. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  163. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  164. + $admin = $this->drupalCreateUser($perms);
  165. + $this->drupalLogin($admin);
  166. // create field
  167. $name = strtolower($this->randomName());
  168. @@ -278,8 +278,10 @@ class LinkUITest extends DrupalWebTestcase {
  169. * sure they are set to the expected results.
  170. */
  171. function testCRUDCreateFieldDefaults() {
  172. - $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
  173. - $this->drupalLogin($this->web_user);
  174. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  175. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  176. + $admin = $this->drupalCreateUser($perms);
  177. + $this->drupalLogin($admin);
  178. // create field
  179. $name = strtolower($this->randomName());
  180. @@ -318,8 +320,10 @@ class LinkUITest extends DrupalWebTestcase {
  181. * sure they are set to the expected results.
  182. */
  183. function testCRUDCreateFieldWithClass() {
  184. - $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
  185. - $this->drupalLogin($this->web_user);
  186. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  187. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  188. + $admin = $this->drupalCreateUser($perms);
  189. + $this->drupalLogin($admin);
  190. // create field
  191. $name = strtolower($this->randomName());
  192. @@ -390,8 +394,10 @@ class LinkUITest extends DrupalWebTestcase {
  193. * sure they are set to the expected results.
  194. */
  195. function testCRUDCreateFieldWithTwoClasses() {
  196. - $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
  197. - $this->drupalLogin($this->web_user);
  198. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  199. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  200. + $admin = $this->drupalCreateUser($perms);
  201. + $this->drupalLogin($admin);
  202. // create field
  203. $name = strtolower($this->randomName());
  204. diff --git a/tests/link.test b/tests/link.test
  205. index 962197f..b030153 100644
  206. --- a/tests/link.test
  207. +++ b/tests/link.test
  208. @@ -23,9 +23,11 @@ class LinkBaseTestClass extends DrupalWebTestCase {
  209. $modules[] = 'field_ui';
  210. $modules[] = 'link';
  211. parent::setUp($modules);
  212. -
  213. - $this->web_user = $this->drupalCreateUser($this->permissions);
  214. - $this->drupalLogin($this->web_user);
  215. +
  216. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  217. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  218. + $admin = $this->drupalCreateUser($perms);
  219. + $this->drupalLogin($admin);
  220. }
  221. protected function createLinkField($node_type = 'page', $settings = array()) {
  222. diff --git a/tests/link.validate.test b/tests/link.validate.test
  223. index a9ac116..f03a64c 100644
  224. --- a/tests/link.validate.test
  225. +++ b/tests/link.validate.test
  226. @@ -61,13 +61,10 @@ class LinkValidateTest extends LinkValidateTestCase {
  227. * Test if we're stopped from posting a bad url on default validation.
  228. */
  229. function test_link_validate_bad_url_validate_default() {
  230. - $this->web_user = $this->drupalCreateUser(array('administer content types',
  231. - 'administer nodes',
  232. - 'administer filters',
  233. - 'access content',
  234. - 'create page content',
  235. - 'access administration pages'));
  236. - $this->drupalLogin($this->web_user);
  237. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  238. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  239. + $admin = $this->drupalCreateUser($perms);
  240. + $this->drupalLogin($admin);
  241. // create field
  242. $name = strtolower($this->randomName());
  243. @@ -106,13 +103,10 @@ class LinkValidateTest extends LinkValidateTestCase {
  244. * Test if we're stopped from posting a bad url with validation on.
  245. */
  246. function test_link_validate_bad_url_validate_on() {
  247. - $this->web_user = $this->drupalCreateUser(array('administer content types',
  248. - 'administer nodes',
  249. - 'administer filters',
  250. - 'access content',
  251. - 'create page content',
  252. - 'access administration pages'));
  253. - $this->drupalLogin($this->web_user);
  254. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  255. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  256. + $admin = $this->drupalCreateUser($perms);
  257. + $this->drupalLogin($admin);
  258. // create field
  259. $name = strtolower($this->randomName());
  260. @@ -152,13 +146,10 @@ class LinkValidateTest extends LinkValidateTestCase {
  261. * Test if we can post a bad url if the validation is expressly turned off.
  262. */
  263. function test_link_validate_bad_url_validate_off() {
  264. - $this->web_user = $this->drupalCreateUser(array('administer content types',
  265. - 'administer nodes',
  266. - 'administer filters',
  267. - 'access content',
  268. - 'create page content',
  269. - 'access administration pages'));
  270. - $this->drupalLogin($this->web_user);
  271. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  272. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  273. + $admin = $this->drupalCreateUser($perms);
  274. + $this->drupalLogin($admin);
  275. // create field
  276. $name = strtolower($this->randomName());
  277. @@ -202,13 +193,10 @@ class LinkValidateTest extends LinkValidateTestCase {
  278. */
  279. function x_test_link_validate_switching_between_validation_status() {
  280. $this->acquireContentTypes(1);
  281. - $this->web_user = $this->drupalCreateUser(array('administer content types',
  282. - 'administer nodes',
  283. - 'access administration pages',
  284. - 'access content',
  285. - 'create ' . $this->content_types[0]->type . ' content',
  286. - 'edit any ' . $this->content_types[0]->type . ' content'));
  287. - $this->drupalLogin($this->web_user);
  288. + $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  289. + $perms = array_keys($perms[array_search('administrator', user_roles())]);
  290. + $admin = $this->drupalCreateUser($perms);
  291. + $this->drupalLogin($admin);
  292. variable_set('node_options_' . $this->content_types[0]->name, array('status', 'promote'));
  293. $field_settings = array(
  294. 'type' => 'link',
  295. @@ -368,7 +356,7 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
  296. 'group' => 'Link',
  297. );
  298. }
  299. -
  300. +
  301. function setUp() {
  302. parent::setUp('link');
  303. }
  304. @@ -429,11 +417,10 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
  305. function testValidateInternalLinks() {
  306. $tempfile = drupal_tempnam('public://files', 'test');
  307. $links = array(
  308. - 'rss.xml',
  309. file_uri_target($tempfile),
  310. drupal_realpath($tempfile),
  311. );
  312. -
  313. +
  314. foreach ($links as $link) {
  315. $type = link_url_type($link);
  316. $this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
  317. @@ -482,8 +469,8 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
  318. //$valid2 = valid_url($link, TRUE);
  319. //$this->assertEqual(TRUE, $valid2, "Using valid_url() on $link.");
  320. }
  321. - // Test if we can make a tld valid:
  322. - variable_set('link_extra_domains', array('frog'));
  323. + // Test if we can make a tld allowable:
  324. + variable_set('link_allowed_domains', array('frog'));
  325. $valid = link_validate_url('http://www.example.frog');
  326. $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.");
  327. }
  328. @@ -495,7 +482,6 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
  329. 'http://4827.0.0.2/',
  330. '//www.example.com/',
  331. 'http://www.testß.com/', // ß not allowed in domain names!
  332. - 'http://www.example.frog/', // Bad TLD
  333. //'http://www.-fudge.com/', // domains can't have sections starting with a dash.
  334. 'http://example.com/index.php?page=this\that',
  335. 'example@example.com',
  336. @@ -504,5 +490,9 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
  337. $valid = link_validate_url($link);
  338. $this->assertEqual(FALSE, $valid, 'Testing that ' . $link . ' is not a valid link.');
  339. }
  340. + // Test if we can make a tld disallowed:
  341. + variable_set('link_allowed_domains', array('toad'));
  342. + $valid = link_validate_url('http://www.example.frog');
  343. + $this->assertEqual(FALSE, $valid, "Testing that http://www.example.frog is an invalid external link if we've not added 'frog' to the list of valid domains.");
  344. }
  345. }
  346. --
  347. 2.7.4 (Apple Git-66)