Extracted text and test files from source code

This commit is contained in:
Manuel Cillero 2017-08-29 09:49:41 +02:00
parent 676a7ea81c
commit e7e66a9245
134 changed files with 0 additions and 14081 deletions

View file

@ -1,132 +0,0 @@
Overview
========
In many cases, it's useful to allow users to define patterns or large
chunks of text that contain programmatically derived values. For example,
form email messages addressed to a given user, or url path aliases
containing the title of a given node. Both examples require bits of data
that vary each time the text is generated -- node titles, user ids, and
so on. Rather than forcing users to embed ugly snippets of PHP, or creating
elaborate and bizarre UIs for configuring the patterns via the browser,
it's most useful to give users a set of 'placeholder' tokens to place in
their text.
Token.module provides a shared API for exposing and using placeholder
tokens and their appropriate replacement values. It does nothing *by
itself* -- other modules can use it to avoid reinventing the wheel.
Using Token Replacement
=======================
To apply token replacement to a chunk of text, you have two options. The
first, and simplest, is:
token_replace($original, $type = 'global', $object = NULL,
$leading = '[', $trailing = ']')
$original is the source text to perform substitutions on: it can be either
a simple string, or an array of multiple strings.
$type and $object are to be used when performing substitution based on a
particular Drupal object. Replacing tokens in an email with values from
a particular user account, or replacing tokens in a path alias pattern with
values from the node being aliased, are two examples.
$type should contain the general object type (node, comment, user, etc.)
while $object should contain the object itself.
$leading and $trailing can be used to override the default token style.
For example, to replace tokens using %this style, pass in '%' and '' for
the $leading and $trailing values. Note that passing in a leading but NOT
trailing value can lead to false positives if two tokens are named in a
similar fashion (%node_term and %node_term_id, for example).
Altering The Replacement Values
===============================
If your module needs to perform additional cleanup work on the replacement
values before doing the actual substitutions (cleaning replacement values
to make them appropriate for path aliasing, truncating them to a particular
length, etc.) you can manually retrieve the list of tokens and replacement
values, then call str_replace() yourself.
token_get_values($type = 'global', $object = NULL)
Pass in the $type and $object as you would with the simpler token_replace()
function. The return value will be an object containing one array of tokens
and one array of values as in this example:
stdClass Object {
[tokens] => array(
[0] => mytoken1,
[1] => mytoken2
),
[values] => array(
[0] => value1,
[1] => value2,
)
}
Providing Placeholder Tokens
============================
Token.module provides a small set of default placeholders for global values
like the name of the currently logged in user, the site's URL, and so on.
Any module can provide additional tokens by implementing two hooks.
Security note: For tokens which include user input, users and modules
expect to see both a ['token-name'] and a ['token-name-raw'] value.
hook_token_values($type, $object = NULL)
========================================
This function should return a keyed array of placeholders, and their
replacement values. $type contains the current context -- 'node', 'user',
'global', etc. $object contains the specific node, user, etc. that
should be used as the basis for the replacements. *Only* generate and
return replacement tokens when $type is something that your module can
really deal with. That helps keep things speedy and avoid needlessly
searching for jillions of replacement tokens. The $options array can
contain additional options (exact use is dynamic and not easily documented).
For example:
function my_user_token_values($type, $object = NULL, $options = array()) {
if ($type == 'user') {
$user = $object;
$tokens['name'] = $user->name;
$tokens['mail'] = $user->mail;
return $tokens;
}
}
hook_token_list($type = 'all')
==============================
This function is used to provide help and inline documentation for all
of the possible replacement tokens.
As with hook_token_values, $type indicates the context that token help
is being generated for. Unlike hook_token_values however, you should
show ALL tokens at the same time if $type is 'all'. As such, the help
text should be keyed by the $type context your module will use when
doing the actual replacements. For example:
function my_user_token_list($type = 'all') {
if ($type == 'user' || $type == 'all') {
$tokens['user']['name'] = t("The user's name");
$tokens['user']['mail'] = t("The user's email address");
return $tokens;
}
}
Examples of more elaborate token replacement setups can be found in the
token_node.inc file that's bundled with token.module.
Security Note
========
If use any of the tokens in the ['raw'] sub-array then please note that these
are unfiltered values which could conceivably contain XSS attacks or other
malicious data. Your module should then provide it's own filtering to ensure the
safety of site users.

