Unverified Commit 511bdacd authored by alexpott's avatar alexpott

Issue #2868725 by msankhala, Krzysztof Domański, xlin, cilefen, dagmar,...

Issue #2868725 by msankhala, Krzysztof Domański, xlin, cilefen, dagmar, dawehner, alexpott, joachim: Refactor how dblog module is rendering links in event details
parent d772fbb0
......@@ -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.
*
......
......@@ -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.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment