Skip to content

Commit

Permalink
[TASK] Migrate LocalizationUtility Test to Functional
Browse files Browse the repository at this point in the history
Avoid heavy mocking in tests by
utilizing Functionals incl. PackageManager
and Caching Framework.

Resolves: #99553
Releases: main
Change-Id: Ie569e87c0710a50ce09cc60f3d9a1773d39d4b4b
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/77391
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Nikita Hovratov <nikita.h@live.de>
Reviewed-by: Nikita Hovratov <nikita.h@live.de>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Tested-by: core-ci <typo3@b13.com>
  • Loading branch information
bmack authored and nhovratov committed Jan 16, 2023
1 parent d325dd0 commit 8dcd616
Showing 1 changed file with 7 additions and 35 deletions.
Expand Up @@ -15,23 +15,17 @@
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\CMS\Extbase\Tests\Unit\Utility;
namespace TYPO3\CMS\Extbase\Tests\Functional\Utility;

use PHPUnit\Framework\MockObject\MockObject;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Localization\LanguageStore;
use TYPO3\CMS\Core\Localization\Locales;
use TYPO3\CMS\Core\Localization\LocalizationFactory;
use TYPO3\CMS\Core\Package\PackageManager;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

class LocalizationUtilityTest extends UnitTestCase
class LocalizationUtilityTest extends FunctionalTestCase
{
protected ConfigurationManagerInterface&MockObject $configurationManagerInterfaceMock;

Expand Down Expand Up @@ -183,10 +177,6 @@ protected function setUp(): void
$property = $reflectionClass->getProperty('configurationManager');
$property->setAccessible(true);
$property->setValue($this->configurationManagerInterfaceMock);

$localizationFactoryMock = $this->createMock(LocalizationFactory::class);
$localizationFactoryMock->method('getParsedData')->with(self::anything())->willReturn([]);
GeneralUtility::setSingletonInstance(LocalizationFactory::class, $localizationFactoryMock);
}

/**
Expand Down Expand Up @@ -342,7 +332,6 @@ public function translateTestWithExplicitLanguageParameters(
array $altLanguageKeys = [],
array $arguments = null
): void {
$packageManagerMock = $this->createMock(PackageManager::class);
$this->configurationManagerInterfaceMock
->method('getConfiguration')
->with('Framework', 'core', null)
Expand All @@ -353,12 +342,7 @@ public function translateTestWithExplicitLanguageParameters(
$property = $reflectionClass->getProperty('LOCAL_LANG');
$property->setAccessible(true);
$property->setValue($this->LOCAL_LANG);
$cacheManagerMock = $this->createMock(CacheManager::class);
$cacheFrontendMock = $this->createMock(FrontendInterface::class);
$cacheManagerMock->method('getCache')->with('l10n')->willReturn($cacheFrontendMock);
$cacheFrontendMock->method('get')->with(self::anything())->willReturn(false);
$cacheFrontendMock->method('set')->with(self::anything())->willReturn(null);
$GLOBALS['LANG'] = new LanguageService(new Locales(), new LocalizationFactory(new LanguageStore($packageManagerMock), $cacheManagerMock), $cacheFrontendMock);
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageServiceFactory::class)->create('default');
self::assertEquals($expected, LocalizationUtility::translate($key, 'core', $arguments, $languageKey, $altLanguageKeys));
}

Expand Down Expand Up @@ -488,7 +472,6 @@ public function loadTypoScriptLabels(array $LOCAL_LANG, array $typoScriptLocalLa
*/
public function clearLabelWithTypoScript(): void
{
$packageManagerMock = $this->createMock(PackageManager::class);
$reflectionClass = new \ReflectionClass(LocalizationUtility::class);

$property = $reflectionClass->getProperty('LOCAL_LANG');
Expand All @@ -514,12 +497,7 @@ public function clearLabelWithTypoScript(): void
$method->setAccessible(true);
$method->invoke(null, 'core', $this->languageFilePath);

$cacheManagerMock = $this->createMock(CacheManager::class);
$cacheFrontendMock = $this->createMock(FrontendInterface::class);
$cacheManagerMock->method('getCache')->with('l10n')->willReturn($cacheFrontendMock);
$cacheFrontendMock->method('get')->with(self::anything())->willReturn(false);
$cacheFrontendMock->method('set')->with(self::anything())->willReturn(null);
$GLOBALS['LANG'] = new LanguageService(new Locales(), new LocalizationFactory(new LanguageStore($packageManagerMock), $cacheManagerMock), $cacheFrontendMock);
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageServiceFactory::class)->create('default');

$result = LocalizationUtility::translate('key1', 'core', null, 'dk');
self::assertNotNull($result);
Expand All @@ -541,7 +519,6 @@ public function translateThrowsExceptionWithEmptyExtensionNameIfKeyIsNotPrefixed
*/
public function translateWillReturnLabelsFromTsEvenIfNoXlfFileExists(): void
{
$packageManagerMock = $this->createMock(PackageManager::class);
$reflectionClass = new \ReflectionClass(LocalizationUtility::class);

$typoScriptLocalLang = [
Expand All @@ -563,12 +540,7 @@ public function translateWillReturnLabelsFromTsEvenIfNoXlfFileExists(): void
$method->setAccessible(true);
$method->invoke(null, 'core', ''); // setting the language file path to an empty string here

$cacheManagerMock = $this->createMock(CacheManager::class);
$cacheFrontendMock = $this->createMock(FrontendInterface::class);
$cacheManagerMock->method('getCache')->with('l10n')->willReturn($cacheFrontendMock);
$cacheFrontendMock->method('get')->with(self::anything())->willReturn(false);
$cacheFrontendMock->method('set')->with(self::anything())->willReturn(null);
$GLOBALS['LANG'] = new LanguageService(new Locales(), new LocalizationFactory(new LanguageStore($packageManagerMock), $cacheManagerMock), $cacheFrontendMock);
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageServiceFactory::class)->create('default');

$result = LocalizationUtility::translate('key1', 'core', null, 'dk');
self::assertNotNull($result);
Expand Down

0 comments on commit 8dcd616

Please sign in to comment.