Index: modules/statistics/statistics.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/statistics/statistics.test,v
retrieving revision 1.23
diff -u -p -r1.23 statistics.test
--- modules/statistics/statistics.test	24 Sep 2010 00:37:44 -0000	1.23
+++ modules/statistics/statistics.test	3 Oct 2010 21:56:16 -0000
@@ -402,12 +402,8 @@ class StatisticsTokenReplaceTestCase ext
   function testStatisticsTokenReplacement() {
     global $language;
 
-    // Create user and node.
-    $user = $this->drupalCreateUser(array('create page content'));
-    $this->drupalLogin($user);
-    $node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $user->uid));
-
-    // Hit the node.
+    // Create and visit a node.
+    $node = $this->drupalCreateNode();
     $this->drupalGet('node/' . $node->nid);
     $statistics = statistics_get($node->nid);
 
@@ -425,5 +421,20 @@ class StatisticsTokenReplaceTestCase ext
       $output = token_replace($input, array('node' => $node), array('language' => $language));
       $this->assertFalse(strcmp($output, $expected), t('Statistics token %token replaced.', array('%token' => $input)));
     }
+
+    // Create a new node but do not visit it.
+    $unvisited_node = $this->drupalCreateNode();
+
+    // Generate and test tokens for the node that has not yet been viewed.
+    $tests = array();
+    $tests['[node:total-count]'] = 0;
+    $tests['[node:day-count]'] = 0;
+    $tests['[node:last-view]'] = '';
+    $tests['[node:last-view:short]'] = '';
+
+    foreach ($tests as $input => $expected) {
+      $output = token_replace($input, array('node' => $unvisited_node), array('language' => $language, 'clear' => TRUE));
+      $this->assertFalse(strcmp($output, $expected), t('Statistics token %token replaced.', array('%token' => $input)));
+    }
   }
 }
Index: modules/statistics/statistics.tokens.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/statistics/statistics.tokens.inc,v
retrieving revision 1.4
diff -u -p -r1.4 statistics.tokens.inc
--- modules/statistics/statistics.tokens.inc	20 Apr 2010 09:48:06 -0000	1.4
+++ modules/statistics/statistics.tokens.inc	3 Oct 2010 21:56:16 -0000
@@ -33,30 +33,33 @@ function statistics_token_info() {
  * Implements hook_tokens().
  */
 function statistics_tokens($type, $tokens, array $data = array(), array $options = array()) {
-  $url_options = array('absolute' => TRUE);
+  $language_code = isset($options['language']->language) ? $options['language']->language : NULL;
   $replacements = array();
 
   if ($type == 'node' & !empty($data['node'])) {
     $node = $data['node'];
 
     foreach ($tokens as $name => $original) {
-      if ($name == 'total-count') {
-        $statistics = statistics_get($node->nid);
-        $replacements[$original] = $statistics['totalcount'];
-      }
-      elseif ($name == 'day-count') {
-        $statistics = statistics_get($node->nid);
-        $replacements[$original] = $statistics['daycount'];
-      }
-      elseif ($name == 'last-view') {
-        $statistics = statistics_get($node->nid);
-        $replacements[$original] = format_date($statistics['timestamp']);
+      switch ($name) {
+        case 'total-count':
+          $statistics = statistics_get($node->nid);
+          $replacements[$original] = $statistics ? $statistics['totalcount'] : 0;
+          break;
+        case 'day-count':
+          $statistics = statistics_get($node->nid);
+          $replacements[$original] = $statistics ? $statistics['daycount'] : 0;
+          break;
+        case 'last-view':
+          $statistics = statistics_get($node->nid);
+          if (!empty($statistics['timestamp'])) {
+            $replacements[$original] = format_date($statistics['timestamp'], 'medium', '', NULL, $language_code);
+          }
+          break;
       }
     }
 
-    if ($created_tokens = token_find_with_prefix($tokens, 'last-view')) {
-      $statistics = statistics_get($node->nid);
-      $replacements += token_generate('date', $created_tokens, array('date' => $statistics['timestamp']), $options);
+    if (($last_view_tokens = token_find_with_prefix($tokens, 'last-view')) && $statistics = statistics_get($node->nid)) {
+      $replacements += token_generate('date', $last_view_tokens, array('date' => $statistics['timestamp']), $options);
     }
   }
 
