link.crud_browser.test 18 KB

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