comparison core/modules/dblog/tests/src/Functional/DbLogTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
2 2
3 namespace Drupal\Tests\dblog\Functional; 3 namespace Drupal\Tests\dblog\Functional;
4 4
5 use Drupal\Component\Utility\Html; 5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Utility\Unicode; 6 use Drupal\Component\Utility\Unicode;
7 use Drupal\Core\Database\Database;
7 use Drupal\Core\Logger\RfcLogLevel; 8 use Drupal\Core\Logger\RfcLogLevel;
8 use Drupal\Core\Url; 9 use Drupal\Core\Url;
9 use Drupal\dblog\Controller\DbLogController; 10 use Drupal\dblog\Controller\DbLogController;
10 use Drupal\Tests\BrowserTestBase; 11 use Drupal\Tests\BrowserTestBase;
11 12
110 $this->assertLinkByHref($context['referer']); 111 $this->assertLinkByHref($context['referer']);
111 112
112 // Verify hostname. 113 // Verify hostname.
113 $this->assertRaw($context['ip'], 'Found hostname on the detail page.'); 114 $this->assertRaw($context['ip'], 'Found hostname on the detail page.');
114 115
116 // Verify location.
117 $this->assertRaw($context['request_uri'], 'Found location on the detail page.');
118
115 // Verify severity. 119 // Verify severity.
116 $this->assertText('Notice', 'The severity was properly displayed on the detail page.'); 120 $this->assertText('Notice', 'The severity was properly displayed on the detail page.');
121 }
122
123 /**
124 * Test individual log event page with missing log attributes.
125 *
126 * In some cases few log attributes are missing. For example:
127 * - Missing referer: When request is made to a specific url directly and
128 * error occurred. In this case there is no referer.
129 * - Incorrect location: When location attribute is incorrect uri which can
130 * not be used to generate a valid link.
131 */
132 public function testLogEventPageWithMissingInfo() {
133 $this->drupalLogin($this->adminUser);
134 $connection = Database::getConnection();
135
136 // Test log event page with missing referer.
137 $this->generateLogEntries(1, [
138 'referer' => NULL,
139 ]);
140 $wid = $connection->query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
141 $this->drupalGet('admin/reports/dblog/event/' . $wid);
142
143 // Verify table headers are present, even though the referrer is missing.
144 $this->assertText('Referrer', 'Referrer header is present on the detail page.');
145
146 // Verify severity.
147 $this->assertText('Notice', 'The severity is properly displayed on the detail page.');
148
149 // Test log event page with incorrect location.
150 $request_uri = '/some/incorrect/url';
151 $this->generateLogEntries(1, [
152 'request_uri' => $request_uri,
153 ]);
154 $wid = $connection->query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
155 $this->drupalGet('admin/reports/dblog/event/' . $wid);
156
157 // Verify table headers are present.
158 $this->assertText('Location', 'Location header is present on the detail page.');
159
160 // Verify severity.
161 $this->assertText('Notice', 'The severity is properly displayed on the detail page.');
162
163 // Verify location is available as plain text.
164 $this->assertEquals($request_uri, $this->cssSelect('table.dblog-event > tbody > tr:nth-child(4) > td')[0]->getHtml());
165 $this->assertNoLink($request_uri);
117 } 166 }
118 167
119 /** 168 /**
120 * Verifies setting of the database log row limit. 169 * Verifies setting of the database log row limit.
121 * 170 *
262 $this->generateLogEntries(1, [ 311 $this->generateLogEntries(1, [
263 'message' => $message, 312 'message' => $message,
264 'link' => $link, 313 'link' => $link,
265 ]); 314 ]);
266 315
267 $result = db_query_range('SELECT wid FROM {watchdog} ORDER BY wid DESC', 0, 1); 316 $result = Database::getConnection()->queryRange('SELECT wid FROM {watchdog} ORDER BY wid DESC', 0, 1);
268 $this->drupalGet('admin/reports/dblog/event/' . $result->fetchField()); 317 $this->drupalGet('admin/reports/dblog/event/' . $result->fetchField());
269 318
270 // Check if the link exists (unescaped). 319 // Check if the link exists (unescaped).
271 $this->assertRaw($link); 320 $this->assertRaw($link);
272 } 321 }
301 $result = db_query('SELECT wid FROM {watchdog} WHERE uid = :uid', [':uid' => $user->id()]); 350 $result = db_query('SELECT wid FROM {watchdog} WHERE uid = :uid', [':uid' => $user->id()]);
302 foreach ($result as $row) { 351 foreach ($result as $row) {
303 $ids[] = $row->wid; 352 $ids[] = $row->wid;
304 } 353 }
305 $count_before = (isset($ids)) ? count($ids) : 0; 354 $count_before = (isset($ids)) ? count($ids) : 0;
306 $this->assertTrue($count_before > 0, format_string('DBLog contains @count records for @name', ['@count' => $count_before, '@name' => $user->getUsername()])); 355 $this->assertTrue($count_before > 0, format_string('DBLog contains @count records for @name', ['@count' => $count_before, '@name' => $user->getAccountName()]));
307 356
308 // Log in the admin user. 357 // Log in the admin user.
309 $this->drupalLogin($this->adminUser); 358 $this->drupalLogin($this->adminUser);
310 // Delete the user created at the start of this test. 359 // Delete the user created at the start of this test.
311 // We need to POST here to invoke batch_process() in the internal browser. 360 // We need to POST here to invoke batch_process() in the internal browser.
474 'channel' => 'system', 523 'channel' => 'system',
475 'message' => 'Log entry added to test the doClearTest clear down.', 524 'message' => 'Log entry added to test the doClearTest clear down.',
476 'variables' => [], 525 'variables' => [],
477 'severity' => RfcLogLevel::NOTICE, 526 'severity' => RfcLogLevel::NOTICE,
478 'link' => NULL, 527 'link' => NULL,
479 'user' => $this->adminUser,
480 'uid' => $this->adminUser->id(), 528 'uid' => $this->adminUser->id(),
481 'request_uri' => $base_root . \Drupal::request()->getRequestUri(), 529 'request_uri' => $base_root . \Drupal::request()->getRequestUri(),
482 'referer' => \Drupal::request()->server->get('HTTP_REFERER'), 530 'referer' => \Drupal::request()->server->get('HTTP_REFERER'),
483 'ip' => '127.0.0.1', 531 'ip' => '127.0.0.1',
484 'timestamp' => REQUEST_TIME, 532 'timestamp' => REQUEST_TIME,
503 */ 551 */
504 public function testFilter() { 552 public function testFilter() {
505 $this->drupalLogin($this->adminUser); 553 $this->drupalLogin($this->adminUser);
506 554
507 // Clear the log to ensure that only generated entries will be found. 555 // Clear the log to ensure that only generated entries will be found.
508 db_delete('watchdog')->execute(); 556 Database::getConnection()->delete('watchdog')->execute();
509 557
510 // Generate 9 random watchdog entries. 558 // Generate 9 random watchdog entries.
511 $type_names = []; 559 $type_names = [];
512 $types = []; 560 $types = [];
513 for ($i = 0; $i < 3; $i++) { 561 for ($i = 0; $i < 3; $i++) {