View file

@ -1,690 +0,0 @@
<?php
/**
* @file
* Tests for the token module.
*/
/**
* Helper test class with some added functions for testing.
*/
class TokenTestHelper extends DrupalWebTestCase {
function setUp(array $modules = array()) {
$modules[] = 'path';
$modules[] = 'token';
$modules[] = 'token_test';
parent::setUp($modules);
// Clear the token static cache.
token_get_values('reset');
}
function assertToken($type, $object, $token, $expected, array $options = array()) {
$this->assertTokens($type, $object, array($token => $expected), $options);
}
function assertTokens($type, $object, array $tokens, array $options = array()) {
$values = token_get_values($type, $object, FALSE, $options);
$values = array_combine($values->tokens, $values->values);
foreach ($tokens as $token => $expected) {
if (!isset($expected)) {
$this->assertTrue(!isset($values[$token]), t("Token value for [@token] was not generated.", array('@token' => $token)));
}
elseif (!isset($values[$token])) {
$this->fail(t("Token value for [@token] was not generated.", array('@token' => $token)));
}
else {
$this->assertIdentical($values[$token], $expected, t("Token value for [@token] was '@actual', expected value '@expected'.", array('@token' => $token, '@actual' => $values[$token], '@expected' => $expected)));
}
}
}
/**
* Make a page request and test for token generation.
*/
function assertPageTokens($url, array $tokens, array $data = array('global' => NULL), array $options = array()) {
if (empty($tokens)) {
return TRUE;
}
$token_page_tokens = array(
'tokens' => $tokens,
'data' => $data,
'options' => $options,
);
variable_set('token_page_tokens', $token_page_tokens);
$options += array('url_options' => array());
$this->drupalGet($url, $options['url_options']);
$this->refreshVariables();
$result = variable_get('token_page_tokens', array());
if (!isset($result['values']) || !is_array($result['values'])) {
return $this->fail('Failed to generate tokens.');
}
foreach ($tokens as $token => $expected) {
if (!isset($expected)) {
$this->assertTrue(!isset($result['values'][$token]) || $result['values'][$token] === $token, t("Token value for @token was not generated.", array('@token' => $token)));
}
elseif (!isset($result['values'][$token])) {
$this->fail(t('Failed to generate token @token.', array('@token' => $token)));
}
else {
$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)));
}
}
}
}
class TokenUnitTestCase extends TokenTestHelper {
public static function getInfo() {
return array(
'name' => 'Token unit tests',
'description' => 'Test basic, low-level token functions.',
'group' => 'Token',
);
}
/**
* Test token_get_invalid_tokens() and token_get_invalid_tokens_by_context().
*/
public function testGetInvalidTokens() {
$tests = array();
$tests[] = array(
'valid tokens' => array(
'[title-raw]',
'[yyyy]',
'[mod-yyyy]',
'[site-name]',
'[site-slogan]',
'[user-id]',
),
'invalid tokens' => array(
'[title-invalid]',
'[invalid]',
'[mod-invalid]',
'[invalid-title]',
'[site-invalid]',
'[uid]',
'[comment-cid]',
),
'types' => array('node'),
);
$tests[] = array(
'valid tokens' => array(
'[title-raw]',
'[yyyy]',
'[mod-yyyy]',
'[site-name]',
'[site-slogan]',
'[user-id]',
'[uid]',
'[comment-cid]',
),
'invalid tokens' => array(
'[title-invalid]',
'[invalid]',
'[mod-invalid]',
'[invalid-title]',
'[site-invalid]',
),
'types' => array('all'),
);
$tests[] = array(
'valid tokens' => array(
'[alpha]',
'[beta-1]',
'[beta-2]',
'[gamma_A]',
'[delta-extra]',
'[epsilon-zeta-A]',
),
'invalid tokens' => array(
'[alpha-plus]',
'[beta]',
'[beta-]',
'[beta_]',
'[beta_1]',
'[beta-A]',
'[gamma]',
'[gamma_]',
'[gamma-A]',
'[delta]',
'[epsilon-zeta-]',
),
'types' => array('all'),
);
foreach ($tests as $test) {
$tokens = array_merge($test['valid tokens'], $test['invalid tokens']);
shuffle($tokens);
$invalid_tokens = token_get_invalid_tokens_by_context(implode(' ', $tokens), $test['types']);
sort($invalid_tokens);
sort($test['invalid tokens']);
$this->assertEqual($invalid_tokens, $test['invalid tokens'], 'Invalid tokens detected properly: ' . implode(', ', $invalid_tokens));
}
}
/**
* Test the $options['clear'] parameter for token_replace().
*/
public function testClearOption() {
$tests[] = array(
'input' => 'Foo [site-name][invalid-token] bar [another-invalid-token] [invalid-token]',
'output' => 'Foo Drupal bar ',
'options' => array('clear' => TRUE),
);
$tests[] = array(
'input' => 'Foo [site-name][invalid-token] bar [another-invalid-token] [invalid-token]',
'output' => 'Foo Drupal[invalid-token] bar [another-invalid-token] [invalid-token]',
'options' => array(),
);
foreach ($tests as $test) {
$output = token_replace($test['input'], 'global', NULL, TOKEN_PREFIX, TOKEN_SUFFIX, $test['options']);
$this->assertIdentical($output, $test['output']);
}
}
/**
* Test whether token-replacement works in various contexts.
*
* @see http://drupal.org/node/733192
*/
function testSystemTokenRecognition() {
global $language;
// Generate prefixes and suffixes for the token context.
$tests = array(
array('prefix' => 'this is the ', 'suffix' => ' site'),
array('prefix' => 'this is the', 'suffix' => 'site'),
array('prefix' => '[', 'suffix' => ']'),
array('prefix' => '', 'suffix' => ']]]'),
array('prefix' => '[[[', 'suffix' => ''),
array('prefix' => ':[:', 'suffix' => '--]'),
array('prefix' => '-[-', 'suffix' => ':]:'),
array('prefix' => '[:', 'suffix' => ']'),
array('prefix' => '[site:', 'suffix' => ':name]'),
array('prefix' => '[site:', 'suffix' => ']'),
);
// Check if the token is recognized in each of the contexts.
foreach ($tests as $test) {
$input = $test['prefix'] . '[site-name]' . $test['suffix'];
$expected = $test['prefix'] . 'Drupal' . $test['suffix'];
$output = token_replace($input);
$this->assertEqual($output, $expected);
}
}
/**
* Test token caching.
*/
function testTokenCaching() {
// Run global tokens once so that the cache is primed.
$tokens = array(
'option-foo' => '',
);
$this->assertTokens('global', NULL, $tokens);
// Run global tokens again with different options. This should return a
// different value for the [option-foo] token.
$tokens = array(
'option-foo' => 'bar',
);
$this->assertTokens('global', NULL, $tokens, array('foo' => 'bar'));
}
/**
* Test the token_scan() function.
*/
function testTokenScan() {
$tests = array(
array('text' => 'Test [foo] [[bar]] test.', 'tokens' => array('foo', 'bar')),
array('text' => 'Test [foo] [] test.', 'tokens' => array('foo')),
array('text' => 'Test [foo][] test.', 'tokens' => array('foo')),
array('text' => 'Test [foo][bar] test.', 'tokens' => array('foo', 'bar')),
// Test the e-mail token syntax.
array('text' => 'Test %foo %%bar test.', 'tokens' => array('foo', 'bar'), 'leading' => '%', 'trailing' => ''),
array('text' => 'Test %foo % test.', 'tokens' => array('foo'), 'leading' => '%', 'trailing' => ''),
array('text' => 'Test %foo% test.', 'tokens' => array('foo'), 'leading' => '%', 'trailing' => ''),
array('text' => 'Test %foo%%bar test.', 'tokens' => array('foo', 'bar'), 'leading' => '%', 'trailing' => ''),
// Test the rules token syntax.
array('text' => 'Test [global:foo] [global:bar] test.', 'tokens' => array('foo', 'bar'), 'leading' => '[global:'),
array('text' => 'Test [node:foo] [node:] test.', 'tokens' => array('foo'), 'leading' => '[node:'),
array('text' => 'Test [node:foo][node:] test.', 'tokens' => array('foo'), 'leading' => '[node:'),
array('text' => 'Test [node:foo][node:bar] test.', 'tokens' => array('foo', 'bar'), 'leading' => '[node:'),
);
foreach ($tests as $test) {
$test += array('leading' => TOKEN_PREFIX, 'trailing' => TOKEN_SUFFIX);
$this->assertEqual(token_scan($test['text'], $test['leading'], $test['trailing']), $test['tokens']);
}
}
}
class TokenNodeTestCase extends TokenTestHelper {
public static function getInfo() {
return array(
'name' => 'Node token tests',
'description' => 'Test the node tokens.',
'group' => 'Token',
);
}
function testNodeTokens() {
$time = time();
$created = gmmktime(0, 0, 0, 11, 19, 1978);
$changed = gmmktime(0, 0, 0, 7, 4, 1984);
$node = $this->drupalCreateNode(array(
'type' => 'page',
'language' => 'und',
'created' => $created,
'log' => '<blink>' . $this->randomName() . '</blink>',
));
$node->changed = $changed;
path_set_alias('node/' . $node->nid, 'content/first-node');
$tokens = array(
'nid' => $node->nid,
'type' => 'page',
'type-name' => 'Page',
'language' => 'und',
'node-path' => 'content/first-node',
'node-url' => url('node/' . $node->nid, array('absolute' => TRUE)),
'small' => '11/19/1978 - 00:00',
'yyyy' => '1978',
'yy' => '78',
'month' => 'November',
'mon' => 'Nov',
'mm' => '11',
'm' => '11',
'ww' => '46',
'date' => '7',
'day' => 'Sunday',
'ddd' => 'Sun',
'dd' => '19',
'd' => '19',
'raw' => 280281600,
'since' => format_interval($time - 280281600),
'mod-small' => '07/04/1984 - 00:00',
'mod-yyyy' => '1984',
'mod-yy' => '84',
'mod-month' => 'July',
'mod-mon' => 'Jul',
'mod-mm' => '07',
'mod-m' => '7',
'mod-ww' => '27',
'mod-date' => '3',
'mod-day' => 'Wednesday',
'mod-ddd' => 'Wed',
'mod-dd' => '04',
'mod-d' => '4',
'mod-raw' => 457747200,
'mod-since' => format_interval($time - 457747200),
'log' => filter_xss($node->log),
'log-raw' => $node->log,
);
$this->assertTokens('node', $node, $tokens);
// Check that a new revision of a node returns different tokens.
$node->revision = TRUE;
$node->title = 'New revision';
node_save($node);
$this->assertTokens('node', $node, array('title' => 'New revision'));
}
}
class TokenCommentTestCase extends TokenTestHelper {
protected $node;
public static function getInfo() {
return array(
'name' => 'Comment token tests',
'description' => 'Test the comment tokens.',
'group' => 'Token',
);
}
function setUp(array $modules = array()) {
$modules[] = 'comment';
parent::setUp($modules);
$this->node = $this->drupalCreateNode(array('comment' => 2));
}
function loadComment($cid) {
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));
}
function createComment(array $comment) {
$comment += array(
'cid' => 0,
'nid' => $this->node->nid,
'pid' => 0,
'uid' => 0,
'subject' => $this->randomName(),
'comment' => $this->randomName(64),
'format' => 1,
'timestamp' => gmmktime(0, 0, 0, 7, 4, 1984),
'status' => COMMENT_PUBLISHED,
);
$cid = comment_save($comment);
return $this->loadComment($cid);
}
function testCommentTokens() {
$time = time();
$comment = $this->createComment(array(
'timestamp' => gmmktime(0, 0, 0, 7, 4, 1984),
));
$tokens = array(
'comment-cid' => $comment->cid,
'comment-nid' => $this->node->nid,
'comment-yyyy' => '1984',
'comment-yy' => '84',
'comment-month' => 'July',
'comment-mon' => 'Jul',
'comment-mm' => '07',
'comment-m' => '7',
'comment-ww' => '27',
'comment-date' => '3',
'comment-day' => 'Wednesday',
'comment-ddd' => 'Wed',
'comment-dd' => '04',
'comment-d' => '4',
'comment-raw' => '457747200',
'comment-since' => format_interval($time - 457747200),
'comment-node-title' => check_plain($this->node->title),
'comment-node-title-raw' => $this->node->title,
);
$this->assertTokens('comment', $comment, $tokens);
}
}
class TokenTaxonomyTestCase extends TokenTestHelper {
protected $vocabulary;
public static function getInfo() {
return array(
'name' => 'Taxonomy and vocabulary token tests',
'description' => 'Test the taxonomy tokens.',
'group' => 'Token',
);
}
function setUp(array $modules = array()) {
$modules[] = 'taxonomy';
parent::setUp($modules);
// Reset the static taxonomy.module caches.
taxonomy_vocabulary_load(0, TRUE);
taxonomy_get_term(0, TRUE);
}
function addVocabulary(array $vocabulary = array()) {
$vocabulary += array(
'name' => drupal_strtolower($this->randomName(5)),
'nodes' => array('story' => 'story'),
);
taxonomy_save_vocabulary($vocabulary);
return (object) $vocabulary;
}
function addTerm(stdClass $vocabulary, array $term = array()) {
$term += array(
'name' => drupal_strtolower($this->randomName(5)),
'vid' => $vocabulary->vid,
);
taxonomy_save_term($term);
return (object) $term;
}
function testTaxonomyTokens() {
$vocabulary = $this->addVocabulary(array(
'name' => '<blink>Vocab Name</blink>',
'description' => '<blink>Vocab Description</blink>',
));
$term = $this->addTerm($vocabulary, array(
'name' => '<blink>Term Name</blink>',
'description' => '<blink>Term Description</blink>',
));
$tokens = array(
'tid' => $term->tid,
'cat' => check_plain($term->name),
'cat-raw' => $term->name,
'cat-description' => 'Term Description',
'vid' => $vocabulary->vid,
'vocab' => check_plain($vocabulary->name),
'vocab-raw' => $vocabulary->name,
'vocab-description' => 'Vocab Description',
'vocab-description-raw' => $vocabulary->description,
);
$this->assertTokens('taxonomy', $term, $tokens);
$tokens = array(
'vocabulary-vid' => $vocabulary->vid,
'vocabulary-name' => check_plain($vocabulary->name),
'vocabulary-name-raw' => $vocabulary->name,
'vocabulary-description' => 'Vocab Description',
'vocabulary-description-raw' => $vocabulary->description,
);
$this->assertTokens('vocabulary', $vocabulary, $tokens);
}
}
class TokenMenuTestCase extends TokenTestHelper {
public static function getInfo() {
return array(
'name' => 'Menu token tests',
'description' => 'Test the menu tokens.',
'group' => 'Token',
);
}
function setUp(array $modules = array()) {
$modules[] = 'menu';
parent::setUp($modules);
}
function testMenuTokens() {
$root_link = array(
'link_path' => 'root',
'link_title' => 'Root link',
'menu_name' => 'primary-links',
);
menu_link_save($root_link);
// Add another link with the root link as the parent
$parent_link = array(
'link_path' => 'root/parent',
'link_title' => 'Parent link',
'menu_name' => 'primary-links',
'plid' => $root_link['mlid'],
);
menu_link_save($parent_link);
$node_link = array(
'enabled' => TRUE,
'link_title' => 'Node link',
'plid' => $parent_link['mlid'],
'customized' => 0,
);
$node = $this->drupalCreateNode(array('menu' => $node_link));
// Test [node:menu] tokens.
$tokens = array(
'menu' => 'Primary links',
'menu-raw' => 'Primary links',
'menupath' => 'Root link/Parent link/Node link',
'menupath-raw' => 'Root link/Parent link/Node link',
'menu-link-title' => 'Node link',
'menu-link-title-raw' => 'Node link',
'menu-link-mlid' => $node->menu['mlid'],
'menu-link-plid' => $node->menu['plid'],
'menu-link-plid' => $parent_link['mlid'],
'menu-link-parent-path' => 'root/parent',
'menu-link-parent-path-raw' => 'root/parent',
);
$this->assertTokens('node', $node, $tokens);
// Reload the node which will not have $node->menu defined and re-test.
$loaded_node = node_load($node->nid);
// We have to reset the token static cache because tokens are cached by
// node ID only and not if the node object has changed.
$this->assertTokens('node', $loaded_node, $tokens, array('reset' => TRUE));
// Regression test for http://drupal.org/node/1317926 to ensure the
// original node object is not changed when calling menu_node_prepare().
$this->assertTrue(!isset($loaded_node->menu), t('The $node->menu property was not modified during token replacement.'), 'Regression');
}
}
/*
* Unit tests for the book tokens provided by Pathauto.
*/
class TokenBookTestCase extends TokenTestHelper {
public static function getInfo() {
return array(
'name' => 'Book tokens',
'description' => 'Tests the book tokens.',
'group' => 'Token',
);
}
function setUp(array $modules = array()) {
$modules[] = 'book';
$modules[] = 'menu';
parent::setUp($modules);
variable_set('book_allowed_types', array('book', 'page'));
}
function testBookTokens() {
// Add a non-book node.
$non_book_node = $this->drupalCreateNode(array('type' => 'book'));
$tokens = array(
'book' => '',
'book-raw' => '',
'book_id' => '',
'bookpath' => '',
'bookpath-raw' => '',
);
$this->assertTokens('node', $non_book_node, $tokens);
// Add a root book page.
$parent_node = $this->drupalCreateNode(array(
'type' => 'book',
'title' => 'Root',
'book' => array('bid' => 'new') + _book_link_defaults('new'),
));
$tokens = array(
'book' => 'Root',
'book-raw' => 'Root',
'book_id' => $parent_node->book['bid'],
'bookpath' => '',
'bookpath-raw' => '',
// Check that even those book menu links have been created for this node,
// that the menu links still return nothing.
'menu' => '',
'menupath' => '',
'menu-link-title' => '',
'menu-link-title-raw' => '',
'menu-link-mlid' => '',
'menu-link-plid' => '',
'menu-link-parent-path' => '',
);
$this->assertTokens('node', $parent_node, $tokens);
// Add a first child page.
$child_node1 = $this->drupalCreateNode(array(
'type' => 'book',
'title' => 'Sub page1',
'book' => array(
'bid' => $parent_node->book['bid'],
'plid' => $parent_node->book['mlid'],
) + _book_link_defaults('new'),
));
$tokens = array(
'book' => 'Root',
'book-raw' => 'Root',
'book_id' => $parent_node->book['bid'],
'bookpath' => 'Root',
'bookpath-raw' => 'Root',
);
$this->assertTokens('node', $child_node1, $tokens);
// Add a second child page.
$child_node2 = $this->drupalCreateNode(array(
'type' => 'book',
'title' => 'Sub page2',
'book' => array(
'bid' => $parent_node->book['bid'],
'plid' => $parent_node->book['mlid'],
) + _book_link_defaults('new'),
));
$tokens = array(
'book' => 'Root',
'book-raw' => 'Root',
'book_id' => $parent_node->book['bid'],
'bookpath' => 'Root',
'bookpath-raw' => 'Root',
);
$this->assertTokens('node', $child_node2, $tokens);
// Add a child page on an existing child page.
$sub_child_node1 = $this->drupalCreateNode(array(
'type' => 'page',
'title' => 'Sub-sub Page1',
'book' => array(
'bid' => $parent_node->book['bid'],
'plid' => $child_node1->book['mlid'],
) + _book_link_defaults('new'),
));
$tokens = array(
'book' => 'Root',
'book-raw' => 'Root',
'book_id' => $parent_node->book['bid'],
'bookpath' => 'Root/Sub page1',
'bookpath-raw' => 'Root/Sub page1',
);
$this->assertTokens('node', $sub_child_node1, $tokens);
}
}
/**
* Test the current page tokens.
*/
class TokenCurrentPageTestCase extends TokenTestHelper {
public static function getInfo() {
return array(
'name' => 'Current page token tests',
'description' => 'Test the current page tokens.',
'group' => 'Token',
);
}
function testCurrentPageTokens() {
$tokens = array(
'[current-page-title]' => '',
'[current-page-path]' => 'node',
'[current-page-url]' => url('node', array('absolute' => TRUE)),
'[current-page-number]' => 1,
);
$this->assertPageTokens('', $tokens);
$node = $this->drupalCreateNode(array('title' => 'Node title', 'path' => 'node-alias'));
$tokens = array(
'[current-page-title]' => 'Node title',
'[current-page-path]' => 'node-alias',
'[current-page-url]' => url("node/{$node->nid}", array('absolute' => TRUE)),
'[current-page-number]' => 1,
);
$this->assertPageTokens("node/{$node->nid}", $tokens);
}
}

