From 06d88b310eae510ab7fb9e132a3f9013abdc3737 Mon Sep 17 00:00:00 2001
From: Anja Leichsenring <aleichsenring@ab-softlab.de>
Date: Sat, 4 Apr 2020 10:08:14 +0200
Subject: [PATCH] [TASK] Move ExceptionHandler tests into real class instances

The accessibleMockForAbstractClasses is a discouraged concept
anyways, and in this case the constructor definition in the
interface caused a test failure.
Moving the tests to concrete classes solves the failure and
also gets rid of the abstract class mock.

Resolves: #90942
Releases: master, 9.5
Change-Id: I902cabd3290761c1525b128eb18822199f337cb5
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/64066
Tested-by: Alexander Schnitzler <git@alexanderschnitzler.de>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Alexander Schnitzler <git@alexanderschnitzler.de>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
---
 .../Error/AbstractExceptionHandlerTest.php    | 69 -------------------
 .../Unit/Error/DebugExceptionHandlerTest.php  | 57 +++++++++++++++
 .../Error/ProductionExceptionHandlerTest.php  | 58 ++++++++++++++++
 3 files changed, 115 insertions(+), 69 deletions(-)
 delete mode 100644 typo3/sysext/core/Tests/Unit/Error/AbstractExceptionHandlerTest.php

