diff --git a/core/modules/statistics/statistics.admin.inc b/core/modules/statistics/statistics.admin.inc index 6606b8b..821b198 100644 --- a/core/modules/statistics/statistics.admin.inc +++ b/core/modules/statistics/statistics.admin.inc @@ -270,5 +270,26 @@ function statistics_settings_form() { '#description' => t('Increment a counter each time content is viewed.'), ); + $form['reset_statistics'] = array( + '#type' => 'fieldset', + '#title' => t('Reset site statistics'), + ); + $form['reset_statistics']['reset'] = array( + '#type' => 'submit', + '#value' => t('Reset'), + '#submit' => array('statistics_reset_submit'), + ); return system_settings_form($form); } + +/** + * Submit callback to clear statistics data. + * + * @ingroup forms + */ +function statistics_reset_submit($form, &$form_state) { + db_truncate('accesslog')->execute(); + db_truncate('node_counter')->execute(); + + drupal_set_message(t('Cleared statistics data.')); +} diff --git a/core/modules/statistics/statistics.test b/core/modules/statistics/statistics.test index 1b7f8ac..fc17898 100644 --- a/core/modules/statistics/statistics.test +++ b/core/modules/statistics/statistics.test @@ -272,7 +272,7 @@ class StatisticsBlockVisitorsTestCase extends StatisticsTestCase { } /** - * Test statistics administration screen. + * Tests statistics administration screen. */ class StatisticsAdminTestCase extends DrupalWebTestCase { protected $privileged_user; @@ -409,10 +409,38 @@ class StatisticsAdminTestCase extends DrupalWebTestCase { ->fetchField(); $this->assertFalse($result, t('Daycounter is zero.')); } + + /** + * Log in an admin user and clear the statistics tables. + */ + protected function testStatisticsClear() { + // Enable access logging. + variable_set('statistics_enable_access_log', 1); + + // Log in the admin user. + $this->drupalLogin($this->privileged_user); + + // Check the current accesslog status, make a page request and verify. + $count_before = db_query("SELECT COUNT(*) FROM {accesslog}")->fetchField(); + $this->drupalGet('admin/reports/hits'); + $count_after = db_query("SELECT COUNT(*) FROM {accesslog}")->fetchField(); + $this->assertEqual($count_before + 1, $count_after, t('Page request added an entry to the accesslog :count_before - :count_after', array(':count_before' => $count_before, ':count_after' => $count_after))); + + // Make post to clear the tables. + $this->drupalPost('admin/config/system/statistics', array(), t('Reset')); + + // Test node_counter table. + $count = db_query("SELECT COUNT(*) FROM {node_counter}")->fetchField(); + $this->assertEqual($count, 0, t('The node_counter table contains :count records after a clear.', array(':count' => $count))); + + // Test accesslog table, note that we should already have 2 items in the table! + $count = db_query("SELECT COUNT(*) FROM {accesslog}")->fetchField(); + $this->assertEqual($count, 2, t('The accesslog table contains :count records after a clear.', array(':count' => $count))); + } } /** - * Test statistics token replacement in strings. + * Tests statistics token replacement in strings. */ class StatisticsTokenReplaceTestCase extends StatisticsTestCase { public static function getInfo() {