From 08582f73614b0ae42cd9190907450324cd58adf5 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Tue, 4 Feb 2020 12:21:46 +0000
Subject: [PATCH] =?UTF-8?q?Issue=20#3101108=20by=20Krzysztof=20Doma=C5=84s?=
 =?UTF-8?q?ki,=20manuel.adan,=20Hardik=5FPatel=5F12,=20dagmar:=20Dblog=20e?=
 =?UTF-8?q?vent=20page=20for=20non-existing=20event=20returns=20200,=20sho?=
 =?UTF-8?q?uld=20be=20404?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../dblog/src/Controller/DbLogController.php  | 116 ++++++++++--------
 .../dblog/tests/src/Functional/DbLogTest.php  |  13 ++
 2 files changed, 75 insertions(+), 54 deletions(-)

diff --git a/core/modules/dblog/src/Controller/DbLogController.php b/core/modules/dblog/src/Controller/DbLogController.php
index c61e50166603..64dbf36eb51d 100644
--- a/core/modules/dblog/src/Controller/DbLogController.php
+++ b/core/modules/dblog/src/Controller/DbLogController.php
@@ -17,6 +17,7 @@
 use Drupal\user\Entity\User;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Drupal\Core\Link;
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 
 /**
  * Returns responses for dblog routes.
@@ -242,64 +243,71 @@ public function overview() {
    * @return array
    *   If the ID is located in the Database Logging table, a build array in the
    *   format expected by \Drupal\Core\Render\RendererInterface::render().
+   *
+   * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
+   *   If no event found for the given ID.
    */
   public function eventDetails($event_id) {
-    $build = [];
-    if ($dblog = $this->database->query('SELECT w.*, u.uid FROM {watchdog} w LEFT JOIN {users} u ON u.uid = w.uid WHERE w.wid = :id', [':id' => $event_id])->fetchObject()) {
-      $severity = RfcLogLevel::getLevels();
-      $message = $this->formatMessage($dblog);
-      $username = [
-        '#theme' => 'username',
-        '#account' => $dblog->uid ? $this->userStorage->load($dblog->uid) : User::getAnonymousUser(),
-      ];
-      $rows = [
-        [
-          ['data' => $this->t('Type'), 'header' => TRUE],
-          $this->t($dblog->type),
-        ],
-        [
-          ['data' => $this->t('Date'), 'header' => TRUE],
-          $this->dateFormatter->format($dblog->timestamp, 'long'),
-        ],
-        [
-          ['data' => $this->t('User'), 'header' => TRUE],
-          ['data' => $username],
-        ],
-        [
-          ['data' => $this->t('Location'), 'header' => TRUE],
-          $this->createLink($dblog->location),
-        ],
-        [
-          ['data' => $this->t('Referrer'), 'header' => TRUE],
-          $this->createLink($dblog->referer),
-        ],
-        [
-          ['data' => $this->t('Message'), 'header' => TRUE],
-          $message,
-        ],
-        [
-          ['data' => $this->t('Severity'), 'header' => TRUE],
-          $severity[$dblog->severity],
-        ],
-        [
-          ['data' => $this->t('Hostname'), 'header' => TRUE],
-          $dblog->hostname,
-        ],
-        [
-          ['data' => $this->t('Operations'), 'header' => TRUE],
-          ['data' => ['#markup' => $dblog->link]],
-        ],
-      ];
-      $build['dblog_table'] = [
-        '#type' => 'table',
-        '#rows' => $rows,
-        '#attributes' => ['class' => ['dblog-event']],
-        '#attached' => [
-          'library' => ['dblog/drupal.dblog'],
-        ],
-      ];
+    $dblog = $this->database->query('SELECT w.*, u.uid FROM {watchdog} w LEFT JOIN {users} u ON u.uid = w.uid WHERE w.wid = :id', [':id' => $event_id])->fetchObject();
+
+    if (empty($dblog)) {
+      throw new NotFoundHttpException();
     }
 
+    $build = [];
+    $severity = RfcLogLevel::getLevels();
+    $message = $this->formatMessage($dblog);
+    $username = [
+      '#theme' => 'username',
+      '#account' => $dblog->uid ? $this->userStorage->load($dblog->uid) : User::getAnonymousUser(),
+    ];
+    $rows = [
+      [
+        ['data' => $this->t('Type'), 'header' => TRUE],
+        $this->t($dblog->type),
+      ],
+      [
+        ['data' => $this->t('Date'), 'header' => TRUE],
+        $this->dateFormatter->format($dblog->timestamp, 'long'),
+      ],
+      [
+        ['data' => $this->t('User'), 'header' => TRUE],
+        ['data' => $username],
+      ],
+      [
+        ['data' => $this->t('Location'), 'header' => TRUE],
+        $this->createLink($dblog->location),
+      ],
+      [
+        ['data' => $this->t('Referrer'), 'header' => TRUE],
+        $this->createLink($dblog->referer),
+      ],
+      [
+        ['data' => $this->t('Message'), 'header' => TRUE],
+        $message,
+      ],
+      [
+        ['data' => $this->t('Severity'), 'header' => TRUE],
+        $severity[$dblog->severity],
+      ],
+      [
+        ['data' => $this->t('Hostname'), 'header' => TRUE],
+        $dblog->hostname,
+      ],
+      [
+        ['data' => $this->t('Operations'), 'header' => TRUE],
+        ['data' => ['#markup' => $dblog->link]],
+      ],
+    ];
+    $build['dblog_table'] = [
+      '#type' => 'table',
+      '#rows' => $rows,
+      '#attributes' => ['class' => ['dblog-event']],
+      '#attached' => [
+        'library' => ['dblog/drupal.dblog'],
+      ],
+    ];
+
     return $build;
   }
 
diff --git a/core/modules/dblog/tests/src/Functional/DbLogTest.php b/core/modules/dblog/tests/src/Functional/DbLogTest.php
index 9f7475b82dbc..79298d83bc69 100644
--- a/core/modules/dblog/tests/src/Functional/DbLogTest.php
+++ b/core/modules/dblog/tests/src/Functional/DbLogTest.php
@@ -135,6 +135,19 @@ public function testLogEventPage() {
     $this->assertText('Notice', 'The severity was properly displayed on the detail page.');
   }
 
+  /**
+   * Test not-existing log event page.
+   */
+  public function testLogEventNotFoundPage() {
+    // Login the admin user.
+    $this->drupalLogin($this->adminUser);
+
+    // Try to read details of not existing event.
+    $this->drupalGet('admin/reports/dblog/event/999999');
+    // Verify 404 response.
+    $this->assertResponse(404);
+  }
+
   /**
    * Test individual log event page with missing log attributes.
    *
-- 
GitLab