. */ namespace SPT\Core; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\MockObject\MockObject; use SP\Core\Language; use SP\Domain\Config\Ports\ConfigDataInterface; use SP\Domain\Http\RequestInterface; use SP\Domain\User\Models\UserPreferences; use SP\Domain\User\Services\UserLoginResponse; use SPT\UnitaryTestCase; /** * Class LanguageTest */ #[Group('unitary')] class LanguageTest extends UnitaryTestCase { private ConfigDataInterface|MockObject $configData; private RequestInterface|MockObject $request; private Language $language; public function testSetLocales() { $locale = 'es_ES'; Language::setLocales($locale); $this->assertEquals($locale . '.utf8', Language::$localeStatus); $this->assertEquals($locale . '.utf8', getenv('LANG')); $this->assertEquals($locale . '.utf8', getenv('LANGUAGE')); } public function testSetLanguage() { $locale = 'es_ES'; $this->context->setLocale($locale); $this->language->setLanguage(); $this->assertEquals($locale . '.utf8', Language::$localeStatus); $this->assertEquals($locale . '.utf8', getenv('LANG')); $this->assertEquals($locale . '.utf8', getenv('LANGUAGE')); } public function testSetLanguageForceWithUserLanguage() { $locale = 'es_ES'; $this->context->setLocale($locale); $this->configData ->expects(self::once()) ->method('getSiteLang') ->willReturn(self::$faker->locale); $userData = $this->context->getUserData(); $userData->setPreferences(new UserPreferences(['lang' => $locale])); $this->language->setLanguage(true); $this->assertEquals($locale . '.utf8', Language::$localeStatus); $this->assertEquals($locale . '.utf8', getenv('LANG')); $this->assertEquals($locale . '.utf8', getenv('LANGUAGE')); $this->assertEquals($locale, $this->context->getLocale()); } public function testSetLanguageForceWithAppLanguage() { $locale = 'es_ES'; $appLocale = 'en_US'; $this->context->setLocale($locale); $this->context->setUserData(new UserLoginResponse()); $this->configData ->expects(self::once()) ->method('getSiteLang') ->willReturn($appLocale); $this->language->setLanguage(true); $this->assertEquals($appLocale . '.utf8', Language::$localeStatus); $this->assertEquals($appLocale . '.utf8', getenv('LANG')); $this->assertEquals($appLocale . '.utf8', getenv('LANGUAGE')); $this->assertEquals($appLocale, $this->context->getLocale()); } public function testSetLanguageForceWithBrowserLanguage() { $locale = 'es_ES'; $browserLocale = 'en_US'; $this->context->setLocale($locale); $this->context->setUserData(new UserLoginResponse()); $this->configData ->expects(self::once()) ->method('getSiteLang') ->willReturn(null); $this->request ->expects(self::once()) ->method('getHeader') ->with('Accept-Language') ->willReturn($browserLocale); $this->language->setLanguage(true); $this->assertEquals($browserLocale . '.utf8', Language::$localeStatus); $this->assertEquals($browserLocale . '.utf8', getenv('LANG')); $this->assertEquals($browserLocale . '.utf8', getenv('LANGUAGE')); $this->assertEquals($browserLocale, $this->context->getLocale()); } public function testGetAvailableLanguages() { $out = Language::getAvailableLanguages(); $this->assertCount(14, $out); } public function testSetAppLocales() { $locale = 'es_ES'; $appLocale = 'en_US'; $this->context->setLocale($locale); $this->configData ->expects(self::exactly(2)) ->method('getSiteLang') ->willReturn($appLocale); $this->language->setAppLocales(); $this->assertEquals($appLocale . '.utf8', Language::$localeStatus); $this->assertEquals($appLocale . '.utf8', getenv('LANG')); $this->assertEquals($appLocale . '.utf8', getenv('LANGUAGE')); } public function testUnsetAppLocales() { $locale = 'es_ES'; $this->context->setLocale($locale); $this->language->unsetAppLocales(); $this->assertEquals($locale . '.utf8', Language::$localeStatus); $this->assertEquals($locale . '.utf8', getenv('LANG')); $this->assertEquals($locale . '.utf8', getenv('LANGUAGE')); } protected function setUp(): void { parent::setUp(); $this->configData = $this->createMock(ConfigDataInterface::class); $this->request = $this->createMock(RequestInterface::class); $this->language = new Language($this->context, $this->configData, $this->request); } }