link.crud_browser.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. $this->web_user = $this->drupalCreateUser(array(
  38. 'administer content types',
  39. 'administer nodes',
  40. 'administer filters',
  41. 'access content',
  42. 'create page content',
  43. 'access administration pages'
  44. ));
  45. $this->drupalLogin($this->web_user);
  46. // create field
  47. $name = strtolower($this->randomName());
  48. $edit = array(
  49. 'fields[_add_new_field][label]' => $name,
  50. 'fields[_add_new_field][field_name]' => $name,
  51. 'fields[_add_new_field][type]' => 'link_field',
  52. 'fields[_add_new_field][widget_type]' => 'link_field',
  53. );
  54. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  55. $this->drupalPost(NULL, array(), t('Save field settings'));
  56. $this->drupalPost(NULL, array(), t('Save settings'));
  57. // Is field created?
  58. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  59. node_types_rebuild();
  60. menu_rebuild();
  61. $permission = 'create page content';
  62. $this->checkPermissions(array($permission), TRUE);
  63. // create page form
  64. //$this->drupalGet('node/add');
  65. $this->drupalGet('node/add/page');
  66. $field_name = 'field_' . $name;
  67. $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
  68. $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
  69. $input_test_cases = array(
  70. array(
  71. 'href' => 'http://example.com/' . $this->randomName(),
  72. 'label' => $this->randomName(),
  73. 'msg' => 'Link found',
  74. 'type' => self::LINK_INPUT_TYPE_GOOD
  75. ),
  76. array(
  77. 'href' => 'http://example.com/' . $this->randomName(),
  78. 'label' => $this->randomName() . '<script>alert("hi");</script>',
  79. 'msg' => 'js label',
  80. 'type' => self::LINK_INPUT_TYPE_BAD_TITLE
  81. ),
  82. array(
  83. 'href' => 'http://example.com/' . $this->randomName(),
  84. 'label' => $this->randomName() . '<script src="http://devil.site.com"></script>',
  85. 'msg' => 'js label',
  86. 'type' => self::LINK_INPUT_TYPE_BAD_TITLE
  87. ),
  88. array(
  89. 'href' => 'http://example.com/' . $this->randomName(),
  90. 'label' => $this->randomName() . '" onmouseover="alert(\'hi\')',
  91. 'msg' => 'js label',
  92. 'type' => self::LINK_INPUT_TYPE_BAD_TITLE
  93. ),
  94. array(
  95. 'href' => 'http://example.com/' . $this->randomName(),
  96. 'label' => $this->randomName() . '\' onmouseover="alert(\'hi\')',
  97. 'msg' => 'js label',
  98. 'type' => self::LINK_INPUT_TYPE_BAD_TITLE
  99. ),
  100. array(
  101. 'href' => 'javascript:alert("http://example.com/' . $this->randomName() . '")',
  102. 'label' => $this->randomName(),
  103. 'msg' => 'js url',
  104. 'type' => self::LINK_INPUT_TYPE_BAD_URL
  105. ),
  106. array(
  107. 'href' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
  108. 'label' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
  109. 'msg' => 'Url with . in querystring',
  110. 'type' => self::LINK_INPUT_TYPE_GOOD,
  111. ),
  112. );
  113. $test_case = array(
  114. 'href' => 'www.example.com/' . $this->randomName(),
  115. 'label' => $this->randomName(),
  116. 'msg' => 'Link found',
  117. 'type' => self::LINK_INPUT_TYPE_GOOD,
  118. );
  119. $test_case['expected_href'] = 'http://' . $test_case['href'];
  120. $input_test_cases[] = $test_case;
  121. foreach ($input_test_cases as $input) {
  122. $this->drupalLogin($this->web_user);
  123. $this->drupalGet('node/add/page');
  124. $edit = array(
  125. 'title' => $input['label'],
  126. $field_name . '[und][0][title]' => $input['label'],
  127. $field_name . '[und][0][url]' => $input['href'],
  128. );
  129. $this->drupalPost(NULL, $edit, t('Save'));
  130. if ($input['type'] == self::LINK_INPUT_TYPE_BAD_URL) {
  131. $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']);
  132. continue;
  133. }
  134. else {
  135. $this->assertRaw(' ' . t('has been created.',
  136. array('@type' => 'Basic Page', '%title' => $edit['title'])),
  137. 'Page created: ' . $input['href']);
  138. }
  139. $url = $this->getUrl();
  140. // change to Anonymous user.
  141. $this->drupalLogout();
  142. $this->drupalGet($url);
  143. //debug($this);
  144. // If simpletest starts using something to override the error system, this will flag
  145. // us and let us know it's broken.
  146. $this->assertFalse(libxml_use_internal_errors(TRUE));
  147. if (isset($input['expected_href'])) {
  148. $path = '//a[@href="' . $input['expected_href'] . '" and text()="' . $input['label'] . '"]';
  149. }
  150. else {
  151. $path = '//a[@href="' . $input['href'] . '" and text()="' . $input['label'] . '"]';
  152. }
  153. $elements = $this->xpath($path);
  154. libxml_use_internal_errors(FALSE);
  155. $this->assertIdentical(isset($elements[0]), $input['type'] == self::LINK_INPUT_TYPE_GOOD, $input['msg']);
  156. }
  157. //libxml_use_internal_errors(FALSE);
  158. }
  159. /**
  160. * Testing that if you use <strong> in a static title for your link, that the
  161. * title actually displays <strong>.
  162. */
  163. function testStaticLinkCreate() {
  164. $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
  165. $this->drupalLogin($this->web_user);
  166. // create field
  167. $name = strtolower($this->randomName());
  168. $field_name = 'field_' . $name;
  169. $edit = array(
  170. 'fields[_add_new_field][label]' => $name,
  171. 'fields[_add_new_field][field_name]' => $name,
  172. 'fields[_add_new_field][type]' => 'link_field',
  173. 'fields[_add_new_field][widget_type]' => 'link_field',
  174. );
  175. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  176. $this->drupalPost(NULL, array(), t('Save field settings'));
  177. $this->drupalPost(NULL, array(
  178. 'instance[settings][title]' => 'value',
  179. 'instance[settings][title_value]' => '<strong>' . $name . '</strong>'), t('Save settings'));
  180. // Is field created?
  181. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  182. // create page form
  183. $this->drupalGet('node/add/page');
  184. $this->assertField($field_name . '[und][0][url]', 'URL found');
  185. $input = array(
  186. 'href' => 'http://example.com/' . $this->randomName()
  187. );
  188. $edit = array(
  189. 'title' => $name,
  190. $field_name . '[und][0][url]' => $input['href'],
  191. );
  192. $this->drupalPost(NULL, $edit, t('Save'));
  193. $url = $this->getUrl();
  194. // change to anonymous user
  195. $this->drupalLogout();
  196. $this->drupalGet($url);
  197. $this->assertRaw(l('<strong>' . $name . '</strong>', $input['href'], array('html' => TRUE)));
  198. }
  199. /**
  200. * Testing that if you have the title but no url, the title is not sanitized twice.
  201. */
  202. function testCRUDTitleOnlyTitleNoLink() {
  203. $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
  204. $this->drupalLogin($this->web_user);
  205. // create field
  206. $name = strtolower($this->randomName());
  207. $field_name = 'field_' . $name;
  208. $edit = array(
  209. 'fields[_add_new_field][label]' => $name,
  210. 'fields[_add_new_field][field_name]' => $name,
  211. 'fields[_add_new_field][type]' => 'link_field',
  212. 'fields[_add_new_field][widget_type]' => 'link_field',
  213. );
  214. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  215. $this->drupalPost(NULL, array(), t('Save field settings'));
  216. $this->drupalPost(NULL, array(
  217. 'instance[settings][url]' => 1,
  218. ), t('Save settings'));
  219. // Is field created?
  220. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  221. // create page form
  222. $this->drupalGet('node/add/page');
  223. $this->assertField($field_name . '[und][0][url]', 'URL found');
  224. $input = array(
  225. 'title' => 'This & That',
  226. 'href' => '',
  227. );
  228. $edit = array(
  229. 'title' => $name,
  230. $field_name . '[und][0][title]' => $input['title'],
  231. $field_name . '[und][0][url]' => $input['href'],
  232. );
  233. $this->drupalPost(NULL, $edit, t('Save'));
  234. $url = $this->getUrl();
  235. // change to anonymous user
  236. $this->drupalLogout();
  237. $this->drupalGet($url);
  238. $this->assertRaw('This &amp; That');
  239. }
  240. /**
  241. * If we're creating a new field and just hit 'save' on the default options, we want to make
  242. * sure they are set to the expected results.
  243. */
  244. function testCRUDCreateFieldDefaults() {
  245. $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
  246. $this->drupalLogin($this->web_user);
  247. // create field
  248. $name = strtolower($this->randomName());
  249. $edit = array(
  250. 'fields[_add_new_field][label]' => $name,
  251. 'fields[_add_new_field][field_name]' => $name,
  252. 'fields[_add_new_field][type]' => 'link_field',
  253. 'fields[_add_new_field][widget_type]' => 'link_field',
  254. );
  255. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  256. $this->drupalPost(NULL, array(), t('Save field settings'));
  257. $this->drupalPost(NULL, array(), t('Save settings'));
  258. // Is field created?
  259. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  260. node_types_rebuild();
  261. menu_rebuild();
  262. _field_info_collate_fields(TRUE);
  263. $instances = field_info_instances('node', 'page');
  264. $instance = $instances['field_' . $name];
  265. $this->assertFalse($instance['required'], 'Make sure field is not required.');
  266. $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
  267. $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
  268. $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
  269. $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
  270. $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
  271. $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
  272. $this->assertFalse($instance['settings']['attributes']['class'], 'By default, no class should be set.');
  273. $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
  274. }
  275. }