Skip to content
Snippets Groups Projects
Commit 71102007 authored by Dries Buytaert's avatar Dries Buytaert
Browse files

- Patch #659578 by mcarbone, jhodgdon: fixed statistics pages and added some...

- Patch #659578 by mcarbone, jhodgdon: fixed statistics pages and added some extra tests. One critical bug less.
parent 8bac5dc6
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -56,7 +56,7 @@ function statistics_top_pages() { ...@@ -56,7 +56,7 @@ function statistics_top_pages() {
array('data' => t('Total page generation time'), 'field' => 'total_time') array('data' => t('Total page generation time'), 'field' => 'total_time')
); );
$query = db_select('accesslog', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort'); $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
$query->addExpression('COUNT(path)', 'hits'); $query->addExpression('COUNT(path)', 'hits');
// MAX(title) avoids having empty node titles which otherwise causes duplicates in the top pages list // MAX(title) avoids having empty node titles which otherwise causes duplicates in the top pages list
$query->addExpression('MAX(title)', 'title'); $query->addExpression('MAX(title)', 'title');
...@@ -64,12 +64,12 @@ function statistics_top_pages() { ...@@ -64,12 +64,12 @@ function statistics_top_pages() {
$query->addExpression('SUM(timer)', 'total_time'); $query->addExpression('SUM(timer)', 'total_time');
$query $query
->fields('accesslog', array('path')) ->fields('a', array('path'))
->groupBy('path') ->groupBy('path')
->limit(30) ->limit(30)
->orderByHeader($header); ->orderByHeader($header);
$count_query = db_select('accesslog', array('target' => 'slave')); $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
$count_query->addExpression('COUNT(DISTINCT path)'); $count_query->addExpression('COUNT(DISTINCT path)');
$query->setCountQuery($count_query); $query->setCountQuery($count_query);
...@@ -164,7 +164,7 @@ function statistics_top_referrers() { ...@@ -164,7 +164,7 @@ function statistics_top_referrers() {
->limit(30) ->limit(30)
->orderByHeader($header); ->orderByHeader($header);
$count_query = db_select('accesslog', array('target' => 'slave')); $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
$count_query->addExpression('COUNT(DISTINCT url)'); $count_query->addExpression('COUNT(DISTINCT url)');
$count_query $count_query
->where('LOWER(url) NOT LIKE :host', array(':host' => '%' . $_SERVER['HTTP_HOST'] . '%')) ->where('LOWER(url) NOT LIKE :host', array(':host' => '%' . $_SERVER['HTTP_HOST'] . '%'))
......
<?php <?php
// $Id$ // $Id$
class StatisticsBlockVisitorsTestCase extends DrupalWebTestCase { /**
public static function getInfo() { * Sets up a base class for the Statistics module.
return array( */
'name' => 'Top visitor blocking', class StatisticsTestCase extends DrupalWebTestCase {
'description' => 'Tests blocking of IP addresses via the top visitors report.',
'group' => 'Statistics'
);
}
function setUp() { function setUp() {
parent::setUp('statistics'); parent::setUp('statistics');
...@@ -16,6 +12,9 @@ class StatisticsBlockVisitorsTestCase extends DrupalWebTestCase { ...@@ -16,6 +12,9 @@ class StatisticsBlockVisitorsTestCase extends DrupalWebTestCase {
// Create user. // Create user.
$this->blocking_user = $this->drupalCreateUser(array('block IP addresses', 'access statistics')); $this->blocking_user = $this->drupalCreateUser(array('block IP addresses', 'access statistics'));
// Enable access logging.
variable_set('statistics_enable_access_log', 1);
// Insert dummy access by anonymous user into access log. // Insert dummy access by anonymous user into access log.
db_insert('accesslog') db_insert('accesslog')
->fields(array( ->fields(array(
...@@ -30,17 +29,92 @@ class StatisticsBlockVisitorsTestCase extends DrupalWebTestCase { ...@@ -30,17 +29,92 @@ class StatisticsBlockVisitorsTestCase extends DrupalWebTestCase {
)) ))
->execute(); ->execute();
} }
}
/**
* Tests that report pages render properly, and that access logging works.
*/
class StatisticsReportsTestCase extends StatisticsTestCase {
public static function getInfo() {
return array(
'name' => 'Statistics reports tests',
'description' => 'Tests display of statistics report pages and access logging.',
'group' => 'Statistics'
);
}
/**
* Verifies that 'Recent hits' renders properly and displays the added hit.
*/
function testRecentHits() {
$this->drupalLogin($this->blocking_user);
$this->drupalGet('admin/reports/hits');
$this->assertText('test', t('Hit title found.'));
$this->assertText('node/1', t('Hit URL found.'));
$this->assertText('Anonymous', t('Hit user found.'));
}
/** /**
* Blocks an IP address via the top visitors report then uses the same page to unblock it. * Verifies that 'Top pages' renders properly and displays the added hit.
*/
function testTopPages() {
$this->drupalLogin($this->blocking_user);
$this->drupalGet('admin/reports/pages');
$this->assertText('test', t('Hit title found.'));
$this->assertText('node/1', t('Hit URL found.'));
}
/**
* Verifies that 'Top referrers' renders properly and displays the added hit.
*/
function testTopReferrers() {
$this->drupalLogin($this->blocking_user);
$this->drupalGet('admin/reports/referrers');
$this->assertText('http://example.com', t('Hit referrer found.'));
}
/**
* Verifies that 'Details' page renders properly and displays the added hit.
*/
function testDetails() {
$this->drupalLogin($this->blocking_user);
$this->drupalGet('admin/reports/access/1');
$this->assertText('test', t('Hit title found.'));
$this->assertText('node/1', t('Hit URL found.'));
$this->assertText('Anonymous', t('Hit user found.'));
}
/**
* Verifies that access logging is working and is reported correctly.
*/
function testAccessLogging() {
$this->drupalLogin($this->blocking_user);
$this->drupalGet('admin/reports/referrers');
$this->drupalGet('admin/reports/hits');
$this->assertText('Top referrers in the past 3 days', t('Hit title found.'));
$this->assertText('admin/reports/referrers', t('Hit URL found.'));
}
}
/**
* Tests that the visitor blocking functionality works.
*/
class StatisticsBlockVisitorsTestCase extends StatisticsTestCase {
public static function getInfo() {
return array(
'name' => 'Top visitor blocking',
'description' => 'Tests blocking of IP addresses via the top visitors report.',
'group' => 'Statistics'
);
}
/**
* Blocks an IP address via the top visitors report and then unblocks it.
*/ */
function testIPAddressBlocking() { function testIPAddressBlocking() {
// IP address for testing. // IP address for testing.
$test_ip_address = '192.168.1.1'; $test_ip_address = '192.168.1.1';
// Enable access logging (redundant since we insert the data manually).
variable_set('statistics_enable_access_log', 1);
// Verify the IP address from accesslog appears on the top visitors page // Verify the IP address from accesslog appears on the top visitors page
// and that a 'block IP address' link is displayed. // and that a 'block IP address' link is displayed.
$this->drupalLogin($this->blocking_user); $this->drupalLogin($this->blocking_user);
...@@ -58,7 +132,8 @@ class StatisticsBlockVisitorsTestCase extends DrupalWebTestCase { ...@@ -58,7 +132,8 @@ class StatisticsBlockVisitorsTestCase extends DrupalWebTestCase {
$this->assertNotEqual($ip, FALSE, t('IP address found in database')); $this->assertNotEqual($ip, FALSE, t('IP address found in database'));
$this->assertRaw(t('The IP address %ip has been blocked.', array('%ip' => $edit['ip'])), t('IP address was blocked.')); $this->assertRaw(t('The IP address %ip has been blocked.', array('%ip' => $edit['ip'])), t('IP address was blocked.'));
// Verify that the block/unblock link on the top visitors page has been altered. // Verify that the block/unblock link on the top visitors page has been
// altered.
$this->drupalGet('admin/reports/visitors'); $this->drupalGet('admin/reports/visitors');
$this->assertText(t('unblock IP address'), t('Unblock IP address link displayed')); $this->assertText(t('unblock IP address'), t('Unblock IP address link displayed'));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment