Commit be5fb88f authored by catch's avatar catch
Browse files

Issue #1594134 by aspilicious: Convert statistics tests to PSR-0.

parent d6701448
......@@ -2,326 +2,17 @@
/**
* @file
* Tests for the Statistics module.
* Definition of Drupal\statistics\Tests\StatisticsAdminTest.
*/
use Drupal\simpletest\WebTestBase;
/**
* Defines a base class for testing the Statistics module.
*/
class StatisticsTestCase extends WebTestBase {
function setUp() {
parent::setUp(array('node', 'block', 'statistics'));
namespace Drupal\statistics\Tests;
// Create Basic page node type.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
}
// Create user.
$this->blocking_user = $this->drupalCreateUser(array(
'access administration pages',
'access site reports',
'access statistics',
'block IP addresses',
'administer blocks',
'administer statistics',
'administer users',
));
$this->drupalLogin($this->blocking_user);
// Enable access logging.
variable_set('statistics_enable_access_log', 1);
variable_set('statistics_count_content_views', 1);
// Insert dummy access by anonymous user into access log.
db_insert('accesslog')
->fields(array(
'title' => 'test',
'path' => 'node/1',
'url' => 'http://example.com',
'hostname' => '192.168.1.1',
'uid' => 0,
'sid' => 10,
'timer' => 10,
'timestamp' => REQUEST_TIME,
))
->execute();
}
}
/**
* Tests that logging via statistics_exit() works for all pages.
*
* We subclass WebTestBase rather than StatisticsTestCase, because we
* want to test requests from an anonymous user.
*/
class StatisticsLoggingTestCase extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Statistics logging tests',
'description' => 'Tests request logging for cached and uncached pages.',
'group' => 'Statistics'
);
}
function setUp() {
parent::setUp(array('statistics', 'block'));
// Create Basic page node type.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
}
$this->auth_user = $this->drupalCreateUser(array('access content', 'create page content', 'edit own page content'));
// Ensure we have a node page to access.
$this->node = $this->drupalCreateNode(array('title' => $this->randomName(255), 'uid' => $this->auth_user->uid));
// Enable page caching.
$config = config('system.performance');
$config->set('cache', 1);
$config->save();
// Enable access logging.
variable_set('statistics_enable_access_log', 1);
variable_set('statistics_count_content_views', 1);
// Clear the logs.
db_truncate('accesslog');
db_truncate('node_counter');
}
/**
* Verifies request logging for cached and uncached pages.
*/
function testLogging() {
$path = 'node/' . $this->node->nid;
$expected = array(
'title' => $this->node->title,
'path' => $path,
);
// Verify logging of an uncached page.
$this->drupalGet($path);
// Manually calling statistics.php, simulating ajax behavior.
$nid = $this->node->nid;
$post = http_build_query(array('nid' => $nid));
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
global $base_url;
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php';
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$this->assertIdentical($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', t('Testing an uncached page.'));
$log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
$this->assertTrue(is_array($log) && count($log) == 1, t('Page request was logged.'));
$this->assertEqual(array_intersect_key($log[0], $expected), $expected);
$node_counter = statistics_get($this->node->nid);
$this->assertIdentical($node_counter['totalcount'], '1');
// Verify logging of a cached page.
$this->drupalGet($path);
// Manually calling statistics.php, simulating ajax behavior.
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$this->assertIdentical($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Testing a cached page.'));
$log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
$this->assertTrue(is_array($log) && count($log) == 2, t('Page request was logged.'));
$this->assertEqual(array_intersect_key($log[1], $expected), $expected);
$node_counter = statistics_get($this->node->nid);
$this->assertIdentical($node_counter['totalcount'], '2');
// Test logging from authenticated users
$this->drupalLogin($this->auth_user);
$this->drupalGet($path);
// Manually calling statistics.php, simulating ajax behavior.
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
// Check the 6th item since login and account pages are also logged
$this->assertTrue(is_array($log) && count($log) == 6, t('Page request was logged.'));
$this->assertEqual(array_intersect_key($log[5], $expected), $expected);
$node_counter = statistics_get($this->node->nid);
$this->assertIdentical($node_counter['totalcount'], '3');
// Visit edit page to generate a title greater than 255.
$path = 'node/' . $this->node->nid . '/edit';
$expected = array(
'title' => truncate_utf8(t('Edit Basic page') . ' ' . $this->node->title, 255),
'path' => $path,
);
$this->drupalGet($path);
$log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
$this->assertTrue(is_array($log) && count($log) == 7, t('Page request was logged.'));
$this->assertEqual(array_intersect_key($log[6], $expected), $expected);
// Create a path longer than 255 characters. Drupal's .htaccess file
// instructs Apache to test paths against the file system before routing to
// index.php. Many file systems restrict file names to 255 characters
// (http://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits), and
// Apache returns a 403 when testing longer file names, but the total path
// length is not restricted.
$long_path = $this->randomName(127) . '/' . $this->randomName(128);
// Test that the long path is properly truncated when logged.
$this->drupalGet($long_path);
$log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
$this->assertTrue(is_array($log) && count($log) == 8, 'Page request was logged for a path over 255 characters.');
$this->assertEqual($log[7]['path'], truncate_utf8($long_path, 255));
}
}
/**
* 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->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.'));
}
/**
* Verifies that 'Top pages' renders properly and displays the added hit.
*/
function testTopPages() {
$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->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->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->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 the "popular content" block.
*/
function testPopularContentBlock() {
// Visit a node to have something show up in the block.
$node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $this->blocking_user->uid));
$this->drupalGet('node/' . $node->nid);
// Manually calling statistics.php, simulating ajax behavior.
$nid = $node->nid;
$post = http_build_query(array('nid' => $nid));
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
global $base_url;
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php';
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
// Configure and save the block.
$block = block_load('statistics', 'popular');
$block->theme = variable_get('theme_default', 'stark');
$block->status = 1;
$block->pages = '';
$block->region = 'sidebar_first';
$block->cache = -1;
$block->visibility = 0;
$edit = array('statistics_block_top_day_num' => 3, 'statistics_block_top_all_num' => 3, 'statistics_block_top_last_num' => 3);
module_invoke('statistics', 'block_save', 'popular', $edit);
drupal_write_record('block', $block);
// Get some page and check if the block is displayed.
$this->drupalGet('user');
$this->assertText('Popular content', t('Found the popular content block.'));
$this->assertText("Today's", t('Found today\'s popular content.'));
$this->assertText('All time', t('Found the alll time popular content.'));
$this->assertText('Last viewed', t('Found the last viewed popular content.'));
$this->assertRaw(l($node->title, 'node/' . $node->nid), t('Found link to visited node.'));
}
}
/**
* 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() {
// IP address for testing.
$test_ip_address = '192.168.1.1';
// Verify the IP address from accesslog appears on the top visitors page
// and that a 'block IP address' link is displayed.
$this->drupalLogin($this->blocking_user);
$this->drupalGet('admin/reports/visitors');
$this->assertText($test_ip_address, t('IP address found.'));
$this->assertText(t('block IP address'), t('Block IP link displayed'));
// Block the IP address.
$this->clickLink('block IP address');
$this->assertText(t('IP address blocking'), t('IP blocking page displayed.'));
$edit = array();
$edit['ip'] = $test_ip_address;
$this->drupalPost('admin/config/people/ip-blocking', $edit, t('Add'));
$ip = db_query("SELECT iid from {blocked_ips} WHERE ip = :ip", array(':ip' => $edit['ip']))->fetchField();
$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.'));
// Verify that the block/unblock link on the top visitors page has been
// altered.
$this->drupalGet('admin/reports/visitors');
$this->assertText(t('unblock IP address'), t('Unblock IP address link displayed'));
// Unblock the IP address.
$this->clickLink('unblock IP address');
$this->assertRaw(t('Are you sure you want to delete %ip?', array('%ip' => $test_ip_address)), t('IP address deletion confirmation found.'));
$edit = array();
$this->drupalPost('admin/config/people/ip-blocking/delete/1', NULL, t('Delete'));
$this->assertRaw(t('The IP address %ip was deleted.', array('%ip' => $test_ip_address)), t('IP address deleted.'));
}
}
use Drupal\simpletest\WebTestBase;
/**
* Tests the statistics administration screen.
*/
class StatisticsAdminTestCase extends WebTestBase {
class StatisticsAdminTest extends WebTestBase {
/**
* A user that has permission to administer and access statistics.
......@@ -504,54 +195,3 @@ function testExpiredLogs() {
$this->assertFalse($result, t('Daycounter is zero.'));
}
}
/**
* Tests statistics token replacement in strings.
*/
class StatisticsTokenReplaceTestCase extends StatisticsTestCase {
public static function getInfo() {
return array(
'name' => 'Statistics token replacement',
'description' => 'Generates text using placeholders for dummy content to check statistics token replacement.',
'group' => 'Statistics',
);
}
/**
* Creates a node, then tests the statistics tokens generated from it.
*/
function testStatisticsTokenReplacement() {
global $language_interface;
// Create user and node.
$user = $this->drupalCreateUser(array('create page content'));
$this->drupalLogin($user);
$node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $user->uid));
// Hit the node.
$this->drupalGet('node/' . $node->nid);
// Manually calling statistics.php, simulating ajax behavior.
$nid = $node->nid;
$post = http_build_query(array('nid' => $nid));
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
global $base_url;
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php';
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$statistics = statistics_get($node->nid);
// Generate and test tokens.
$tests = array();
$tests['[node:total-count]'] = 1;
$tests['[node:day-count]'] = 1;
$tests['[node:last-view]'] = format_date($statistics['timestamp']);
$tests['[node:last-view:short]'] = format_date($statistics['timestamp'], 'short');
// Test to make sure that we generated something for each token.
$this->assertFalse(in_array(0, array_map('strlen', $tests)), t('No empty tokens generated.'));
foreach ($tests as $input => $expected) {
$output = token_replace($input, array('node' => $node), array('language' => $language_interface));
$this->assertEqual($output, $expected, t('Statistics token %token replaced.', array('%token' => $input)));
}
}
}
<?php
/**
* @file
* Definition of Drupal\statistics\Tests\StatisticsBlockVisitorsTest.
*/
namespace Drupal\statistics\Tests;
/**
* Tests that the visitor blocking functionality works.
*/
class StatisticsBlockVisitorsTest extends StatisticsTestBase {
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() {
// IP address for testing.
$test_ip_address = '192.168.1.1';
// Verify the IP address from accesslog appears on the top visitors page
// and that a 'block IP address' link is displayed.
$this->drupalLogin($this->blocking_user);
$this->drupalGet('admin/reports/visitors');
$this->assertText($test_ip_address, t('IP address found.'));
$this->assertText(t('block IP address'), t('Block IP link displayed'));
// Block the IP address.
$this->clickLink('block IP address');
$this->assertText(t('IP address blocking'), t('IP blocking page displayed.'));
$edit = array();
$edit['ip'] = $test_ip_address;
$this->drupalPost('admin/config/people/ip-blocking', $edit, t('Add'));
$ip = db_query("SELECT iid from {blocked_ips} WHERE ip = :ip", array(':ip' => $edit['ip']))->fetchField();
$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.'));
// Verify that the block/unblock link on the top visitors page has been
// altered.
$this->drupalGet('admin/reports/visitors');
$this->assertText(t('unblock IP address'), t('Unblock IP address link displayed'));
// Unblock the IP address.
$this->clickLink('unblock IP address');
$this->assertRaw(t('Are you sure you want to delete %ip?', array('%ip' => $test_ip_address)), t('IP address deletion confirmation found.'));
$edit = array();
$this->drupalPost('admin/config/people/ip-blocking/delete/1', NULL, t('Delete'));
$this->assertRaw(t('The IP address %ip was deleted.', array('%ip' => $test_ip_address)), t('IP address deleted.'));
}
}
<?php
/**
* @file
* Definition of Drupal\statistics\Tests\StatisticsLoggingTest.
*/
namespace Drupal\statistics\Tests;
use Drupal\simpletest\WebTestBase;
use PDO;
/**
* Tests that logging via statistics_exit() works for all pages.
*
* We subclass WebTestBase rather than StatisticsTestBase, because we
* want to test requests from an anonymous user.
*/
class StatisticsLoggingTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Statistics logging tests',
'description' => 'Tests request logging for cached and uncached pages.',
'group' => 'Statistics'
);
}
function setUp() {
parent::setUp(array('statistics', 'block'));
// Create Basic page node type.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
}
$this->auth_user = $this->drupalCreateUser(array('access content', 'create page content', 'edit own page content'));
// Ensure we have a node page to access.
$this->node = $this->drupalCreateNode(array('title' => $this->randomName(255), 'uid' => $this->auth_user->uid));
// Enable page caching.
$config = config('system.performance');
$config->set('cache', 1);
$config->save();
// Enable access logging.
variable_set('statistics_enable_access_log', 1);
variable_set('statistics_count_content_views', 1);
// Clear the logs.
db_truncate('accesslog');
db_truncate('node_counter');
}
/**
* Verifies request logging for cached and uncached pages.
*/
function testLogging() {
$path = 'node/' . $this->node->nid;
$expected = array(
'title' => $this->node->title,
'path' => $path,
);
// Verify logging of an uncached page.
$this->drupalGet($path);
// Manually calling statistics.php, simulating ajax behavior.
$nid = $this->node->nid;
$post = http_build_query(array('nid' => $nid));
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
global $base_url;
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php';
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$this->assertIdentical($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', t('Testing an uncached page.'));
$log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
$this->assertTrue(is_array($log) && count($log) == 1, t('Page request was logged.'));
$this->assertEqual(array_intersect_key($log[0], $expected), $expected);
$node_counter = statistics_get($this->node->nid);
$this->assertIdentical($node_counter['totalcount'], '1');
// Verify logging of a cached page.
$this->drupalGet($path);
// Manually calling statistics.php, simulating ajax behavior.
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$this->assertIdentical($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', t('Testing a cached page.'));
$log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
$this->assertTrue(is_array($log) && count($log) == 2, t('Page request was logged.'));
$this->assertEqual(array_intersect_key($log[1], $expected), $expected);
$node_counter = statistics_get($this->node->nid);
$this->assertIdentical($node_counter['totalcount'], '2');
// Test logging from authenticated users
$this->drupalLogin($this->auth_user);
$this->drupalGet($path);
// Manually calling statistics.php, simulating ajax behavior.
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
// Check the 6th item since login and account pages are also logged
$this->assertTrue(is_array($log) && count($log) == 6, t('Page request was logged.'));
$this->assertEqual(array_intersect_key($log[5], $expected), $expected);
$node_counter = statistics_get($this->node->nid);
$this->assertIdentical($node_counter['totalcount'], '3');
// Visit edit page to generate a title greater than 255.
$path = 'node/' . $this->node->nid . '/edit';
$expected = array(
'title' => truncate_utf8(t('Edit Basic page') . ' ' . $this->node->title, 255),
'path' => $path,
);
$this->drupalGet($path);
$log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
$this->assertTrue(is_array($log) && count($log) == 7, t('Page request was logged.'));
$this->assertEqual(array_intersect_key($log[6], $expected), $expected);
// Create a path longer than 255 characters. Drupal's .htaccess file
// instructs Apache to test paths against the file system before routing to
// index.php. Many file systems restrict file names to 255 characters
// (http://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits), and
// Apache returns a 403 when testing longer file names, but the total path
// length is not restricted.
$long_path = $this->randomName(127) . '/' . $this->randomName(128);
// Test that the long path is properly truncated when logged.
$this->drupalGet($long_path);
$log = db_query('SELECT * FROM {accesslog}')->fetchAll(PDO::FETCH_ASSOC);
$this->assertTrue(is_array($log) && count($log) == 8, 'Page request was logged for a path over 255 characters.');
$this->assertEqual($log[7]['path'], truncate_utf8($long_path, 255));
}
}