View file

@ -1,11 +0,0 @@
name = TokenSTARTER
description = Provides additional tokens and a base on which to build your own tokens.
package = Core - extended
dependencies[] = token
core = 6.x
; Information added by drupal.org packaging script on 2012-09-12
version = "6.x-1.19"
core = "6.x"
project = "token"
datestamp = "1347470077"

View file

@ -1,68 +0,0 @@
<?php
/**
* @file
* The Token API module.
*
* The Token module provides an API for providing tokens to other modules.
* Tokens are small bits of text that can be placed into larger documents
* via simple placeholders, like %site-name or [user].
*
* @ingroup token
*/
/**
* Implementation of hook_token_list().
*/
function tokenSTARTER_token_list($type = 'all') {
$tokens = array();
if ($type == 'global' || $type == 'all') {
$tokens['global']['random-sha1'] = t("A randomly generated SHA1 hash.");
$tokens['global']['site-date-timestamp'] = t('The current timestamp in seconds past January 1, 1970.');
$tokens['global']['random-num-1'] = t('A randomly generated single-digit number.');
$tokens['global']['random-num-3'] = t('A randomly generated three-digit number.');
$tokens['global']['random-num-10'] = t('A randomly generated ten-digit number.');
$tokens['global']['random-alpha-1'] = t('Randomly generated single-digit letter.');
$tokens['global']['random-alpha-3'] = t('Randomly generated three-digit letters.');
$tokens['global']['random-alpha-10'] = t('Randomly generated ten-digit letters.');
}
if ($type == 'node' || $type == 'all') {
// Node tokens here.
}
return $tokens;
}
/**
* Implementation of hook_token_values().
*/
function tokenSTARTER_token_values($type, $object = NULL, $options = array()) {
$values = array();
switch ($type) {
case 'global':
$values['random-sha1'] = sha1(rand());
// Create random numbers.
$values['random-num-1'] = mt_rand(0, 9);
$values['random-num-3'] = mt_rand(100, 999);
$values['random-num-10'] = mt_rand(10000, 99999) . mt_rand(10000, 99999);
// Create random letters.
$letters = range('a', 'z');
$values['random-alpha-1'] = $letters[array_rand($letters, 1)];
shuffle($letters);
$values['random-alpha-3'] = implode('', array_slice($letters, 0, 3));
shuffle($letters);
$values['random-alpha-10'] = implode('', array_slice($letters, 0, 10));
// Create a UNIX timestamp token.
$time = time();
$tz = variable_get('date_default_timezone', 0);
$values['site-date-timestamp'] = format_date($time, 'custom', 'Y', $tz);
break;
case 'node':
// Node tokens here.
break;
}
return $values;
}

