1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
<?php
// $Id$
/**
* Sets up a base class for the Statistics module.
*/
class StatisticsTestCase extends DrupalWebTestCase {
function setUp() {
parent::setUp('statistics');
// Create user.
$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.
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 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.'));
}
/**
* 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() {
// 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('Save'));
$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.'));
}
}
|