link.crud_browser.test 14 KB

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