Newer
Older
Dries Buytaert
committed
<?php
// $Id$
Dries Buytaert
committed
/**
* Sets up a base class for the Statistics module.
*/
class StatisticsTestCase extends DrupalWebTestCase {
Dries Buytaert
committed
function setUp() {
parent::setUp('statistics');
// Create user.
$this->blocking_user = $this->drupalCreateUser(array('block IP addresses', 'access statistics', 'administer blocks', 'administer statistics', 'administer users'));
$this->drupalLogin($this->blocking_user);
Dries Buytaert
committed
Dries Buytaert
committed
// Enable access logging.
variable_set('statistics_enable_access_log', 1);
variable_set('statistics_count_content_views', 1);
Dries Buytaert
committed
Dries Buytaert
committed
// Insert dummy access by anonymous user into access log.
Dries Buytaert
committed
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();
Dries Buytaert
committed
}
Dries Buytaert
committed
}
/**
* 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.'));
}
Dries Buytaert
committed
/**
Dries Buytaert
committed
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
* 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.'));
}
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
/**
* 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);
// Configure and save the block.
$block = block_load('statistics', 'popular');
$block->theme = 'garland';
$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[LANGUAGE_NONE][0]['value'], 'node/' . $node->nid), t('Found link to visited node.'));
}
Dries Buytaert
committed
}
/**
* 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.
Dries Buytaert
committed
*/
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.
Dries Buytaert
committed
$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'));
Dries Buytaert
committed
$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'));
Dries Buytaert
committed
$this->assertRaw(t('The IP address %ip has been blocked.', array('%ip' => $edit['ip'])), t('IP address was blocked.'));
Dries Buytaert
committed
// Verify that the block/unblock link on the top visitors page has been
// altered.
Dries Buytaert
committed
$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'));
Dries Buytaert
committed
$this->assertRaw(t('The IP address %ip was deleted.', array('%ip' => $test_ip_address)), t('IP address deleted.'));
}
}
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* Test statistics administration screen.
*/
class StatisticsAdminTestCase extends DrupalWebTestCase {
protected $privileged_user;
protected $test_node;
public static function getInfo() {
return array(
'name' => 'Test statistics admin.',
'description' => 'Tests the statistics admin.',
'group' => 'Statistics'
);
}
function setUp() {
parent::setUp('statistics');
$this->privileged_user = $this->drupalCreateUser(array('access statistics', 'administer statistics', 'view post access counter', 'create page content'));
$this->drupalLogin($this->privileged_user);
$this->test_node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $this->privileged_user->uid));
}
/**
* Verifies that the statistics settings page works.
*/
function testStatisticsSettings() {
$this->assertFalse(variable_get('statistics_enable_access_log', 0), t('Access log is disabled by default.'));
$this->assertFalse(variable_get('statistics_count_content_views', 0), t('Count content view log is disabled by default.'));
$this->drupalGet('admin/reports/pages');
$this->assertRaw(t('No statistics available.'), t('Verifying text shown when no statistics is available.'));
// Enable access log and counter on content view.
$edit['statistics_enable_access_log'] = 1;
$edit['statistics_count_content_views'] = 1;
$this->drupalPost('admin/config/system/statistics', $edit, t('Save configuration'));
$this->assertTrue(variable_get('statistics_enable_access_log'), t('Access log is enabled.'));
$this->assertTrue(variable_get('statistics_count_content_views'), t('Count content view log is enabled.'));
// Hit the node.
$this->drupalGet('node/' . $this->test_node->nid);
$this->drupalGet('admin/reports/pages');
$this->assertText('node/1', t('Test node found.'));
// Hit the node again (the counter is incremented after the hit, so
// "1 read" will actually be shown when the node is hit the second time).
$this->drupalGet('node/' . $this->test_node->nid);
$this->assertText('1 read', t('Node is read once.'));
$this->drupalGet('node/' . $this->test_node->nid);
$this->assertText('2 reads', t('Node is read 2 times.'));
}
/**
* Tests that when a node is deleted, the node counter is deleted too.
*/
function testDeleteNode() {
variable_set('statistics_count_content_views', 1);
$this->drupalGet('node/' . $this->test_node->nid);
$result = db_select('node_counter', 'n')
->fields('n', array('nid'))
->condition('n.nid', $this->test_node->nid)
->execute()
->fetchAssoc();
$this->assertEqual($result['nid'], $this->test_node->nid, 'Verifying that the node counter is incremented.');
node_delete($this->test_node->nid);
$result = db_select('node_counter', 'n')
->fields('n', array('nid'))
->condition('n.nid', $this->test_node->nid)
->execute()
->fetchAssoc();
$this->assertFalse($result, 'Verifying that the node counter is deleted.');
}
/**
* Tests that accesslog reflects when a user is deleted.
*/
function testDeleteUser() {
variable_set('statistics_enable_access_log', 1);
variable_set('user_cancel_method', 'user_cancel_delete');
$this->drupalLogout($this->privileged_user);
$account = $this->drupalCreateUser(array('access content', 'cancel account'));
$this->drupalLogin($account);
$this->drupalGet('node/' . $this->test_node->nid);
$account = user_load($account->uid, TRUE);
$this->drupalGet('user/' . $account->uid . '/edit');
$this->drupalPost(NULL, NULL, t('Cancel account'));
$timestamp = time();
$this->drupalPost(NULL, NULL, t('Cancel account'));
// Confirm account cancellation request.
$this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
$this->assertFalse(user_load($account->uid, TRUE), t('User is not found in the database.'));
$this->drupalGet('admin/reports/visitors');
$this->assertNoText($account->name, t('Did not find user in visitor statistics.'));
}
/**
* Tests that cron clears day counts and expired access logs.
*/
function testExpiredLogs() {
variable_set('statistics_enable_access_log', 1);
variable_set('statistics_count_content_views', 1);
variable_set('statistics_day_timestamp', 8640000);
variable_set('statistics_flush_accesslog_timer', 1);
$this->drupalGet('node/' . $this->test_node->nid);
$this->drupalGet('node/' . $this->test_node->nid);
$this->assertText('1 read', t('Node is read once.'));
$this->drupalGet('admin/reports/pages');
$this->assertText('node/' . $this->test_node->nid, t('Hit URL found.'));
// statistics_cron will subtract the statistics_flush_accesslog_timer
// variable from REQUEST_TIME in the delete query, so wait two secs here to
// make sure the access log will be flushed for the node just hit.
sleep(2);
$this->cronRun();
$this->drupalGet('admin/reports/pages');
$this->assertNoText('node/' . $this->test_node->nid, t('No hit URL found.'));
$result = db_select('node_counter', 'nc')
->fields('nc', array('daycount'))
->condition('nid', $this->test_node->nid, '=')
->execute()
->fetchField();
$this->assertFalse($result, t('Daycounter is zero.'));
}
}