1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests for dblog.module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Tests logging messages to the database.
|
10
|
*/
|
11
|
class DBLogTestCase extends DrupalWebTestCase {
|
12
|
|
13
|
/**
|
14
|
* A user with some relevant administrative permissions.
|
15
|
*
|
16
|
* @var object
|
17
|
*/
|
18
|
protected $big_user;
|
19
|
|
20
|
/**
|
21
|
* A user without any permissions.
|
22
|
*
|
23
|
* @var object
|
24
|
*/
|
25
|
protected $any_user;
|
26
|
|
27
|
public static function getInfo() {
|
28
|
return array(
|
29
|
'name' => 'DBLog functionality',
|
30
|
'description' => 'Generate events and verify dblog entries; verify user access to log reports based on persmissions.',
|
31
|
'group' => 'DBLog',
|
32
|
);
|
33
|
}
|
34
|
|
35
|
/**
|
36
|
* Enable modules and create users with specific permissions.
|
37
|
*/
|
38
|
function setUp() {
|
39
|
parent::setUp('dblog', 'blog', 'poll');
|
40
|
// Create users.
|
41
|
$this->big_user = $this->drupalCreateUser(array('administer site configuration', 'access administration pages', 'access site reports', 'administer users'));
|
42
|
$this->any_user = $this->drupalCreateUser(array());
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* Tests Database Logging module functionality through interfaces.
|
47
|
*
|
48
|
* First logs in users, then creates database log events, and finally tests
|
49
|
* Database Logging module functionality through both the admin and user
|
50
|
* interfaces.
|
51
|
*/
|
52
|
function testDBLog() {
|
53
|
// Login the admin user.
|
54
|
$this->drupalLogin($this->big_user);
|
55
|
|
56
|
$row_limit = 100;
|
57
|
$this->verifyRowLimit($row_limit);
|
58
|
$this->verifyCron($row_limit);
|
59
|
$this->verifyEvents();
|
60
|
$this->verifyReports();
|
61
|
|
62
|
// Login the regular user.
|
63
|
$this->drupalLogin($this->any_user);
|
64
|
$this->verifyReports(403);
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Verifies setting of the database log row limit.
|
69
|
*
|
70
|
* @param int $row_limit
|
71
|
* The row limit.
|
72
|
*/
|
73
|
private function verifyRowLimit($row_limit) {
|
74
|
// Change the database log row limit.
|
75
|
$edit = array();
|
76
|
$edit['dblog_row_limit'] = $row_limit;
|
77
|
$this->drupalPost('admin/config/development/logging', $edit, t('Save configuration'));
|
78
|
$this->assertResponse(200);
|
79
|
|
80
|
// Check row limit variable.
|
81
|
$current_limit = variable_get('dblog_row_limit', 1000);
|
82
|
$this->assertTrue($current_limit == $row_limit, format_string('[Cache] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit)));
|
83
|
// Verify dblog row limit equals specified row limit.
|
84
|
$current_limit = unserialize(db_query("SELECT value FROM {variable} WHERE name = :dblog_limit", array(':dblog_limit' => 'dblog_row_limit'))->fetchField());
|
85
|
$this->assertTrue($current_limit == $row_limit, format_string('[Variable table] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit)));
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Verifies that cron correctly applies the database log row limit.
|
90
|
*
|
91
|
* @param int $row_limit
|
92
|
* The row limit.
|
93
|
*/
|
94
|
private function verifyCron($row_limit) {
|
95
|
// Generate additional log entries.
|
96
|
$this->generateLogEntries($row_limit + 10);
|
97
|
// Verify that the database log row count exceeds the row limit.
|
98
|
$count = db_query('SELECT COUNT(wid) FROM {watchdog}')->fetchField();
|
99
|
$this->assertTrue($count > $row_limit, format_string('Dblog row count of @count exceeds row limit of @limit', array('@count' => $count, '@limit' => $row_limit)));
|
100
|
|
101
|
// Run a cron job.
|
102
|
$this->cronRun();
|
103
|
// Verify that the database log row count equals the row limit plus one
|
104
|
// because cron adds a record after it runs.
|
105
|
$count = db_query('SELECT COUNT(wid) FROM {watchdog}')->fetchField();
|
106
|
$this->assertTrue($count == $row_limit + 1, format_string('Dblog row count of @count equals row limit of @limit plus one', array('@count' => $count, '@limit' => $row_limit)));
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Generates a number of random database log events.
|
111
|
*
|
112
|
* @param int $count
|
113
|
* Number of watchdog entries to generate.
|
114
|
* @param string $type
|
115
|
* (optional) The type of watchdog entry. Defaults to 'custom'.
|
116
|
* @param int $severity
|
117
|
* (optional) The severity of the watchdog entry. Defaults to WATCHDOG_NOTICE.
|
118
|
*/
|
119
|
private function generateLogEntries($count, $type = 'custom', $severity = WATCHDOG_NOTICE) {
|
120
|
global $base_root;
|
121
|
|
122
|
// Prepare the fields to be logged
|
123
|
$log = array(
|
124
|
'type' => $type,
|
125
|
'message' => 'Log entry added to test the dblog row limit.',
|
126
|
'variables' => array(),
|
127
|
'severity' => $severity,
|
128
|
'link' => NULL,
|
129
|
'user' => $this->big_user,
|
130
|
'uid' => isset($this->big_user->uid) ? $this->big_user->uid : 0,
|
131
|
'request_uri' => $base_root . request_uri(),
|
132
|
'referer' => $_SERVER['HTTP_REFERER'],
|
133
|
'ip' => ip_address(),
|
134
|
'timestamp' => REQUEST_TIME,
|
135
|
);
|
136
|
$message = 'Log entry added to test the dblog row limit. Entry #';
|
137
|
for ($i = 0; $i < $count; $i++) {
|
138
|
$log['message'] = $message . $i;
|
139
|
dblog_watchdog($log);
|
140
|
}
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Confirms that database log reports are displayed at the correct paths.
|
145
|
*
|
146
|
* @param int $response
|
147
|
* (optional) HTTP response code. Defaults to 200.
|
148
|
*/
|
149
|
private function verifyReports($response = 200) {
|
150
|
$quote = ''';
|
151
|
|
152
|
// View the database log help page.
|
153
|
$this->drupalGet('admin/help/dblog');
|
154
|
$this->assertResponse($response);
|
155
|
if ($response == 200) {
|
156
|
$this->assertText(t('Database logging'), 'DBLog help was displayed');
|
157
|
}
|
158
|
|
159
|
// View the database log report page.
|
160
|
$this->drupalGet('admin/reports/dblog');
|
161
|
$this->assertResponse($response);
|
162
|
if ($response == 200) {
|
163
|
$this->assertText(t('Recent log messages'), 'DBLog report was displayed');
|
164
|
}
|
165
|
|
166
|
// View the database log page-not-found report page.
|
167
|
$this->drupalGet('admin/reports/page-not-found');
|
168
|
$this->assertResponse($response);
|
169
|
if ($response == 200) {
|
170
|
$this->assertText(t('Top ' . $quote . 'page not found' . $quote . ' errors'), 'DBLog page-not-found report was displayed');
|
171
|
}
|
172
|
|
173
|
// View the database log access-denied report page.
|
174
|
$this->drupalGet('admin/reports/access-denied');
|
175
|
$this->assertResponse($response);
|
176
|
if ($response == 200) {
|
177
|
$this->assertText(t('Top ' . $quote . 'access denied' . $quote . ' errors'), 'DBLog access-denied report was displayed');
|
178
|
}
|
179
|
|
180
|
// View the database log event page.
|
181
|
$this->drupalGet('admin/reports/event/1');
|
182
|
$this->assertResponse($response);
|
183
|
if ($response == 200) {
|
184
|
$this->assertText(t('Details'), 'DBLog event node was displayed');
|
185
|
}
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* Generates and then verifies various types of events.
|
190
|
*/
|
191
|
private function verifyEvents() {
|
192
|
// Invoke events.
|
193
|
$this->doUser();
|
194
|
$this->doNode('article');
|
195
|
$this->doNode('blog');
|
196
|
$this->doNode('page');
|
197
|
$this->doNode('poll');
|
198
|
|
199
|
// When a user account is canceled, any content they created remains but the
|
200
|
// uid = 0. Their blog entry shows as "'s blog" on the home page. Records
|
201
|
// in the watchdog table related to that user have the uid set to zero.
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* Generates and then verifies some user events.
|
206
|
*/
|
207
|
private function doUser() {
|
208
|
// Set user variables.
|
209
|
$name = $this->randomName();
|
210
|
$pass = user_password();
|
211
|
// Add a user using the form to generate an add user event (which is not
|
212
|
// triggered by drupalCreateUser).
|
213
|
$edit = array();
|
214
|
$edit['name'] = $name;
|
215
|
$edit['mail'] = $name . '@example.com';
|
216
|
$edit['pass[pass1]'] = $pass;
|
217
|
$edit['pass[pass2]'] = $pass;
|
218
|
$edit['status'] = 1;
|
219
|
$this->drupalPost('admin/people/create', $edit, t('Create new account'));
|
220
|
$this->assertResponse(200);
|
221
|
// Retrieve the user object.
|
222
|
$user = user_load_by_name($name);
|
223
|
$this->assertTrue($user != NULL, format_string('User @name was loaded', array('@name' => $name)));
|
224
|
// pass_raw property is needed by drupalLogin.
|
225
|
$user->pass_raw = $pass;
|
226
|
// Login user.
|
227
|
$this->drupalLogin($user);
|
228
|
// Logout user.
|
229
|
$this->drupalLogout();
|
230
|
// Fetch the row IDs in watchdog that relate to the user.
|
231
|
$result = db_query('SELECT wid FROM {watchdog} WHERE uid = :uid', array(':uid' => $user->uid));
|
232
|
foreach ($result as $row) {
|
233
|
$ids[] = $row->wid;
|
234
|
}
|
235
|
$count_before = (isset($ids)) ? count($ids) : 0;
|
236
|
$this->assertTrue($count_before > 0, format_string('DBLog contains @count records for @name', array('@count' => $count_before, '@name' => $user->name)));
|
237
|
|
238
|
// Login the admin user.
|
239
|
$this->drupalLogin($this->big_user);
|
240
|
// Delete the user created at the start of this test.
|
241
|
// We need to POST here to invoke batch_process() in the internal browser.
|
242
|
$this->drupalPost('user/' . $user->uid . '/cancel', array('user_cancel_method' => 'user_cancel_reassign'), t('Cancel account'));
|
243
|
|
244
|
// View the database log report.
|
245
|
$this->drupalGet('admin/reports/dblog');
|
246
|
$this->assertResponse(200);
|
247
|
|
248
|
// Verify that the expected events were recorded.
|
249
|
// Add user.
|
250
|
// Default display includes name and email address; if too long, the email
|
251
|
// address is replaced by three periods.
|
252
|
$this->assertLogMessage(t('New user: %name (%email).', array('%name' => $name, '%email' => $user->mail)), 'DBLog event was recorded: [add user]');
|
253
|
// Login user.
|
254
|
$this->assertLogMessage(t('Session opened for %name.', array('%name' => $name)), 'DBLog event was recorded: [login user]');
|
255
|
// Logout user.
|
256
|
$this->assertLogMessage(t('Session closed for %name.', array('%name' => $name)), 'DBLog event was recorded: [logout user]');
|
257
|
// Delete user.
|
258
|
$message = t('Deleted user: %name %email.', array('%name' => $name, '%email' => '<' . $user->mail . '>'));
|
259
|
$message_text = truncate_utf8(filter_xss($message, array()), 56, TRUE, TRUE);
|
260
|
// Verify that the full message displays on the details page.
|
261
|
$link = FALSE;
|
262
|
if ($links = $this->xpath('//a[text()="' . html_entity_decode($message_text) . '"]')) {
|
263
|
// Found link with the message text.
|
264
|
$links = array_shift($links);
|
265
|
foreach ($links->attributes() as $attr => $value) {
|
266
|
if ($attr == 'href') {
|
267
|
// Extract link to details page.
|
268
|
$link = drupal_substr($value, strpos($value, 'admin/reports/event/'));
|
269
|
$this->drupalGet($link);
|
270
|
// Check for full message text on the details page.
|
271
|
$this->assertRaw($message, 'DBLog event details was found: [delete user]');
|
272
|
break;
|
273
|
}
|
274
|
}
|
275
|
}
|
276
|
$this->assertTrue($link, 'DBLog event was recorded: [delete user]');
|
277
|
// Visit random URL (to generate page not found event).
|
278
|
$not_found_url = $this->randomName(60);
|
279
|
$this->drupalGet($not_found_url);
|
280
|
$this->assertResponse(404);
|
281
|
// View the database log page-not-found report page.
|
282
|
$this->drupalGet('admin/reports/page-not-found');
|
283
|
$this->assertResponse(200);
|
284
|
// Check that full-length URL displayed.
|
285
|
$this->assertText($not_found_url, 'DBLog event was recorded: [page not found]');
|
286
|
}
|
287
|
|
288
|
/**
|
289
|
* Generates and then verifies some node events.
|
290
|
*
|
291
|
* @param string $type
|
292
|
* A node type (e.g., 'article', 'page' or 'poll').
|
293
|
*/
|
294
|
private function doNode($type) {
|
295
|
// Create user.
|
296
|
$perm = array('create ' . $type . ' content', 'edit own ' . $type . ' content', 'delete own ' . $type . ' content');
|
297
|
$user = $this->drupalCreateUser($perm);
|
298
|
// Login user.
|
299
|
$this->drupalLogin($user);
|
300
|
|
301
|
// Create a node using the form in order to generate an add content event
|
302
|
// (which is not triggered by drupalCreateNode).
|
303
|
$edit = $this->getContent($type);
|
304
|
$langcode = LANGUAGE_NONE;
|
305
|
$title = $edit["title"];
|
306
|
$this->drupalPost('node/add/' . $type, $edit, t('Save'));
|
307
|
$this->assertResponse(200);
|
308
|
// Retrieve the node object.
|
309
|
$node = $this->drupalGetNodeByTitle($title);
|
310
|
$this->assertTrue($node != NULL, format_string('Node @title was loaded', array('@title' => $title)));
|
311
|
// Edit the node.
|
312
|
$edit = $this->getContentUpdate($type);
|
313
|
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
314
|
$this->assertResponse(200);
|
315
|
// Delete the node.
|
316
|
$this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
|
317
|
$this->assertResponse(200);
|
318
|
// View the node (to generate page not found event).
|
319
|
$this->drupalGet('node/' . $node->nid);
|
320
|
$this->assertResponse(404);
|
321
|
// View the database log report (to generate access denied event).
|
322
|
$this->drupalGet('admin/reports/dblog');
|
323
|
$this->assertResponse(403);
|
324
|
|
325
|
// Login the admin user.
|
326
|
$this->drupalLogin($this->big_user);
|
327
|
// View the database log report.
|
328
|
$this->drupalGet('admin/reports/dblog');
|
329
|
$this->assertResponse(200);
|
330
|
|
331
|
// Verify that node events were recorded.
|
332
|
// Was node content added?
|
333
|
$this->assertLogMessage(t('@type: added %title.', array('@type' => $type, '%title' => $title)), 'DBLog event was recorded: [content added]');
|
334
|
// Was node content updated?
|
335
|
$this->assertLogMessage(t('@type: updated %title.', array('@type' => $type, '%title' => $title)), 'DBLog event was recorded: [content updated]');
|
336
|
// Was node content deleted?
|
337
|
$this->assertLogMessage(t('@type: deleted %title.', array('@type' => $type, '%title' => $title)), 'DBLog event was recorded: [content deleted]');
|
338
|
|
339
|
// View the database log access-denied report page.
|
340
|
$this->drupalGet('admin/reports/access-denied');
|
341
|
$this->assertResponse(200);
|
342
|
// Verify that the 'access denied' event was recorded.
|
343
|
$this->assertText(t('admin/reports/dblog'), 'DBLog event was recorded: [access denied]');
|
344
|
|
345
|
// View the database log page-not-found report page.
|
346
|
$this->drupalGet('admin/reports/page-not-found');
|
347
|
$this->assertResponse(200);
|
348
|
// Verify that the 'page not found' event was recorded.
|
349
|
$this->assertText(t('node/@nid', array('@nid' => $node->nid)), 'DBLog event was recorded: [page not found]');
|
350
|
}
|
351
|
|
352
|
/**
|
353
|
* Creates random content based on node content type.
|
354
|
*
|
355
|
* @param string $type
|
356
|
* Node content type (e.g., 'article').
|
357
|
*
|
358
|
* @return array
|
359
|
* Random content needed by various node types.
|
360
|
*/
|
361
|
private function getContent($type) {
|
362
|
$langcode = LANGUAGE_NONE;
|
363
|
switch ($type) {
|
364
|
case 'poll':
|
365
|
$content = array(
|
366
|
"title" => $this->randomName(8),
|
367
|
'choice[new:0][chtext]' => $this->randomName(32),
|
368
|
'choice[new:1][chtext]' => $this->randomName(32),
|
369
|
);
|
370
|
break;
|
371
|
|
372
|
default:
|
373
|
$content = array(
|
374
|
"title" => $this->randomName(8),
|
375
|
"body[$langcode][0][value]" => $this->randomName(32),
|
376
|
);
|
377
|
break;
|
378
|
}
|
379
|
return $content;
|
380
|
}
|
381
|
|
382
|
/**
|
383
|
* Creates random content as an update based on node content type.
|
384
|
*
|
385
|
* @param string $type
|
386
|
* Node content type (e.g., 'article').
|
387
|
*
|
388
|
* @return array
|
389
|
* Random content needed by various node types.
|
390
|
*/
|
391
|
private function getContentUpdate($type) {
|
392
|
switch ($type) {
|
393
|
case 'poll':
|
394
|
$content = array(
|
395
|
'choice[chid:1][chtext]' => $this->randomName(32),
|
396
|
'choice[chid:2][chtext]' => $this->randomName(32),
|
397
|
);
|
398
|
break;
|
399
|
|
400
|
default:
|
401
|
$langcode = LANGUAGE_NONE;
|
402
|
$content = array(
|
403
|
"body[$langcode][0][value]" => $this->randomName(32),
|
404
|
);
|
405
|
break;
|
406
|
}
|
407
|
return $content;
|
408
|
}
|
409
|
|
410
|
/**
|
411
|
* Tests the addition and clearing of log events through the admin interface.
|
412
|
*
|
413
|
* Logs in the admin user, creates a database log event, and tests the
|
414
|
* functionality of clearing the database log through the admin interface.
|
415
|
*/
|
416
|
protected function testDBLogAddAndClear() {
|
417
|
global $base_root;
|
418
|
// Get a count of how many watchdog entries already exist.
|
419
|
$count = db_query('SELECT COUNT(*) FROM {watchdog}')->fetchField();
|
420
|
$log = array(
|
421
|
'type' => 'custom',
|
422
|
'message' => 'Log entry added to test the doClearTest clear down.',
|
423
|
'variables' => array(),
|
424
|
'severity' => WATCHDOG_NOTICE,
|
425
|
'link' => NULL,
|
426
|
'user' => $this->big_user,
|
427
|
'uid' => isset($this->big_user->uid) ? $this->big_user->uid : 0,
|
428
|
'request_uri' => $base_root . request_uri(),
|
429
|
'referer' => $_SERVER['HTTP_REFERER'],
|
430
|
'ip' => ip_address(),
|
431
|
'timestamp' => REQUEST_TIME,
|
432
|
);
|
433
|
// Add a watchdog entry.
|
434
|
dblog_watchdog($log);
|
435
|
// Make sure the table count has actually been incremented.
|
436
|
$this->assertEqual($count + 1, db_query('SELECT COUNT(*) FROM {watchdog}')->fetchField(), format_string('dblog_watchdog() added an entry to the dblog :count', array(':count' => $count)));
|
437
|
// Login the admin user.
|
438
|
$this->drupalLogin($this->big_user);
|
439
|
// Post in order to clear the database table.
|
440
|
$this->drupalPost('admin/reports/dblog', array(), t('Clear log messages'));
|
441
|
// Count the rows in watchdog that previously related to the deleted user.
|
442
|
$count = db_query('SELECT COUNT(*) FROM {watchdog}')->fetchField();
|
443
|
$this->assertEqual($count, 0, format_string('DBLog contains :count records after a clear.', array(':count' => $count)));
|
444
|
}
|
445
|
|
446
|
/**
|
447
|
* Tests the database log filter functionality at admin/reports/dblog.
|
448
|
*/
|
449
|
protected function testFilter() {
|
450
|
$this->drupalLogin($this->big_user);
|
451
|
|
452
|
// Clear the log to ensure that only generated entries will be found.
|
453
|
db_delete('watchdog')->execute();
|
454
|
|
455
|
// Generate 9 random watchdog entries.
|
456
|
$type_names = array();
|
457
|
$types = array();
|
458
|
for ($i = 0; $i < 3; $i++) {
|
459
|
$type_names[] = $type_name = $this->randomName();
|
460
|
$severity = WATCHDOG_EMERGENCY;
|
461
|
for ($j = 0; $j < 3; $j++) {
|
462
|
$types[] = $type = array(
|
463
|
'count' => $j + 1,
|
464
|
'type' => $type_name,
|
465
|
'severity' => $severity++,
|
466
|
);
|
467
|
$this->generateLogEntries($type['count'], $type['type'], $type['severity']);
|
468
|
}
|
469
|
}
|
470
|
|
471
|
// View the database log page.
|
472
|
$this->drupalGet('admin/reports/dblog');
|
473
|
|
474
|
// Confirm that all the entries are displayed.
|
475
|
$count = $this->getTypeCount($types);
|
476
|
foreach ($types as $key => $type) {
|
477
|
$this->assertEqual($count[$key], $type['count'], 'Count matched');
|
478
|
}
|
479
|
|
480
|
// Filter by each type and confirm that entries with various severities are
|
481
|
// displayed.
|
482
|
foreach ($type_names as $type_name) {
|
483
|
$edit = array(
|
484
|
'type[]' => array($type_name),
|
485
|
);
|
486
|
$this->drupalPost(NULL, $edit, t('Filter'));
|
487
|
|
488
|
// Count the number of entries of this type.
|
489
|
$type_count = 0;
|
490
|
foreach ($types as $type) {
|
491
|
if ($type['type'] == $type_name) {
|
492
|
$type_count += $type['count'];
|
493
|
}
|
494
|
}
|
495
|
|
496
|
$count = $this->getTypeCount($types);
|
497
|
$this->assertEqual(array_sum($count), $type_count, 'Count matched');
|
498
|
}
|
499
|
|
500
|
// Set the filter to match each of the two filter-type attributes and
|
501
|
// confirm the correct number of entries are displayed.
|
502
|
foreach ($types as $key => $type) {
|
503
|
$edit = array(
|
504
|
'type[]' => array($type['type']),
|
505
|
'severity[]' => array($type['severity']),
|
506
|
);
|
507
|
$this->drupalPost(NULL, $edit, t('Filter'));
|
508
|
|
509
|
$count = $this->getTypeCount($types);
|
510
|
$this->assertEqual(array_sum($count), $type['count'], 'Count matched');
|
511
|
}
|
512
|
|
513
|
// Clear all logs and make sure the confirmation message is found.
|
514
|
$this->drupalPost('admin/reports/dblog', array(), t('Clear log messages'));
|
515
|
$this->assertText(t('Database log cleared.'), 'Confirmation message found');
|
516
|
}
|
517
|
|
518
|
/**
|
519
|
* Gets the database log event information from the browser page.
|
520
|
*
|
521
|
* @return array
|
522
|
* List of log events where each event is an array with following keys:
|
523
|
* - severity: (int) A database log severity constant.
|
524
|
* - type: (string) The type of database log event.
|
525
|
* - message: (string) The message for this database log event.
|
526
|
* - user: (string) The user associated with this database log event.
|
527
|
*/
|
528
|
protected function getLogEntries() {
|
529
|
$entries = array();
|
530
|
if ($table = $this->xpath('.//table[@id="admin-dblog"]')) {
|
531
|
$table = array_shift($table);
|
532
|
foreach ($table->tbody->tr as $row) {
|
533
|
$entries[] = array(
|
534
|
'severity' => $this->getSeverityConstant($row['class']),
|
535
|
'type' => $this->asText($row->td[1]),
|
536
|
'message' => $this->asText($row->td[3]),
|
537
|
'user' => $this->asText($row->td[4]),
|
538
|
);
|
539
|
}
|
540
|
}
|
541
|
return $entries;
|
542
|
}
|
543
|
|
544
|
/**
|
545
|
* Gets the count of database log entries by database log event type.
|
546
|
*
|
547
|
* @param array $types
|
548
|
* The type information to compare against.
|
549
|
*
|
550
|
* @return array
|
551
|
* The count of each type keyed by the key of the $types array.
|
552
|
*/
|
553
|
protected function getTypeCount(array $types) {
|
554
|
$entries = $this->getLogEntries();
|
555
|
$count = array_fill(0, count($types), 0);
|
556
|
foreach ($entries as $entry) {
|
557
|
foreach ($types as $key => $type) {
|
558
|
if ($entry['type'] == $type['type'] && $entry['severity'] == $type['severity']) {
|
559
|
$count[$key]++;
|
560
|
break;
|
561
|
}
|
562
|
}
|
563
|
}
|
564
|
return $count;
|
565
|
}
|
566
|
|
567
|
/**
|
568
|
* Gets the watchdog severity constant corresponding to the CSS class.
|
569
|
*
|
570
|
* @param string $class
|
571
|
* CSS class attribute.
|
572
|
*
|
573
|
* @return int|null
|
574
|
* The watchdog severity constant or NULL if not found.
|
575
|
*
|
576
|
* @ingroup logging_severity_levels
|
577
|
*/
|
578
|
protected function getSeverityConstant($class) {
|
579
|
// Reversed array from dblog_overview().
|
580
|
$map = array(
|
581
|
'dblog-debug' => WATCHDOG_DEBUG,
|
582
|
'dblog-info' => WATCHDOG_INFO,
|
583
|
'dblog-notice' => WATCHDOG_NOTICE,
|
584
|
'dblog-warning' => WATCHDOG_WARNING,
|
585
|
'dblog-error' => WATCHDOG_ERROR,
|
586
|
'dblog-critical' => WATCHDOG_CRITICAL,
|
587
|
'dblog-alert' => WATCHDOG_ALERT,
|
588
|
'dblog-emerg' => WATCHDOG_EMERGENCY,
|
589
|
);
|
590
|
|
591
|
// Find the class that contains the severity.
|
592
|
$classes = explode(' ', $class);
|
593
|
foreach ($classes as $class) {
|
594
|
if (isset($map[$class])) {
|
595
|
return $map[$class];
|
596
|
}
|
597
|
}
|
598
|
return NULL;
|
599
|
}
|
600
|
|
601
|
/**
|
602
|
* Extracts the text contained by the XHTML element.
|
603
|
*
|
604
|
* @param SimpleXMLElement $element
|
605
|
* Element to extract text from.
|
606
|
*
|
607
|
* @return string
|
608
|
* Extracted text.
|
609
|
*/
|
610
|
protected function asText(SimpleXMLElement $element) {
|
611
|
if (!is_object($element)) {
|
612
|
return $this->fail('The element is not an element.');
|
613
|
}
|
614
|
return trim(html_entity_decode(strip_tags($element->asXML())));
|
615
|
}
|
616
|
|
617
|
/**
|
618
|
* Confirms that a log message appears on the database log overview screen.
|
619
|
*
|
620
|
* This function should only be used for the admin/reports/dblog page, because
|
621
|
* it checks for the message link text truncated to 56 characters. Other log
|
622
|
* pages have no detail links so they contain the full message text.
|
623
|
*
|
624
|
* @param string $log_message
|
625
|
* The database log message to check.
|
626
|
* @param string $message
|
627
|
* The message to pass to simpletest.
|
628
|
*/
|
629
|
protected function assertLogMessage($log_message, $message) {
|
630
|
$message_text = truncate_utf8(filter_xss($log_message, array()), 56, TRUE, TRUE);
|
631
|
// After filter_xss(), HTML entities should be converted to their character
|
632
|
// equivalents because assertLink() uses this string in xpath() to query the
|
633
|
// Document Object Model (DOM).
|
634
|
$this->assertLink(html_entity_decode($message_text), 0, $message);
|
635
|
}
|
636
|
}
|
637
|
|