token.test 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. <?php
  2. /**
  3. * @file
  4. * Test integration for the token module.
  5. */
  6. /**
  7. * Helper test class with some added functions for testing.
  8. */
  9. class TokenTestHelper extends DrupalWebTestCase {
  10. protected $profile = 'testing';
  11. public function setUp($modules = array()) {
  12. $modules[] = 'path';
  13. $modules[] = 'token';
  14. $modules[] = 'token_test';
  15. parent::setUp($modules);
  16. variable_set('clean_url', 1);
  17. }
  18. function assertToken($type, array $data, $token, $expected, array $options = array()) {
  19. return $this->assertTokens($type, $data, array($token => $expected), $options);
  20. }
  21. function assertTokens($type, array $data, array $tokens, array $options = array()) {
  22. $input = $this->mapTokenNames($type, array_keys($tokens));
  23. $replacements = token_generate($type, $input, $data, $options);
  24. foreach ($tokens as $name => $expected) {
  25. $token = $input[$name];
  26. if (!isset($expected)) {
  27. $this->assertTrue(!isset($values[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
  28. }
  29. elseif (!isset($replacements[$token])) {
  30. $this->fail(t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
  31. }
  32. elseif (!empty($options['regex'])) {
  33. $this->assertTrue(preg_match('/^' . $expected . '$/', $replacements[$token]), t("Token value for @token was '@actual', matching regular expression pattern '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected)));
  34. }
  35. else {
  36. $this->assertIdentical($replacements[$token], $expected, t("Token value for @token was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected)));
  37. }
  38. }
  39. return $replacements;
  40. }
  41. function mapTokenNames($type, array $tokens = array()) {
  42. $return = array();
  43. foreach ($tokens as $token) {
  44. $return[$token] = "[$type:$token]";
  45. }
  46. return $return;
  47. }
  48. function assertNoTokens($type, array $data, array $tokens, array $options = array()) {
  49. $input = $this->mapTokenNames($type, $tokens);
  50. $replacements = token_generate($type, $input, $data, $options);
  51. foreach ($tokens as $name) {
  52. $token = $input[$name];
  53. $this->assertTrue(!isset($replacements[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
  54. }
  55. return $values;
  56. }
  57. function saveAlias($source, $alias, $language = LANGUAGE_NONE) {
  58. $alias = array(
  59. 'source' => $source,
  60. 'alias' => $alias,
  61. 'language' => $language,
  62. );
  63. path_save($alias);
  64. return $alias;
  65. }
  66. function saveEntityAlias($entity_type, $entity, $alias, $language = LANGUAGE_NONE) {
  67. $uri = entity_uri($entity_type, $entity);
  68. return $this->saveAlias($uri['path'], $alias, $language);
  69. }
  70. /**
  71. * Make a page request and test for token generation.
  72. */
  73. function assertPageTokens($url, array $tokens, array $data = array(), array $options = array()) {
  74. if (empty($tokens)) {
  75. return TRUE;
  76. }
  77. $token_page_tokens = array(
  78. 'tokens' => $tokens,
  79. 'data' => $data,
  80. 'options' => $options,
  81. );
  82. variable_set('token_page_tokens', $token_page_tokens);
  83. $options += array('url_options' => array());
  84. $this->drupalGet($url, $options['url_options']);
  85. $this->refreshVariables();
  86. $result = variable_get('token_page_tokens', array());
  87. if (!isset($result['values']) || !is_array($result['values'])) {
  88. return $this->fail('Failed to generate tokens.');
  89. }
  90. foreach ($tokens as $token => $expected) {
  91. if (!isset($expected)) {
  92. $this->assertTrue(!isset($result['values'][$token]) || $result['values'][$token] === $token, t("Token value for @token was not generated.", array('@token' => $token)));
  93. }
  94. elseif (!isset($result['values'][$token])) {
  95. $this->fail(t('Failed to generate token @token.', array('@token' => $token)));
  96. }
  97. else {
  98. $this->assertIdentical($result['values'][$token], (string) $expected, t("Token value for @token was '@actual', expected value '@expected'.", array('@token' => $token, '@actual' => $result['values'][$token], '@expected' => $expected)));
  99. }
  100. }
  101. }
  102. }
  103. class TokenUnitTestCase extends TokenTestHelper {
  104. public static function getInfo() {
  105. return array(
  106. 'name' => 'Token unit tests',
  107. 'description' => 'Test basic, low-level token functions.',
  108. 'group' => 'Token',
  109. );
  110. }
  111. /**
  112. * Test token_get_invalid_tokens() and token_get_invalid_tokens_by_context().
  113. */
  114. public function testGetInvalidTokens() {
  115. $tests = array();
  116. $tests[] = array(
  117. 'valid tokens' => array(
  118. '[node:title]',
  119. '[node:created:short]',
  120. '[node:created:custom:invalid]',
  121. '[node:created:custom:mm-YYYY]',
  122. '[node:colons:in:name]',
  123. '[site:name]',
  124. '[site:slogan]',
  125. '[current-date:short]',
  126. '[current-user:uid]',
  127. '[current-user:ip-address]',
  128. ),
  129. 'invalid tokens' => array(
  130. '[node:title:invalid]',
  131. '[node:created:invalid]',
  132. '[node:created:short:invalid]',
  133. '[node:colons:in:name:invalid]',
  134. '[invalid:title]',
  135. '[site:invalid]',
  136. '[user:ip-address]',
  137. '[user:uid]',
  138. '[comment:cid]',
  139. // Deprecated tokens
  140. '[node:tnid]',
  141. '[node:type]',
  142. '[node:type-name]',
  143. '[date:short]',
  144. ),
  145. 'types' => array('node'),
  146. );
  147. $tests[] = array(
  148. 'valid tokens' => array(
  149. '[node:title]',
  150. '[node:created:short]',
  151. '[node:created:custom:invalid]',
  152. '[node:created:custom:mm-YYYY]',
  153. '[node:colons:in:name]',
  154. '[site:name]',
  155. '[site:slogan]',
  156. '[user:uid]',
  157. '[current-date:short]',
  158. '[current-user:uid]',
  159. ),
  160. 'invalid tokens' => array(
  161. '[node:title:invalid]',
  162. '[node:created:invalid]',
  163. '[node:created:short:invalid]',
  164. '[node:colons:in:name:invalid]',
  165. '[invalid:title]',
  166. '[site:invalid]',
  167. '[user:ip-address]',
  168. '[comment:cid]',
  169. // Deprecated tokens
  170. '[node:tnid]',
  171. '[node:type]',
  172. '[node:type-name]',
  173. ),
  174. 'types' => array('all'),
  175. );
  176. foreach ($tests as $test) {
  177. $tokens = array_merge($test['valid tokens'], $test['invalid tokens']);
  178. shuffle($tokens);
  179. $invalid_tokens = token_get_invalid_tokens_by_context(implode(' ', $tokens), $test['types']);
  180. sort($invalid_tokens);
  181. sort($test['invalid tokens']);
  182. $this->assertEqual($invalid_tokens, $test['invalid tokens'], 'Invalid tokens detected properly: ' . implode(', ', $invalid_tokens));
  183. }
  184. }
  185. }
  186. class TokenURLTestCase extends TokenTestHelper {
  187. public static function getInfo() {
  188. return array(
  189. 'name' => 'URL token tests',
  190. 'description' => 'Test the URL tokens.',
  191. 'group' => 'Token',
  192. );
  193. }
  194. public function setUp($modules = array()) {
  195. parent::setUp($modules);
  196. $this->saveAlias('node/1', 'first-node');
  197. }
  198. function testURLTokens() {
  199. $tokens = array(
  200. 'absolute' => 'http://example.com/first-node',
  201. 'relative' => base_path() . 'first-node',
  202. 'path' => 'first-node',
  203. 'brief' => 'example.com/first-node',
  204. 'args:value:0' => 'first-node',
  205. 'args:value:1' => NULL,
  206. 'args:value:N' => NULL,
  207. 'unaliased' => 'http://example.com/node/1',
  208. 'unaliased:relative' => base_path() . 'node/1',
  209. 'unaliased:path' => 'node/1',
  210. 'unaliased:brief' => 'example.com/node/1',
  211. 'unaliased:args:value:0' => 'node',
  212. 'unaliased:args:value:1' => '1',
  213. 'unaliased:args:value:2' => NULL,
  214. // Deprecated tokens.
  215. 'alias' => 'first-node',
  216. );
  217. $this->assertTokens('url', array('path' => 'node/1', 'options' => array('base_url' => 'http://example.com')), $tokens);
  218. }
  219. }
  220. class TokenCommentTestCase extends TokenTestHelper {
  221. public static function getInfo() {
  222. return array(
  223. 'name' => 'Comment token tests',
  224. 'description' => 'Test the comment tokens.',
  225. 'group' => 'Token',
  226. );
  227. }
  228. public function setUp($modules = array()) {
  229. $modules[] = 'comment';
  230. parent::setUp($modules);
  231. }
  232. function testCommentTokens() {
  233. $node = $this->drupalCreateNode(array('comment' => COMMENT_NODE_OPEN));
  234. $parent_comment = new stdClass;
  235. $parent_comment->nid = $node->nid;
  236. $parent_comment->pid = 0;
  237. $parent_comment->cid = NULL;
  238. $parent_comment->uid = 0;
  239. $parent_comment->name = 'anonymous user';
  240. $parent_comment->mail = 'anonymous@example.com';
  241. $parent_comment->subject = $this->randomName();
  242. $parent_comment->timestamp = mt_rand($node->created, REQUEST_TIME);
  243. $parent_comment->language = LANGUAGE_NONE;
  244. $parent_comment->body[LANGUAGE_NONE][0] = $this->randomName();
  245. comment_save($parent_comment);
  246. $tokens = array(
  247. 'url' => url('comment/' . $parent_comment->cid, array('fragment' => 'comment-' . $parent_comment->cid, 'absolute' => TRUE)),
  248. 'url:absolute' => url('comment/' . $parent_comment->cid, array('fragment' => 'comment-' . $parent_comment->cid, 'absolute' => TRUE)),
  249. 'url:relative' => url('comment/' . $parent_comment->cid, array('fragment' => 'comment-' . $parent_comment->cid, 'absolute' => FALSE)),
  250. 'url:path' => 'comment/' . $parent_comment->cid,
  251. 'parent:url:absolute' => NULL,
  252. );
  253. $this->assertTokens('comment', array('comment' => $parent_comment), $tokens);
  254. $comment = new stdClass();
  255. $comment->nid = $node->nid;
  256. $comment->pid = $parent_comment->cid;
  257. $comment->cid = NULL;
  258. $comment->uid = 1;
  259. $comment->subject = $this->randomName();
  260. $comment->timestamp = mt_rand($parent_comment->created, REQUEST_TIME);
  261. $comment->language = LANGUAGE_NONE;
  262. $comment->body[LANGUAGE_NONE][0] = $this->randomName();
  263. comment_save($comment);
  264. $tokens = array(
  265. 'url' => url('comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid, 'absolute' => TRUE)),
  266. 'url:absolute' => url('comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid, 'absolute' => TRUE)),
  267. 'url:relative' => url('comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid, 'absolute' => FALSE)),
  268. 'url:path' => 'comment/' . $comment->cid,
  269. 'parent:url:absolute' => url('comment/' . $parent_comment->cid, array('fragment' => 'comment-' . $parent_comment->cid, 'absolute' => TRUE)),
  270. );
  271. $this->assertTokens('comment', array('comment' => $comment), $tokens);
  272. }
  273. }
  274. class TokenNodeTestCase extends TokenTestHelper {
  275. protected $profile = 'standard';
  276. public static function getInfo() {
  277. return array(
  278. 'name' => 'Node and content type token tests',
  279. 'description' => 'Test the node and content type tokens.',
  280. 'group' => 'Token',
  281. );
  282. }
  283. function testNodeTokens() {
  284. $source_node = $this->drupalCreateNode(array('log' => $this->randomName(), 'path' => array('alias' => 'content/source-node')));
  285. $tokens = array(
  286. 'source' => NULL,
  287. 'source:nid' => NULL,
  288. 'log' => $source_node->log,
  289. 'url:path' => 'content/source-node',
  290. 'url:absolute' => url("node/{$source_node->nid}", array('absolute' => TRUE)),
  291. 'url:relative' => url("node/{$source_node->nid}", array('absolute' => FALSE)),
  292. 'url:unaliased:path' => "node/{$source_node->nid}",
  293. 'content-type' => 'Basic page',
  294. 'content-type:name' => 'Basic page',
  295. 'content-type:machine-name' => 'page',
  296. 'content-type:description' => "Use <em>basic pages</em> for your static content, such as an 'About us' page.",
  297. 'content-type:node-count' => 1,
  298. 'content-type:edit-url' => url('admin/structure/types/manage/page', array('absolute' => TRUE)),
  299. // Deprecated tokens.
  300. 'tnid' => 0,
  301. 'type' => 'page',
  302. 'type-name' => 'Basic page',
  303. 'url:alias' => 'content/source-node',
  304. );
  305. $this->assertTokens('node', array('node' => $source_node), $tokens);
  306. $translated_node = $this->drupalCreateNode(array('tnid' => $source_node->nid, 'type' => 'article'));
  307. $tokens = array(
  308. 'source' => $source_node->title,
  309. 'source:nid' => $source_node->nid,
  310. 'log' => '',
  311. 'url:path' => "node/{$translated_node->nid}",
  312. 'url:absolute' => url("node/{$translated_node->nid}", array('absolute' => TRUE)),
  313. 'url:relative' => url("node/{$translated_node->nid}", array('absolute' => FALSE)),
  314. 'url:unaliased:path' => "node/{$translated_node->nid}",
  315. 'content-type' => 'Article',
  316. 'content-type:name' => 'Article',
  317. 'content-type:machine-name' => 'article',
  318. 'content-type:description' => "Use <em>articles</em> for time-sensitive content like news, press releases or blog posts.",
  319. 'content-type:node-count' => 1,
  320. 'content-type:edit-url' => url('admin/structure/types/manage/article', array('absolute' => TRUE)),
  321. // Deprecated tokens.
  322. 'type' => 'article',
  323. 'type-name' => 'Article',
  324. 'tnid' => $source_node->nid,
  325. 'url:alias' => "node/{$translated_node->nid}",
  326. );
  327. $this->assertTokens('node', array('node' => $translated_node), $tokens);
  328. }
  329. }
  330. class TokenMenuTestCase extends TokenTestHelper {
  331. public static function getInfo() {
  332. return array(
  333. 'name' => 'Menu link and menu token tests',
  334. 'description' => 'Test the menu tokens.',
  335. 'group' => 'Token',
  336. );
  337. }
  338. public function setUp($modules = array()) {
  339. $modules[] = 'menu';
  340. parent::setUp($modules);
  341. }
  342. function testMenuTokens() {
  343. // Add a root link.
  344. $root_link = array(
  345. 'link_path' => 'root',
  346. 'link_title' => 'Root link',
  347. 'menu_name' => 'main-menu',
  348. );
  349. menu_link_save($root_link);
  350. // Add another link with the root link as the parent
  351. $parent_link = array(
  352. 'link_path' => 'root/parent',
  353. 'link_title' => 'Parent link',
  354. 'menu_name' => 'main-menu',
  355. 'plid' => $root_link['mlid'],
  356. );
  357. menu_link_save($parent_link);
  358. // Test menu link tokens.
  359. $tokens = array(
  360. 'mlid' => $parent_link['mlid'],
  361. 'title' => 'Parent link',
  362. 'menu' => 'Main menu',
  363. 'menu:name' => 'Main menu',
  364. 'menu:machine-name' => 'main-menu',
  365. 'menu:description' => 'The <em>Main</em> menu is used on many sites to show the major sections of the site, often in a top navigation bar.',
  366. 'menu:menu-link-count' => 2,
  367. 'menu:edit-url' => url("admin/structure/menu/manage/main-menu", array('absolute' => TRUE)),
  368. 'url' => url('root/parent', array('absolute' => TRUE)),
  369. 'url:absolute' => url('root/parent', array('absolute' => TRUE)),
  370. 'url:relative' => url('root/parent', array('absolute' => FALSE)),
  371. 'url:path' => 'root/parent',
  372. 'url:alias' => 'root/parent',
  373. 'edit-url' => url("admin/structure/menu/item/{$parent_link['mlid']}/edit", array('absolute' => TRUE)),
  374. 'parent' => 'Root link',
  375. 'parent:mlid' => $root_link['mlid'],
  376. 'parent:title' => 'Root link',
  377. 'parent:menu' => 'Main menu',
  378. 'parent:parent' => NULL,
  379. 'parents' => 'Root link',
  380. 'parents:count' => 1,
  381. 'parents:keys' => $root_link['mlid'],
  382. 'root' => 'Root link',
  383. 'root:mlid' => $root_link['mlid'],
  384. 'root:parent' => NULL,
  385. 'root:root' => NULL,
  386. );
  387. $this->assertTokens('menu-link', array('menu-link' => $parent_link), $tokens);
  388. // Add a node menu link
  389. $node_link = array(
  390. 'enabled' => TRUE,
  391. 'link_title' => 'Node link',
  392. 'plid' => $parent_link['mlid'],
  393. 'customized' => 0,
  394. 'description' => '',
  395. );
  396. $node = $this->drupalCreateNode(array('menu' => $node_link));
  397. // Test [node:menu] tokens.
  398. $tokens = array(
  399. 'menu-link' => 'Node link',
  400. 'menu-link:mlid' => $node->menu['mlid'],
  401. 'menu-link:title' => 'Node link',
  402. 'menu-link:menu' => 'Main menu',
  403. 'menu-link:url' => url('node/' . $node->nid, array('absolute' => TRUE)),
  404. 'menu-link:url:path' => 'node/' . $node->nid,
  405. 'menu-link:edit-url' => url("admin/structure/menu/item/{$node->menu['mlid']}/edit", array('absolute' => TRUE)),
  406. 'menu-link:parent' => 'Parent link',
  407. 'menu-link:parent:mlid' => $node->menu['plid'],
  408. 'menu-link:parent:mlid' => $parent_link['mlid'],
  409. 'menu-link:parents' => 'Root link, Parent link',
  410. 'menu-link:parents:count' => 2,
  411. 'menu-link:parents:keys' => $root_link['mlid'] . ', ' . $parent_link['mlid'],
  412. 'menu-link:root' => 'Root link',
  413. 'menu-link:root:mlid' => $root_link['mlid'],
  414. );
  415. $this->assertTokens('node', array('node' => $node), $tokens);
  416. // Reload the node which will not have $node->menu defined and re-test.
  417. $loaded_node = node_load($node->nid);
  418. $this->assertTokens('node', array('node' => $loaded_node), $tokens);
  419. // Regression test for http://drupal.org/node/1317926 to ensure the
  420. // original node object is not changed when calling menu_node_prepare().
  421. $this->assertTrue(!isset($loaded_node->menu), t('The $node->menu property was not modified during token replacement.'), 'Regression');
  422. }
  423. }
  424. class TokenTaxonomyTestCase extends TokenTestHelper {
  425. protected $vocab;
  426. public static function getInfo() {
  427. return array(
  428. 'name' => 'Taxonomy and vocabulary token tests',
  429. 'description' => 'Test the taxonomy tokens.',
  430. 'group' => 'Token',
  431. );
  432. }
  433. public function setUp($modules = array()) {
  434. $modules[] = 'taxonomy';
  435. parent::setUp($modules);
  436. // Create the default tags vocabulary.
  437. $vocabulary = (object) array(
  438. 'name' => 'Tags',
  439. 'machine_name' => 'tags',
  440. );
  441. taxonomy_vocabulary_save($vocabulary);
  442. $this->vocab = $vocabulary;
  443. }
  444. /**
  445. * Test the additional taxonomy term tokens.
  446. */
  447. function testTaxonomyTokens() {
  448. $root_term = $this->addTerm($this->vocab, array('name' => 'Root term', 'path' => array('alias' => 'root-term')));
  449. $tokens = array(
  450. 'url' => url("taxonomy/term/{$root_term->tid}", array('absolute' => TRUE)),
  451. 'url:absolute' => url("taxonomy/term/{$root_term->tid}", array('absolute' => TRUE)),
  452. 'url:relative' => url("taxonomy/term/{$root_term->tid}", array('absolute' => FALSE)),
  453. 'url:path' => 'root-term',
  454. 'url:unaliased:path' => "taxonomy/term/{$root_term->tid}",
  455. 'edit-url' => url("taxonomy/term/{$root_term->tid}/edit", array('absolute' => TRUE)),
  456. 'parents' => NULL,
  457. 'parents:count' => NULL,
  458. 'parents:keys' => NULL,
  459. 'root' => NULL,
  460. // Deprecated tokens
  461. 'url:alias' => 'root-term',
  462. );
  463. $this->assertTokens('term', array('term' => $root_term), $tokens);
  464. $parent_term = $this->addTerm($this->vocab, array('name' => 'Parent term', 'parent' => $root_term->tid));
  465. $tokens = array(
  466. 'url' => url("taxonomy/term/{$parent_term->tid}", array('absolute' => TRUE)),
  467. 'url:absolute' => url("taxonomy/term/{$parent_term->tid}", array('absolute' => TRUE)),
  468. 'url:relative' => url("taxonomy/term/{$parent_term->tid}", array('absolute' => FALSE)),
  469. 'url:path' => "taxonomy/term/{$parent_term->tid}",
  470. 'url:unaliased:path' => "taxonomy/term/{$parent_term->tid}",
  471. 'edit-url' => url("taxonomy/term/{$parent_term->tid}/edit", array('absolute' => TRUE)),
  472. 'parents' => 'Root term',
  473. 'parents:count' => 1,
  474. 'parents:keys' => $root_term->tid,
  475. 'root' => check_plain($root_term->name),
  476. 'root:tid' => $root_term->tid,
  477. // Deprecated tokens
  478. 'url:alias' => "taxonomy/term/{$parent_term->tid}",
  479. );
  480. $this->assertTokens('term', array('term' => $parent_term), $tokens);
  481. $term = $this->addTerm($this->vocab, array('name' => 'Test term', 'parent' => $parent_term->tid));
  482. $tokens = array(
  483. 'parents' => 'Root term, Parent term',
  484. 'parents:count' => 2,
  485. 'parents:keys' => implode(', ', array($root_term->tid, $parent_term->tid)),
  486. );
  487. $this->assertTokens('term', array('term' => $term), $tokens);
  488. }
  489. /**
  490. * Test the additional vocabulary tokens.
  491. */
  492. function testVocabularyTokens() {
  493. $vocabulary = $this->vocab;
  494. $tokens = array(
  495. 'machine-name' => 'tags',
  496. 'edit-url' => url("admin/structure/taxonomy/{$vocabulary->machine_name}/edit", array('absolute' => TRUE)),
  497. );
  498. $this->assertTokens('vocabulary', array('vocabulary' => $vocabulary), $tokens);
  499. }
  500. function addVocabulary(array $vocabulary = array()) {
  501. $vocabulary += array(
  502. 'name' => drupal_strtolower($this->randomName(5)),
  503. 'nodes' => array('article' => 'article'),
  504. );
  505. $vocabulary = (object) $vocabulary;
  506. taxonomy_vocabulary_save($vocabulary);
  507. return $vocabulary;
  508. }
  509. function addTerm(stdClass $vocabulary, array $term = array()) {
  510. $term += array(
  511. 'name' => drupal_strtolower($this->randomName(5)),
  512. 'vid' => $vocabulary->vid,
  513. );
  514. $term = (object) $term;
  515. taxonomy_term_save($term);
  516. return $term;
  517. }
  518. }
  519. class TokenUserTestCase extends TokenTestHelper {
  520. protected $account = NULL;
  521. public static function getInfo() {
  522. return array(
  523. 'name' => 'User token tests',
  524. 'description' => 'Test the user tokens.',
  525. 'group' => 'Token',
  526. );
  527. }
  528. public function setUp($modules = array()) {
  529. parent::setUp($modules);
  530. // Enable user pictures.
  531. variable_set('user_pictures', 1);
  532. variable_set('user_picture_file_size', '');
  533. // Set up the pictures directory.
  534. $picture_path = file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures');
  535. if (!file_prepare_directory($picture_path, FILE_CREATE_DIRECTORY)) {
  536. $this->fail('Could not create directory ' . $picture_path . '.');
  537. }
  538. $this->account = $this->drupalCreateUser(array('administer users'));
  539. $this->drupalLogin($this->account);
  540. }
  541. function testUserTokens() {
  542. // Add a user picture to the account.
  543. $image = current($this->drupalGetTestFiles('image'));
  544. $edit = array('files[picture_upload]' => drupal_realpath($image->uri));
  545. $this->drupalPost('user/' . $this->account->uid . '/edit', $edit, t('Save'));
  546. // Load actual user data from database.
  547. $this->account = user_load($this->account->uid, TRUE);
  548. $this->assertTrue(!empty($this->account->picture->fid), 'User picture uploaded.');
  549. $user_tokens = array(
  550. 'picture' => theme('user_picture', array('account' => $this->account)),
  551. 'picture:fid' => $this->account->picture->fid,
  552. 'picture:size-raw' => 125,
  553. 'ip-address' => NULL,
  554. 'roles' => implode(', ', $this->account->roles),
  555. 'roles:keys' => implode(', ', array_keys($this->account->roles)),
  556. );
  557. $this->assertTokens('user', array('user' => $this->account), $user_tokens);
  558. $edit = array('user_pictures' => FALSE);
  559. $this->drupalPost('admin/config/people/accounts', $edit, 'Save configuration');
  560. $this->assertText('The configuration options have been saved.');
  561. // Remove the simpletest-created user role.
  562. user_role_delete(end($this->account->roles));
  563. $this->account = user_load($this->account->uid, TRUE);
  564. $user_tokens = array(
  565. 'picture' => NULL,
  566. 'picture:fid' => NULL,
  567. 'ip-address' => NULL,
  568. 'roles' => 'authenticated user',
  569. 'roles:keys' => (string) DRUPAL_AUTHENTICATED_RID,
  570. );
  571. $this->assertTokens('user', array('user' => $this->account), $user_tokens);
  572. // The ip address token should work for the current user token type.
  573. $tokens = array(
  574. 'ip-address' => ip_address(),
  575. );
  576. $this->assertTokens('current-user', array(), $tokens);
  577. $anonymous = drupal_anonymous_user();
  578. // Mess with the role array to ensure we still get consistent output.
  579. $anonymous->roles[DRUPAL_ANONYMOUS_RID] = DRUPAL_ANONYMOUS_RID;
  580. $tokens = array(
  581. 'roles' => 'anonymous user',
  582. 'roles:keys' => (string) DRUPAL_ANONYMOUS_RID,
  583. );
  584. $this->assertTokens('user', array('user' => $anonymous), $tokens);
  585. }
  586. }
  587. class TokenEntityTestCase extends TokenTestHelper {
  588. public static function getInfo() {
  589. return array(
  590. 'name' => 'Entity token tests',
  591. 'description' => 'Test the entity tokens.',
  592. 'group' => 'Token',
  593. );
  594. }
  595. public function setUp($modules = array()) {
  596. $modules[] = 'taxonomy';
  597. parent::setUp($modules);
  598. // Create the default tags vocabulary.
  599. $vocabulary = (object) array(
  600. 'name' => 'Tags',
  601. 'machine_name' => 'tags',
  602. );
  603. taxonomy_vocabulary_save($vocabulary);
  604. $this->vocab = $vocabulary;
  605. }
  606. function testEntityMapping() {
  607. $this->assertIdentical(token_get_entity_mapping('token', 'node'), 'node');
  608. $this->assertIdentical(token_get_entity_mapping('token', 'term'), 'taxonomy_term');
  609. $this->assertIdentical(token_get_entity_mapping('token', 'vocabulary'), 'taxonomy_vocabulary');
  610. $this->assertIdentical(token_get_entity_mapping('token', 'invalid'), FALSE);
  611. $this->assertIdentical(token_get_entity_mapping('token', 'invalid', TRUE), 'invalid');
  612. $this->assertIdentical(token_get_entity_mapping('entity', 'node'), 'node');
  613. $this->assertIdentical(token_get_entity_mapping('entity', 'taxonomy_term'), 'term');
  614. $this->assertIdentical(token_get_entity_mapping('entity', 'taxonomy_vocabulary'), 'vocabulary');
  615. $this->assertIdentical(token_get_entity_mapping('entity', 'invalid'), FALSE);
  616. $this->assertIdentical(token_get_entity_mapping('entity', 'invalid', TRUE), 'invalid');
  617. // Test that when we send the mis-matched entity type into token_replace()
  618. // that we still get the tokens replaced.
  619. $vocabulary = taxonomy_vocabulary_machine_name_load('tags');
  620. $term = $this->addTerm($vocabulary);
  621. $this->assertIdentical(token_replace('[vocabulary:name]', array('taxonomy_vocabulary' => $vocabulary)), $vocabulary->name);
  622. $this->assertIdentical(token_replace('[term:name][term:vocabulary:name]', array('taxonomy_term' => $term)), $term->name . $vocabulary->name);
  623. }
  624. function addTerm(stdClass $vocabulary, array $term = array()) {
  625. $term += array(
  626. 'name' => drupal_strtolower($this->randomName(5)),
  627. 'vid' => $vocabulary->vid,
  628. );
  629. $term = (object) $term;
  630. taxonomy_term_save($term);
  631. return $term;
  632. }
  633. /**
  634. * Test the [entity:original:*] tokens.
  635. */
  636. function testEntityOriginal() {
  637. $node = $this->drupalCreateNode(array('title' => 'Original title'));
  638. $tokens = array(
  639. 'nid' => $node->nid,
  640. 'title' => 'Original title',
  641. 'original' => NULL,
  642. 'original:nid' => NULL,
  643. );
  644. $this->assertTokens('node', array('node' => $node), $tokens);
  645. // Emulate the original entity property that would be available from
  646. // node_save() and change the title for the node.
  647. $node->original = entity_load_unchanged('node', $node->nid);
  648. $node->title = 'New title';
  649. $tokens = array(
  650. 'nid' => $node->nid,
  651. 'title' => 'New title',
  652. 'original' => 'Original title',
  653. 'original:nid' => $node->nid,
  654. );
  655. $this->assertTokens('node', array('node' => $node), $tokens);
  656. }
  657. }
  658. /**
  659. * Test the profile tokens.
  660. */
  661. class TokenProfileTestCase extends TokenTestHelper {
  662. private $account;
  663. public static function getInfo() {
  664. return array(
  665. 'name' => 'Profile token tests',
  666. 'description' => 'Test the profile tokens.',
  667. 'group' => 'Token',
  668. );
  669. }
  670. public function setUp($modules = array()) {
  671. $modules[] = 'profile';
  672. parent::setUp($modules);
  673. $this->account = $this->drupalCreateUser(array('administer users'));
  674. $this->drupalLogin($this->account);
  675. }
  676. /**
  677. * Test the profile tokens.
  678. */
  679. function testProfileTokens() {
  680. $field_types = _profile_field_types();
  681. foreach (array_keys($field_types) as $field_type) {
  682. $field = array();
  683. switch ($field_type) {
  684. case 'checkbox':
  685. $field['title'] = 'This is a checkbox';
  686. break;
  687. case 'selection':
  688. $field['options'] = implode("\n", array('Red', 'Blue', 'Green'));
  689. break;
  690. }
  691. $this->addProfileField($field_type, $field);
  692. }
  693. // Submit the profile fields for the user.
  694. $edit = array(
  695. 'profile_textfield' => 'This is a text field',
  696. 'profile_textarea' => "First paragraph.\n\nSecond paragraph.",
  697. 'profile_checkbox' => TRUE,
  698. 'profile_selection' => 'Red',
  699. 'profile_list' => ' Drupal , Joomla ',
  700. 'profile_url' => 'http://www.example.com/',
  701. 'profile_date[month]' => 5,
  702. 'profile_date[day]' => 20,
  703. 'profile_date[year]' => 1984,
  704. );
  705. $this->drupalPost("user/{$this->account->uid}/edit/SimpleTest", $edit, 'Save');
  706. $account = user_load($this->account->uid, TRUE);
  707. // Test the profile token values.
  708. $tokens = array(
  709. 'profile-textfield' => 'This is a text field',
  710. 'profile-textarea' => "<p>First paragraph.</p>\n<p>Second paragraph.</p>\n",
  711. 'profile-checkbox' => 'This is a checkbox',
  712. 'profile-selection' => 'Red',
  713. 'profile-list' => 'Drupal, Joomla',
  714. 'profile-url' => 'http://www.example.com/',
  715. 'profile-date' => format_date(453859200, 'medium', '', NULL),
  716. 'profile-date:raw' => '453859200',
  717. 'profile-date:custom:Y' => '1984',
  718. );
  719. $this->assertTokens('user', array('user' => $account), $tokens);
  720. // 'Un-select' the checkbox and select profile fields.
  721. $edit = array(
  722. 'profile_checkbox' => FALSE,
  723. 'profile_selection' => '0',
  724. );
  725. $this->drupalPost("user/{$this->account->uid}/edit/SimpleTest", $edit, 'Save');
  726. $account = user_load($this->account->uid, TRUE);
  727. // The checkbox and select profile tokens should no longer return a value.
  728. $tokens = array(
  729. 'profile-checkbox' => NULL,
  730. 'profile-selection' => NULL,
  731. );
  732. $this->assertTokens('user', array('user' => $account), $tokens);
  733. }
  734. /**
  735. * Add a profile field.
  736. *
  737. * @param $type
  738. * The profile field type.
  739. * @param $field
  740. * (optional) An array of the profile field properties.
  741. *
  742. * @return
  743. * The saved profile field record object.
  744. *
  745. * @see drupal_form_submit()
  746. */
  747. function addProfileField($type, array $field = array()) {
  748. $field += array(
  749. 'type' => $type,
  750. 'category' => 'SimpleTest',
  751. 'title' => $this->randomName(8),
  752. 'name' => 'profile_' . $type,
  753. 'explanation' => $this->randomName(50),
  754. 'autocomplete' => 0,
  755. 'required' => 0,
  756. 'register' => 0,
  757. );
  758. drupal_write_record('profile_field', $field);
  759. // Verify the profile field was created successfully.
  760. $saved_field = db_query("SELECT * FROM {profile_field} WHERE type = :type AND name = :name", array(':type' => $type, ':name' => $field['name']))->fetchObject();
  761. if (empty($saved_field)) {
  762. $this->fail(t('Failed to create profile field @name.', array('@name' => $saved_field->name)));
  763. }
  764. return $saved_field;
  765. }
  766. }
  767. /**
  768. * Test the current page tokens.
  769. */
  770. class TokenCurrentPageTestCase extends TokenTestHelper {
  771. public static function getInfo() {
  772. return array(
  773. 'name' => 'Current page token tests',
  774. 'description' => 'Test the [current-page:*] tokens.',
  775. 'group' => 'Token',
  776. );
  777. }
  778. function testCurrentPageTokens() {
  779. $tokens = array(
  780. '[current-page:title]' => t('Welcome to @site-name', array('@site-name' => variable_get('site_name', 'Drupal'))),
  781. '[current-page:url]' => url('node', array('absolute' => TRUE)),
  782. '[current-page:url:absolute]' => url('node', array('absolute' => TRUE)),
  783. '[current-page:url:relative]' => url('node', array('absolute' => FALSE)),
  784. '[current-page:url:path]' => 'node',
  785. '[current-page:url:args:value:0]' => 'node',
  786. '[current-page:url:args:value:1]' => NULL,
  787. '[current-page:url:unaliased]' => url('node', array('absolute' => TRUE, 'alias' => TRUE)),
  788. '[current-page:page-number]' => 1,
  789. '[current-page:query:foo]' => NULL,
  790. '[current-page:query:bar]' => NULL,
  791. '[current-page:query:q]' => 'node',
  792. // Deprecated tokens
  793. '[current-page:arg:0]' => 'node',
  794. '[current-page:arg:1]' => NULL,
  795. );
  796. $this->assertPageTokens('', $tokens);
  797. $node = $this->drupalCreateNode(array('title' => 'Node title', 'path' => array('alias' => 'node-alias')));
  798. $tokens = array(
  799. '[current-page:title]' => 'Node title',
  800. '[current-page:url]' => url("node/{$node->nid}", array('absolute' => TRUE)),
  801. '[current-page:url:absolute]' => url("node/{$node->nid}", array('absolute' => TRUE)),
  802. '[current-page:url:relative]' => url("node/{$node->nid}", array('absolute' => FALSE)),
  803. '[current-page:url:alias]' => 'node-alias',
  804. '[current-page:url:args:value:0]' => 'node-alias',
  805. '[current-page:url:args:value:1]' => NULL,
  806. '[current-page:url:unaliased]' => url("node/{$node->nid}", array('absolute' => TRUE, 'alias' => TRUE)),
  807. '[current-page:url:unaliased:args:value:0]' => 'node',
  808. '[current-page:url:unaliased:args:value:1]' => $node->nid,
  809. '[current-page:url:unaliased:args:value:2]' => NULL,
  810. '[current-page:page-number]' => 1,
  811. '[current-page:query:foo]' => 'bar',
  812. '[current-page:query:bar]' => NULL,
  813. '[current-page:query:q]' => 'node/1',
  814. // Deprecated tokens
  815. '[current-page:arg:0]' => 'node',
  816. '[current-page:arg:1]' => 1,
  817. '[current-page:arg:2]' => NULL,
  818. );
  819. $this->assertPageTokens("node/{$node->nid}", $tokens, array(), array('url_options' => array('query' => array('foo' => 'bar'))));
  820. }
  821. }
  822. class TokenArrayTestCase extends TokenTestHelper {
  823. public static function getInfo() {
  824. return array(
  825. 'name' => 'Array token tests',
  826. 'description' => 'Test the array tokens.',
  827. 'group' => 'Token',
  828. );
  829. }
  830. function testArrayTokens() {
  831. // Test a simple array.
  832. $array = array(0 => 'a', 1 => 'b', 2 => 'c', 4 => 'd');
  833. $tokens = array(
  834. 'first' => 'a',
  835. 'last' => 'd',
  836. 'value:0' => 'a',
  837. 'value:2' => 'c',
  838. 'count' => 4,
  839. 'keys' => '0, 1, 2, 4',
  840. 'keys:value:3' => '4',
  841. 'keys:join' => '0124',
  842. 'reversed' => 'd, c, b, a',
  843. 'reversed:keys' => '4, 2, 1, 0',
  844. 'join:/' => 'a/b/c/d',
  845. 'join' => 'abcd',
  846. 'join:, ' => 'a, b, c, d',
  847. 'join: ' => 'a b c d',
  848. );
  849. $this->assertTokens('array', array('array' => $array), $tokens);
  850. // Test a mixed simple and render array.
  851. // 2 => c, 0 => a, 4 => d, 1 => b
  852. $array = array(
  853. '#property' => 'value',
  854. 0 => 'a',
  855. 1 => array('#markup' => 'b', '#weight' => 0.01),
  856. 2 => array('#markup' => 'c', '#weight' => -10),
  857. 4 => array('#markup' => 'd', '#weight' => 0),
  858. );
  859. $tokens = array(
  860. 'first' => 'c',
  861. 'last' => 'b',
  862. 'value:0' => 'a',
  863. 'value:2' => 'c',
  864. 'value:#property' => NULL,
  865. 'count' => 4,
  866. 'keys' => '2, 0, 4, 1',
  867. 'keys:value:3' => '1',
  868. 'keys:join' => '2041',
  869. 'reversed' => 'b, d, a, c',
  870. 'reversed:keys' => '1, 4, 0, 2',
  871. 'join:/' => 'c/a/d/b',
  872. 'join' => 'cadb',
  873. 'join:, ' => 'c, a, d, b',
  874. 'join: ' => 'c a d b',
  875. );
  876. $this->assertTokens('array', array('array' => $array), $tokens);
  877. }
  878. }
  879. class TokenRandomTestCase extends TokenTestHelper {
  880. public static function getInfo() {
  881. return array(
  882. 'name' => 'Random token tests',
  883. 'description' => 'Test the random tokens.',
  884. 'group' => 'Token',
  885. );
  886. }
  887. function testRandomTokens() {
  888. $tokens = array(
  889. 'number' => '[0-9]{1,}',
  890. 'hash:md5' => '[0-9a-f]{32}',
  891. 'hash:sha1' => '[0-9a-f]{40}',
  892. 'hash:sha256' => '[0-9a-f]{64}',
  893. 'hash:invalid-algo' => NULL,
  894. );
  895. $first_set = $this->assertTokens('random', array(), $tokens, array('regex' => TRUE));
  896. $second_set = $this->assertTokens('random', array(), $tokens, array('regex' => TRUE));
  897. foreach ($first_set as $token => $value) {
  898. $this->assertNotIdentical($first_set[$token], $second_set[$token]);
  899. }
  900. }
  901. }
  902. /**
  903. * @todo Remove when http://drupal.org/node/1173706 is fixed.
  904. */
  905. class TokenDateTestCase extends TokenTestHelper {
  906. public static function getInfo() {
  907. return array(
  908. 'name' => 'Date token tests',
  909. 'description' => 'Test the date tokens.',
  910. 'group' => 'Token',
  911. );
  912. }
  913. function testDateTokens() {
  914. $tokens = array(
  915. 'token_test' => '1984',
  916. 'invalid_format' => NULL,
  917. );
  918. $this->assertTokens('date', array('date' => 453859200), $tokens);
  919. }
  920. }
  921. class TokenFileTestCase extends TokenTestHelper {
  922. public static function getInfo() {
  923. return array(
  924. 'name' => 'File token tests',
  925. 'description' => 'Test the file tokens.',
  926. 'group' => 'Token',
  927. );
  928. }
  929. function testFileTokens() {
  930. // Create a test file object.
  931. $file = new stdClass();
  932. $file->fid = 1;
  933. $file->filename = 'test.png';
  934. $file->filesize = 100;
  935. $file->uri = 'public://images/test.png';
  936. $file->filemime = 'image/png';
  937. $tokens = array(
  938. 'basename' => 'test.png',
  939. 'extension' => 'png',
  940. 'size-raw' => 100,
  941. );
  942. $this->assertTokens('file', array('file' => $file), $tokens);
  943. // Test a file with no extension and a fake name.
  944. $file->filename = 'Test PNG image';
  945. $file->uri = 'public://images/test';
  946. $tokens = array(
  947. 'basename' => 'test',
  948. 'extension' => '',
  949. 'size-raw' => 100,
  950. );
  951. $this->assertTokens('file', array('file' => $file), $tokens);
  952. }
  953. }
  954. class TokenBlockTestCase extends TokenTestHelper {
  955. public static function getInfo() {
  956. return array(
  957. 'name' => 'Block token tests',
  958. 'description' => 'Test the block title token replacement.',
  959. 'group' => 'Token',
  960. );
  961. }
  962. public function setUp($modules = array()) {
  963. $modules[] = 'block';
  964. parent::setUp($modules);
  965. $this->admin_user = $this->drupalCreateUser(array('access content', 'administer blocks'));
  966. $this->drupalLogin($this->admin_user);
  967. }
  968. public function testBlockTitleTokens() {
  969. $edit['title'] = '[user:name]';
  970. $edit['info'] = 'Test token title block';
  971. $edit['body[value]'] = 'This is the test token title block.';
  972. $this->drupalPost('admin/structure/block/add', $edit, 'Save block');
  973. // Ensure token validation is working on the block.
  974. $this->assertText('Block title is using the following invalid tokens: [user:name].');
  975. // Create the block for real now with a valid title.
  976. $edit['title'] = '[current-page:title] block title';
  977. $edit['regions[bartik]'] = 'sidebar_first';
  978. $this->drupalPost(NULL, $edit, 'Save block');
  979. $this->drupalGet('node');
  980. $this->assertText('Welcome to ' . variable_get('site_name', 'Drupal') . ' block title');
  981. // Ensure that tokens are not double-escaped when output as a block title.
  982. $node = $this->drupalCreateNode(array('title' => "Site's first node"));
  983. $this->drupalGet('node/' . $node->nid);
  984. // The apostraphe should only be escaped once via check_plain().
  985. $this->assertRaw("Site&#039;s first node block title");
  986. }
  987. }