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