diff --git a/typo3/sysext/core/Tests/Unit/Error/AbstractExceptionHandlerTest.php b/typo3/sysext/core/Tests/Unit/Error/AbstractExceptionHandlerTest.php
deleted file mode 100644
index 751f6040ae42..000000000000
--- a/typo3/sysext/core/Tests/Unit/Error/AbstractExceptionHandlerTest.php
+++ /dev/null
@@ -1,69 +0,0 @@
-<?php
-declare(strict_types = 1);
-namespace TYPO3\CMS\Core\Tests\Unit\Error;
-
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-
-use TYPO3\CMS\Core\Error\AbstractExceptionHandler;
-use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
-
-/**
- * Testcase for the AbstractExceptionHandlerTest class
- */
-class AbstractExceptionHandlerTest extends UnitTestCase
-{
-    /**
-     * Data provider with allowed contexts.
-     *
-     * @return array
-     */
-    public function exampleUrlsForTokenAnonymization(): array
-    {
-        return [
-            'url with valid token' => [
-                'http://localhost/typo3/index.php?M=foo&moduleToken=5f1f7d447f22886e8ea206693b0d530ccd6b2b36',
-                'http://localhost/typo3/index.php?M=foo&moduleToken=--AnonymizedToken--'
-            ],
-            'url with valid token in the middle' => [
-                'http://localhost/typo3/index.php?M=foo&moduleToken=5f1f7d447f22886e8ea206693b0d530ccd6b2b36&param=asdf',
-                'http://localhost/typo3/index.php?M=foo&moduleToken=--AnonymizedToken--&param=asdf'
-            ],
-            'url with invalid token' => [
-                'http://localhost/typo3/index.php?M=foo&moduleToken=5f1f7d447f22886e8/e',
-                'http://localhost/typo3/index.php?M=foo&moduleToken=5f1f7d447f22886e8/e',
-            ],
-            'url with empty token' => [
-                'http://localhost/typo3/index.php?M=foo&moduleToken=',
-                'http://localhost/typo3/index.php?M=foo&moduleToken=',
-            ],
-            'url with no token' => [
-                'http://localhost/typo3/index.php?M=foo',
-                'http://localhost/typo3/index.php?M=foo',
-            ],
-        ];
-    }
-
-    /**
-     * @test
-     * @dataProvider exampleUrlsForTokenAnonymization
-     * @param string $originalUrl
-     * @param string $expectedUrl
-     */
-    public function anonymizeTokenReturnsCorrectModifiedUrl(string $originalUrl, string $expectedUrl)
-    {
-        $mock = $this->getAccessibleMockForAbstractClass(AbstractExceptionHandler::class, ['dummy']);
-        $anonymizedUrl = $mock->_call('anonymizeToken', $originalUrl);
-        self::assertSame($expectedUrl, $anonymizedUrl);
-    }
-}
diff --git a/typo3/sysext/core/Tests/Unit/Error/DebugExceptionHandlerTest.php b/typo3/sysext/core/Tests/Unit/Error/DebugExceptionHandlerTest.php
index 49981e05a979..01941d670f15 100644
--- a/typo3/sysext/core/Tests/Unit/Error/DebugExceptionHandlerTest.php
+++ b/typo3/sysext/core/Tests/Unit/Error/DebugExceptionHandlerTest.php
@@ -14,6 +14,10 @@ namespace TYPO3\CMS\Core\Tests\Unit\Error;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Prophecy\Argument;
+use Psr\Log\LoggerInterface;
+use TYPO3\CMS\Core\Error\DebugExceptionHandler;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 /**
@@ -52,4 +56,57 @@ class DebugExceptionHandlerTest extends UnitTestCase
         self::assertStringContainsString(htmlspecialchars($message), $output);
         self::assertStringNotContainsString($message, $output);
     }
+
+    /**
+     * Data provider with allowed contexts.
+     *
+     * @return string[][]
+     */
+    public function exampleUrlsForTokenAnonymization(): array
+    {
+        return [
+            'url with valid token' => [
+                'http://localhost/typo3/index.php?M=foo&moduleToken=5f1f7d447f22886e8ea206693b0d530ccd6b2b36',
+                'http://localhost/typo3/index.php?M=foo&moduleToken=--AnonymizedToken--'
+            ],
+            'url with valid token in the middle' => [
+                'http://localhost/typo3/index.php?M=foo&moduleToken=5f1f7d447f22886e8ea206693b0d530ccd6b2b36&param=asdf',
+                'http://localhost/typo3/index.php?M=foo&moduleToken=--AnonymizedToken--&param=asdf'
+            ],
+            'url with invalid token' => [
+                'http://localhost/typo3/index.php?M=foo&moduleToken=5f1f7d447f22886e8/e',
+                'http://localhost/typo3/index.php?M=foo&moduleToken=5f1f7d447f22886e8/e',
+            ],
+            'url with empty token' => [
+                'http://localhost/typo3/index.php?M=foo&moduleToken=',
+                'http://localhost/typo3/index.php?M=foo&moduleToken=',
+            ],
+            'url with no token' => [
+                'http://localhost/typo3/index.php?M=foo',
+                'http://localhost/typo3/index.php?M=foo',
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     * @dataProvider exampleUrlsForTokenAnonymization
+     * @param string $originalUrl
+     * @param string $expectedUrl
+     */
+    public function logEntriesContainAnonymousTokens(string $originalUrl, string $expectedUrl)
+    {
+        $subject = new DebugExceptionHandler();
+        $logger = $this->prophesize(LoggerInterface::class);
+        $logger->critical(Argument::containingString($expectedUrl), Argument::cetera())->shouldBeCalled();
+        $subject->setLogger($logger->reveal());
+
+        GeneralUtility::setIndpEnv('TYPO3_REQUEST_URL', $originalUrl);
+
+        $exception = new \Exception('message', 1476049367);
+        ob_start();
+        $subject->echoExceptionWeb($exception);
+        // output is caught, so it does not pollute the test run
+        ob_end_clean();
+    }
 }
diff --git a/typo3/sysext/core/Tests/Unit/Error/ProductionExceptionHandlerTest.php b/typo3/sysext/core/Tests/Unit/Error/ProductionExceptionHandlerTest.php
index 3086e18da119..193255ef1bf4 100644
--- a/typo3/sysext/core/Tests/Unit/Error/ProductionExceptionHandlerTest.php
+++ b/typo3/sysext/core/Tests/Unit/Error/ProductionExceptionHandlerTest.php
@@ -14,6 +14,10 @@ namespace TYPO3\CMS\Core\Tests\Unit\Error;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Prophecy\Argument;
+use Psr\Log\LoggerInterface;
+use TYPO3\CMS\Core\Error\ProductionExceptionHandler;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 /**
@@ -75,4 +79,58 @@ class ProductionExceptionHandlerTest extends UnitTestCase
         self::assertStringContainsString(htmlspecialchars($title), $output);
         self::assertStringNotContainsString($title, $output);
     }
+
+    /**
+     * Data provider with allowed contexts.
+     *
+     * @return string[][]
+     */
+    public function exampleUrlsForTokenAnonymization(): array
+    {
+        return [
+            'url with valid token' => [
+                'http://localhost/typo3/index.php?M=foo&moduleToken=5f1f7d447f22886e8ea206693b0d530ccd6b2b36',
+                'http://localhost/typo3/index.php?M=foo&moduleToken=--AnonymizedToken--'
+            ],
+            'url with valid token in the middle' => [
+                'http://localhost/typo3/index.php?M=foo&moduleToken=5f1f7d447f22886e8ea206693b0d530ccd6b2b36&param=asdf',
+                'http://localhost/typo3/index.php?M=foo&moduleToken=--AnonymizedToken--&param=asdf'
+            ],
+            'url with invalid token' => [
+                'http://localhost/typo3/index.php?M=foo&moduleToken=5f1f7d447f22886e8/e',
+                'http://localhost/typo3/index.php?M=foo&moduleToken=5f1f7d447f22886e8/e',
+            ],
+            'url with empty token' => [
+                'http://localhost/typo3/index.php?M=foo&moduleToken=',
+                'http://localhost/typo3/index.php?M=foo&moduleToken=',
+            ],
+            'url with no token' => [
+                'http://localhost/typo3/index.php?M=foo',
+                'http://localhost/typo3/index.php?M=foo',
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     * @dataProvider exampleUrlsForTokenAnonymization
+     * @param string $originalUrl
+     * @param string $expectedUrl
+     */
+    public function logEntriesContainAnonymousTokens(string $originalUrl, string $expectedUrl)
+    {
+        $subject = new ProductionExceptionHandler();
+        $logger = $this->prophesize(LoggerInterface::class);
+        $logger->critical(Argument::containingString($expectedUrl), Argument::cetera())->shouldBeCalled();
+        $subject->setLogger($logger->reveal());
+
+        GeneralUtility::setIndpEnv('TYPO3_REQUEST_URL', $originalUrl);
+        $GLOBALS['BE_USER'] = null;
+
+        $exception = new \Exception('message', 1476049365);
+        ob_start();
+        $subject->echoExceptionWeb($exception);
+        // output is caught, so it does not pollute the test run
+        ob_end_clean();
+    }
 }
-- 
GitLab