link.crud_browser.test 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. );
  107. $test_case = array(
  108. 'href' => 'www.example.com/'. $this->randomName(),
  109. 'label' => $this->randomName(),
  110. 'msg' => 'Link found',
  111. 'type' => self::LINK_INPUT_TYPE_GOOD,
  112. );
  113. $test_case['expected_href'] = 'http://'. $test_case['href'];
  114. $input_test_cases[] = $test_case;
  115. foreach ($input_test_cases as $input) {
  116. $this->drupalLogin($this->web_user);
  117. $this->drupalGet('node/add/page');
  118. $edit = array(
  119. 'title' => $input['label'],
  120. $field_name . '[und][0][title]' => $input['label'],
  121. $field_name . '[und][0][url]' => $input['href'],
  122. );
  123. $this->drupalPost(NULL, $edit, t('Save'));
  124. if ($input['type'] == self::LINK_INPUT_TYPE_BAD_URL) {
  125. $this->assertRaw(t('The value provided for %field is not a valid URL.', array('%field' => $name)), 'Not a valid URL: ' . $input['href']);
  126. continue;
  127. }
  128. else {
  129. $this->assertRaw(t(' has been created.',
  130. array('@type' => 'Basic Page', '%title' => $edit['title'])),
  131. 'Page created: ' . $input['href']);
  132. }
  133. $url = $this->getUrl();
  134. // change to anonym user
  135. $this->drupalLogout();
  136. $this->drupalGet($url);
  137. //debug($this);
  138. // If simpletest starts using something to override the error system, this will flag
  139. // us and let us know it's broken.
  140. $this->assertFalse(libxml_use_internal_errors(TRUE));
  141. if (isset($input['expected_href'])) {
  142. $path = '//a[@href="'. $input['expected_href'] .'" and text()="'. $input['label'] .'"]';
  143. }
  144. else {
  145. $path = '//a[@href="'. $input['href'] .'" and text()="'. $input['label'] .'"]';
  146. }
  147. //$this->pass(htmlentities($path));
  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. $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
  160. $this->drupalLogin($this->web_user);
  161. // create field
  162. $name = strtolower($this->randomName());
  163. $field_name = 'field_'. $name;
  164. $edit = array(
  165. 'fields[_add_new_field][label]' => $name,
  166. 'fields[_add_new_field][field_name]' => $name,
  167. 'fields[_add_new_field][type]' => 'link_field',
  168. 'fields[_add_new_field][widget_type]' => 'link_field',
  169. );
  170. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  171. $this->drupalPost(NULL, array(), t('Save field settings'));
  172. $this->drupalPost(NULL, array(
  173. 'instance[settings][title]' => 'value',
  174. 'instance[settings][title_value]' => '<strong>'. $name .'</strong>'), t('Save settings'));
  175. // Is field created?
  176. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  177. // create page form
  178. $this->drupalGet('node/add/page');
  179. $this->assertField($field_name . '[und][0][url]', 'URL found');
  180. $input = array(
  181. 'href' => 'http://example.com/' . $this->randomName()
  182. );
  183. $edit = array(
  184. 'title' => $name,
  185. $field_name . '[und][0][url]' => $input['href'],
  186. );
  187. $this->drupalPost(NULL, $edit, t('Save'));
  188. $url = $this->getUrl();
  189. // change to anonymous user
  190. $this->drupalLogout();
  191. $this->drupalGet($url);
  192. $this->assertRaw(l('<strong>'. $name .'</strong>', $input['href'], array('html' => TRUE)));
  193. }
  194. /**
  195. * If we're creating a new field and just hit 'save' on the default options, we want to make
  196. * sure they are set to the expected results.
  197. */
  198. function testCRUDCreateFieldDefaults() {
  199. $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
  200. $this->drupalLogin($this->web_user);
  201. // create field
  202. $name = strtolower($this->randomName());
  203. $edit = array(
  204. 'fields[_add_new_field][label]' => $name,
  205. 'fields[_add_new_field][field_name]' => $name,
  206. 'fields[_add_new_field][type]' => 'link_field',
  207. 'fields[_add_new_field][widget_type]' => 'link_field',
  208. );
  209. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  210. $this->drupalPost(NULL, array(), t('Save field settings'));
  211. $this->drupalPost(NULL, array(), t('Save settings'));
  212. // Is field created?
  213. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  214. node_types_rebuild();
  215. menu_rebuild();
  216. //_content_type_info(TRUE);
  217. //$fields = content_fields();
  218. //$field = $fields['field_'. $name];
  219. //$field = field_info_field('field_'. $name);
  220. _field_info_collate_fields(TRUE);
  221. $instances = field_info_instances('node', 'page');
  222. //$this->debug($instances);
  223. //$this->assert('debug', '<pre>'. print_r($instances, TRUE) .'</pre>', 'Debug');
  224. $instance = $instances['field_'. $name];
  225. //$this->assertTrue(1 === $instance['validate_url'], 'Make sure validation is on.');
  226. $this->assertFalse($instance['required'], 'Make sure field is not required.');
  227. $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
  228. $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be off by default.');
  229. $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
  230. $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
  231. $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
  232. $this->assertFalse($instance['settings']['attributes']['class'], 'By default, no class should be set.');
  233. $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
  234. //$this->fail('<pre>'. print_r($fields['field_'. $name], TRUE) .'</pre>');
  235. }
  236. }