diff --git a/modules/statistics/statistics.admin.inc b/modules/statistics/statistics.admin.inc index 6606b8b..31567b2 100644 --- a/modules/statistics/statistics.admin.inc +++ b/modules/statistics/statistics.admin.inc @@ -270,5 +270,28 @@ 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'), + '#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); } + +/** + * Submit callback; clear statistical information. + * + * @ingroup forms + */ +function statistics_reset_submit(&$form_state, $form) { + db_truncate('accesslog')->execute(); + db_truncate('node_counter')->execute(); + + drupal_set_message(t('Cleared statistical information.')); +} diff --git a/modules/statistics/statistics.test b/modules/statistics/statistics.test index 126828f..6a2fac9 100644 --- a/modules/statistics/statistics.test +++ b/modules/statistics/statistics.test @@ -10,6 +10,9 @@ */ class StatisticsTestCase extends DrupalWebTestCase { + /** + * Overrides DrupalWebTestCase::setUp(). + */ function setUp() { parent::setUp('statistics'); @@ -60,6 +63,9 @@ class StatisticsLoggingTestCase extends DrupalWebTestCase { ); } + /** + * Overrides DrupalWebTestCase::setUp(). + */ function setUp() { parent::setUp('statistics'); @@ -260,7 +266,7 @@ class StatisticsBlockVisitorsTestCase extends StatisticsTestCase { } /** - * Test statistics administration screen. + * Tests statistics administration screen. */ class StatisticsAdminTestCase extends DrupalWebTestCase { protected $privileged_user; @@ -274,6 +280,9 @@ class StatisticsAdminTestCase extends DrupalWebTestCase { ); } + /** + * Overrides DrupalWebTestCase::setUp(). + */ function setUp() { parent::setUp('statistics'); $this->privileged_user = $this->drupalCreateUser(array('access statistics', 'administer statistics', 'view post access counter', 'create page content')); @@ -400,7 +409,7 @@ class StatisticsAdminTestCase extends DrupalWebTestCase { } /** - * Test statistics token replacement in strings. + * Tests statistics token replacement in strings. */ class StatisticsTokenReplaceTestCase extends StatisticsTestCase { public static function getInfo() { @@ -442,3 +451,57 @@ class StatisticsTokenReplaceTestCase extends StatisticsTestCase { } } } + +/** + * Tests clearing statistics data via the admin user interface. + */ +class StatisticsTestCase extends DrupalWebTestCase { + protected $big_user; + + public static function getInfo() { + return array( + 'name' => 'Statistics functionality', + 'description' => 'Clear the statistics table.', + 'group' => 'Statistics', + ); + } + + /** + * Overrides DrupalWebTestCase::setUp(). + */ + 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/config/system/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))); + } +}