| 1 |
<?php
|
| 2 |
// $Id: statistics.test,v 1.11 2009/08/22 09:44:55 dries Exp $
|
| 3 |
|
| 4 |
class StatisticsBlockVisitorsTestCase extends DrupalWebTestCase {
|
| 5 |
public static function getInfo() {
|
| 6 |
return array(
|
| 7 |
'name' => 'Top visitor blocking',
|
| 8 |
'description' => 'Tests blocking of IP addresses via the top visitors report.',
|
| 9 |
'group' => 'Statistics'
|
| 10 |
);
|
| 11 |
}
|
| 12 |
|
| 13 |
function setUp() {
|
| 14 |
parent::setUp('statistics');
|
| 15 |
|
| 16 |
// Create user.
|
| 17 |
$this->blocking_user = $this->drupalCreateUser(array('block IP addresses', 'access statistics'));
|
| 18 |
|
| 19 |
// Insert dummy access by anonymous user into access log.
|
| 20 |
db_insert('accesslog')
|
| 21 |
->fields(array(
|
| 22 |
'title' => 'test',
|
| 23 |
'path' => 'node/1',
|
| 24 |
'url' => 'http://example.com',
|
| 25 |
'hostname' => '192.168.1.1',
|
| 26 |
'uid' => 0,
|
| 27 |
'sid' => 10,
|
| 28 |
'timer' => 10,
|
| 29 |
'timestamp' => REQUEST_TIME,
|
| 30 |
))
|
| 31 |
->execute();
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Blocks an IP address via the top visitors report then uses the same page to unblock it.
|
| 36 |
*/
|
| 37 |
function testIPAddressBlocking() {
|
| 38 |
// IP address for testing.
|
| 39 |
$test_ip_address = '192.168.1.1';
|
| 40 |
|
| 41 |
// Enable access logging (redundant since we insert the data manually).
|
| 42 |
variable_set('statistics_enable_access_log', 1);
|
| 43 |
|
| 44 |
// Verify the IP address from accesslog appears on the top visitors page
|
| 45 |
// and that a 'block IP address' link is displayed.
|
| 46 |
$this->drupalLogin($this->blocking_user);
|
| 47 |
$this->drupalGet('admin/reports/visitors');
|
| 48 |
$this->assertText($test_ip_address, t('IP address found.'));
|
| 49 |
$this->assertText(t('block IP address'), t('Block IP link displayed'));
|
| 50 |
|
| 51 |
// Block the IP address.
|
| 52 |
$this->clickLink('block IP address');
|
| 53 |
$this->assertText(t('IP address blocking'), t('IP blocking page displayed.'));
|
| 54 |
$edit = array();
|
| 55 |
$edit['ip'] = $test_ip_address;
|
| 56 |
$this->drupalPost('admin/config/people/ip-blocking', $edit, t('Save'));
|
| 57 |
$ip = db_query("SELECT iid from {blocked_ips} WHERE ip = :ip", array(':ip' => $edit['ip']))->fetchField();
|
| 58 |
$this->assertNotEqual($ip, FALSE, t('IP address found in database'));
|
| 59 |
$this->assertRaw(t('The IP address %ip has been blocked.', array('%ip' => $edit['ip'])), t('IP address was blocked.'));
|
| 60 |
|
| 61 |
// Verify that the block/unblock link on the top visitors page has been altered.
|
| 62 |
$this->drupalGet('admin/reports/visitors');
|
| 63 |
$this->assertText(t('unblock IP address'), t('Unblock IP address link displayed'));
|
| 64 |
|
| 65 |
// Unblock the IP address.
|
| 66 |
$this->clickLink('unblock IP address');
|
| 67 |
$this->assertRaw(t('Are you sure you want to delete %ip?', array('%ip' => $test_ip_address)), t('IP address deletion confirmation found.'));
|
| 68 |
$edit = array();
|
| 69 |
$this->drupalPost('admin/config/people/ip-blocking/delete/1', NULL, t('Delete'));
|
| 70 |
$this->assertRaw(t('The IP address %ip was deleted.', array('%ip' => $test_ip_address)), t('IP address deleted.'));
|
| 71 |
}
|
| 72 |
}
|