diff options
Diffstat (limited to 'modules/system')
-rw-r--r-- | modules/system/system.test | 71 | ||||
-rw-r--r-- | modules/system/system.tokens.inc | 53 |
2 files changed, 85 insertions, 39 deletions
diff --git a/modules/system/system.test b/modules/system/system.test index e20fd769e..d94f114a2 100644 --- a/modules/system/system.test +++ b/modules/system/system.test @@ -1490,6 +1490,77 @@ class TokenReplaceTestCase extends DrupalWebTestCase { $generated = token_generate('node', $raw_tokens, array('node' => $node), array('sanitize' => FALSE)); $this->assertFalse(strcmp($generated['[node:title]'], $node->title), t('Unsanitized token generated properly.')); } + + /** + * Tests the generation of all system site information tokens. + */ + function testSystemSiteTokenReplacement() { + global $language; + $url_options = array( + 'absolute' => TRUE, + 'language' => $language, + ); + + // Set a few site variables. + variable_set('site_name', '<strong>Drupal<strong>'); + variable_set('site_slogan', '<blink>Slogan</blink>'); + variable_set('site_mission', '<em>Mission</em>'); + + // Generate and test sanitized tokens. + $tests = array(); + $tests['[site:name]'] = check_plain(variable_get('site_name', 'Drupal')); + $tests['[site:slogan]'] = check_plain(variable_get('site_slogan', '')); + $tests['[site:mission]'] = filter_xss(variable_get('site_mission', '')); + $tests['[site:mail]'] = 'simpletest@example.com'; + $tests['[site:url]'] = url('<front>', $url_options); + $tests['[site:url-brief]'] = preg_replace('!^https?://!', '', url('<front>', $url_options)); + $tests['[site:login-url]'] = url('user', $url_options); + + // Test to make sure that we generated something for each token. + $this->assertFalse(in_array(0, array_map('strlen', $tests)), t('No empty tokens generated.')); + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array(), array('language' => $language)); + $this->assertFalse(strcmp($output, $expected), t('Sanitized system site information token %token replaced.', array('%token' => $input))); + } + + // Generate and test unsanitized tokens. + $tests['[site:name]'] = variable_get('site_name', 'Drupal'); + $tests['[site:slogan]'] = variable_get('site_slogan', ''); + $tests['[site:mission]'] = variable_get('site_mission', ''); + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array(), array('language' => $language, 'sanitize' => FALSE)); + $this->assertFalse(strcmp($output, $expected), t('Unsanitized system site information token %token replaced.', array('%token' => $input))); + } + } + + /** + * Tests the generation of all system date tokens. + */ + function testSystemDateTokenReplacement() { + global $language; + + // Set time to one hour before request. + $date = REQUEST_TIME - 3600; + + // Generate and test tokens. + $tests = array(); + $tests['[date:short]'] = format_date($date, 'short', '', NULL, $language->language); + $tests['[date:medium]'] = format_date($date, 'medium', '', NULL, $language->language); + $tests['[date:long]'] = format_date($date, 'long', '', NULL, $language->language); + $tests['[date:custom:m/j/Y]'] = format_date($date, 'custom', 'm/j/Y', NULL, $language->language); + $tests['[date:since]'] = format_interval((REQUEST_TIME - $date), 2, $language->language); + $tests['[date:raw]'] = filter_xss($date); + + // Test to make sure that we generated something for each token. + $this->assertFalse(in_array(0, array_map('strlen', $tests)), t('No empty tokens generated.')); + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array('date' => $date), array('language' => $language)); + $this->assertFalse(strcmp($output, $expected), t('Date token %token replaced.', array('%token' => $input))); + } + } } class InfoFileParserTestCase extends DrupalUnitTestCase { diff --git a/modules/system/system.tokens.inc b/modules/system/system.tokens.inc index e86bf8490..501ab8fd5 100644 --- a/modules/system/system.tokens.inc +++ b/modules/system/system.tokens.inc @@ -76,7 +76,7 @@ function system_token_info() { ); $date['since'] = array( 'name' => t("Time-since"), - 'description' => t("A data in 'time-since' format. (%date)", array('%date' => format_interval(REQUEST_TIME - 360, 2))), + 'description' => t("A date in 'time-since' format. (%date)", array('%date' => format_interval(REQUEST_TIME - 360, 2))), ); $date['raw'] = array( 'name' => t("Raw timestamp"), @@ -93,10 +93,6 @@ function system_token_info() { 'name' => t("User ID"), 'description' => t("The unique ID of the user who owns the file."), ); - $file['nid'] = array( - 'name' => t("Node ID"), - 'description' => t("The unique ID of the node the file is attached to."), - ); $file['name'] = array( 'name' => t("File name"), 'description' => t("The name of the file on disk."), @@ -107,7 +103,7 @@ function system_token_info() { ); $file['path'] = array( 'name' => t("Path"), - 'description' => t("The location of the file on disk."), + 'description' => t("The location of the file relative to Drupal root."), ); $file['mime'] = array( 'name' => t("MIME type"), @@ -117,7 +113,7 @@ function system_token_info() { 'name' => t("File size"), 'description' => t("The size of the file, in kilobytes."), ); - $file['path'] = array( + $file['url'] = array( 'name' => t("URL"), 'description' => t("The web-accessible URL for the file."), ); @@ -126,11 +122,6 @@ function system_token_info() { 'description' => t("The date the file was most recently changed."), 'type' => 'date', ); - $file['node'] = array( - 'name' => t("Node"), - 'description' => t("The node the file is attached to."), - 'type' => 'date', - ); $file['owner'] = array( 'name' => t("Owner"), 'description' => t("The user who originally uploaded the file."), @@ -155,6 +146,7 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a if (isset($language)) { $url_options['language'] = $language; } + $langcode = (isset($language) ? $language->language : NULL); $sanitize = !empty($options['sanitize']); $replacements = array(); @@ -203,14 +195,9 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a else { $date = $data['date']; } - $langcode = (isset($language) ? $language->language : NULL); foreach ($tokens as $name => $original) { switch ($name) { - case 'raw': - $replacements[$original] = filter_xss($date); - break; - case 'short': $replacements[$original] = format_date($date, 'short', '', NULL, $langcode); break; @@ -226,6 +213,10 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a case 'since': $replacements[$original] = format_interval((REQUEST_TIME - $date), 2, $langcode); break; + + case 'raw': + $replacements[$original] = filter_xss($date); + break; } } @@ -250,10 +241,6 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a $replacements[$original] = $file->uid; break; - case 'nid': - $replacements[$original] = $file->nid; - break; - // Essential file data case 'name': $replacements[$original] = $sanitize ? check_plain($file->filename) : $file->filename; @@ -264,7 +251,7 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a break; case 'path': - $replacements[$original] = $sanitize ? filter_xss($file->filepath) : $file->filepath; + $replacements[$original] = $sanitize ? filter_xss($file->uri) : $file->uri; break; case 'mime': @@ -276,39 +263,27 @@ function system_tokens($type, $tokens, array $data = array(), array $options = a break; case 'url': - $replacements[$original] = url(file_create_url($file->filepath), $url_options); + $replacements[$original] = url(file_create_url($file->uri), $url_options); break; // These tokens are default variations on the chained tokens handled below. - case 'node': - if ($nid = $file->nid) { - $node = node_load($file->nid); - $replacements[$original] = $sanitize ? filter_xss($node->title) : $node->title; - } - break; - case 'timestamp': - $replacements[$original] = format_date($file->timestamp, 'medium', '', NULL, (isset($language) ? $language->language : NULL)); + $replacements[$original] = format_date($file->timestamp, 'medium', '', NULL, $langcode); break; case 'owner': $account = user_load($file->uid); - $replacements[$original] = $sanitize ? filter_xss($user->name) : $user->name; + $replacements[$original] = $sanitize ? filter_xss($account->name) : $account->name; break; } } - if ($node_tokens = token_find_with_prefix($tokens, 'node')) { - $node = node_load($file->nid); - $replacements += token_generate('node', $node_tokens, array('node' => $node), $language, $sanitize); - } - if ($date_tokens = token_find_with_prefix($tokens, 'timestamp')) { - $replacements += token_generate('date', $date_tokens, array('date' => $file->timestamp), $language, $sanitize); + $replacements += token_generate('date', $date_tokens, array('date' => $file->timestamp), $options); } if (($owner_tokens = token_find_with_prefix($tokens, 'owner')) && $account = user_load($file->uid)) { - $replacements += token_generate('user', $owner_tokens, array('user' => $account), $language, $sanitize); + $replacements += token_generate('user', $owner_tokens, array('user' => $account), $options); } } |