link.crud_browser.test 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. /**
  3. * @file
  4. * Testing CRUD API in the browser.
  5. */
  6. /**
  7. * Testing that users can not input bad URLs or labels
  8. */
  9. class LinkUITest extends DrupalWebTestcase {
  10. /**
  11. * Link supposed to be good
  12. */
  13. const LINK_INPUT_TYPE_GOOD = 0;
  14. /**
  15. * Link supposed to have a bad title
  16. */
  17. const LINK_INPUT_TYPE_BAD_TITLE = 1;
  18. /**
  19. * Link supposed to have a bad URL
  20. */
  21. const LINK_INPUT_TYPE_BAD_URL = 2;
  22. public static function getInfo() {
  23. return array(
  24. 'name' => 'Link CRUD - browser test',
  25. 'description' => 'Tests the field CRUD (create, read, update, delete) API 2.',
  26. 'group' => 'Link',
  27. );
  28. }
  29. function setUp() {
  30. parent::setUp('field_ui', 'link');
  31. }
  32. /**
  33. * Creates a link field for the "page" type and creates a page with a link.
  34. */
  35. function testLinkCreate() {
  36. //libxml_use_internal_errors(true);
  37. $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  38. $perms = array_keys($perms[array_search('administrator', user_roles())]);
  39. $admin = $this->drupalCreateUser($perms);
  40. $this->drupalLogin($admin);
  41. // create field
  42. $name = strtolower($this->randomName());
  43. $edit = array(
  44. 'fields[_add_new_field][label]' => $name,
  45. 'fields[_add_new_field][field_name]' => $name,
  46. 'fields[_add_new_field][type]' => 'link_field',
  47. 'fields[_add_new_field][widget_type]' => 'link_field',
  48. );
  49. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  50. $this->drupalPost(NULL, array(), t('Save field settings'));
  51. $this->drupalPost(NULL, array(), t('Save settings'));
  52. // Is field created?
  53. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  54. node_types_rebuild();
  55. menu_rebuild();
  56. $permission = 'create page content';
  57. $this->checkPermissions(array($permission), TRUE);
  58. // create page form
  59. //$this->drupalGet('node/add');
  60. $this->drupalGet('node/add/page');
  61. $field_name = 'field_' . $name;
  62. $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
  63. $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
  64. $input_test_cases = array(
  65. array(
  66. 'href' => 'http://example.com/' . $this->randomName(),
  67. 'label' => $this->randomName(),
  68. 'msg' => 'Link found',
  69. 'type' => self::LINK_INPUT_TYPE_GOOD
  70. ),
  71. array(
  72. 'href' => 'http://example.com/' . $this->randomName(),
  73. 'label' => $this->randomName() . '<script>alert("hi");</script>',
  74. 'msg' => 'js label',
  75. 'type' => self::LINK_INPUT_TYPE_BAD_TITLE
  76. ),
  77. array(
  78. 'href' => 'http://example.com/' . $this->randomName(),
  79. 'label' => $this->randomName() . '<script src="http://devil.site.com"></script>',
  80. 'msg' => 'js label',
  81. 'type' => self::LINK_INPUT_TYPE_BAD_TITLE
  82. ),
  83. array(
  84. 'href' => 'http://example.com/' . $this->randomName(),
  85. 'label' => $this->randomName() . '" onmouseover="alert(\'hi\')',
  86. 'msg' => 'js label',
  87. 'type' => self::LINK_INPUT_TYPE_BAD_TITLE
  88. ),
  89. array(
  90. 'href' => 'http://example.com/' . $this->randomName(),
  91. 'label' => $this->randomName() . '\' onmouseover="alert(\'hi\')',
  92. 'msg' => 'js label',
  93. 'type' => self::LINK_INPUT_TYPE_BAD_TITLE
  94. ),
  95. array(
  96. 'href' => 'javascript:alert("http://example.com/' . $this->randomName() . '")',
  97. 'label' => $this->randomName(),
  98. 'msg' => 'js url',
  99. 'type' => self::LINK_INPUT_TYPE_BAD_URL
  100. ),
  101. array(
  102. 'href' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
  103. 'label' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
  104. 'msg' => 'Url with . in querystring',
  105. 'type' => self::LINK_INPUT_TYPE_GOOD,
  106. ),
  107. );
  108. $test_case = array(
  109. 'href' => 'www.example.com/' . $this->randomName(),
  110. 'label' => $this->randomName(),
  111. 'msg' => 'Link found',
  112. 'type' => self::LINK_INPUT_TYPE_GOOD,
  113. );
  114. $test_case['expected_href'] = 'http://' . $test_case['href'];
  115. $input_test_cases[] = $test_case;
  116. foreach ($input_test_cases as $input) {
  117. $this->drupalLogin($admin);
  118. $this->drupalGet('node/add/page');
  119. $edit = array(
  120. 'title' => $input['label'],
  121. $field_name . '[und][0][title]' => $input['label'],
  122. $field_name . '[und][0][url]' => $input['href'],
  123. );
  124. $this->drupalPost(NULL, $edit, t('Save'));
  125. if ($input['type'] == self::LINK_INPUT_TYPE_BAD_URL) {
  126. $this->assertRaw(t('The value %value provided for %field is not a valid URL.', array('%field' => $name, '%value' => trim($input['href']))), 'Not a valid URL: ' . $input['href']);
  127. continue;
  128. }
  129. else {
  130. $this->assertRaw(' ' . t('has been created.',
  131. array('@type' => 'Basic Page', '%title' => $edit['title'])),
  132. 'Page created: ' . $input['href']);
  133. }
  134. $url = $this->getUrl();
  135. // change to Anonymous user.
  136. $this->drupalLogout();
  137. $this->drupalGet($url);
  138. //debug($this);
  139. // If simpletest starts using something to override the error system, this will flag
  140. // us and let us know it's broken.
  141. $this->assertFalse(libxml_use_internal_errors(TRUE));
  142. if (isset($input['expected_href'])) {
  143. $path = '//a[@href="' . $input['expected_href'] . '" and text()="' . $input['label'] . '"]';
  144. }
  145. else {
  146. $path = '//a[@href="' . $input['href'] . '" and text()="' . $input['label'] . '"]';
  147. }
  148. $elements = $this->xpath($path);
  149. libxml_use_internal_errors(FALSE);
  150. $this->assertIdentical(isset($elements[0]), $input['type'] == self::LINK_INPUT_TYPE_GOOD, $input['msg']);
  151. }
  152. //libxml_use_internal_errors(FALSE);
  153. }
  154. /**
  155. * Testing that if you use <strong> in a static title for your link, that the
  156. * title actually displays <strong>.
  157. */
  158. function testStaticLinkCreate() {
  159. $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  160. $perms = array_keys($perms[array_search('administrator', user_roles())]);
  161. $admin = $this->drupalCreateUser($perms);
  162. $this->drupalLogin($admin);
  163. // create field
  164. $name = strtolower($this->randomName());
  165. $field_name = 'field_' . $name;
  166. $edit = array(
  167. 'fields[_add_new_field][label]' => $name,
  168. 'fields[_add_new_field][field_name]' => $name,
  169. 'fields[_add_new_field][type]' => 'link_field',
  170. 'fields[_add_new_field][widget_type]' => 'link_field',
  171. );
  172. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  173. $this->drupalPost(NULL, array(), t('Save field settings'));
  174. $this->drupalPost(NULL, array(
  175. 'instance[settings][title]' => 'value',
  176. 'instance[settings][title_value]' => '<strong>' . $name . '</strong>'), t('Save settings'));
  177. // Is field created?
  178. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  179. // create page form
  180. $this->drupalGet('node/add/page');
  181. $this->assertField($field_name . '[und][0][url]', 'URL found');
  182. $input = array(
  183. 'href' => 'http://example.com/' . $this->randomName()
  184. );
  185. $edit = array(
  186. 'title' => $name,
  187. $field_name . '[und][0][url]' => $input['href'],
  188. );
  189. $this->drupalPost(NULL, $edit, t('Save'));
  190. $url = $this->getUrl();
  191. // change to anonymous user
  192. $this->drupalLogout();
  193. $this->drupalGet($url);
  194. $this->assertRaw(l('<strong>' . $name . '</strong>', $input['href'], array('html' => TRUE)));
  195. }
  196. /**
  197. * Testing that if you have the title but no url, the title is not sanitized twice.
  198. */
  199. function testCRUDTitleOnlyTitleNoLink() {
  200. $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  201. $perms = array_keys($perms[array_search('administrator', user_roles())]);
  202. $admin = $this->drupalCreateUser($perms);
  203. $this->drupalLogin($admin);
  204. // create field
  205. $name = strtolower($this->randomName());
  206. $field_name = 'field_' . $name;
  207. $edit = array(
  208. 'fields[_add_new_field][label]' => $name,
  209. 'fields[_add_new_field][field_name]' => $name,
  210. 'fields[_add_new_field][type]' => 'link_field',
  211. 'fields[_add_new_field][widget_type]' => 'link_field',
  212. );
  213. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  214. $this->drupalPost(NULL, array(), t('Save field settings'));
  215. $this->drupalPost(NULL, array(
  216. 'instance[settings][url]' => 1,
  217. ), t('Save settings'));
  218. // Is field created?
  219. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  220. // create page form
  221. $this->drupalGet('node/add/page');
  222. $this->assertField($field_name . '[und][0][url]', 'URL found');
  223. $input = array(
  224. 'title' => 'This & That',
  225. 'href' => '',
  226. );
  227. $edit = array(
  228. 'title' => $name,
  229. $field_name . '[und][0][title]' => $input['title'],
  230. $field_name . '[und][0][url]' => $input['href'],
  231. );
  232. $this->drupalPost(NULL, $edit, t('Save'));
  233. $url = $this->getUrl();
  234. // change to anonymous user
  235. $this->drupalLogout();
  236. $this->drupalGet($url);
  237. $this->assertRaw('This &amp; That');
  238. }
  239. /**
  240. * If we're creating a new field and just hit 'save' on the default options, we want to make
  241. * sure they are set to the expected results.
  242. */
  243. function testCRUDCreateFieldDefaults() {
  244. $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  245. $perms = array_keys($perms[array_search('administrator', user_roles())]);
  246. $admin = $this->drupalCreateUser($perms);
  247. $this->drupalLogin($admin);
  248. // create field
  249. $name = strtolower($this->randomName());
  250. $edit = array(
  251. 'fields[_add_new_field][label]' => $name,
  252. 'fields[_add_new_field][field_name]' => $name,
  253. 'fields[_add_new_field][type]' => 'link_field',
  254. 'fields[_add_new_field][widget_type]' => 'link_field',
  255. );
  256. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  257. $this->drupalPost(NULL, array(), t('Save field settings'));
  258. $this->drupalPost(NULL, array(), t('Save settings'));
  259. // Is field created?
  260. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  261. node_types_rebuild();
  262. menu_rebuild();
  263. _field_info_collate_fields(TRUE);
  264. $instances = field_info_instances('node', 'page');
  265. $instance = $instances['field_' . $name];
  266. $this->assertFalse($instance['required'], 'Make sure field is not required.');
  267. $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
  268. $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
  269. $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
  270. $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
  271. $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
  272. $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
  273. $this->assertFalse($instance['settings']['attributes']['class'], 'By default, no class should be set.');
  274. $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
  275. }
  276. /**
  277. * If we're creating a new field and just hit 'save' on the default options, we want to make
  278. * sure they are set to the expected results.
  279. */
  280. function testCRUDCreateFieldWithClass() {
  281. $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  282. $perms = array_keys($perms[array_search('administrator', user_roles())]);
  283. $admin = $this->drupalCreateUser($perms);
  284. $this->drupalLogin($admin);
  285. // create field
  286. $name = strtolower($this->randomName());
  287. $edit = array(
  288. 'fields[_add_new_field][label]' => $name,
  289. 'fields[_add_new_field][field_name]' => $name,
  290. 'fields[_add_new_field][type]' => 'link_field',
  291. 'fields[_add_new_field][widget_type]' => 'link_field',
  292. );
  293. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  294. $this->drupalPost(NULL, array(), t('Save field settings'));
  295. $link_class_name = 'basic-link-' . strtolower($this->randomName());
  296. $edit = array(
  297. 'instance[settings][attributes][class]' => $link_class_name,
  298. );
  299. $this->drupalPost(NULL, $edit, t('Save settings'));
  300. // Is field created?
  301. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  302. node_types_rebuild();
  303. menu_rebuild();
  304. _field_info_collate_fields(TRUE);
  305. $instances = field_info_instances('node', 'page');
  306. $instance = $instances['field_' . $name];
  307. $this->assertFalse($instance['required'], 'Make sure field is not required.');
  308. $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
  309. $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
  310. $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
  311. $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
  312. $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
  313. $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
  314. $this->assertEqual($instance['settings']['attributes']['class'], $link_class_name, 'One class should be set.');
  315. $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
  316. // Now, let's create a node with this field and make sure the link shows up:
  317. // create page form
  318. $field_name = 'field_' . $name;
  319. $this->drupalGet('node/add/page');
  320. $this->assertField($field_name . '[und][0][url]', 'URL found');
  321. $input = array(
  322. 'title' => 'This & That',
  323. 'href' => 'http://www.example.com/',
  324. );
  325. $edit = array(
  326. 'title' => $field_name,
  327. $field_name . '[und][0][title]' => $input['title'],
  328. $field_name . '[und][0][url]' => $input['href'],
  329. );
  330. $this->drupalPost(NULL, $edit, t('Save'));
  331. $url = $this->getUrl();
  332. // change to anonymous user
  333. $this->drupalLogout();
  334. $this->drupalGet($url);
  335. $this->assertRaw('This &amp; That');
  336. $this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Class $link_class_name exists on page.");
  337. }
  338. /**
  339. * If we're creating a new field and just hit 'save' on the default options, we want to make
  340. * sure they are set to the expected results.
  341. */
  342. function testCRUDCreateFieldWithTwoClasses() {
  343. $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  344. $perms = array_keys($perms[array_search('administrator', user_roles())]);
  345. $admin = $this->drupalCreateUser($perms);
  346. $this->drupalLogin($admin);
  347. // create field
  348. $name = strtolower($this->randomName());
  349. $edit = array(
  350. 'fields[_add_new_field][label]' => $name,
  351. 'fields[_add_new_field][field_name]' => $name,
  352. 'fields[_add_new_field][type]' => 'link_field',
  353. 'fields[_add_new_field][widget_type]' => 'link_field',
  354. );
  355. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  356. $this->drupalPost(NULL, array(), t('Save field settings'));
  357. $link_class_name = 'basic-link ' . strtoupper($this->randomName());
  358. $edit = array(
  359. 'instance[settings][attributes][class]' => $link_class_name,
  360. );
  361. $this->drupalPost(NULL, $edit, t('Save settings'));
  362. // Is field created?
  363. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  364. node_types_rebuild();
  365. menu_rebuild();
  366. _field_info_collate_fields(TRUE);
  367. $instances = field_info_instances('node', 'page');
  368. $instance = $instances['field_' . $name];
  369. $this->assertFalse($instance['required'], 'Make sure field is not required.');
  370. $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
  371. $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
  372. $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
  373. $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
  374. $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
  375. $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
  376. $this->assertEqual($instance['settings']['attributes']['class'], $link_class_name, 'Two classes should be set.');
  377. $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
  378. // Now, let's create a node with this field and make sure the link shows up:
  379. // create page form
  380. $field_name = 'field_' . $name;
  381. $this->drupalGet('node/add/page');
  382. $this->assertField($field_name . '[und][0][url]', 'URL found');
  383. $input = array(
  384. 'title' => 'This & That',
  385. 'href' => 'http://www.example.com/',
  386. );
  387. $edit = array(
  388. 'title' => $field_name,
  389. $field_name . '[und][0][title]' => $input['title'],
  390. $field_name . '[und][0][url]' => $input['href'],
  391. );
  392. $this->drupalPost(NULL, $edit, t('Save'));
  393. $url = $this->getUrl();
  394. // change to anonymous user
  395. $this->drupalLogout();
  396. $this->drupalGet($url);
  397. $this->assertRaw('This &amp; That');
  398. $this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Classes $link_class_name exist on page.");
  399. }
  400. }