[BUGFIX] Tests: Better mocking in ext:core 94/28394/2
authorChristian Kuhn <lolli@schwarzbu.ch>
Fri, 14 Mar 2014 16:19:24 +0000 (17:19 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Fri, 14 Mar 2014 18:58:59 +0000 (19:58 +0100)
Some tests still rely on initialized $GLOBALS['TYPO3_DB']. The patch
handles some easy cases in ext:core.

Change-Id: I5b720154035cc5f8b156a9358806673daaf9c9ec
Resolves: #56918
Related: #51436
Releases: 6.2
Reviewed-on: https://review.typo3.org/28394
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/core/Tests/Unit/Authentication/BackendUserAuthenticationTest.php
typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php

index 9c480c5..cd7c1ad 100644 (file)
@@ -54,20 +54,12 @@ class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                'recursivedeleteFolder' => FALSE
        );
 
-
-       /**
-        * @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
-        */
-       protected $fixture = NULL;
-
        public function setUp() {
                // reset hooks
                $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'] = array();
-               $this->fixture = new \TYPO3\CMS\Core\Authentication\BackendUserAuthentication();
        }
 
        public function tearDown() {
-               unset($this->fixture);
                \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::purgeInstances();
                parent::tearDown();
        }
@@ -86,23 +78,26 @@ class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        '',
                        FALSE
                );
+               $formProtection->expects($this->once())->method('clean');
+
                \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'] = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array(), array(), '', FALSE);
                $GLOBALS['BE_USER']->user = array('uid' => uniqid());
+               $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
 
-               $formProtection->expects($this->once())->method('clean');
-               $this->fixture->logoff();
+               $subject = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('dummy'), array(), '', FALSE);
+               $subject->logoff();
        }
 
        /**
         * @return array
         */
        public function getTSConfigDataProvider() {
-
                $completeConfiguration = array(
                        'value' => 'oneValue',
                        'value.' => array('oneProperty' => 'oneValue'),
@@ -244,9 +239,10 @@ class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function getTSConfigReturnsCorrectArrayForGivenObjectString(array $completeConfiguration, $objectString, array $expectedConfiguration) {
-               $this->fixture->userTS = $completeConfiguration;
+               $subject = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('dummy'), array(), '', FALSE);
+               $subject->userTS = $completeConfiguration;
 
-               $actualConfiguration = $this->fixture->getTSConfig($objectString);
+               $actualConfiguration = $subject->getTSConfig($objectString);
                $this->assertSame($expectedConfiguration, $actualConfiguration);
        }
 
@@ -309,14 +305,14 @@ class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @dataProvider getFilePermissionsTakesUserDefaultAndStoragePermissionsIntoAccountIfUserIsNotAdminDataProvider
         */
        public function getFilePermissionsTakesUserDefaultPermissionsFromTsConfigIntoAccountIfUserIsNotAdmin(array $userTsConfiguration) {
-               $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('isAdmin'));
+               $subject = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('isAdmin'));
 
-               $this->fixture
+               $subject
                        ->expects($this->any())
                        ->method('isAdmin')
                        ->will($this->returnValue(FALSE));
 
-               $this->fixture->userTS = array(
+               $subject->userTS = array(
                        'permissions.' => array(
                                'file.' => array(
                                        'default.' => $userTsConfiguration
@@ -332,7 +328,7 @@ class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        }
                );
 
-               $this->assertEquals($expectedPermissions, $this->fixture->getFilePermissions());
+               $this->assertEquals($expectedPermissions, $subject->getFilePermissions());
        }
 
        /**
@@ -446,21 +442,21 @@ class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @dataProvider getFilePermissionsFromStorageDataProvider
         */
        public function getFilePermissionsFromStorageOverwritesDefaultPermissions(array $defaultPermissions, $storageUid, array $storagePermissions, array $expectedPermissions) {
-               $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('isAdmin', 'getFilePermissions'));
+               $subject = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('isAdmin', 'getFilePermissions'));
                $storageMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
                $storageMock->expects($this->any())->method('getUid')->will($this->returnValue($storageUid));
 
-               $this->fixture
+               $subject
                        ->expects($this->any())
                        ->method('isAdmin')
                        ->will($this->returnValue(FALSE));
 
-               $this->fixture
+               $subject
                        ->expects($this->any())
                        ->method('getFilePermissions')
                        ->will($this->returnValue($defaultPermissions));
 
