link.crud_browser.test 18 KB

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