[BUGFIX] Better mocking in FormProtection tests 50/24750/2
authorChristian Kuhn <lolli@schwarzbu.ch>
Mon, 14 Oct 2013 19:39:41 +0000 (21:39 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 14 Oct 2013 20:11:37 +0000 (22:11 +0200)
Some FormProtection tests rely on an instantiated BE_USER object.
The patch takes care of proper mocking in this case and removes
the BE_USER instantiation from unit test bootstrap.

Change-Id: I85005db863bab10ccd61eb910153733b82e2b65e
Resolves: #52819
Releases: 6.2
Reviewed-on: https://review.typo3.org/24750
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/core/Build/UnitTestsBootstrap.php
typo3/sysext/core/Tests/Unit/Authentication/BackendUserAuthenticationTest.php
typo3/sysext/core/Tests/Unit/FormProtection/FormProtectionFactoryTest.php

index fb7adb6..7a09053 100644 (file)
@@ -107,13 +107,4 @@ require PATH_site . '/typo3/sysext/core/Classes/Core/Bootstrap.php';
        // TODO: Fix FAL tests which fatal without initialized storages
        ->initializeBackendUserMounts()
        // TODO: Needed at least for FormProtection tests
-       ->initializeLanguageObject();
-
-/**
- * This is needed for some tests (e.g. FormProtection)
- * TODO: properly mock these tests
- */
-$GLOBALS['BE_USER']->user = array(
-       'uid' => 42,
-       'username' => 'unit_test_fake_user',
-);
\ No newline at end of file
+       ->initializeLanguageObject();
\ No newline at end of file
index 567f758..09d7003 100644 (file)
@@ -77,10 +77,23 @@ class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        /**
         * @test
         */
-       public function logoffCleansFormProtection() {
-               $formProtection = $this->getMock('TYPO3\\CMS\\Core\\FormProtection\\BackendFormProtection', array('clean'));
-               $formProtection->expects($this->atLeastOnce())->method('clean');
-               \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::set('TYPO3\\CMS\\Core\\FormProtection\\BackendFormProtection', $formProtection);
+       public function logoffCleansFormProtectionIfBackendUserIsLoggedIn() {
+               $formProtection = $this->getMock(
+                       'TYPO3\\CMS\\Core\\FormProtection\\BackendFormProtection',
+                       array('clean'),
+                       array(),
+                       '',
+                       FALSE
+               );
+               \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::set(
+                       'TYPO3\\CMS\\Core\\FormProtection\\BackendFormProtection',
+                       $formProtection
+               );
+               // logoff() call the static factory that has a dependency to a valid BE_USER object. Mock this away
+               $GLOBALS['BE_USER'] = $this->getMock('TYPO3\CMS\Core\Authentication\BackendUserAuthentication', array(), array(), '', FALSE);
+               $GLOBALS['BE_USER']->user = array('uid' => uniqid());
+
+               $formProtection->expects($this->once())->method('clean');
                $this->fixture->logoff();
        }
 
index 554ef6a..90dca44 100644 (file)
@@ -63,6 +63,8 @@ class FormProtectionFactoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function getForTypeBackEndWithExistingBackEndReturnsBackEndFormProtection() {
+               $GLOBALS['BE_USER'] = $this->getMock('TYPO3\CMS\Core\Authentication\BackendUserAuthentication', array(), array(), '', FALSE);
+               $GLOBALS['BE_USER']->user = array('uid' => uniqid());
                $this->assertTrue(\TYPO3\CMS\Core\FormProtection\FormProtectionFactory::get('TYPO3\\CMS\\Core\\FormProtection\\BackendFormProtection') instanceof \TYPO3\CMS\Core\FormProtection\BackendFormProtection);
        }
 
@@ -70,6 +72,8 @@ class FormProtectionFactoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function getForTypeBackEndCalledTwoTimesReturnsTheSameInstance() {
+               $GLOBALS['BE_USER'] = $this->getMock('TYPO3\CMS\Core\Authentication\BackendUserAuthentication', array(), array(), '', FALSE);
+               $GLOBALS['BE_USER']->user = array('uid' => uniqid());
                $this->assertSame(\TYPO3\CMS\Core\FormProtection\FormProtectionFactory::get('TYPO3\\CMS\\Core\\FormProtection\\BackendFormProtection'), \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::get('TYPO3\\CMS\\Core\\FormProtection\\BackendFormProtection'));
        }
 
@@ -91,6 +95,8 @@ class FormProtectionFactoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function getForTypesInstallToolAndBackEndReturnsDifferentInstances() {
+               $GLOBALS['BE_USER'] = $this->getMock('TYPO3\CMS\Core\Authentication\BackendUserAuthentication', array(), array(), '', FALSE);
+               $GLOBALS['BE_USER']->user = array('uid' => uniqid());
                $this->assertNotSame(\TYPO3\CMS\Core\FormProtection\FormProtectionFactory::get('TYPO3\\CMS\\Core\\FormProtection\\InstallToolFormProtection'), \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::get('TYPO3\\CMS\\Core\\FormProtection\\BackendFormProtection'));
        }