Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\dblog\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Html;
|
Chris@0
|
6 use Drupal\Component\Utility\Unicode;
|
Chris@0
|
7 use Drupal\Core\Logger\RfcLogLevel;
|
Chris@0
|
8 use Drupal\Core\Url;
|
Chris@0
|
9 use Drupal\dblog\Controller\DbLogController;
|
Chris@0
|
10 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Generate events and verify dblog entries; verify user access to log reports
|
Chris@0
|
14 * based on permissions.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @group dblog
|
Chris@0
|
17 */
|
Chris@0
|
18 class DbLogTest extends BrowserTestBase {
|
Chris@4
|
19 use FakeLogEntries;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Modules to enable.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var array
|
Chris@0
|
25 */
|
Chris@0
|
26 public static $modules = ['dblog', 'node', 'forum', 'help', 'block'];
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * A user with some relevant administrative permissions.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @var \Drupal\user\UserInterface
|
Chris@0
|
32 */
|
Chris@0
|
33 protected $adminUser;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * A user without any permissions.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @var \Drupal\user\UserInterface
|
Chris@0
|
39 */
|
Chris@0
|
40 protected $webUser;
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * {@inheritdoc}
|
Chris@0
|
44 */
|
Chris@0
|
45 protected function setUp() {
|
Chris@0
|
46 parent::setUp();
|
Chris@0
|
47 $this->drupalPlaceBlock('system_breadcrumb_block');
|
Chris@0
|
48 $this->drupalPlaceBlock('page_title_block');
|
Chris@0
|
49
|
Chris@0
|
50 // Create users with specific permissions.
|
Chris@0
|
51 $this->adminUser = $this->drupalCreateUser(['administer site configuration', 'access administration pages', 'access site reports', 'administer users']);
|
Chris@0
|
52 $this->webUser = $this->drupalCreateUser([]);
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Tests Database Logging module functionality through interfaces.
|
Chris@0
|
57 *
|
Chris@0
|
58 * First logs in users, then creates database log events, and finally tests
|
Chris@0
|
59 * Database Logging module functionality through both the admin and user
|
Chris@0
|
60 * interfaces.
|
Chris@0
|
61 */
|
Chris@0
|
62 public function testDbLog() {
|
Chris@0
|
63 // Log in the admin user.
|
Chris@0
|
64 $this->drupalLogin($this->adminUser);
|
Chris@0
|
65
|
Chris@0
|
66 $row_limit = 100;
|
Chris@0
|
67 $this->verifyRowLimit($row_limit);
|
Chris@0
|
68 $this->verifyEvents();
|
Chris@0
|
69 $this->verifyReports();
|
Chris@0
|
70 $this->verifyBreadcrumbs();
|
Chris@0
|
71 $this->verifyLinkEscaping();
|
Chris@0
|
72 // Verify the overview table sorting.
|
Chris@0
|
73 $orders = ['Date', 'Type', 'User'];
|
Chris@0
|
74 $sorts = ['asc', 'desc'];
|
Chris@0
|
75 foreach ($orders as $order) {
|
Chris@0
|
76 foreach ($sorts as $sort) {
|
Chris@0
|
77 $this->verifySort($sort, $order);
|
Chris@0
|
78 }
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 // Log in the regular user.
|
Chris@0
|
82 $this->drupalLogin($this->webUser);
|
Chris@0
|
83 $this->verifyReports(403);
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Test individual log event page.
|
Chris@0
|
88 */
|
Chris@0
|
89 public function testLogEventPage() {
|
Chris@0
|
90 // Login the admin user.
|
Chris@0
|
91 $this->drupalLogin($this->adminUser);
|
Chris@0
|
92
|
Chris@0
|
93 // Since referrer and location links vary by how the tests are run, inject
|
Chris@0
|
94 // fake log data to test these.
|
Chris@0
|
95 $context = [
|
Chris@0
|
96 'request_uri' => 'http://example.com?dblog=1',
|
Chris@0
|
97 'referer' => 'http://example.org?dblog=2',
|
Chris@0
|
98 'uid' => 0,
|
Chris@0
|
99 'channel' => 'testing',
|
Chris@0
|
100 'link' => 'foo/bar',
|
Chris@0
|
101 'ip' => '0.0.1.0',
|
Chris@0
|
102 'timestamp' => REQUEST_TIME,
|
Chris@0
|
103 ];
|
Chris@0
|
104 \Drupal::service('logger.dblog')->log(RfcLogLevel::NOTICE, 'Test message', $context);
|
Chris@0
|
105 $wid = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
|
Chris@0
|
106
|
Chris@0
|
107 // Verify the links appear correctly.
|
Chris@0
|
108 $this->drupalGet('admin/reports/dblog/event/' . $wid);
|
Chris@0
|
109 $this->assertLinkByHref($context['request_uri']);
|
Chris@0
|
110 $this->assertLinkByHref($context['referer']);
|
Chris@0
|
111
|
Chris@0
|
112 // Verify hostname.
|
Chris@0
|
113 $this->assertRaw($context['ip'], 'Found hostname on the detail page.');
|
Chris@0
|
114
|
Chris@0
|
115 // Verify severity.
|
Chris@0
|
116 $this->assertText('Notice', 'The severity was properly displayed on the detail page.');
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * Verifies setting of the database log row limit.
|
Chris@0
|
121 *
|
Chris@0
|
122 * @param int $row_limit
|
Chris@0
|
123 * The row limit.
|
Chris@0
|
124 */
|
Chris@0
|
125 private function verifyRowLimit($row_limit) {
|
Chris@0
|
126 // Change the database log row limit.
|
Chris@0
|
127 $edit = [];
|
Chris@0
|
128 $edit['dblog_row_limit'] = $row_limit;
|
Chris@0
|
129 $this->drupalPostForm('admin/config/development/logging', $edit, t('Save configuration'));
|
Chris@0
|
130 $this->assertResponse(200);
|
Chris@0
|
131
|
Chris@0
|
132 // Check row limit variable.
|
Chris@0
|
133 $current_limit = $this->config('dblog.settings')->get('row_limit');
|
Chris@0
|
134 $this->assertTrue($current_limit == $row_limit, format_string('[Cache] Row limit variable of @count equals row limit of @limit', ['@count' => $current_limit, '@limit' => $row_limit]));
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 /**
|
Chris@0
|
138 * Clear the entry logs by clicking on 'Clear log messages' button.
|
Chris@0
|
139 */
|
Chris@0
|
140 protected function clearLogsEntries() {
|
Chris@0
|
141 $this->drupalGet(Url::fromRoute('dblog.confirm'));
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 /**
|
Chris@0
|
145 * Filters the logs according to the specific severity and log entry type.
|
Chris@0
|
146 *
|
Chris@0
|
147 * @param string $type
|
Chris@0
|
148 * (optional) The log entry type.
|
Chris@0
|
149 * @param string $severity
|
Chris@0
|
150 * (optional) The log entry severity.
|
Chris@0
|
151 */
|
Chris@0
|
152 protected function filterLogsEntries($type = NULL, $severity = NULL) {
|
Chris@0
|
153 $edit = [];
|
Chris@0
|
154 if (isset($type)) {
|
Chris@0
|
155 $edit['type[]'] = $type;
|
Chris@0
|
156 }
|
Chris@0
|
157 if (isset($severity)) {
|
Chris@0
|
158 $edit['severity[]'] = $severity;
|
Chris@0
|
159 }
|
Chris@0
|
160 $this->drupalPostForm(NULL, $edit, t('Filter'));
|
Chris@0
|
161 }
|
Chris@0
|
162
|
Chris@0
|
163 /**
|
Chris@0
|
164 * Confirms that database log reports are displayed at the correct paths.
|
Chris@0
|
165 *
|
Chris@0
|
166 * @param int $response
|
Chris@0
|
167 * (optional) HTTP response code. Defaults to 200.
|
Chris@0
|
168 */
|
Chris@0
|
169 private function verifyReports($response = 200) {
|
Chris@0
|
170 // View the database log help page.
|
Chris@0
|
171 $this->drupalGet('admin/help/dblog');
|
Chris@0
|
172 $this->assertResponse($response);
|
Chris@0
|
173 if ($response == 200) {
|
Chris@0
|
174 $this->assertText(t('Database Logging'), 'DBLog help was displayed');
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 // View the database log report page.
|
Chris@0
|
178 $this->drupalGet('admin/reports/dblog');
|
Chris@0
|
179 $this->assertResponse($response);
|
Chris@0
|
180 if ($response == 200) {
|
Chris@0
|
181 $this->assertText(t('Recent log messages'), 'DBLog report was displayed');
|
Chris@0
|
182 }
|
Chris@0
|
183
|
Chris@0
|
184 $this->drupalGet('admin/reports/dblog/confirm');
|
Chris@0
|
185 $this->assertResponse($response);
|
Chris@0
|
186 if ($response == 200) {
|
Chris@0
|
187 $this->assertText(t('Are you sure you want to delete the recent logs?'), 'DBLog clear logs form was displayed');
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 // View the database log page-not-found report page.
|
Chris@0
|
191 $this->drupalGet('admin/reports/page-not-found');
|
Chris@0
|
192 $this->assertResponse($response);
|
Chris@0
|
193 if ($response == 200) {
|
Chris@0
|
194 $this->assertText("Top 'page not found' errors", 'DBLog page-not-found report was displayed');
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 // View the database log access-denied report page.
|
Chris@0
|
198 $this->drupalGet('admin/reports/access-denied');
|
Chris@0
|
199 $this->assertResponse($response);
|
Chris@0
|
200 if ($response == 200) {
|
Chris@0
|
201 $this->assertText("Top 'access denied' errors", 'DBLog access-denied report was displayed');
|
Chris@0
|
202 }
|
Chris@0
|
203
|
Chris@0
|
204 // View the database log event page.
|
Chris@0
|
205 $wid = db_query('SELECT MIN(wid) FROM {watchdog}')->fetchField();
|
Chris@0
|
206 $this->drupalGet('admin/reports/dblog/event/' . $wid);
|
Chris@0
|
207 $this->assertResponse($response);
|
Chris@0
|
208 if ($response == 200) {
|
Chris@0
|
209 $this->assertText(t('Details'), 'DBLog event node was displayed');
|
Chris@0
|
210 }
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 /**
|
Chris@0
|
214 * Generates and then verifies breadcrumbs.
|
Chris@0
|
215 */
|
Chris@0
|
216 private function verifyBreadcrumbs() {
|
Chris@0
|
217 // View the database log event page.
|
Chris@0
|
218 $wid = db_query('SELECT MIN(wid) FROM {watchdog}')->fetchField();
|
Chris@0
|
219 $this->drupalGet('admin/reports/dblog/event/' . $wid);
|
Chris@0
|
220 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
|
Chris@0
|
221 $this->assertEqual(current($this->xpath($xpath))->getText(), 'Recent log messages', 'DBLogs link displayed at breadcrumb in event page.');
|
Chris@0
|
222 }
|
Chris@0
|
223
|
Chris@0
|
224 /**
|
Chris@0
|
225 * Generates and then verifies various types of events.
|
Chris@0
|
226 */
|
Chris@0
|
227 private function verifyEvents() {
|
Chris@0
|
228 // Invoke events.
|
Chris@0
|
229 $this->doUser();
|
Chris@0
|
230 $this->drupalCreateContentType(['type' => 'article', 'name' => t('Article')]);
|
Chris@0
|
231 $this->drupalCreateContentType(['type' => 'page', 'name' => t('Basic page')]);
|
Chris@0
|
232 $this->doNode('article');
|
Chris@0
|
233 $this->doNode('page');
|
Chris@0
|
234 $this->doNode('forum');
|
Chris@0
|
235
|
Chris@0
|
236 // When a user account is canceled, any content they created remains but the
|
Chris@0
|
237 // uid = 0. Records in the watchdog table related to that user have the uid
|
Chris@0
|
238 // set to zero.
|
Chris@0
|
239 }
|
Chris@0
|
240
|
Chris@0
|
241 /**
|
Chris@0
|
242 * Verifies the sorting functionality of the database logging reports table.
|
Chris@0
|
243 *
|
Chris@0
|
244 * @param string $sort
|
Chris@0
|
245 * The sort direction.
|
Chris@0
|
246 * @param string $order
|
Chris@0
|
247 * The order by which the table should be sorted.
|
Chris@0
|
248 */
|
Chris@0
|
249 public function verifySort($sort = 'asc', $order = 'Date') {
|
Chris@0
|
250 $this->drupalGet('admin/reports/dblog', ['query' => ['sort' => $sort, 'order' => $order]]);
|
Chris@0
|
251 $this->assertResponse(200);
|
Chris@0
|
252 $this->assertText(t('Recent log messages'), 'DBLog report was displayed correctly and sorting went fine.');
|
Chris@0
|
253 }
|
Chris@0
|
254
|
Chris@0
|
255 /**
|
Chris@0
|
256 * Tests the escaping of links in the operation row of a database log detail
|
Chris@0
|
257 * page.
|
Chris@0
|
258 */
|
Chris@0
|
259 private function verifyLinkEscaping() {
|
Chris@0
|
260 $link = \Drupal::l('View', Url::fromRoute('entity.node.canonical', ['node' => 1]));
|
Chris@0
|
261 $message = 'Log entry added to do the verifyLinkEscaping test.';
|
Chris@0
|
262 $this->generateLogEntries(1, [
|
Chris@0
|
263 'message' => $message,
|
Chris@0
|
264 'link' => $link,
|
Chris@0
|
265 ]);
|
Chris@0
|
266
|
Chris@0
|
267 $result = db_query_range('SELECT wid FROM {watchdog} ORDER BY wid DESC', 0, 1);
|
Chris@0
|
268 $this->drupalGet('admin/reports/dblog/event/' . $result->fetchField());
|
Chris@0
|
269
|
Chris@0
|
270 // Check if the link exists (unescaped).
|
Chris@0
|
271 $this->assertRaw($link);
|
Chris@0
|
272 }
|
Chris@0
|
273
|
Chris@0
|
274 /**
|
Chris@0
|
275 * Generates and then verifies some user events.
|
Chris@0
|
276 */
|
Chris@0
|
277 private function doUser() {
|
Chris@0
|
278 // Set user variables.
|
Chris@0
|
279 $name = $this->randomMachineName();
|
Chris@0
|
280 $pass = user_password();
|
Chris@0
|
281 // Add a user using the form to generate an add user event (which is not
|
Chris@0
|
282 // triggered by drupalCreateUser).
|
Chris@0
|
283 $edit = [];
|
Chris@0
|
284 $edit['name'] = $name;
|
Chris@0
|
285 $edit['mail'] = $name . '@example.com';
|
Chris@0
|
286 $edit['pass[pass1]'] = $pass;
|
Chris@0
|
287 $edit['pass[pass2]'] = $pass;
|
Chris@0
|
288 $edit['status'] = 1;
|
Chris@0
|
289 $this->drupalPostForm('admin/people/create', $edit, t('Create new account'));
|
Chris@0
|
290 $this->assertResponse(200);
|
Chris@0
|
291 // Retrieve the user object.
|
Chris@0
|
292 $user = user_load_by_name($name);
|
Chris@0
|
293 $this->assertTrue($user != NULL, format_string('User @name was loaded', ['@name' => $name]));
|
Chris@0
|
294 // pass_raw property is needed by drupalLogin.
|
Chris@0
|
295 $user->passRaw = $pass;
|
Chris@0
|
296 // Log in user.
|
Chris@0
|
297 $this->drupalLogin($user);
|
Chris@0
|
298 // Log out user.
|
Chris@0
|
299 $this->drupalLogout();
|
Chris@0
|
300 // Fetch the row IDs in watchdog that relate to the user.
|
Chris@0
|
301 $result = db_query('SELECT wid FROM {watchdog} WHERE uid = :uid', [':uid' => $user->id()]);
|
Chris@0
|
302 foreach ($result as $row) {
|
Chris@0
|
303 $ids[] = $row->wid;
|
Chris@0
|
304 }
|
Chris@0
|
305 $count_before = (isset($ids)) ? count($ids) : 0;
|
Chris@0
|
306 $this->assertTrue($count_before > 0, format_string('DBLog contains @count records for @name', ['@count' => $count_before, '@name' => $user->getUsername()]));
|
Chris@0
|
307
|
Chris@0
|
308 // Log in the admin user.
|
Chris@0
|
309 $this->drupalLogin($this->adminUser);
|
Chris@0
|
310 // Delete the user created at the start of this test.
|
Chris@0
|
311 // We need to POST here to invoke batch_process() in the internal browser.
|
Chris@0
|
312 $this->drupalPostForm('user/' . $user->id() . '/cancel', ['user_cancel_method' => 'user_cancel_reassign'], t('Cancel account'));
|
Chris@0
|
313
|
Chris@0
|
314 // View the database log report.
|
Chris@0
|
315 $this->drupalGet('admin/reports/dblog');
|
Chris@0
|
316 $this->assertResponse(200);
|
Chris@0
|
317
|
Chris@0
|
318 // Verify that the expected events were recorded.
|
Chris@0
|
319 // Add user.
|
Chris@0
|
320 // Default display includes name and email address; if too long, the email
|
Chris@0
|
321 // address is replaced by three periods.
|
Chris@0
|
322 $this->assertLogMessage(t('New user: %name %email.', ['%name' => $name, '%email' => '<' . $user->getEmail() . '>']), 'DBLog event was recorded: [add user]');
|
Chris@0
|
323 // Log in user.
|
Chris@0
|
324 $this->assertLogMessage(t('Session opened for %name.', ['%name' => $name]), 'DBLog event was recorded: [login user]');
|
Chris@0
|
325 // Log out user.
|
Chris@0
|
326 $this->assertLogMessage(t('Session closed for %name.', ['%name' => $name]), 'DBLog event was recorded: [logout user]');
|
Chris@0
|
327 // Delete user.
|
Chris@0
|
328 $message = t('Deleted user: %name %email.', ['%name' => $name, '%email' => '<' . $user->getEmail() . '>']);
|
Chris@0
|
329 $message_text = Unicode::truncate(Html::decodeEntities(strip_tags($message)), 56, TRUE, TRUE);
|
Chris@0
|
330 // Verify that the full message displays on the details page.
|
Chris@0
|
331 $link = FALSE;
|
Chris@0
|
332 if ($links = $this->xpath('//a[text()="' . $message_text . '"]')) {
|
Chris@0
|
333 // Found link with the message text.
|
Chris@0
|
334 $links = array_shift($links);
|
Chris@0
|
335 $value = $links->getAttribute('href');
|
Chris@0
|
336
|
Chris@0
|
337 // Extract link to details page.
|
Chris@4
|
338 $link = mb_substr($value, strpos($value, 'admin/reports/dblog/event/'));
|
Chris@0
|
339 $this->drupalGet($link);
|
Chris@0
|
340 // Check for full message text on the details page.
|
Chris@0
|
341 $this->assertRaw($message, 'DBLog event details was found: [delete user]');
|
Chris@0
|
342 }
|
Chris@0
|
343 $this->assertTrue($link, 'DBLog event was recorded: [delete user]');
|
Chris@0
|
344 // Visit random URL (to generate page not found event).
|
Chris@0
|
345 $not_found_url = $this->randomMachineName(60);
|
Chris@0
|
346 $this->drupalGet($not_found_url);
|
Chris@0
|
347 $this->assertResponse(404);
|
Chris@0
|
348 // View the database log page-not-found report page.
|
Chris@0
|
349 $this->drupalGet('admin/reports/page-not-found');
|
Chris@0
|
350 $this->assertResponse(200);
|
Chris@0
|
351 // Check that full-length URL displayed.
|
Chris@0
|
352 $this->assertText($not_found_url, 'DBLog event was recorded: [page not found]');
|
Chris@0
|
353 }
|
Chris@0
|
354
|
Chris@0
|
355 /**
|
Chris@0
|
356 * Generates and then verifies some node events.
|
Chris@0
|
357 *
|
Chris@0
|
358 * @param string $type
|
Chris@0
|
359 * A node type (e.g., 'article', 'page' or 'forum').
|
Chris@0
|
360 */
|
Chris@0
|
361 private function doNode($type) {
|
Chris@0
|
362 // Create user.
|
Chris@0
|
363 $perm = ['create ' . $type . ' content', 'edit own ' . $type . ' content', 'delete own ' . $type . ' content'];
|
Chris@0
|
364 $user = $this->drupalCreateUser($perm);
|
Chris@0
|
365 // Log in user.
|
Chris@0
|
366 $this->drupalLogin($user);
|
Chris@0
|
367
|
Chris@0
|
368 // Create a node using the form in order to generate an add content event
|
Chris@0
|
369 // (which is not triggered by drupalCreateNode).
|
Chris@0
|
370 $edit = $this->getContent($type);
|
Chris@0
|
371 $title = $edit['title[0][value]'];
|
Chris@0
|
372 $this->drupalPostForm('node/add/' . $type, $edit, t('Save'));
|
Chris@0
|
373 $this->assertResponse(200);
|
Chris@0
|
374 // Retrieve the node object.
|
Chris@0
|
375 $node = $this->drupalGetNodeByTitle($title);
|
Chris@0
|
376 $this->assertTrue($node != NULL, format_string('Node @title was loaded', ['@title' => $title]));
|
Chris@0
|
377 // Edit the node.
|
Chris@0
|
378 $edit = $this->getContentUpdate($type);
|
Chris@0
|
379 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
|
Chris@0
|
380 $this->assertResponse(200);
|
Chris@0
|
381 // Delete the node.
|
Chris@0
|
382 $this->drupalPostForm('node/' . $node->id() . '/delete', [], t('Delete'));
|
Chris@0
|
383 $this->assertResponse(200);
|
Chris@0
|
384 // View the node (to generate page not found event).
|
Chris@0
|
385 $this->drupalGet('node/' . $node->id());
|
Chris@0
|
386 $this->assertResponse(404);
|
Chris@0
|
387 // View the database log report (to generate access denied event).
|
Chris@0
|
388 $this->drupalGet('admin/reports/dblog');
|
Chris@0
|
389 $this->assertResponse(403);
|
Chris@0
|
390
|
Chris@0
|
391 // Log in the admin user.
|
Chris@0
|
392 $this->drupalLogin($this->adminUser);
|
Chris@0
|
393 // View the database log report.
|
Chris@0
|
394 $this->drupalGet('admin/reports/dblog');
|
Chris@0
|
395 $this->assertResponse(200);
|
Chris@0
|
396
|
Chris@0
|
397 // Verify that node events were recorded.
|
Chris@0
|
398 // Was node content added?
|
Chris@0
|
399 $this->assertLogMessage(t('@type: added %title.', ['@type' => $type, '%title' => $title]), 'DBLog event was recorded: [content added]');
|
Chris@0
|
400 // Was node content updated?
|
Chris@0
|
401 $this->assertLogMessage(t('@type: updated %title.', ['@type' => $type, '%title' => $title]), 'DBLog event was recorded: [content updated]');
|
Chris@0
|
402 // Was node content deleted?
|
Chris@0
|
403 $this->assertLogMessage(t('@type: deleted %title.', ['@type' => $type, '%title' => $title]), 'DBLog event was recorded: [content deleted]');
|
Chris@0
|
404
|
Chris@0
|
405 // View the database log access-denied report page.
|
Chris@0
|
406 $this->drupalGet('admin/reports/access-denied');
|
Chris@0
|
407 $this->assertResponse(200);
|
Chris@0
|
408 // Verify that the 'access denied' event was recorded.
|
Chris@0
|
409 $this->assertText('admin/reports/dblog', 'DBLog event was recorded: [access denied]');
|
Chris@0
|
410
|
Chris@0
|
411 // View the database log page-not-found report page.
|
Chris@0
|
412 $this->drupalGet('admin/reports/page-not-found');
|
Chris@0
|
413 $this->assertResponse(200);
|
Chris@0
|
414 // Verify that the 'page not found' event was recorded.
|
Chris@0
|
415 $this->assertText('node/' . $node->id(), 'DBLog event was recorded: [page not found]');
|
Chris@0
|
416 }
|
Chris@0
|
417
|
Chris@0
|
418 /**
|
Chris@0
|
419 * Creates random content based on node content type.
|
Chris@0
|
420 *
|
Chris@0
|
421 * @param string $type
|
Chris@0
|
422 * Node content type (e.g., 'article').
|
Chris@0
|
423 *
|
Chris@0
|
424 * @return array
|
Chris@0
|
425 * Random content needed by various node types.
|
Chris@0
|
426 */
|
Chris@0
|
427 private function getContent($type) {
|
Chris@0
|
428 switch ($type) {
|
Chris@0
|
429 case 'forum':
|
Chris@0
|
430 $content = [
|
Chris@0
|
431 'title[0][value]' => $this->randomMachineName(8),
|
Chris@0
|
432 'taxonomy_forums' => 1,
|
Chris@0
|
433 'body[0][value]' => $this->randomMachineName(32),
|
Chris@0
|
434 ];
|
Chris@0
|
435 break;
|
Chris@0
|
436
|
Chris@0
|
437 default:
|
Chris@0
|
438 $content = [
|
Chris@0
|
439 'title[0][value]' => $this->randomMachineName(8),
|
Chris@0
|
440 'body[0][value]' => $this->randomMachineName(32),
|
Chris@0
|
441 ];
|
Chris@0
|
442 break;
|
Chris@0
|
443 }
|
Chris@0
|
444 return $content;
|
Chris@0
|
445 }
|
Chris@0
|
446
|
Chris@0
|
447 /**
|
Chris@0
|
448 * Creates random content as an update based on node content type.
|
Chris@0
|
449 *
|
Chris@0
|
450 * @param string $type
|
Chris@0
|
451 * Node content type (e.g., 'article').
|
Chris@0
|
452 *
|
Chris@0
|
453 * @return array
|
Chris@0
|
454 * Random content needed by various node types.
|
Chris@0
|
455 */
|
Chris@0
|
456 private function getContentUpdate($type) {
|
Chris@0
|
457 $content = [
|
Chris@0
|
458 'body[0][value]' => $this->randomMachineName(32),
|
Chris@0
|
459 ];
|
Chris@0
|
460 return $content;
|
Chris@0
|
461 }
|
Chris@0
|
462
|
Chris@0
|
463 /**
|
Chris@0
|
464 * Tests the addition and clearing of log events through the admin interface.
|
Chris@0
|
465 *
|
Chris@0
|
466 * Logs in the admin user, creates a database log event, and tests the
|
Chris@0
|
467 * functionality of clearing the database log through the admin interface.
|
Chris@0
|
468 */
|
Chris@0
|
469 public function testDBLogAddAndClear() {
|
Chris@0
|
470 global $base_root;
|
Chris@0
|
471 // Get a count of how many watchdog entries already exist.
|
Chris@0
|
472 $count = db_query('SELECT COUNT(*) FROM {watchdog}')->fetchField();
|
Chris@0
|
473 $log = [
|
Chris@0
|
474 'channel' => 'system',
|
Chris@0
|
475 'message' => 'Log entry added to test the doClearTest clear down.',
|
Chris@0
|
476 'variables' => [],
|
Chris@0
|
477 'severity' => RfcLogLevel::NOTICE,
|
Chris@0
|
478 'link' => NULL,
|
Chris@0
|
479 'user' => $this->adminUser,
|
Chris@0
|
480 'uid' => $this->adminUser->id(),
|
Chris@0
|
481 'request_uri' => $base_root . \Drupal::request()->getRequestUri(),
|
Chris@0
|
482 'referer' => \Drupal::request()->server->get('HTTP_REFERER'),
|
Chris@0
|
483 'ip' => '127.0.0.1',
|
Chris@0
|
484 'timestamp' => REQUEST_TIME,
|
Chris@0
|
485 ];
|
Chris@0
|
486 // Add a watchdog entry.
|
Chris@0
|
487 $this->container->get('logger.dblog')->log($log['severity'], $log['message'], $log);
|
Chris@0
|
488 // Make sure the table count has actually been incremented.
|
Chris@0
|
489 $this->assertEqual($count + 1, db_query('SELECT COUNT(*) FROM {watchdog}')->fetchField(), format_string('\Drupal\dblog\Logger\DbLog->log() added an entry to the dblog :count', [':count' => $count]));
|
Chris@0
|
490 // Log in the admin user.
|
Chris@0
|
491 $this->drupalLogin($this->adminUser);
|
Chris@0
|
492 // Post in order to clear the database table.
|
Chris@0
|
493 $this->clearLogsEntries();
|
Chris@0
|
494 // Confirm that the logs should be cleared.
|
Chris@0
|
495 $this->drupalPostForm(NULL, [], 'Confirm');
|
Chris@0
|
496 // Count the rows in watchdog that previously related to the deleted user.
|
Chris@0
|
497 $count = db_query('SELECT COUNT(*) FROM {watchdog}')->fetchField();
|
Chris@0
|
498 $this->assertEqual($count, 0, format_string('DBLog contains :count records after a clear.', [':count' => $count]));
|
Chris@0
|
499 }
|
Chris@0
|
500
|
Chris@0
|
501 /**
|
Chris@0
|
502 * Tests the database log filter functionality at admin/reports/dblog.
|
Chris@0
|
503 */
|
Chris@0
|
504 public function testFilter() {
|
Chris@0
|
505 $this->drupalLogin($this->adminUser);
|
Chris@0
|
506
|
Chris@0
|
507 // Clear the log to ensure that only generated entries will be found.
|
Chris@0
|
508 db_delete('watchdog')->execute();
|
Chris@0
|
509
|
Chris@0
|
510 // Generate 9 random watchdog entries.
|
Chris@0
|
511 $type_names = [];
|
Chris@0
|
512 $types = [];
|
Chris@0
|
513 for ($i = 0; $i < 3; $i++) {
|
Chris@0
|
514 $type_names[] = $type_name = $this->randomMachineName();
|
Chris@0
|
515 $severity = RfcLogLevel::EMERGENCY;
|
Chris@0
|
516 for ($j = 0; $j < 3; $j++) {
|
Chris@0
|
517 $types[] = $type = [
|
Chris@0
|
518 'count' => $j + 1,
|
Chris@0
|
519 'type' => $type_name,
|
Chris@0
|
520 'severity' => $severity++,
|
Chris@0
|
521 ];
|
Chris@0
|
522 $this->generateLogEntries($type['count'], [
|
Chris@0
|
523 'channel' => $type['type'],
|
Chris@0
|
524 'severity' => $type['severity'],
|
Chris@0
|
525 ]);
|
Chris@0
|
526 }
|
Chris@0
|
527 }
|
Chris@0
|
528
|
Chris@0
|
529 // View the database log page.
|
Chris@0
|
530 $this->drupalGet('admin/reports/dblog');
|
Chris@0
|
531
|
Chris@0
|
532 // Confirm that all the entries are displayed.
|
Chris@0
|
533 $count = $this->getTypeCount($types);
|
Chris@0
|
534 foreach ($types as $key => $type) {
|
Chris@0
|
535 $this->assertEqual($count[$key], $type['count'], 'Count matched');
|
Chris@0
|
536 }
|
Chris@0
|
537
|
Chris@0
|
538 // Filter by each type and confirm that entries with various severities are
|
Chris@0
|
539 // displayed.
|
Chris@0
|
540 foreach ($type_names as $type_name) {
|
Chris@0
|
541 $this->filterLogsEntries($type_name);
|
Chris@0
|
542
|
Chris@0
|
543 // Count the number of entries of this type.
|
Chris@0
|
544 $type_count = 0;
|
Chris@0
|
545 foreach ($types as $type) {
|
Chris@0
|
546 if ($type['type'] == $type_name) {
|
Chris@0
|
547 $type_count += $type['count'];
|
Chris@0
|
548 }
|
Chris@0
|
549 }
|
Chris@0
|
550
|
Chris@0
|
551 $count = $this->getTypeCount($types);
|
Chris@0
|
552 $this->assertEqual(array_sum($count), $type_count, 'Count matched');
|
Chris@0
|
553 }
|
Chris@0
|
554
|
Chris@0
|
555 // Set the filter to match each of the two filter-type attributes and
|
Chris@0
|
556 // confirm the correct number of entries are displayed.
|
Chris@0
|
557 foreach ($types as $type) {
|
Chris@0
|
558 $this->filterLogsEntries($type['type'], $type['severity']);
|
Chris@0
|
559
|
Chris@0
|
560 $count = $this->getTypeCount($types);
|
Chris@0
|
561 $this->assertEqual(array_sum($count), $type['count'], 'Count matched');
|
Chris@0
|
562 }
|
Chris@0
|
563
|
Chris@0
|
564 $this->drupalGet('admin/reports/dblog', ['query' => ['order' => 'Type']]);
|
Chris@0
|
565 $this->assertResponse(200);
|
Chris@0
|
566 $this->assertText(t('Operations'), 'Operations text found');
|
Chris@0
|
567
|
Chris@0
|
568 // Clear all logs and make sure the confirmation message is found.
|
Chris@0
|
569 $this->clearLogsEntries();
|
Chris@0
|
570 // Confirm that the logs should be cleared.
|
Chris@0
|
571 $this->drupalPostForm(NULL, [], 'Confirm');
|
Chris@0
|
572 $this->assertText(t('Database log cleared.'), 'Confirmation message found');
|
Chris@0
|
573 }
|
Chris@0
|
574
|
Chris@0
|
575 /**
|
Chris@0
|
576 * Gets the database log event information from the browser page.
|
Chris@0
|
577 *
|
Chris@0
|
578 * @return array
|
Chris@0
|
579 * List of log events where each event is an array with following keys:
|
Chris@0
|
580 * - severity: (int) A database log severity constant.
|
Chris@0
|
581 * - type: (string) The type of database log event.
|
Chris@0
|
582 * - message: (string) The message for this database log event.
|
Chris@0
|
583 * - user: (string) The user associated with this database log event.
|
Chris@0
|
584 */
|
Chris@0
|
585 protected function getLogEntries() {
|
Chris@0
|
586 $entries = [];
|
Chris@0
|
587 if ($table = $this->getLogsEntriesTable()) {
|
Chris@0
|
588 foreach ($table as $row) {
|
Chris@0
|
589 $cells = $row->findAll('css', 'td');
|
Chris@0
|
590 $entries[] = [
|
Chris@0
|
591 'severity' => $this->getSeverityConstant($row->getAttribute('class')),
|
Chris@0
|
592 'type' => $cells[1]->getText(),
|
Chris@0
|
593 'message' => $cells[3]->getText(),
|
Chris@0
|
594 'user' => $cells[4]->getText(),
|
Chris@0
|
595 ];
|
Chris@0
|
596 }
|
Chris@0
|
597 }
|
Chris@0
|
598 return $entries;
|
Chris@0
|
599 }
|
Chris@0
|
600
|
Chris@0
|
601 /**
|
Chris@0
|
602 * Find the Logs table in the DOM.
|
Chris@0
|
603 *
|
Chris@0
|
604 * @return \SimpleXMLElement[]
|
Chris@0
|
605 * The return value of a xpath search.
|
Chris@0
|
606 */
|
Chris@0
|
607 protected function getLogsEntriesTable() {
|
Chris@0
|
608 return $this->xpath('.//table[@id="admin-dblog"]/tbody/tr');
|
Chris@0
|
609 }
|
Chris@0
|
610
|
Chris@0
|
611 /**
|
Chris@0
|
612 * Gets the count of database log entries by database log event type.
|
Chris@0
|
613 *
|
Chris@0
|
614 * @param array $types
|
Chris@0
|
615 * The type information to compare against.
|
Chris@0
|
616 *
|
Chris@0
|
617 * @return array
|
Chris@0
|
618 * The count of each type keyed by the key of the $types array.
|
Chris@0
|
619 */
|
Chris@0
|
620 protected function getTypeCount(array $types) {
|
Chris@0
|
621 $entries = $this->getLogEntries();
|
Chris@0
|
622 $count = array_fill(0, count($types), 0);
|
Chris@0
|
623 foreach ($entries as $entry) {
|
Chris@0
|
624 foreach ($types as $key => $type) {
|
Chris@0
|
625 if ($entry['type'] == $type['type'] && $entry['severity'] == $type['severity']) {
|
Chris@0
|
626 $count[$key]++;
|
Chris@0
|
627 break;
|
Chris@0
|
628 }
|
Chris@0
|
629 }
|
Chris@0
|
630 }
|
Chris@0
|
631 return $count;
|
Chris@0
|
632 }
|
Chris@0
|
633
|
Chris@0
|
634 /**
|
Chris@0
|
635 * Gets the watchdog severity constant corresponding to the CSS class.
|
Chris@0
|
636 *
|
Chris@0
|
637 * @param string $class
|
Chris@0
|
638 * CSS class attribute.
|
Chris@0
|
639 *
|
Chris@0
|
640 * @return int|null
|
Chris@0
|
641 * The watchdog severity constant or NULL if not found.
|
Chris@0
|
642 */
|
Chris@0
|
643 protected function getSeverityConstant($class) {
|
Chris@0
|
644 $map = array_flip(DbLogController::getLogLevelClassMap());
|
Chris@0
|
645
|
Chris@0
|
646 // Find the class that contains the severity.
|
Chris@0
|
647 $classes = explode(' ', $class);
|
Chris@0
|
648 foreach ($classes as $class) {
|
Chris@0
|
649 if (isset($map[$class])) {
|
Chris@0
|
650 return $map[$class];
|
Chris@0
|
651 }
|
Chris@0
|
652 }
|
Chris@0
|
653 return NULL;
|
Chris@0
|
654 }
|
Chris@0
|
655
|
Chris@0
|
656 /**
|
Chris@0
|
657 * Confirms that a log message appears on the database log overview screen.
|
Chris@0
|
658 *
|
Chris@0
|
659 * This function should only be used for the admin/reports/dblog page, because
|
Chris@0
|
660 * it checks for the message link text truncated to 56 characters. Other log
|
Chris@0
|
661 * pages have no detail links so they contain the full message text.
|
Chris@0
|
662 *
|
Chris@0
|
663 * @param string $log_message
|
Chris@0
|
664 * The database log message to check.
|
Chris@0
|
665 * @param string $message
|
Chris@0
|
666 * The message to pass to simpletest.
|
Chris@0
|
667 */
|
Chris@0
|
668 protected function assertLogMessage($log_message, $message) {
|
Chris@0
|
669 $message_text = Unicode::truncate(Html::decodeEntities(strip_tags($log_message)), 56, TRUE, TRUE);
|
Chris@0
|
670 $this->assertLink($message_text, 0, $message);
|
Chris@0
|
671 }
|
Chris@0
|
672
|
Chris@0
|
673 /**
|
Chris@0
|
674 * Tests that the details page displays correctly for a temporary user.
|
Chris@0
|
675 */
|
Chris@0
|
676 public function testTemporaryUser() {
|
Chris@0
|
677 // Create a temporary user.
|
Chris@0
|
678 $tempuser = $this->drupalCreateUser();
|
Chris@0
|
679 $tempuser_uid = $tempuser->id();
|
Chris@0
|
680
|
Chris@0
|
681 // Log in as the admin user.
|
Chris@0
|
682 $this->drupalLogin($this->adminUser);
|
Chris@0
|
683
|
Chris@0
|
684 // Generate a single watchdog entry.
|
Chris@0
|
685 $this->generateLogEntries(1, ['user' => $tempuser, 'uid' => $tempuser_uid]);
|
Chris@0
|
686 $wid = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
|
Chris@0
|
687
|
Chris@0
|
688 // Check if the full message displays on the details page.
|
Chris@0
|
689 $this->drupalGet('admin/reports/dblog/event/' . $wid);
|
Chris@0
|
690 $this->assertText('Dblog test log message');
|
Chris@0
|
691
|
Chris@0
|
692 // Delete the user.
|
Chris@0
|
693 user_delete($tempuser->id());
|
Chris@0
|
694 $this->drupalGet('user/' . $tempuser_uid);
|
Chris@0
|
695 $this->assertResponse(404);
|
Chris@0
|
696
|
Chris@0
|
697 // Check if the full message displays on the details page.
|
Chris@0
|
698 $this->drupalGet('admin/reports/dblog/event/' . $wid);
|
Chris@0
|
699 $this->assertText('Dblog test log message');
|
Chris@0
|
700 }
|
Chris@0
|
701
|
Chris@0
|
702 /**
|
Chris@0
|
703 * Make sure HTML tags are filtered out in the log overview links.
|
Chris@0
|
704 */
|
Chris@0
|
705 public function testOverviewLinks() {
|
Chris@0
|
706 $this->drupalLogin($this->adminUser);
|
Chris@0
|
707 $this->generateLogEntries(1, ['message' => "<script>alert('foo');</script><strong>Lorem</strong> ipsum dolor sit amet, consectetur adipiscing & elit."]);
|
Chris@0
|
708 $this->drupalGet('admin/reports/dblog');
|
Chris@0
|
709 $this->assertResponse(200);
|
Chris@0
|
710 // Make sure HTML tags are filtered out.
|
Chris@0
|
711 $this->assertRaw('title="alert('foo');Lorem');
|
Chris@0
|
712 $this->assertNoRaw("<script>alert('foo');</script>");
|
Chris@0
|
713
|
Chris@0
|
714 // Make sure HTML tags are filtered out in admin/reports/dblog/event/ too.
|
Chris@0
|
715 $this->generateLogEntries(1, ['message' => "<script>alert('foo');</script> <strong>Lorem ipsum</strong>"]);
|
Chris@0
|
716 $wid = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
|
Chris@0
|
717 $this->drupalGet('admin/reports/dblog/event/' . $wid);
|
Chris@0
|
718 $this->assertNoRaw("<script>alert('foo');</script>");
|
Chris@0
|
719 $this->assertRaw("alert('foo'); <strong>Lorem ipsum</strong>");
|
Chris@0
|
720 }
|
Chris@0
|
721
|
Chris@0
|
722 /**
|
Chris@0
|
723 * Test sorting for entries with the same timestamp.
|
Chris@0
|
724 */
|
Chris@0
|
725 public function testSameTimestampEntries() {
|
Chris@0
|
726 $this->drupalLogin($this->adminUser);
|
Chris@0
|
727
|
Chris@0
|
728 $this->generateLogEntries(1, ['timestamp' => 1498062000, 'type' => 'same_time', 'message' => 'First']);
|
Chris@0
|
729 $this->generateLogEntries(1, ['timestamp' => 1498062000, 'type' => 'same_time', 'message' => 'Second']);
|
Chris@0
|
730 $this->generateLogEntries(1, ['timestamp' => 1498062000, 'type' => 'same_time', 'message' => 'Third']);
|
Chris@0
|
731
|
Chris@0
|
732 $this->drupalGet('admin/reports/dblog');
|
Chris@0
|
733
|
Chris@0
|
734 $entries = $this->getLogEntries();
|
Chris@0
|
735 $this->assertEquals($entries[0]['message'], 'Third Entry #0');
|
Chris@0
|
736 $this->assertEquals($entries[1]['message'], 'Second Entry #0');
|
Chris@0
|
737 $this->assertEquals($entries[2]['message'], 'First Entry #0');
|
Chris@0
|
738 }
|
Chris@0
|
739
|
Chris@0
|
740 }
|