diff --git a/core/modules/statistics/statistics.admin.inc b/core/modules/statistics/statistics.admin.inc index 87ae6a5..a77543d 100644 --- a/core/modules/statistics/statistics.admin.inc +++ b/core/modules/statistics/statistics.admin.inc @@ -305,6 +305,26 @@ function statistics_settings_form() { '#default_value' => variable_get('statistics_count_content_views', 0), '#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 bb28b01..10cd1ed 100644 --- a/core/modules/statistics/statistics.test +++ b/core/modules/statistics/statistics.test @@ -503,6 +503,35 @@ class StatisticsAdminTestCase extends WebTestBase { ->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, 'Page request added an entry to the accesslog'); + + // 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, 'The node_counter table contains 0 records after a clear.'); + + // 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, 'The accesslog table contains 2 records after a clear.'); + } } /**