-               $this->fixture->userTS = array(
+               $subject->userTS = array(
                        'permissions.' => array(
                                'file.' => array(
                                        'storage.' => array(
@@ -470,7 +466,7 @@ class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        )
                );
 
-               $this->assertEquals($expectedPermissions, $this->fixture->getFilePermissionsForStorage($storageMock));
+               $this->assertEquals($expectedPermissions, $subject->getFilePermissionsForStorage($storageMock));
        }
 
        /**
@@ -481,21 +477,21 @@ class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @dataProvider getFilePermissionsFromStorageDataProvider
         */
        public function getFilePermissionsFromStorageAlwaysReturnsDefaultPermissionsForAdmins(array $defaultPermissions, $storageUid, array $storagePermissions) {
-               $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('isAdmin', 'getFilePermissions'));
+               $subject = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('isAdmin', 'getFilePermissions'));
                $storageMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
                $storageMock->expects($this->any())->method('getUid')->will($this->returnValue($storageUid));
 
-               $this->fixture
+               $subject
                        ->expects($this->any())
                        ->method('isAdmin')
                        ->will($this->returnValue(TRUE));
 
-               $this->fixture
+               $subject
                        ->expects($this->any())
                        ->method('getFilePermissions')
                        ->will($this->returnValue($defaultPermissions));
 
-               $this->fixture->userTS = array(
+               $subject->userTS = array(
                        'permissions.' => array(
                                'file.' => array(
                                        'storage.' => array(
@@ -505,7 +501,7 @@ class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        )
                );
 
-               $this->assertEquals($defaultPermissions, $this->fixture->getFilePermissionsForStorage($storageMock));
+               $this->assertEquals($defaultPermissions, $subject->getFilePermissionsForStorage($storageMock));
        }
 
        /**
@@ -647,25 +643,25 @@ class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @dataProvider getFilePermissionsTakesUserDefaultPermissionsFromRecordIntoAccountIfUserIsNotAdminDataProvider
         */
        public function getFilePermissionsTakesUserDefaultPermissionsFromRecordIntoAccountIfUserIsNotAdmin($permissionValue, $expectedPermissions) {
-               $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('isAdmin'));
+               $subject = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('isAdmin'));
 
-               $this->fixture
+               $subject
                        ->expects($this->any())
                        ->method('isAdmin')
                        ->will($this->returnValue(FALSE));
 
-               $this->fixture->userTS = array();
-               $this->fixture->groupData['file_permissions'] = $permissionValue;
-               $this->assertEquals($expectedPermissions, $this->fixture->getFilePermissions());
+               $subject->userTS = array();
+               $subject->groupData['file_permissions'] = $permissionValue;
+               $this->assertEquals($expectedPermissions, $subject->getFilePermissions());
        }
 
        /**
         * @test
         */
        public function getFilePermissionsGrantsAllPermissionsToAdminUsers() {
-               $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('isAdmin'));
+               $subject = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('isAdmin'));
 
-               $this->fixture
+               $subject
                        ->expects($this->any())
                        ->method('isAdmin')
                        ->will($this->returnValue(TRUE));
@@ -689,7 +685,7 @@ class BackendUserAuthenticationTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        'recursivedeleteFolder' => TRUE
                );
 
-               $this->assertEquals($expectedPermissions, $this->fixture->getFilePermissions());
+               $this->assertEquals($expectedPermissions, $subject->getFilePermissions());
        }
 
 }
index 6a43a8d..3f94eb6 100644 (file)
@@ -176,6 +176,7 @@ class DataHandlerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @dataProvider inputValuesStringsDataProvider
         */
        public function inputValueCheckRecognizesStringValuesAsIntegerValuesCorrectly($value, $expectedReturnValue) {
+               $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
                $tcaFieldConf = array(
                        'input' => array(),
                        'eval' => 'int',
@@ -310,6 +311,7 @@ class DataHandlerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function doesCheckFlexFormValueHookGetsCalled() {
+               $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
                $hookClass = uniqid('tx_coretest');
                $hookMock = $this->getMock($hookClass, array('checkFlexFormValue_beforeMerge'));
                $hookMock->expects($this->once())->method('checkFlexFormValue_beforeMerge');
index a3b35d1..efb2a79 100644 (file)
@@ -4468,6 +4468,7 @@ text with a ' . $urlMatch . '$|s'),
         * @param string $expected Text with correctly detected URLs
         */
        public function substUrlsInPlainText($input, $expectedPreg) {
+               $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
                $this->assertTrue(preg_match($expectedPreg, Utility\GeneralUtility::substUrlsInPlainText($input, 1, 'http://example.com/index.php')) == 1);
        }