[TASK] Doctrine: Migrate FrontendUserAuthentication 84/49284/5
authorMarkus Hoelzle <typo3@markus-hoelzle.de>
Mon, 1 Aug 2016 13:09:02 +0000 (15:09 +0200)
committerOliver Hader <oliver.hader@typo3.org>
Tue, 2 Aug 2016 11:38:22 +0000 (13:38 +0200)
Migrate FrontendUserAuthentication and rewrite failing
unit tests for FlashMessageQueue as functional tests

Resolves: #77330
Releases: master
Change-Id: Ib109bddc1a4b1272c179a0e2ff41363462c3b5e7
Reviewed-on: https://review.typo3.org/49284
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
typo3/sysext/core/Classes/Authentication/AbstractUserAuthentication.php
typo3/sysext/core/Tests/Functional/Messaging/FlashMessageQueueTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Messaging/FlashMessageQueueTest.php [deleted file]
typo3/sysext/frontend/Classes/Authentication/FrontendUserAuthentication.php

index 20189c0..ca9f7d6 100644 (file)
@@ -18,7 +18,6 @@ use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Crypto\Random;
 use TYPO3\CMS\Core\Database\Connection;
 use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Core\Database\DatabaseConnection;
 use TYPO3\CMS\Core\Database\Query\QueryHelper;
 use TYPO3\CMS\Core\Database\Query\Restriction\DefaultRestrictionContainer;
 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
@@ -1661,13 +1660,4 @@ abstract class AbstractUserAuthentication
 
         return $user;
     }
-
-    /**
-     * Get global database connection
-     * @return DatabaseConnection
-     */
-    protected function getDatabaseConnection()
-    {
-        return $GLOBALS['TYPO3_DB'];
-    }
 }
