Add tests for findLanguage()
This commit is contained in:
parent
fe411788b7
commit
70396581eb
|
@ -109,22 +109,22 @@ class Factory implements IFactory {
|
||||||
|
|
||||||
$userId = \OC_User::getUser(); // FIXME not available in non-static?
|
$userId = \OC_User::getUser(); // FIXME not available in non-static?
|
||||||
|
|
||||||
if ($userId && $this->config->getUserValue($userId, 'core', 'lang')) {
|
$userLang = $userId !== false ? $this->config->getUserValue($userId, 'core', 'lang') : null;
|
||||||
$lang = $this->config->getUserValue($userId, 'core', 'lang');
|
if ($userLang) {
|
||||||
$this->requestLanguage = $lang;
|
$this->requestLanguage = $userLang;
|
||||||
if ($this->languageExists($app, $lang)) {
|
if ($this->languageExists($app, $userLang)) {
|
||||||
return $lang;
|
return $userLang;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$defaultLanguage = $this->config->getSystemValue('default_language', false);
|
$defaultLanguage = $this->config->getSystemValue('default_language', false);
|
||||||
|
|
||||||
if ($defaultLanguage !== false) {
|
if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) {
|
||||||
return $defaultLanguage;
|
return $defaultLanguage;
|
||||||
}
|
}
|
||||||
|
|
||||||
$lang = $this->setLanguageFromRequest($app);
|
$lang = $this->setLanguageFromRequest($app);
|
||||||
if ($userId && $app === null && !$this->config->getUserValue($userId, 'core', 'lang')) {
|
if ($userId !== false && $app === null && !$userLang) {
|
||||||
$this->config->setUserValue($userId, 'core', 'lang', $lang);
|
$this->config->setUserValue($userId, 'core', 'lang', $lang);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,16 +13,27 @@ use OC\L10N\Factory;
|
||||||
use Test\TestCase;
|
use Test\TestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Test_L10n
|
* Class FactoryTest
|
||||||
|
*
|
||||||
|
* @package Test\L10N
|
||||||
* @group DB
|
* @group DB
|
||||||
*/
|
*/
|
||||||
class FactoryTest extends TestCase {
|
class FactoryTest extends TestCase {
|
||||||
|
|
||||||
|
/** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
protected $config;
|
||||||
|
|
||||||
/** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
|
/** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
protected $request;
|
protected $request;
|
||||||
|
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
|
/** @var \OCP\IConfig $request */
|
||||||
|
$this->config = $this->getMockBuilder('OCP\IConfig')
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
|
||||||
/** @var \OCP\IRequest $request */
|
/** @var \OCP\IRequest $request */
|
||||||
$this->request = $this->getMockBuilder('OCP\IRequest')
|
$this->request = $this->getMockBuilder('OCP\IRequest')
|
||||||
->disableOriginalConstructor()
|
->disableOriginalConstructor()
|
||||||
|
@ -34,22 +45,98 @@ class FactoryTest extends TestCase {
|
||||||
* @return Factory|\PHPUnit_Framework_MockObject_MockObject
|
* @return Factory|\PHPUnit_Framework_MockObject_MockObject
|
||||||
*/
|
*/
|
||||||
protected function getFactory(array $methods = []) {
|
protected function getFactory(array $methods = []) {
|
||||||
/** @var \OCP\IConfig $config */
|
|
||||||
$config = $this->getMock('OCP\IConfig');
|
|
||||||
|
|
||||||
if (!empty($methods)) {
|
if (!empty($methods)) {
|
||||||
return $this->getMockBuilder('OC\L10N\Factory')
|
return $this->getMockBuilder('OC\L10N\Factory')
|
||||||
->setConstructorArgs([
|
->setConstructorArgs([
|
||||||
$config,
|
$this->config,
|
||||||
$this->request,
|
$this->request,
|
||||||
])
|
])
|
||||||
->setMethods($methods)
|
->setMethods($methods)
|
||||||
->getMock();
|
->getMock();
|
||||||
} else {
|
} else {
|
||||||
return new Factory($config, $this->request);
|
return new Factory($this->config, $this->request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function dataFindLanguage() {
|
||||||
|
return [
|
||||||
|
[null, false, 1, 'de', true, null, null, null, null, null, 'de'],
|
||||||
|
[null, 'test', 2, 'de', false, 'ru', true, null, null, null, 'ru'],
|
||||||
|
[null, 'test', 1, '', null, 'ru', true, null, null, null, 'ru'],
|
||||||
|
[null, 'test', 3, 'de', false, 'ru', false, 'cz', true, null, 'cz'],
|
||||||
|
[null, 'test', 2, '', null, 'ru', false, 'cz', true, null, 'cz'],
|
||||||
|
[null, 'test', 1, '', null, '', null, 'cz', true, null, 'cz'],
|
||||||
|
[null, 'test', 3, 'de', false, 'ru', false, 'cz', false, 'ar', 'ar'],
|
||||||
|
[null, 'test', 2, '', null, 'ru', false, 'cz', false, 'ar', 'ar'],
|
||||||
|
[null, 'test', 1, '', null, '', null, 'cz', false, 'ar', 'ar'],
|
||||||
|
[null, 'test', 0, '', null, '', null, false, null, 'ar', 'ar'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataFindLanguage
|
||||||
|
*
|
||||||
|
* @param string|null $app
|
||||||
|
* @param string|null $user
|
||||||
|
* @param int $existsCalls
|
||||||
|
* @param string $storedRequestLang
|
||||||
|
* @param bool $srlExists
|
||||||
|
* @param string|null $userLang
|
||||||
|
* @param bool $ulExists
|
||||||
|
* @param string|false $defaultLang
|
||||||
|
* @param bool $dlExists
|
||||||
|
* @param string|null $requestLang
|
||||||
|
* @param string $expected
|
||||||
|
*/
|
||||||
|
public function testFindLanguage($app, $user, $existsCalls, $storedRequestLang, $srlExists, $userLang, $ulExists, $defaultLang, $dlExists, $requestLang, $expected) {
|
||||||
|
$factory = $this->getFactory([
|
||||||
|
'languageExists',
|
||||||
|
'setLanguageFromRequest',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$session = $this->getMockBuilder('OCP\ISession')
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
$session->expects($this->any())
|
||||||
|
->method('get')
|
||||||
|
->with('user_id')
|
||||||
|
->willReturn($user);
|
||||||
|
$userSession = $this->getMockBuilder('OC\User\Session')
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
$userSession->expects($this->any())
|
||||||
|
->method('getSession')
|
||||||
|
->willReturn($session);
|
||||||
|
|
||||||
|
$this->invokePrivate($factory, 'requestLanguage', [$storedRequestLang]);
|
||||||
|
|
||||||
|
$factory->expects($this->exactly($existsCalls))
|
||||||
|
->method('languageExists')
|
||||||
|
->willReturnMap([
|
||||||
|
[$app, $storedRequestLang, $srlExists],
|
||||||
|
[$app, $userLang, $ulExists],
|
||||||
|
[$app, $defaultLang, $dlExists],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$factory->expects($requestLang !== null ? $this->once() : $this->never())
|
||||||
|
->method('setLanguageFromRequest')
|
||||||
|
->willReturn($requestLang);
|
||||||
|
|
||||||
|
$this->config->expects($userLang !== null ? $this->any() : $this->never())
|
||||||
|
->method('getUserValue')
|
||||||
|
->with($this->anything(), 'core', 'lang')
|
||||||
|
->willReturn($userLang);
|
||||||
|
|
||||||
|
$this->config->expects($defaultLang !== null ? $this->once() : $this->never())
|
||||||
|
->method('getSystemValue')
|
||||||
|
->with('default_language', false)
|
||||||
|
->willReturn($defaultLang);
|
||||||
|
|
||||||
|
$this->overwriteService('UserSession', $userSession);
|
||||||
|
$this->assertSame($expected, $factory->findLanguage($app));
|
||||||
|
$this->restoreService('UserSession');
|
||||||
|
}
|
||||||
|
|
||||||
public function dataLanguageExists() {
|
public function dataLanguageExists() {
|
||||||
return [
|
return [
|
||||||
[null, 'en', [], true],
|
[null, 'en', [], true],
|
||||||
|
|
|
@ -36,6 +36,46 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
|
||||||
static protected $realDatabase = null;
|
static protected $realDatabase = null;
|
||||||
static private $wasDatabaseAllowed = false;
|
static private $wasDatabaseAllowed = false;
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
protected $services = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @param mixed $newService
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function overwriteService($name, $newService) {
|
||||||
|
if (isset($this->services[$name])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->services[$name] = \OC::$server->query($name);
|
||||||
|
\OC::$server->registerService($name, function () use ($newService) {
|
||||||
|
return $newService;
|
||||||
|
});
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function restoreService($name) {
|
||||||
|
if (isset($this->services[$name])) {
|
||||||
|
$oldService = $this->services[$name];
|
||||||
|
\OC::$server->registerService($name, function () use ($oldService) {
|
||||||
|
return $oldService;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
unset($this->services[$name]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
protected function getTestTraits() {
|
protected function getTestTraits() {
|
||||||
$traits = [];
|
$traits = [];
|
||||||
$class = $this;
|
$class = $this;
|
||||||
|
|
Loading…
Reference in New Issue