link.token.test 14 KB

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