Index: modules/statistics/statistics.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/statistics/statistics.admin.inc,v retrieving revision 1.21 diff -u -p -r1.21 statistics.admin.inc --- modules/statistics/statistics.admin.inc 13 Apr 2009 10:40:13 -0000 1.21 +++ modules/statistics/statistics.admin.inc 21 Apr 2009 19:24:23 -0000 @@ -260,5 +260,27 @@ function statistics_settings_form() { '#options' => $options, '#description' => t('Increment a counter each time content is viewed.')); + $form['reset_statistics'] = array( + '#type' => 'fieldset', + '#title' => t('Reset site statistics'), + '#description' => t('Reset the statistical information of the site.'), + ); + $form['reset_statistics']['reset'] = array( + '#type' => 'submit', + '#value' => t('Reset site statistics'), + '#submit' => array('statistics_reset_submit'), + ); + return system_settings_form($form, TRUE); } + +/** + * Submit callback; clear statistical information. + * + * @ingroup forms + */ +function statistics_reset_submit(&$form_state, $form) { + db_delete('accesslog')->execute(); + db_delete('node_counter')->execute(); + drupal_set_message(t('Cleared statistical information.')); +} Index: modules/statistics/statistics.test =================================================================== RCS file: /cvs/drupal/drupal/modules/statistics/statistics.test,v retrieving revision 1.8 diff -u -p -r1.8 statistics.test --- modules/statistics/statistics.test 13 Apr 2009 10:40:13 -0000 1.8 +++ modules/statistics/statistics.test 21 Apr 2009 19:24:23 -0000 @@ -70,3 +70,54 @@ class StatisticsBlockVisitorsTestCase ex $this->assertRaw(t('The IP address %ip was deleted.', array('%ip' => $test_ip_address)), t('IP address deleted.')); } } + +class StatisticsTestCase extends DrupalWebTestCase { + protected $big_user; + + public static function getInfo() { + return array( + 'name' => t('Statistics functionality'), + 'description' => t('Clear the statistics table.'), + 'group' => t('Statistics'), + ); + } + + /** + * Enable required modules and create users with specific permissions. + */ + function setUp() { + parent::setUp('statistics'); + // Create user. + $this->big_user = $this->drupalCreateUser(array('administer site configuration', 'access administration pages', 'access statistics', 'administer statistics')); + } + + /** + * Login an admin user and clear the statistics tables. + */ + protected function testStatisticsClear() { + global $base_root; + + // Enable access logging (redundant since we insert the data manually). + variable_set('statistics_enable_access_log', 1); + + // Login the admin user. + $this->drupalLogin($this->big_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 table. + $this->drupalPost('admin/settings/statistics', array(), t('Reset site statistics')); + + // 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))); + } +}