diff --git a/typo3/sysext/core/Tests/Functional/Messaging/FlashMessageQueueTest.php b/typo3/sysext/core/Tests/Functional/Messaging/FlashMessageQueueTest.php
new file mode 100644 (file)
index 0000000..da65594
--- /dev/null
@@ -0,0 +1,154 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Functional\Messaging;
+
+/*
+ * 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\Messaging\FlashMessage;
+use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
+use TYPO3\CMS\Core\Tests\FunctionalTestCase;
+
+/**
+ * Test case
+ */
+class FlashMessageQueueTest extends FunctionalTestCase
+{
+    /**
+     * @test
+     */
+    public function getAllMessagesContainsEnqueuedMessage()
+    {
+        $this->setUpBackendUserFromFixture(1);
+        $flashMessage = new FlashMessage('Foo', 'Bar', FlashMessage::OK, true);
+        $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
+        $flashMessageQueue->addMessage($flashMessage);
+        $this->assertEquals([$flashMessage], $flashMessageQueue->getAllMessages());
+    }
+
+    /**
+     * @test
+     */
+    public function messagesCanBeFilteredBySeverity()
+    {
+        $this->setUpBackendUserFromFixture(1);
+        $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
+        $messages = array(
+            0 => new FlashMessage('This is a test message', 1, FlashMessage::NOTICE),
+            1 => new FlashMessage('This is another test message', 2, FlashMessage::WARNING)
+        );
+        $flashMessageQueue->enqueue($messages[0]);
+        $flashMessageQueue->enqueue($messages[1]);
+
+        $filteredFlashMessages = $flashMessageQueue->getAllMessages(FlashMessage::NOTICE);
+
+        $this->assertEquals(count($filteredFlashMessages), 1);
+
+        reset($filteredFlashMessages);
+        $flashMessage = current($filteredFlashMessages);
+        $this->assertEquals($messages[0], $flashMessage);
+    }
+
+    /**
+     * @test
+     */
+    public function getAllMessagesAndFlushContainsEnqueuedMessage()
+    {
+        $this->setUpBackendUserFromFixture(1);
+        $flashMessage = new FlashMessage('Foo', 'Bar', FlashMessage::OK, true);
+        $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
+        $flashMessageQueue->addMessage($flashMessage);
+        $this->assertEquals([$flashMessage], $flashMessageQueue->getAllMessagesAndFlush());
+    }
+
+    /**
+     * @test
+     */
+    public function getAllMessagesAndFlushClearsSessionStack()
+    {
+        $this->setUpBackendUserFromFixture(1);
+        $flashMessage = new FlashMessage('Foo', 'Bar', FlashMessage::OK, true);
+        $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
+        $flashMessageQueue->addMessage($flashMessage);
+        $flashMessageQueue->getAllMessagesAndFlush();
+        $this->assertEquals([], $flashMessageQueue->getAllMessagesAndFlush());
+    }
+
+    /**
+     * @test
+     */
+    public function getMessagesAndFlushCanFilterBySeverity()
+    {
+        $this->setUpBackendUserFromFixture(1);
+        $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
+        $messages = array(
+            0 => new FlashMessage('This is a test message', 1, FlashMessage::NOTICE),
+            1 => new FlashMessage('This is another test message', 2, FlashMessage::WARNING)
+        );
+        $flashMessageQueue->addMessage($messages[0]);
+        $flashMessageQueue->addMessage($messages[1]);
+
+        $filteredFlashMessages = $flashMessageQueue->getAllMessagesAndFlush(FlashMessage::NOTICE);
+
+        $this->assertEquals(count($filteredFlashMessages), 1);
+
+        reset($filteredFlashMessages);
+        $flashMessage = current($filteredFlashMessages);
+        $this->assertEquals($messages[0], $flashMessage);
+
+        $this->assertEquals([], $flashMessageQueue->getAllMessages(FlashMessage::NOTICE));
+        $this->assertEquals([$messages[1]], array_values($flashMessageQueue->getAllMessages()));
+    }
+
+    /**
+     * @test
+     */
+    public function getAllMessagesReturnsSessionFlashMessageAndTransientFlashMessage()
+    {
+        $this->setUpBackendUserFromFixture(1);
+        $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
+        $flashMessage1 = new FlashMessage('Transient', 'Title', FlashMessage::OK, false);
+        $flashMessage2 = new FlashMessage('Session', 'Title', FlashMessage::OK, true);
+        $flashMessageQueue->addMessage($flashMessage1);
+        $flashMessageQueue->addMessage($flashMessage2);
+
+        $this->assertCount(2, $flashMessageQueue->getAllMessages());
+    }
+
+    /**
+     * @test
+     */
+    public function clearClearsTheQueue()
+    {
+        $this->setUpBackendUserFromFixture(1);
+        $flashMessage = new FlashMessage('Foo', 'Bar', FlashMessage::OK, true);
+        $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
+        $flashMessageQueue->addMessage($flashMessage);
+        $flashMessageQueue->clear();
+        $this->assertCount(0, $flashMessageQueue);
+    }
+
+    /**
+     * @test
+     */
+    public function toArrayOnlyRespectsTransientFlashMessages()
+    {
+        $this->setUpBackendUserFromFixture(1);
+        $flashMessageQueue = new FlashMessageQueue('core.template.flashMessages');
+        $flashMessage1 = new FlashMessage('Transient', 'Title', FlashMessage::OK, false);
+        $flashMessage2 = new FlashMessage('Session', 'Title', FlashMessage::OK, true);
+        $flashMessageQueue->addMessage($flashMessage1);
+        $flashMessageQueue->addMessage($flashMessage2);
+
+        $this->assertCount(1, $flashMessageQueue);
+    }
+}
diff --git a/typo3/sysext/core/Tests/Unit/Messaging/FlashMessageQueueTest.php b/typo3/sysext/core/Tests/Unit/Messaging/FlashMessageQueueTest.php
deleted file mode 100644 (file)
index e4c0aed..0000000
+++ /dev/null
@@ -1,182 +0,0 @@
-<?php
-namespace TYPO3\CMS\Core\Tests\Unit\Messaging;
-
-/*
- * 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!
- */
-
-/**
- * Test case
- */
-class FlashMessageQueueTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
-{
-    /**
-     * @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
-     */
-    protected $flashMessageQueue;
-
-    /**
-     * @var \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication|\PHPUnit_Framework_MockObject_MockObject
-     */
-    protected $frontendUser;
-
-    protected function setUp()
-    {
-        $this->frontendUser = $this->getMockBuilder(\TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication::class)
-            ->setMethods(array('dummy'))
-            ->getMock();
-        $this->flashMessageQueue = $this->getAccessibleMock(
-            \TYPO3\CMS\Core\Messaging\FlashMessageQueue::class,
-            array('getUserByContext'),
-            array('core.template.flashMessages')
-        );
-
-        $this->flashMessageQueue->expects($this->any())->method('getUserByContext')->will($this->returnValue($this->frontendUser));
-    }
-
-    /**
-     * @test
-     */
-    public function userSessionInitiallyIsEmpty()
-    {
-        $this->assertSame(array(), $this->flashMessageQueue->_call('getFlashMessagesFromSession'));
-    }
-
-    /**
-     * @test
-     */
-    public function enqueueTransientFlashMessageKeepsSessionEmpty()
-    {
-        $this->flashMessageQueue->enqueue(new \TYPO3\CMS\Core\Messaging\FlashMessage('Foo', 'Bar', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false));
-
-        $this->assertSame(array(), $this->flashMessageQueue->_call('getFlashMessagesFromSession'));
-    }
-
-    /**
-     * @test
-     */
-    public function enqueueSessionFlashMessageWritesSessionEntry()
-    {
-        $flashMessage = new \TYPO3\CMS\Core\Messaging\FlashMessage('Foo', 'Bar', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, true);
-        $this->flashMessageQueue->enqueue($flashMessage);
-
-        $this->assertSame(array($flashMessage), $this->flashMessageQueue->_call('getFlashMessagesFromSession'));
-    }
-
-    /**
-     * @test
-     */
-    public function getAllMessagesReturnsSessionFlashMessageAndTransientFlashMessage()
-    {
-        $flashMessage1 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false);
-        $flashMessage2 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Session', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, true);
-        $this->flashMessageQueue->enqueue($flashMessage1);
-        $this->flashMessageQueue->enqueue($flashMessage2);
-
-        $this->assertCount(2, $this->flashMessageQueue->getAllMessages());
-    }
-
-    /**
-     * @test
-     */
-    public function clearClearsTheQueue()
-    {
-        $flashMessage1 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false);
-        $flashMessage2 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false);
-        $this->flashMessageQueue->enqueue($flashMessage1);
-        $this->flashMessageQueue->enqueue($flashMessage2);
-        $this->flashMessageQueue->clear();
-
-        $this->assertSame(0, $this->flashMessageQueue->count());
-    }
-
-    /**
-     * @test
-     */
-    public function toArrayOnlyRespectsTransientFlashMessages()
-    {
-        $flashMessage1 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, false);
-        $flashMessage2 = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, true);
-        $this->flashMessageQueue->enqueue($flashMessage1);
-        $this->flashMessageQueue->enqueue($flashMessage2);
-
-        $this->assertCount(1, $this->flashMessageQueue->toArray());
-    }
-
-    /**
-     * @test
-     */
-    public function toArrayReturnsEmptyArrayWithForEmptyQueue()
-    {
-        $this->assertSame(array(), $this->flashMessageQueue->toArray());
-    }
-
-    /**
-     * @test
-     */
-    public function getAllMessagesAndFlushClearsSessionStack()
-    {
-        $flashMessage = new \TYPO3\CMS\Core\Messaging\FlashMessage('Transient', 'Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, true);
-        $this->flashMessageQueue->enqueue($flashMessage);
-        $this->flashMessageQueue->getAllMessagesAndFlush();
-
-        /** @var $frontendUser \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication */
-        $frontendUser = $this->flashMessageQueue->_call('getUserByContext');
-
-        $this->assertNull($frontendUser->getSessionData('core.template.flashMessages'));
-    }
-
-    /**
-     * @test
-     */
-    public function messagesCanBeFilteredBySeverity()
-    {
-        $messages = array(
-            0 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is a test message', 1, \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE),
-            1 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is another test message', 2, \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING)
-        );
-        $this->flashMessageQueue->enqueue($messages[0]);
-        $this->flashMessageQueue->enqueue($messages[1]);
-
-        $filteredFlashMessages = $this->flashMessageQueue->getAllMessages(\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE);
-
-        $this->assertEquals(count($filteredFlashMessages), 1);
-
-        reset($filteredFlashMessages);
-        $flashMessage = current($filteredFlashMessages);
-        $this->assertEquals($messages[0], $flashMessage);
-    }
-
-    /**
-     * @test
-     */
-    public function getMessagesAndFlushCanAlsoFilterBySeverity()
-    {
-        $messages = array(
-            0 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is a test message', 1, \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE),
-            1 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is another test message', 2, \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING)
-        );
-        $this->flashMessageQueue->enqueue($messages[0]);
-        $this->flashMessageQueue->enqueue($messages[1]);
-
-        $filteredFlashMessages = $this->flashMessageQueue->getAllMessagesAndFlush(\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE);
-
-        $this->assertEquals(count($filteredFlashMessages), 1);
-
-        reset($filteredFlashMessages);
-        $flashMessage = current($filteredFlashMessages);
-        $this->assertEquals($messages[0], $flashMessage);
-
-        $this->assertEquals(array(), $this->flashMessageQueue->getAllMessages(\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE));
-        $this->assertEquals(array($messages[1]), array_values($this->flashMessageQueue->getAllMessages()));
-    }
-}
index 133a431..32b9eb1 100644 (file)
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\Frontend\Authentication;
  */
 
 use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
