link.token.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. /**
  3. * @file
  4. * Contains simpletests making sure token integration works.
  5. */
  6. /**
  7. * Testing that tokens can be used in link titles.
  8. */
  9. class LinkTokenTest extends LinkBaseTestClass {
  10. /**
  11. * Get Info.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Link tokens - browser test',
  16. 'description' => 'Tests including tokens in link titles, making sure they appear in node views.',
  17. 'group' => 'Link',
  18. 'dependencies' => array('token'),
  19. );
  20. }
  21. /**
  22. * Setup.
  23. */
  24. public function setUp($modules = array()) {
  25. parent::setUp(array('token'));
  26. }
  27. /**
  28. * Creates a link field with a required title enabled for user-entered tokens.
  29. *
  30. * Creates a node with a token in the link title and checks the value.
  31. */
  32. public function testUserTokenLinkCreate() {
  33. // Create field.
  34. $settings = array(
  35. 'instance[settings][enable_tokens]' => 1,
  36. );
  37. $field_name = $this->createLinkField('page',
  38. $settings);
  39. // Create page form.
  40. $this->drupalGet('node/add/page');
  41. // $field_name = 'field_' . $name;.
  42. $this->assertField($field_name . '[und][0][title]', 'Title found');
  43. $this->assertField($field_name . '[und][0][url]', 'URL found');
  44. $input = array(
  45. 'href' => 'http://example.com/' . $this->randomName(),
  46. 'label' => $this->randomName(),
  47. );
  48. // $this->drupalLogin($this->web_user);.
  49. $this->drupalGet('node/add/page');
  50. $edit = array(
  51. 'title' => $input['label'],
  52. $field_name . '[und][0][title]' => $input['label'] . " [node:content-type:machine-name]",
  53. $field_name . '[und][0][url]' => $input['href'],
  54. );
  55. $this->drupalPost(NULL, $edit, t('Save'));
  56. $url = $this->getUrl();
  57. // Change to anonymous user.
  58. $this->drupalLogout();
  59. $this->drupalGet($url);
  60. $this->assertRaw(l($input['label'] . ' page', $input['href']));
  61. }
  62. /**
  63. * Creates a link field with a static title and an admin-entered token.
  64. *
  65. * Creates a node with a link and checks the title value.
  66. */
  67. public function testStaticTokenLinkCreate() {
  68. // Create field.
  69. $name = $this->randomName();
  70. $settings = array(
  71. 'instance[settings][title]' => 'value',
  72. 'instance[settings][title_value]' => $name . ' [node:content-type:machine-name]',
  73. );
  74. $field_name = $this->createLinkField('page', $settings);
  75. // Create page form.
  76. $this->drupalGet('node/add/page');
  77. $this->assertField($field_name . '[und][0][url]', 'URL found');
  78. $input = array(
  79. 'href' => 'http://example.com/' . $this->randomName(),
  80. );
  81. // $this->drupalLogin($this->web_user);.
  82. $this->drupalGet('node/add/page');
  83. $edit = array(
  84. 'title' => $name,
  85. $field_name . '[und][0][url]' => $input['href'],
  86. );
  87. $this->drupalPost(NULL, $edit, t('Save'));
  88. $url = $this->getUrl();
  89. // Change to anonymous user.
  90. $this->drupalLogout();
  91. $this->drupalGet($url);
  92. $this->assertRaw(l($name . ' page', $input['href']));
  93. }
  94. /**
  95. * Creates a link field with a static title and an admin-entered token.
  96. *
  97. * Creates a node with a link and checks the title value.
  98. *
  99. * Basically, I want to make sure the [title-raw] token works, because it's a
  100. * token that changes from node to node, where [type]'s always going to be the
  101. * same.
  102. */
  103. public function testStaticTokenLinkCreate2() {
  104. // Create field.
  105. $name = $this->randomName();
  106. $settings = array(
  107. 'instance[settings][title]' => 'value',
  108. 'instance[settings][title_value]' => $name . ' [node:title]',
  109. );
  110. $field_name = $this->createLinkField('page', $settings);
  111. // Create page form.
  112. $this->drupalGet('node/add/page');
  113. $this->assertField($field_name . '[und][0][url]', 'URL found');
  114. $input = array(
  115. 'href' => 'http://example.com/' . $this->randomName(),
  116. );
  117. // $this->drupalLogin($this->web_user);.
  118. $this->drupalGet('node/add/page');
  119. $edit = array(
  120. 'title' => $name,
  121. $field_name . '[und][0][url]' => $input['href'],
  122. );
  123. $this->drupalPost(NULL, $edit, t('Save'));
  124. $url = $this->getUrl();
  125. // Change to anonymous user.
  126. $this->drupalLogout();
  127. $this->drupalGet($url);
  128. $this->assertRaw(l($name . ' ' . $name, $input['href']));
  129. }
  130. /**
  131. * This test doesn't seem to actually work, due to lack of 'title' in url.
  132. *
  133. * @codingStandardsIgnoreStart
  134. */
  135. public function _test_Link_With_Title_Attribute_token_url_form() {
  136. // @codingStandardsIgnoreEnd
  137. /* $this->loginWithPermissions($this->permissions);
  138. $this->acquireContentTypes(1);
  139. $field_settings = array(
  140. 'type' => 'link',
  141. 'widget_type' => 'link',
  142. 'type_name' => $this->content_types[0]->name,
  143. 'attributes' => array(
  144. 'class' => '',
  145. 'target' => 'default',
  146. 'rel' => 'nofollow',
  147. 'title' => '',
  148. ),
  149. );
  150. $field = $this->createField($field_settings, 0);
  151. //$this->fail('<pre>'. print_r($field, TRUE) .'</pre>');
  152. $field_name = $field['field_name'];
  153. $field_db_info = content_database_info($field);
  154. $url_type = str_replace('_', '-', $this->content_types[0]->type);
  155. $edit = array('attributes[title]' => '['. $field_name .'-url]',
  156. 'enable_tokens' => TRUE);
  157. // @codingStandardsIgnoreLine
  158. $this->drupalPost('admin/content/node-type/'. $url_type .'/fields/'. $field['field_name'],
  159. $edit, t('Save field settings'));
  160. $this->assertText(t('Saved field @field_name', array('@field_name' => $field['field_name'])));*/
  161. $name = $this->randomName();
  162. $settings = array(
  163. 'instance[settings][attributes][rel]' => 'nofollow',
  164. );
  165. $field_name = $this->createLinkField('page', $settings);
  166. // So, having saved this field_name, let's see if it works...
  167. // $this->acquireNodes(1);
  168. // $node = node_load($this->nodes[0]->nid);
  169. // $this->drupalGet('node/'. $this->nodes[0]->nid);.
  170. $edit = array();
  171. $test_link_url = 'http://www.example.com/test';
  172. $edit[$field_name . '[und][0][url]'] = $test_link_url;
  173. $title = 'title_' . $this->randomName(20);
  174. $edit[$field_name . '[und][0][title]'] = $title;
  175. $edit['title'] = $name;
  176. $this->drupalGet('node/add/page');
  177. $this->drupalPost(NULL, $edit, t('Save'));
  178. // Make sure we get a new version!
  179. // $node = node_load($this->nodes[0]->nid, NULL, TRUE);.
  180. $this->assertText(t('Basic page @title has been updated.',
  181. array('@title' => $name)));
  182. // $this->drupalGet('node/'. $node->nid);.
  183. $this->assertText($title, 'Make sure the link title/text shows');
  184. $this->assertRaw(' title="' . $test_link_url . '"', "Do we show the link url as the title attribute?");
  185. $this->assertNoRaw(' title="[' . $field_name . '-url]"');
  186. $this->assertTrue(module_exists('token'), t('Assure that Token Module is enabled.'));
  187. // $this->fail($this->content);.
  188. }
  189. /**
  190. * Link With Title Attribute token title form.
  191. *
  192. * If the title of the link is set to the title attribute, then the title
  193. * attribute isn't supposed to show.
  194. *
  195. * @codingStandardsIgnoreStart
  196. */
  197. public function _test_Link_With_Title_Attribute_token_title_form() {
  198. // @codingStandardsIgnoreEnd
  199. $this->loginWithPermissions($this->permissions);
  200. $this->acquireContentTypes(1);
  201. $field_settings = array(
  202. 'type' => 'link',
  203. 'widget_type' => 'link',
  204. 'type_name' => $this->content_types[0]->name,
  205. 'attributes' => array(
  206. 'class' => '',
  207. 'target' => 'default',
  208. 'rel' => 'nofollow',
  209. 'title' => '',
  210. ),
  211. );
  212. $field = $this->createField($field_settings, 0);
  213. $field_name = $field['field_name'];
  214. $url_type = str_replace('_', '-', $this->content_types[0]->type);
  215. $edit = array(
  216. 'attributes[title]' => '[' . $field_name . '-title]',
  217. 'enable_tokens' => TRUE,
  218. );
  219. $this->drupalPost('admin/content/node-type/' . $url_type . '/fields/' . $field['field_name'],
  220. $edit, t('Save field settings'));
  221. $this->assertText(t('Saved field @field_name', array('@field_name' => $field['field_name'])));
  222. // So, having saved this field_name, let's see if it works...
  223. $this->acquireNodes(1);
  224. $this->drupalGet('node/' . $this->nodes[0]->nid);
  225. $edit = array();
  226. $edit[$field['field_name'] . '[0][url]'] = 'http://www.example.com/test';
  227. $title = 'title_' . $this->randomName(20);
  228. $edit[$field['field_name'] . '[0][title]'] = $title;
  229. $this->drupalPost('node/' . $this->nodes[0]->nid . '/edit', $edit, t('Save'));
  230. // Make sure we get a new version!
  231. $node = node_load($this->nodes[0]->nid, NULL, TRUE);
  232. $this->assertText(t('@type @title has been updated.',
  233. array(
  234. '@title' => $node->title,
  235. '@type' => $this->content_types[0]->name,
  236. )));
  237. $this->drupalGet('node/' . $node->nid);
  238. $this->assertText($title, 'Make sure the link title/text shows');
  239. $this->assertNoRaw(' title="' . $title . '"', "We should not show the link title as the title attribute?");
  240. $this->assertNoRaw(' title="[' . $field_name . '-title]"');
  241. // $this->fail($this->content);.
  242. }
  243. /**
  244. * Trying to set the url to contain a token.
  245. *
  246. * @codingStandardsIgnoreStart
  247. */
  248. public function _testUserTokenLinkCreateInURL() {
  249. //@codingStandardsIgnoreEnd
  250. $this->web_user = $this->drupalCreateUser(array(
  251. 'administer content types',
  252. 'administer fields',
  253. 'access content',
  254. 'create page content',
  255. ));
  256. $this->drupalLogin($this->web_user);
  257. // Create field.
  258. $name = strtolower($this->randomName());
  259. $edit = array(
  260. '_add_new_field[label]' => $name,
  261. '_add_new_field[field_name]' => $name,
  262. '_add_new_field[type]' => 'link',
  263. '_add_new_field[widget_type]' => 'link',
  264. );
  265. $this->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
  266. $this->drupalPost(NULL, array(
  267. 'title' => 'required',
  268. 'enable_tokens' => 1,
  269. ), t('Save field settings'));
  270. // Is field created?
  271. $this->assertRaw(t('Added field %label.', array('%label' => $name)), 'Field added');
  272. // Create page form.
  273. $this->drupalGet('node/add/page');
  274. $field_name = 'field_' . $name;
  275. $this->assertField($field_name . '[0][title]', 'Title found');
  276. $this->assertField($field_name . '[0][url]', 'URL found');
  277. $input = array(
  278. 'href' => 'http://example.com/' . $this->randomName(),
  279. 'label' => $this->randomName(),
  280. );
  281. $this->drupalLogin($this->web_user);
  282. $this->drupalGet('node/add/page');
  283. $edit = array(
  284. 'title' => $input['label'],
  285. $field_name . '[0][title]' => $input['label'],
  286. $field_name . '[0][url]' => $input['href'] . "/[type]",
  287. );
  288. $this->drupalPost(NULL, $edit, t('Save'));
  289. $url = $this->getUrl();
  290. // Change to anonymous user.
  291. $this->drupalLogout();
  292. $this->drupalGet($url);
  293. $this->assertRaw(l($input['label'], $input['href'] . '/page'));
  294. // $this->fail($this->content);.
  295. }
  296. /**
  297. * Trying to set the url to contain a token.
  298. *
  299. * @codingStandardsIgnoreStart
  300. */
  301. public function _testUserTokenLinkCreateInURL2() {
  302. // @codingStandardsIgnoreEnd
  303. $this->web_user = $this->drupalCreateUser(array(
  304. 'administer content types',
  305. 'administer fields',
  306. 'access content',
  307. 'create page content',
  308. ));
  309. $this->drupalLogin($this->web_user);
  310. // Create field.
  311. $name = strtolower($this->randomName());
  312. $edit = array(
  313. '_add_new_field[label]' => $name,
  314. '_add_new_field[field_name]' => $name,
  315. '_add_new_field[type]' => 'link',
  316. '_add_new_field[widget_type]' => 'link',
  317. );
  318. $this->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
  319. $this->drupalPost(NULL, array(
  320. 'title' => 'required',
  321. 'enable_tokens' => 1,
  322. ), t('Save field settings'));
  323. // Is field created?
  324. $this->assertRaw(t('Added field %label.', array('%label' => $name)), 'Field added');
  325. // Create page form.
  326. $this->drupalGet('node/add/page');
  327. $field_name = 'field_' . $name;
  328. $this->assertField($field_name . '[0][title]', 'Title found');
  329. $this->assertField($field_name . '[0][url]', 'URL found');
  330. $input = array(
  331. 'href' => 'http://example.com/' . $this->randomName(),
  332. 'label' => $this->randomName(),
  333. );
  334. $this->drupalLogin($this->web_user);
  335. $this->drupalGet('node/add/page');
  336. $edit = array(
  337. 'title' => $input['label'],
  338. $field_name . '[0][title]' => $input['label'],
  339. $field_name . '[0][url]' => $input['href'] . "/[author-uid]",
  340. );
  341. $this->drupalPost(NULL, $edit, t('Save'));
  342. $url = $this->getUrl();
  343. // Change to anonymous user.
  344. $this->drupalLogout();
  345. $this->drupalGet($url);
  346. $this->assertRaw(l($input['label'], $input['href'] . '/' . $this->web_user->uid));
  347. }
  348. /**
  349. * CRUD Title Only Title No Link.
  350. *
  351. * Test that if you have a title and no url on a field which does not have
  352. * tokens enabled, that the title is sanitized once.
  353. *
  354. * @codingStandardsIgnoreStart
  355. */
  356. public function testCRUDTitleOnlyTitleNoLink2() {
  357. //@codingStandardsIgnoreEnd
  358. $this->web_user = $this->drupalCreateUser(array(
  359. 'administer content types',
  360. 'administer fields',
  361. 'access content',
  362. 'create page content',
  363. ));
  364. $this->drupalLogin($this->web_user);
  365. // Create field.
  366. $name = strtolower($this->randomName());
  367. $field_name = 'field_' . $name;
  368. $edit = array(
  369. 'fields[_add_new_field][label]' => $name,
  370. 'fields[_add_new_field][field_name]' => $name,
  371. 'fields[_add_new_field][type]' => 'link_field',
  372. 'fields[_add_new_field][widget_type]' => 'link_field',
  373. );
  374. $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
  375. $this->drupalPost(NULL, array(), t('Save field settings'));
  376. $this->drupalPost(NULL, array(
  377. 'instance[settings][url]' => 1,
  378. 'instance[settings][enable_tokens]' => 0,
  379. ), t('Save settings'));
  380. // Is field created?
  381. $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
  382. // Create page form.
  383. $this->drupalGet('node/add/page');
  384. $this->assertField($field_name . '[und][0][url]', 'URL found');
  385. $input = array(
  386. 'title' => 'This & That',
  387. 'href' => '',
  388. );
  389. $edit = array(
  390. 'title' => $name,
  391. $field_name . '[und][0][title]' => $input['title'],
  392. $field_name . '[und][0][url]' => $input['href'],
  393. );
  394. $this->drupalPost(NULL, $edit, t('Save'));
  395. $url = $this->getUrl();
  396. // Change to anonymous user.
  397. $this->drupalLogout();
  398. $this->drupalGet($url);
  399. $this->assertRaw('This &amp; That');
  400. }
  401. }