| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: token.test,v 1.1.2.14 2010/08/09 18:39:54 davereid Exp $ 3 4 /** 5 * @file 6 * Tests for the token and token_actions modules. 7 */ 8 9 /** 10 * Helper test class with some added functions for testing. 11 */ 12 class TokenTestHelper extends DrupalWebTestCase { 13 function setUp() { 14 // Call parent::setUp() allowing test cases to pass further modules. 15 $modules = func_get_args(); 16 $modules = array_merge(array('path', 'token'), $modules); 17 18 $parent_callback = 'parent::setUp'; 19 if (version_compare(PHP_VERSION, '5.3.0', '<')) { 20 $parent_callback = array($this, 'parent::setUp'); 21 } 22 call_user_func_array($parent_callback, $modules); 23 } 24 25 function assertToken($type, $object, $token, $expected, array $options = array()) { 26 $this->assertTokens($type, $object, array($token => $expected), $options); 27 } 28 29 function assertTokens($type, $object, array $tokens, array $options = array()) { 30 $values = token_get_values($type, $object, TRUE, $options); 31 $values = array_combine($values->tokens, $values->values); 32 foreach ($tokens as $token => $expected) { 33 $this->assertIdentical($values[$token], $expected, t("Token value for [@token] was '@actual', expected value '@expected'.", array('@token' => $token, '@actual' => $values[$token], '@expected' => $expected))); 34 } 35 } 36 } 37 38 class TokenUnitTestCase extends TokenTestHelper { 39 public static function getInfo() { 40 return array( 41 'name' => 'Token unit tests', 42 'description' => 'Test basic, low-level token functions.', 43 'group' => 'Token', 44 ); 45 } 46 47 /** 48 * Test token_get_invalid_tokens() and token_get_invalid_tokens_by_context(). 49 */ 50 public function testGetInvalidTokens() { 51 $tests = array(); 52 $tests[] = array( 53 'valid tokens' => array( 54 '[title-raw]', 55 '[yyyy]', 56 '[mod-yyyy]', 57 '[site-name]', 58 '[site-slogan]', 59 ), 60 'invalid tokens' => array( 61 '[title-invalid]', 62 '[invalid]', 63 '[mod-invalid]', 64 '[invalid-title]', 65 '[site-invalid]', 66 ), 67 'types' => array('node'), 68 ); 69 70 foreach ($tests as $test) { 71 $tokens = array_merge($test['valid tokens'], $test['invalid tokens']); 72 shuffle($tokens); 73 74 $invalid_tokens = token_get_invalid_tokens_by_context(implode(' ', $tokens), $test['types']); 75 76 sort($invalid_tokens); 77 sort($test['invalid tokens']); 78 $this->assertEqual($invalid_tokens, $test['invalid tokens'], 'Invalid tokens detected properly.'); 79 } 80 } 81 } 82 83 class TokenNodeTestCase extends TokenTestHelper { 84 public static function getInfo() { 85 return array( 86 'name' => 'Node token tests', 87 'description' => 'Test the node tokens.', 88 'group' => 'Token', 89 ); 90 } 91 92 function testNodeTokens() { 93 $time = time(); 94 $created = gmmktime(0, 0, 0, 11, 19, 1978); 95 $changed = gmmktime(0, 0, 0, 7, 4, 1984); 96 $node = $this->drupalCreateNode(array( 97 'type' => 'page', 98 'language' => 'und', 99 'created' => $created, 100 'log' => '<blink>' . $this->randomName() . '</blink>', 101 )); 102 $node->changed = $changed; 103 path_set_alias('node/' . $node->nid, 'content/first-node'); 104 105 $tokens = array( 106 'nid' => $node->nid, 107 'type' => 'page', 108 'type-name' => 'Page', 109 'language' => 'und', 110 'node-path' => 'content/first-node', 111 'node-url' => url('node/' . $node->nid, array('absolute' => TRUE)), 112 'small' => '11/19/1978 - 00:00', 113 'yyyy' => '1978', 114 'yy' => '78', 115 'month' => 'November', 116 'mon' => 'Nov', 117 'mm' => '11', 118 'm' => '11', 119 'ww' => '46', 120 'date' => '7', 121 'day' => 'Sunday', 122 'ddd' => 'Sun', 123 'dd' => '19', 124 'd' => '19', 125 'raw' => 280281600, 126 'since' => format_interval($time - 280281600), 127 'mod-small' => '07/04/1984 - 00:00', 128 'mod-yyyy' => '1984', 129 'mod-yy' => '84', 130 'mod-month' => 'July', 131 'mod-mon' => 'Jul', 132 'mod-mm' => '07', 133 'mod-m' => '7', 134 'mod-ww' => '27', 135 'mod-date' => '3', 136 'mod-day' => 'Wednesday', 137 'mod-ddd' => 'Wed', 138 'mod-dd' => '04', 139 'mod-d' => '4', 140 'mod-raw' => 457747200, 141 'mod-since' => format_interval($time - 457747200), 142 'log' => filter_xss($node->log), 143 'log-raw' => $node->log, 144 ); 145 $this->assertTokens('node', $node, $tokens); 146 } 147 } 148 149 class TokenCommentTestCase extends TokenTestHelper { 150 protected $node; 151 152 public static function getInfo() { 153 return array( 154 'name' => 'Comment token tests', 155 'description' => 'Test the comment tokens.', 156 'group' => 'Token', 157 ); 158 } 159 160 function setUp() { 161 parent::setUp('comment'); 162 $this->node = $this->drupalCreateNode(array('comment' => 2)); 163 } 164 165 function loadComment($cid) { 166 return db_fetch_object(db_query('SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.signature_format, u.picture, u.data, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d', $cid)); 167 } 168 169 function createComment(array $comment) { 170 $comment += array( 171 'cid' => 0, 172 'nid' => $this->node->nid, 173 'pid' => 0, 174 'uid' => 0, 175 'subject' => $this->randomName(), 176 'comment' => $this->randomName(64), 177 'format' => 1, 178 'timestamp' => gmmktime(0, 0, 0, 7, 4, 1984), 179 'status' => COMMENT_PUBLISHED, 180 ); 181 182 $cid = comment_save($comment); 183 return $this->loadComment($cid); 184 } 185 186 function testCommentTokens() { 187 $time = time(); 188 $comment = $this->createComment(array( 189 'timestamp' => gmmktime(0, 0, 0, 7, 4, 1984), 190 )); 191 192 $tokens = array( 193 'comment-cid' => $comment->cid, 194 'comment-nid' => $this->node->nid, 195 'comment-yyyy' => '1984', 196 'comment-yy' => '84', 197 'comment-month' => 'July', 198 'comment-mon' => 'Jul', 199 'comment-mm' => '07', 200 'comment-m' => '7', 201 'comment-ww' => '27', 202 'comment-date' => '3', 203 'comment-day' => 'Wednesday', 204 'comment-ddd' => 'Wed', 205 'comment-dd' => '04', 206 'comment-d' => '4', 207 'comment-raw' => '457747200', 208 'comment-since' => format_interval($time - 457747200), 209 'comment-node-title' => check_plain($this->node->title), 210 'comment-node-title-raw' => $this->node->title, 211 ); 212 $this->assertTokens('comment', $comment, $tokens); 213 214 } 215 } 216 217 class TokenTaxonomyTestCase extends TokenTestHelper { 218 protected $vocabulary; 219 220 public static function getInfo() { 221 return array( 222 'name' => 'Taxonomy token tests', 223 'description' => 'Test the taxonomy tokens.', 224 'group' => 'Token', 225 ); 226 } 227 228 function setUp() { 229 parent::setUp('taxonomy'); 230 // Reset the static taxonomy.module caches. 231 taxonomy_vocabulary_load(0, TRUE); 232 taxonomy_get_term(0, TRUE); 233 } 234 235 function addVocabulary(array $vocabulary = array()) { 236 $vocabulary += array( 237 'name' => drupal_strtolower($this->randomName(5)), 238 'nodes' => array('story' => 'story'), 239 ); 240 taxonomy_save_vocabulary($vocabulary); 241 return (object) $vocabulary; 242 } 243 244 function addTerm(stdClass $vocabulary, array $term = array()) { 245 $term += array( 246 'name' => drupal_strtolower($this->randomName(5)), 247 'vid' => $vocabulary->vid, 248 ); 249 taxonomy_save_term($term); 250 return (object) $term; 251 } 252 253 function testTaxonomyTokens() { 254 $vocabulary = $this->addVocabulary(array( 255 'name' => '<blink>Vocab Name</blink>', 256 'description' => '<blink>Vocab Description</blink>', 257 )); 258 $term = $this->addTerm($vocabulary, array( 259 'name' => '<blink>Term Name</blink>', 260 'description' => '<blink>Term Description</blink>', 261 )); 262 263 $tokens = array( 264 'tid' => $term->tid, 265 'cat' => check_plain($term->name), 266 'cat-raw' => $term->name, 267 'cat-description' => 'Term Description', 268 'vid' => $vocabulary->vid, 269 'vocab' => check_plain($vocabulary->name), 270 'vocab-raw' => $vocabulary->name, 271 'vocab-description' => 'Vocab Description', 272 ); 273 $this->assertTokens('taxonomy', $term, $tokens); 274 } 275 } 276 277 class TokenMenuTestCase extends TokenTestHelper { 278 public static function getInfo() { 279 return array( 280 'name' => 'Menu token tests', 281 'description' => 'Test the menu tokens.', 282 'group' => 'Token', 283 ); 284 } 285 286 function setUp() { 287 parent::setUp('menu'); 288 } 289 290 function testMenuTokens() { 291 $root_link = array( 292 'link_path' => '<front>', 293 'link_title' => 'Front link', 294 'menu_name' => 'primary-links', 295 ); 296 menu_link_save($root_link); 297 298 $node_link = array( 299 'enabled' => TRUE, 300 'link_title' => 'Node link', 301 'plid' => $root_link['mlid'], 302 'customized' => 0, 303 ); 304 $node = $this->drupalCreateNode(array('menu' => $node_link)); 305 306 // Test [node:menu] tokens. 307 $tokens = array( 308 'menu' => 'Primary links', 309 'menu-link-title' => 'Node link', 310 'menu-link-mlid' => $node->menu['mlid'], 311 'menu-link-plid' => $node->menu['plid'], 312 'menu-link-plid' => $root_link['mlid'], 313 ); 314 $this->assertTokens('node', $node, $tokens); 315 } 316 } 317 318 class TokenTestCase extends DrupalWebTestCase { 319 public static function getInfo() { 320 return array( 321 'name' => t('Token and token action tests'), 322 'description' => t('Test some of the token actions and tokens.'), 323 'group' => t('Token'), 324 ); 325 } 326 327 function setUp() { 328 parent::setUp('token', 'token_actions', 'trigger'); 329 } 330 331 /** 332 * Test various behaviors for anonymous users. 333 */ 334 function testTokenActionsFunctionalTest() { 335 // Create a user with permission to view the actions administration pages. 336 $user = $this->drupalCreateUser(array('administer actions', 'administer site configuration', 'administer users')); 337 $this->drupalLogin($user); 338 339 // Set the site name to something more exciting than Drupal / simpletest@example.com. 340 $settings = array(); 341 $site_name = $this->randomName(); 342 $site_mail = $this->randomName() .'@example.com'; 343 $settings['site_name'] = $site_name; 344 $settings['site_mail'] = $site_mail; 345 346 $this->drupalPost('admin/settings/site-information', $settings, t('Save configuration')); 347 $this->assertText('saved', 'Site settings saved.'); 348 349 // Create an action to display to users 350 $action = array(); 351 $action['action'] = md5('token_actions_message_action'); 352 $this->drupalPost('admin/settings/actions', $action, t('Create')); 353 354 // Configure the action to use a handful of tokens. 355 $action = array(); 356 $action_description = $this->randomName(); 357 $action['actions_description'] = $action_description; 358 $action['message'] = 'Hello simpletest | [user-name] | [user-id] | [user-mail] | [site-name] | [site-mail]'; 359 $this->drupalPost('admin/settings/actions/configure/'. md5('token_actions_message_action'), $action, t('Save')); 360 361 // Trigger the action when a user is created. 362 $trigger = array(); 363 $trigger['aid'] = md5('1'); //TODO don't hardcode the 1. 364 // TODO this should be assigned to a specific aid like aid_4, but that's not possible b/c actions creates non-unique form names. 365 // so instead we use "aid" which is the "after a user is created" trigger. 366 $this->drupalPost('admin/build/trigger/user', $trigger, t('Assign')); 367 // TODOXXX confirm each post gets saved. 368 369 // Create a user to trigger the action. 370 $edit = array(); 371 $edit['name'] = $this->randomName(); 372 $edit['mail'] = $edit['name'] .'@example.com'; 373 $pass = user_password(); 374 $edit['pass[pass1]'] = $pass; 375 $edit['pass[pass2]'] = $pass; 376 377 $this->drupalPost('admin/user/user/create', $edit, t('Create new account')); 378 $this->assertText('Hello simpletest | '. $user->name .' | '. $user->uid .' | '. $user->mail .' | '. $site_name .' | '. $site_mail, 'Tokenized message displays'); 379 380 } 381 382 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |