/[drupal]/contributions/modules/token/token.test
ViewVC logotype

Contents of /contributions/modules/token/token.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Tue Jul 21 14:20:25 2009 UTC (4 months ago) by eaton
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +59 -0 lines
File MIME type: text/x-php
Resyncing token code with #113614, and adding token.test to the module project.
1 <?php
2 // $Id$
3
4 /**
5 * Test token replacement in strings.
6 */
7 class TokenReplaceTestCase extends DrupalWebTestCase {
8 public static function getInfo() {
9 return array(
10 'name' => 'Token replacement',
11 'description' => 'Generates text using placeholders for dummy content to check token replacement.',
12 'group' => 'System',
13 );
14 }
15
16 /**
17 * Creates a user and a node, then tests the tokens generated from them.
18 */
19 function testTokenReplacement() {
20 // Create the initial objects.
21 $account = $this->drupalCreateUser();
22 $node = $this->drupalCreateNode(array('uid' => $account->uid));
23 $node->title = '<blink>Blinking Text</blink>';
24 global $user;
25
26 $source = '[node:title]'; // Title of the node we passed in
27 $source .= '[node:author:name]'; // Node author's name
28 $source .= '[node:created:since]'; // Time since the node was created
29 $source .= '[current-user:name]'; // Current user's name
30 $source .= '[user:name]'; // No user passed in, should be untouched
31 $source .= '[date:small]'; // Small date format of REQUEST_TIME
32 $source .= '[bogus:token]'; // Nonexistent token, should be untouched
33
34 $target = check_plain($node->title);
35 $target .= check_plain($account->name);
36 $target .= format_interval(REQUEST_TIME - $node->created, 2);
37 $target .= check_plain($user->name);
38 $target .= '[user:name]';
39 $target .= format_date(REQUEST_TIME, 'small');
40 $target .= '[bogus:token]';
41
42 $result = token_replace($source, array('node' => $node));
43
44 // Check that the results of token_generate are sanitized properly. This does NOT
45 // test the cleanliness of every token -- just that the $sanitize flag is being
46 // passed properly through the call stack and being handled correctly by a 'known'
47 // token, [node:title].
48 $this->assertFalse(strcmp($target, $result), t('Basic placeholder tokens replaced.'));
49
50 $raw_tokens = array(
51 'node' => array('title' => '[node:title]'),
52 );
53 $generated = token_generate($raw_tokens, array('node' => $node));
54 $this->assertFalse(strcmp($generated['[node:title]'], check_plain($node->title)), t('Token sanitized.'));
55
56 $generated = token_generate($raw_tokens, array('node' => $node), array('sanitize' => FALSE));
57 $this->assertFalse(strcmp($generated['[node:title]'], $node->title), t('Unsanitized token generated properly.'));
58 }
59 }

  ViewVC Help
Powered by ViewVC 1.1.2