@@ -399,16 +400,18 @@ class FrontendUserAuthentication extends AbstractUserAuthentication
      */
     public function fetchSessionData()
     {
-        $db = $this->getDatabaseConnection();
         // Gets SesData if any AND if not already selected by session fixation check in ->isExistingSessionRecord()
         if ($this->id && empty($this->sesData)) {
-            $statement = $db->prepare_SELECTquery('*', 'fe_session_data', 'hash = :hash');
-            $statement->execute(array(':hash' => $this->id));
-            if (($sesDataRow = $statement->fetch()) !== false) {
+            $sesDataRow = GeneralUtility::makeInstance(ConnectionPool::class)
+                ->getConnectionForTable('fe_session_data')->select(
+                    ['*'],
+                    'fe_session_data',
+                    ['hash' => $this->id]
+                )->fetch();
+            if ($sesDataRow !== null) {
                 $this->sesData = unserialize($sesDataRow['content']);
                 $this->sessionDataTimestamp = $sesDataRow['tstamp'];
             }
-            $statement->free();
         }
     }
 
@@ -427,7 +430,8 @@ class FrontendUserAuthentication extends AbstractUserAuthentication
         if ($this->userData_change) {
             $this->writeUC('');
         }
-        $db = $this->getDatabaseConnection();
+        $databaseConnection = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getConnectionForTable('fe_session_data');
         if ($this->sesData_change && $this->id) {
             if (empty($this->sesData)) {
                 // Remove session-data
@@ -444,7 +448,7 @@ class FrontendUserAuthentication extends AbstractUserAuthentication
                     'tstamp' => $GLOBALS['EXEC_TIME']
                 );
                 $this->sessionDataTimestamp = $GLOBALS['EXEC_TIME'];
-                $db->exec_INSERTquery('fe_session_data', $insertFields);
+                $databaseConnection->insert('fe_session_data', $insertFields);
                 // Now set the cookie (= fix the session)
                 $this->setSessionCookie();
             } else {
@@ -454,7 +458,7 @@ class FrontendUserAuthentication extends AbstractUserAuthentication
                     'tstamp' => $GLOBALS['EXEC_TIME']
                 );
                 $this->sessionDataTimestamp = $GLOBALS['EXEC_TIME'];
-                $db->exec_UPDATEquery('fe_session_data', 'hash=' . $db->fullQuoteStr($this->id, 'fe_session_data'), $updateFields);
+                $databaseConnection->update('fe_session_data', $updateFields, ['hash' => $this->id]);
             }
         }
     }
