link.token.test 14 KB

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