From 511bdacdd29f39dc5cc3273a5a123efae08dd830 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Mon, 18 Mar 2019 11:35:06 +0000 Subject: [PATCH] =?UTF-8?q?Issue=20#2868725=20by=20msankhala,=20Krzysztof?= =?UTF-8?q?=20Doma=C5=84ski,=20xlin,=20cilefen,=20dagmar,=20dawehner,=20al?= =?UTF-8?q?expott,=20joachim:=20Refactor=20how=20dblog=20module=20is=20ren?= =?UTF-8?q?dering=20links=20in=20event=20details?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dblog/src/Controller/DbLogController.php | 23 ++++++++- .../dblog/tests/src/Functional/DbLogTest.php | 48 +++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/core/modules/dblog/src/Controller/DbLogController.php b/core/modules/dblog/src/Controller/DbLogController.php index dc0f6c4fc69e..54bc7ea6cabf 100644 --- a/core/modules/dblog/src/Controller/DbLogController.php +++ b/core/modules/dblog/src/Controller/DbLogController.php @@ -5,6 +5,7 @@ use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Xss; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Database\Connection; use Drupal\Core\Datetime\DateFormatterInterface; @@ -14,6 +15,7 @@ use Drupal\Core\Url; use Drupal\user\Entity\User; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Link; /** * Returns responses for dblog routes. @@ -264,11 +266,11 @@ public function eventDetails($event_id) { ], [ ['data' => $this->t('Location'), 'header' => TRUE], - $this->l($dblog->location, $dblog->location ? Url::fromUri($dblog->location) : Url::fromRoute('<none>')), + $this->createLink($dblog->location), ], [ ['data' => $this->t('Referrer'), 'header' => TRUE], - $this->l($dblog->referer, $dblog->referer ? Url::fromUri($dblog->referer) : Url::fromRoute('<none>')), + $this->createLink($dblog->referer), ], [ ['data' => $this->t('Message'), 'header' => TRUE], @@ -369,6 +371,23 @@ public function formatMessage($row) { return $message; } + /** + * Creates a Link object if the provided URI is valid. + * + * @param string|null $uri + * The uri string to convert into link if valid. + * + * @return \Drupal\Core\Link|string|null + * Return a Link object if the uri can be converted as a link. In case of + * empty uri or invalid, fallback to the provided $uri. + */ + protected function createLink($uri) { + if (UrlHelper::isValid($uri, TRUE)) { + return new Link($uri, Url::fromUri($uri)); + } + return $uri; + } + /** * Shows the most frequent log messages of a given event type. * diff --git a/core/modules/dblog/tests/src/Functional/DbLogTest.php b/core/modules/dblog/tests/src/Functional/DbLogTest.php index fcf05c41e9c2..0756315ee56d 100644 --- a/core/modules/dblog/tests/src/Functional/DbLogTest.php +++ b/core/modules/dblog/tests/src/Functional/DbLogTest.php @@ -113,10 +113,58 @@ public function testLogEventPage() { // Verify hostname. $this->assertRaw($context['ip'], 'Found hostname on the detail page.'); + // Verify location. + $this->assertRaw($context['request_uri'], 'Found location on the detail page.'); + // Verify severity. $this->assertText('Notice', 'The severity was properly displayed on the detail page.'); } + /** + * Test individual log event page with missing log attributes. + * + * In some cases few log attributes are missing. For example: + * - Missing referer: When request is made to a specific url directly and + * error occurred. In this case there is no referer. + * - Incorrect location: When location attribute is incorrect uri which can + * not be used to generate a valid link. + */ + public function testLogEventPageWithMissingInfo() { + $this->drupalLogin($this->adminUser); + $connection = Database::getConnection(); + + // Test log event page with missing referer. + $this->generateLogEntries(1, [ + 'referer' => NULL, + ]); + $wid = $connection->query('SELECT MAX(wid) FROM {watchdog}')->fetchField(); + $this->drupalGet('admin/reports/dblog/event/' . $wid); + + // Verify table headers are present, even though the referrer is missing. + $this->assertText('Referrer', 'Referrer header is present on the detail page.'); + + // Verify severity. + $this->assertText('Notice', 'The severity is properly displayed on the detail page.'); + + // Test log event page with incorrect location. + $request_uri = '/some/incorrect/url'; + $this->generateLogEntries(1, [ + 'request_uri' => $request_uri, + ]); + $wid = $connection->query('SELECT MAX(wid) FROM {watchdog}')->fetchField(); + $this->drupalGet('admin/reports/dblog/event/' . $wid); + + // Verify table headers are present. + $this->assertText('Location', 'Location header is present on the detail page.'); + + // Verify severity. + $this->assertText('Notice', 'The severity is properly displayed on the detail page.'); + + // Verify location is available as plain text. + $this->assertEquals($request_uri, $this->cssSelect('table.dblog-event > tbody > tr:nth-child(4) > td')[0]->getHtml()); + $this->assertNoLink($request_uri); + } + /** * Verifies setting of the database log row limit. * -- GitLab