@@ -466,9 +470,11 @@ class FrontendUserAuthentication extends AbstractUserAuthentication
      */
     public function removeSessionData()
     {
-        $db = $this->getDatabaseConnection();
         $this->sessionDataTimestamp = null;
-        $db->exec_DELETEquery('fe_session_data', 'hash=' . $db->fullQuoteStr($this->id, 'fe_session_data'));
+        GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('fe_session_data')->delete(
+                'fe_session_data',
+                ['hash' => $this->id]
+            );
     }
 
     /**
@@ -494,15 +500,15 @@ class FrontendUserAuthentication extends AbstractUserAuthentication
      */
     protected function regenerateSessionId()
     {
-        $db = $this->getDatabaseConnection();
         $oldSessionId = $this->id;
         parent::regenerateSessionId();
         // Update session data with new ID
-        $db->exec_UPDATEquery(
-            'fe_session_data',
-            'hash=' . $db->fullQuoteStr($oldSessionId, 'fe_session_data'),
-            array('hash' => $this->id)
-        );
+        GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('fe_session_data')
+            ->update(
+                'fe_session_data',
+                ['hash' => $this->id],
+                ['hash' => $oldSessionId]
+            );
 
         // We force the cookie to be set later in the authentication process
         $this->dontSetCookie = false;
@@ -517,7 +523,10 @@ class FrontendUserAuthentication extends AbstractUserAuthentication
     public function gc()
     {
         $timeoutTimeStamp = (int)($GLOBALS['EXEC_TIME'] - $this->sessionDataLifetime);
-        $this->getDatabaseConnection()->exec_DELETEquery('fe_session_data', 'tstamp < ' . $timeoutTimeStamp);
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('fe_session_data');
+        $queryBuilder->delete('fe_session_data')
+            ->where($queryBuilder->expr()->lt('tstamp', $timeoutTimeStamp))
+            ->execute();
         parent::gc();
     }
 
@@ -662,15 +671,13 @@ class FrontendUserAuthentication extends AbstractUserAuthentication
         $count = parent::isExistingSessionRecord($id);
         // Check if there are any fe_session_data records for the session ID the client claims to have
         if ($count == false) {
-            $statement = $this->getDatabaseConnection()->prepare_SELECTquery('content,tstamp', 'fe_session_data', 'hash = :hash');
-            $res = $statement->execute(array(':hash' => $id));
-            if ($res !== false) {
-                if ($sesDataRow = $statement->fetch()) {
-                    $count = true;
-                    $this->sesData = unserialize($sesDataRow['content']);
-                    $this->sessionDataTimestamp = $sesDataRow['tstamp'];
-                }
-                $statement->free();
+            $sesDataRow = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('fe_session_data')
+                ->select(['content', 'tstamp'], 'fe_session_data', ['hash' => $id])->fetch();
+
+            if ($sesDataRow !== null) {
+                $count = true;
+                $this->sesData = unserialize($sesDataRow['content']);
+                $this->sessionDataTimestamp = $sesDataRow['tstamp'];
             }
         }
         return $count;