token.test 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. 'count' => 4,
  865. 'keys' => '2, 0, 4, 1',
  866. 'keys:value:3' => '1',
  867. 'keys:join' => '2041',
  868. 'reversed' => 'b, d, a, c',
  869. 'reversed:keys' => '1, 4, 0, 2',
  870. 'join:/' => 'c/a/d/b',
  871. 'join' => 'cadb',
  872. 'join:, ' => 'c, a, d, b',
  873. 'join: ' => 'c a d b',
  874. );
  875. $this->assertTokens('array', array('array' => $array), $tokens);
  876. }
  877. }
  878. class TokenRandomTestCase extends TokenTestHelper {
  879. public static function getInfo() {
  880. return array(
  881. 'name' => 'Random token tests',
  882. 'description' => 'Test the random tokens.',
  883. 'group' => 'Token',
  884. );
  885. }
  886. function testRandomTokens() {
  887. $tokens = array(
  888. 'number' => '[0-9]{1,}',
  889. 'hash:md5' => '[0-9a-f]{32}',
  890. 'hash:sha1' => '[0-9a-f]{40}',
  891. 'hash:sha256' => '[0-9a-f]{64}',
  892. 'hash:invalid-algo' => NULL,
  893. );
  894. $first_set = $this->assertTokens('random', array(), $tokens, array('regex' => TRUE));
  895. $second_set = $this->assertTokens('random', array(), $tokens, array('regex' => TRUE));
  896. foreach ($first_set as $token => $value) {
  897. $this->assertNotIdentical($first_set[$token], $second_set[$token]);
  898. }
  899. }
  900. }
  901. /**
  902. * @todo Remove when http://drupal.org/node/1173706 is fixed.
  903. */
  904. class TokenDateTestCase extends TokenTestHelper {
  905. public static function getInfo() {
  906. return array(
  907. 'name' => 'Date token tests',
  908. 'description' => 'Test the date tokens.',
  909. 'group' => 'Token',
  910. );
  911. }
  912. function testDateTokens() {
  913. $tokens = array(
  914. 'token_test' => '1984',
  915. 'invalid_format' => NULL,
  916. );
  917. $this->assertTokens('date', array('date' => 453859200), $tokens);
  918. }
  919. }
  920. class TokenFileTestCase extends TokenTestHelper {
  921. public static function getInfo() {
  922. return array(
  923. 'name' => 'File token tests',
  924. 'description' => 'Test the file tokens.',
  925. 'group' => 'Token',
  926. );
  927. }
  928. function testFileTokens() {
  929. // Create a test file object.
  930. $file = new stdClass();
  931. $file->fid = 1;
  932. $file->filename = 'test.png';
  933. $file->filesize = 100;
  934. $file->uri = 'public://images/test.png';
  935. $file->filemime = 'image/png';
  936. $tokens = array(
  937. 'basename' => 'test.png',
  938. 'extension' => 'png',
  939. 'size-raw' => 100,
  940. );
  941. $this->assertTokens('file', array('file' => $file), $tokens);
  942. // Test a file with no extension and a fake name.
  943. $file->filename = 'Test PNG image';
  944. $file->uri = 'public://images/test';
  945. $tokens = array(
  946. 'basename' => 'test',
  947. 'extension' => '',
  948. 'size-raw' => 100,
  949. );
  950. $this->assertTokens('file', array('file' => $file), $tokens);
  951. }
  952. }
  953. class TokenBlockTestCase extends TokenTestHelper {
  954. public static function getInfo() {
  955. return array(
  956. 'name' => 'Block token tests',
  957. 'description' => 'Test the block title token replacement.',
  958. 'group' => 'Token',
  959. );
  960. }
  961. public function setUp($modules = array()) {
  962. $modules[] = 'block';
  963. parent::setUp($modules);
  964. $this->admin_user = $this->drupalCreateUser(array('access content', 'administer blocks'));
  965. $this->drupalLogin($this->admin_user);
  966. }
  967. public function testBlockTitleTokens() {
  968. $edit['title'] = '[user:name]';
  969. $edit['info'] = 'Test token title block';
  970. $edit['body[value]'] = 'This is the test token title block.';
  971. $this->drupalPost('admin/structure/block/add', $edit, 'Save block');
  972. // Ensure token validation is working on the block.
  973. $this->assertText('The Block title is using the following invalid tokens: [user:name].');
  974. // Create the block for real now with a valid title.
  975. $edit['title'] = '[current-page:title] block title';
  976. $edit['regions[bartik]'] = 'sidebar_first';
  977. $this->drupalPost(NULL, $edit, 'Save block');
  978. $this->drupalGet('node');
  979. $this->assertText('Welcome to ' . variable_get('site_name', 'Drupal') . ' block title');
  980. // Ensure that tokens are not double-escaped when output as a block title.
  981. $node = $this->drupalCreateNode(array('title' => "Site's first node"));
  982. $this->drupalGet('node/' . $node->nid);
  983. // The apostraphe should only be escaped once via check_plain().
  984. $this->assertRaw("Site&#039;s first node block title");
  985. }
  986. }