View file

@ -1,79 +0,0 @@
<?php
/**
* @file
* Tests for the token_actions module.
*/
class TokenActionsTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => t('Token action tests'),
'description' => t('Test some of the token actions and tokens.'),
'group' => t('Token'),
);
}
function setUp() {
parent::setUp('token', 'token_actions', 'trigger');
$user = $this->drupalCreateUser(array('administer actions', 'administer site configuration', 'administer users'));
$this->drupalLogin($user);
}
/**
* Test user actions and triggers.
*/
function testUserActions() {
$insert_action = $this->createAction('token_actions_message_action', array(
'message' => 'Yay [site-name] has a new user [user] with an ID of [uid] and e-mail address of [mail]!',
));
$this->assignTriggerAction('user', 'insert', $insert_action);
// Create a user to trigger the action.
$edit = array();
$edit['name'] = $this->randomName();
$edit['mail'] = $edit['name'] .'@example.com';
$edit['pass[pass1]'] = $this->randomName();
$edit['pass[pass2]'] = $edit['pass[pass1]'];
$this->drupalPost('admin/user/user/create', $edit, t('Create new account'));
$account = user_load(array('name' => $edit['name']));
$this->assertText("Yay Drupal has a new user {$account->name} with an ID of {$account->uid} and e-mail address of {$account->mail}!", 'Tokenized message displays');
}
/**
* Create an action.
*
* @param $action
* The machine name of the action.
* @param $edit
* An optional array to pass onto drupalPost() for configuring the action.
*
* @return
* The created action object.
*/
function createAction($action, $edit = array()) {
$edit += array(
'actions_description' => $this->randomName(),
);
$this->drupalPost('admin/settings/actions/configure/'. md5($action), $edit, t('Save'));
$this->assertText('The action has been successfully saved.');
return db_fetch_object(db_query("SELECT * FROM {actions} WHERE type = 'system' AND callback = '%s' AND description = '%s'", $action, $edit['actions_description']));
}
/**
* Assign an action to a trigger.
*
* @param $type
* The trigger type.
* @param $trigger
* The trigger.
* @param $action
* The action object.
*/
function assignTriggerAction($type, $trigger, $action) {
$edit['aid'] = md5($action->aid);
$this->drupalPost("admin/build/trigger/{$type}", $edit, 'Assign', array(), array(), "trigger-{$type}-{$trigger}-assign-form");
return $this->assertLinkByHref("admin/build/trigger/unassign/{$type}/{$trigger}/{$edit['aid']}", 0, t('Action assigned to @type @trigger trigger.', array('@type' => $type, '@trigger' => $trigger)));
}
}