link.token.test 12 KB

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