Line # | Frequency | Source Line |
1 | | <?php
|
2 | | // $Id: dblog.test,v 1.1 2008/04/02 22:33:48 boombatower Exp $
|
3 | |
|
4 | | class DBLogModuleTestCase extends DrupalTestCase {
|
5 | | protected $big_user;
|
6 | | protected $any_user;
|
7 | |
|
8 | | /**
|
9 | | * Implementation of getInfo().
|
10 | | */
|
11 | | function getInfo() {
|
12 | | return array(
|
13 | 1 | 'name' => t('DBLog functionality'),
|
14 | | 'description' => t('Generate events and verify dblog entries; verify user access to log reports based on persmissions.'),
|
15 | | 'group' => t('DBLog Tests'),
|
16 | | );
|
17 | | }
|
18 | |
|
19 | | /**
|
20 | | * Enable modules and create users with specific permissions.
|
21 | | */
|
22 | | function setUp() {
|
23 | | parent::setUp();
|
24 | |
|
25 | | $this->drupalModuleEnable('dblog'); // Probably already enabled
|
26 | | $this->drupalModuleEnable('blog');
|
27 | | $this->drupalModuleEnable('poll');
|
28 | |
|
29 | | // Create users.
|
30 | | $this->big_user = $this->drupalCreateUser(array('administer site configuration', 'access administration pages', 'access site reports', 'administer users'));
|
31 | | $this->any_user = $this->drupalCreateUser(array());
|
32 | | }
|
33 | |
|
34 | | /**
|
35 | | * Login users, create dblog events, and test dblog functionality through the admin and user interfaces.
|
36 | | */
|
37 | | function testDBLog() {
|
38 | | // Login the admin user.
|
39 | | $this->drupalLogin($this->big_user);
|
40 | |
|
41 | | $row_limit = 100;
|
42 | | $this->verifyRowLimit($row_limit);
|
43 | | $this->verifyCron($row_limit);
|
44 | | $this->verifyEvents();
|
45 | | $this->verifyReports();
|
46 | |
|
47 | | // Login the regular user.
|
48 | | $user = $this->drupalLogin($this->any_user);
|
49 | | $this->verifyReports(403);
|
50 | | }
|
51 | |
|
52 | | /**
|
53 | | * Verify setting of the dblog row limit.
|
54 | | *
|
55 | | * @param integer $count Log row limit.
|
56 | | */
|
57 | | private function verifyRowLimit($row_limit) {
|
58 | | // Change the dblog row limit.
|
59 | | $edit = array();
|
60 | | $edit['dblog_row_limit'] = $row_limit;
|
61 | | $this->drupalPost('admin/settings/logging/dblog', $edit, t('Save configuration'));
|
62 | | $this->assertResponse(200);
|
63 | | // Check row limit variable.
|
64 | | $current_limit = variable_get('dblog_row_limit', 1000);
|
65 | | $this->assertTrue($current_limit == $row_limit, t('[Cache] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit)));
|
66 | | // Verify dblog row limit equals specified row limit.
|
67 | | $current_limit = unserialize(db_result(db_query("SELECT value FROM {variable} WHERE name = '%s'", 'dblog_row_limit')));
|
68 | | $this->assertTrue($current_limit == $row_limit, t('[Variable table] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit)));
|
69 | | }
|
70 | |
|
71 | | /**
|
72 | | * Verify cron applies the dblog row limit.
|
73 | | *
|
74 | | * @param integer $count Log row limit.
|
75 | | */
|
76 | | private function verifyCron($row_limit) {
|
77 | | // Check row limit variable.
|
78 | | $current_limit = variable_get('dblog_row_limit', 1000);
|
79 | | $this->assertTrue($current_limit == $row_limit, t('[Cache] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit)));
|
80 | |
|
81 | | // Generate additional log entries.
|
82 | | $this->generateLogEntries($row_limit + 10);
|
83 | | // Verify dblog row count exceeds row limit.
|
84 | | $count = db_result(db_query('SELECT COUNT(wid) FROM {watchdog}'));
|
85 | | $this->assertTrue($count > $row_limit, t('Dblog row count of @count exceeds row limit of @limit', array('@count' => $count, '@limit' => $row_limit)));
|
86 | |
|
87 | | // Check row limit variable.
|
88 | | variable_set('dblog_row_limit', $row_limit);
|
89 | | $current_limit = variable_get('dblog_row_limit', 1000);
|
90 | | $this->assertTrue($current_limit == $row_limit, t('[Cache] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit)));
|
91 | |
|
92 | | // Run cron job.
|
93 | | dblog_cron();
|
94 | | // Verify dblog row count equals row limit.
|
95 | | $count = db_result(db_query('SELECT COUNT(wid) FROM {watchdog}'));
|
96 | | $this->assertTrue($count == $row_limit, t('Dblog row count of @count equals row limit of @limit', array('@count' => $count, '@limit' => $row_limit)));
|
97 | |
|
98 | | // Manually cleanup the watchdog table.
|
99 | | $max = db_result(db_query('SELECT MAX(wid) FROM {watchdog}'));
|
100 | | db_query('DELETE FROM {watchdog} WHERE wid <= %d', $max - $row_limit); // Change query condition to '<='
|
101 | | // Verify dblog row count equals row limit.
|
102 | | $count = db_result(db_query('SELECT COUNT(wid) FROM {watchdog}'));
|
103 | | $this->assertTrue($count == $row_limit, t('Dblog row count of @count equals row limit of @limit', array('@count' => $count, '@limit' => $row_limit)));
|
104 | | }
|
105 | |
|
106 | | /**
|
107 | | * Generate dblog entries.
|
108 | | *
|
109 | | * @param integer $count Log row limit.
|
110 | | */
|
111 | | private function generateLogEntries($count) {
|
112 | | global $base_root;
|
113 | |
|
114 | | // Prepare the fields to be logged
|
115 | | $log = array(
|
116 | | 'type' => 'custom',
|
117 | | 'message' => 'Log entry added to test the dblog row limit.',
|
118 | | 'variables' => array(),
|
119 | | 'severity' => WATCHDOG_NOTICE,
|
120 | | 'link' => NULL,
|
121 | | 'user' => $this->big_user,
|
122 | | 'request_uri' => $base_root . request_uri(),
|
123 | | 'referer' => referer_uri(),
|
124 | | 'ip' => ip_address(),
|
125 | | 'timestamp' => time(),
|
126 | | );
|
127 | | $message = 'Log entry added to test the dblog row limit.';
|
128 | | for ($i = 0; $i < $count; $i++) {
|
129 | | $log['message'] = $i. ' => ' .$message;
|
130 | | dblog_watchdog($log);
|
131 | | }
|
132 | | }
|
133 | |
|
134 | | /**
|
135 | | * Verify the logged in user has the desired access to the various dblog nodes.
|
136 | | *
|
137 | | * @param integer $response HTTP response code.
|
138 | | */
|
139 | | private function verifyReports($response = 200) {
|
140 | | $quote = ''';
|
141 | |
|
142 | | // View dblog help node.
|
143 | | $this->drupalGet('admin/help/dblog');
|
144 | | $this->assertResponse($response);
|
145 | | if ($response == 200) {
|
146 | | $this->assertText(t('Database logging'), t('DBLog help was displayed'));
|
147 | | }
|
148 | |
|
149 | | // View dblog report node.
|
150 | | $this->drupalGet('admin/reports/dblog');
|
151 | | $this->assertResponse($response);
|
152 | | if ($response == 200) {
|
153 | | $this->assertText(t('Recent log entries'), t('DBLog report was displayed'));
|
154 | | }
|
155 | |
|
156 | | // View dblog page-not-found report node.
|
157 | | $this->drupalGet('admin/reports/page-not-found');
|
158 | | $this->assertResponse($response);
|
159 | | if ($response == 200) {
|
160 | | $this->assertText(t('Top '. $quote .'page not found'. $quote .' errors'), t('DBLog page-not-found report was displayed'));
|
161 | | }
|
162 | |
|
163 | | // View dblog access-denied report node.
|
164 | | $this->drupalGet('admin/reports/access-denied');
|
165 | | $this->assertResponse($response);
|
166 | | if ($response == 200) {
|
167 | | $this->assertText(t('Top '. $quote .'access denied'. $quote .' errors'), t('DBLog access-denied report was displayed'));
|
168 | | }
|
169 | |
|
170 | | // View dblog event node.
|
171 | | $this->drupalGet('admin/reports/event/1');
|
172 | | $this->assertResponse($response);
|
173 | | if ($response == 200) {
|
174 | | $this->assertText(t('Details'), t('DBLog event node was displayed'));
|
175 | | }
|
176 | | }
|
177 | |
|
178 | | /**
|
179 | | * Verify events.
|
180 | | */
|
181 | | private function verifyEvents() {
|
182 | | // Invoke events.
|
183 | | $this->doUser();
|
184 | | $this->doNode('article');
|
185 | | $this->doNode('blog');
|
186 | | $this->doNode('page');
|
187 | | $this->doNode('poll');
|
188 | |
|
189 | | // When a user is deleted, any content they created remains but the uid = 0. Their blog entry shows as "'s blog" on the home page.
|
190 | | // Records in the watchdog table related to that user have the uid set to zero.
|
191 | | }
|
192 | |
|
193 | | /**
|
194 | | * Generate and verify user events.
|
195 | | *
|
196 | | */
|
197 | | private function doUser()
|
198 | | {
|
199 | | // Set user variables.
|
200 | | $name = $this->randomName();
|
201 | | $pass = user_password();
|
202 | | // Add user using form to generate add user event (which is not triggered by drupalCreateUser).
|
203 | | $edit = array();
|
204 | | $edit['name'] = $name;
|
205 | | $edit['mail'] = $name .'@example.com';
|
206 | | $edit['pass[pass1]'] = $pass;
|
207 | | $edit['pass[pass2]'] = $pass;
|
208 | | $edit['status'] = 1;
|
209 | | $this->drupalPost('admin/user/user/create', $edit, t('Create new account'));
|
210 | | $this->assertResponse(200);
|
211 | | // Retrieve user object.
|
212 | | $user = user_load(array('name' => $name)); //, 'pass' => $pass, 'status' => 1));
|
213 | | $this->assertTrue($user != null, t('User @name was loaded', array('@name' => $name)));
|
214 | | $user->pass_raw = $pass; // Needed by drupalLogin.
|
215 | | // Login user.
|
216 | | $this->drupalLogin($user);
|
217 | | // Logout user.
|
218 | | $this->drupalLogout();
|
219 | | // Fetch row ids in watchdog that relate to the user.
|
220 | | $result = db_query('SELECT wid FROM {watchdog} WHERE uid = %d', $user->uid);
|
221 | | for ($i = 0; $i < mysqli_num_rows($result); $i++) {
|
222 | | $ids[] = db_result($result);
|
223 | | }
|
224 | | if (isset($ids)) {
|
225 | | $count_before = count($ids);
|
226 | | }
|
227 | | $this->assertTrue($count_before > 0, t('DBLog contains @count records for @name', array('@count' => $count_before, '@name' => $user->name)));
|
228 | | // Delete user.
|
229 | | user_delete(array(), $user->uid);
|
230 | | // Count rows that have uids for the user.
|
231 | | $count = db_result(db_query('SELECT COUNT(wid) FROM {watchdog} WHERE uid = %d', $user->uid));
|
232 | | $this->assertTrue($count == 0, t('DBLog contains @count records for @name', array('@count' => $count, '@name' => $user->name)));
|
233 | | // Fetch row ids in watchdog that previously related to the deleted user.
|
234 | | $result = db_query('SELECT wid FROM {watchdog} WHERE uid = 0 AND wid IN (%s)', implode(', ', $ids));
|
235 | | unset($ids);
|
236 | | for ($i = 0; $i < mysqli_num_rows($result); $i++) {
|
237 | | $ids[] = db_result($result);
|
238 | | }
|
239 | | if (isset($ids)) {
|
240 | | $count_after = count($ids);
|
241 | | }
|
242 | | $this->assertTrue($count_after == $count_before, t('DBLog contains @count records for @name that now have uid = 0', array('@count' => $count_before, '@name' => $user->name)));
|
243 | | unset($ids);
|
244 | | // Fetch row ids in watchdog that relate to the user.
|
245 | | $result = db_query('SELECT wid FROM {watchdog} WHERE uid = %d', $user->uid);
|
246 | | for ($i = 0; $i < mysqli_num_rows($result); $i++) {
|
247 | | $ids[] = db_result($result);
|
248 | | }
|
249 | | $this->assertTrue(!isset($ids), t('DBLog contains no records for @name', array('@name' => $user->name)));
|
250 | |
|
251 | | // Login the admin user.
|
252 | | $this->drupalLogin($this->big_user);
|
253 | | // View the dblog report.
|
254 | | $this->drupalGet('admin/reports/dblog');
|
255 | | $this->assertResponse(200);
|
256 | |
|
257 | | // Verify events were recorded.
|
258 | | // Add user.
|
259 | | // Default display includes name and email address; if too long then email is replaced by three periods.
|
260 | | // $this->assertRaw(t('New user: %name (%mail)', array('%name' => $edit['name'], '%mail' => $edit['mail'])), t('DBLog event was recorded: [add user]'));
|
261 | | $this->assertRaw(t('New user: %name', array('%name' => $name)), t('DBLog event was recorded: [add user]'));
|
262 | | // Login user.
|
263 | | $this->assertRaw(t('Session opened for %name', array('%name' => $name)), t('DBLog event was recorded: [login user]'));
|
264 | | // Logout user.
|
265 | | $this->assertRaw(t('Session closed for %name', array('%name' => $name)), t('DBLog event was recorded: [logout user]'));
|
266 | | // Delete user.
|
267 | | $this->assertRaw(t('Deleted user: %name', array('%name' => $name)), t('DBLog event was recorded: [delete user]'));
|
268 | | }
|
269 | |
|
270 | | /**
|
271 | | * Generate and verify node events.
|
272 | | *
|
273 | | * @param string $type Content type.
|
274 | | */
|
275 | | private function doNode($type) {
|
276 | | // Create user.
|
277 | | $perm = array('create '. $type .' content', 'edit own '. $type .' content', 'delete own '. $type .' content');
|
278 | | $user = $this->drupalCreateUser($perm);
|
279 | | // Login user.
|
280 | | $this->drupalLogin($user);
|
281 | |
|
282 | | // Create node using form to generate add content event (which is not triggered by drupalCreateNode).
|
283 | | $edit = $this->getContent($type);
|
284 | | $title = $edit['title'];
|
285 | | $this->drupalPost('node/add/'. $type, $edit, t('Save'));
|
286 | | $this->assertResponse(200);
|
287 | | // Retrieve node object.
|
288 | | $node = node_load(array('title' => $title));
|
289 | | $this->assertTrue($node != null, t('Node @title was loaded', array('@title' => $title)));
|
290 | | // Edit node.
|
291 | | $edit = $this->getContentUpdate($type);
|
292 | | $this->drupalPost('node/'. $node->nid .'/edit', $edit, t('Save'));
|
293 | | $this->assertResponse(200);
|
294 | | // Delete node.
|
295 | | $this->drupalPost('node/'. $node->nid .'/delete', array(), t('Delete'));
|
296 | | $this->assertResponse(200);
|
297 | | // View node (to generate page not found event).
|
298 | | $this->drupalGet('node/'. $node->nid);
|
299 | | $this->assertResponse(404);
|
300 | | // View the dblog report (to generate access denied event).
|
301 | | $this->drupalGet('admin/reports/dblog');
|
302 | | $this->assertResponse(403);
|
303 | |
|
304 | | // Login the admin user.
|
305 | | $this->drupalLogin($this->big_user);
|
306 | | // View the dblog report.
|
307 | | $this->drupalGet('admin/reports/dblog');
|
308 | | $this->assertResponse(200);
|
309 | |
|
310 | | // Verify events were recorded.
|
311 | | // Content added.
|
312 | | $this->assertRaw(t('@type: added %title', array('@type' => $type, '%title' => $title)), t('DBLog event was recorded: [content added]'));
|
313 | | // Content updated.
|
314 | | $this->assertRaw(t('@type: updated %title', array('@type' => $type, '%title' => $title)), t('DBLog event was recorded: [content updated]'));
|
315 | | // Content deleted.
|
316 | | $this->assertRaw(t('@type: deleted %title', array('@type' => $type, '%title' => $title)), t('DBLog event was recorded: [content deleted]'));
|
317 | |
|
318 | | // View dblog access-denied report node.
|
319 | | $this->drupalGet('admin/reports/access-denied');
|
320 | | $this->assertResponse(200);
|
321 | | // Access denied.
|
322 | | $this->assertText(t('admin/reports/dblog'), t('DBLog event was recorded: [page not found]'));
|
323 | |
|
324 | | // View dblog page-not-found report node.
|
325 | | $this->drupalGet('admin/reports/page-not-found');
|
326 | | $this->assertResponse(200);
|
327 | | // Page not found.
|
328 | | $this->assertText(t('node/@nid', array('@nid' => $node->nid)), t('DBLog event was recorded: [page not found]'));
|
329 | | }
|
330 | |
|
331 | | /**
|
332 | | * Create content based on content type.
|
333 | | *
|
334 | | * @param string $type Content type.
|
335 | | * @return array Content.
|
336 | | */
|
337 | | private function getContent($type) {
|
338 | | switch ($type) {
|
339 | | case 'poll':
|
340 | | $content = array(
|
341 | | 'title' => $this->randomName(8),
|
342 | | 'choice[0][chtext]' => $this->randomName(32),
|
343 | | 'choice[1][chtext]' => $this->randomName(32),
|
344 | | );
|
345 | | break;
|
346 | |
|
347 | | default:
|
348 | | $content = array(
|
349 | | 'title' => $this->randomName(8),
|
350 | | 'body' => $this->randomName(32),
|
351 | | );
|
352 | | break;
|
353 | | }
|
354 | | return $content;
|
355 | | }
|
356 | |
|
357 | | /**
|
358 | | * Create content update based on content type.
|
359 | | *
|
360 | | * @param string $type Content type.
|
361 | | * @return array Content.
|
362 | | */
|
363 | | private function getContentUpdate($type) {
|
364 | | switch ($type) {
|
365 | | case 'poll':
|
366 | | $content = array(
|
367 | | 'choice[0][chtext]' => $this->randomName(32),
|
368 | | 'choice[1][chtext]' => $this->randomName(32),
|
369 | | );
|
370 | | break;
|
371 | |
|
372 | | default:
|
373 | | $content = array(
|
374 | | 'body' => $this->randomName(32),
|
375 | | );
|
376 | | break;
|
377 | | }
|
378 | | return $content;
|
379 | | }
|
380 | | }
|