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?
|
||||
|
||||
if ($userId && $this->config->getUserValue($userId, 'core', 'lang')) {
|
||||
$lang = $this->config->getUserValue($userId, 'core', 'lang');
|
||||
$this->requestLanguage = $lang;
|
||||
if ($this->languageExists($app, $lang)) {
|
||||
return $lang;
|
||||
$userLang = $userId !== false ? $this->config->getUserValue($userId, 'core', 'lang') : null;
|
||||
if ($userLang) {
|
||||
$this->requestLanguage = $userLang;
|
||||
if ($this->languageExists($app, $userLang)) {
|
||||
return $userLang;
|
||||
}
|
||||
}
|
||||
|
||||
$defaultLanguage = $this->config->getSystemValue('default_language', false);
|
||||
|
||||
if ($defaultLanguage !== false) {
|
||||
if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) {
|
||||
return $defaultLanguage;
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
|
|
|
@ -13,16 +13,27 @@ use OC\L10N\Factory;
|
|||
use Test\TestCase;
|
||||
|
||||
/**
|
||||
* Class Test_L10n
|
||||
* Class FactoryTest
|
||||
*
|
||||
* @package Test\L10N
|
||||
* @group DB
|
||||
*/
|
||||
class FactoryTest extends TestCase {
|
||||
|
||||
/** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
||||
protected $config;
|
||||
|
||||
/** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
|
||||
protected $request;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
/** @var \OCP\IConfig $request */
|
||||
$this->config = $this->getMockBuilder('OCP\IConfig')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
/** @var \OCP\IRequest $request */
|
||||
$this->request = $this->getMockBuilder('OCP\IRequest')
|
||||
->disableOriginalConstructor()
|
||||
|
@ -34,22 +45,98 @@ class FactoryTest extends TestCase {
|
|||
* @return Factory|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected function getFactory(array $methods = []) {
|
||||
/** @var \OCP\IConfig $config */
|
||||
$config = $this->getMock('OCP\IConfig');
|
||||
|
||||
if (!empty($methods)) {
|
||||
return $this->getMockBuilder('OC\L10N\Factory')
|
||||
->setConstructorArgs([
|
||||
$config,
|
||||
$this->config,
|
||||
$this->request,
|
||||
])
|
||||
->setMethods($methods)
|
||||
->getMock();
|
||||
} 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() {
|
||||
return [
|
||||
[null, 'en', [], true],
|
||||
|
|
|
@ -36,6 +36,46 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
|
|||
static protected $realDatabase = null;
|
||||
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() {
|
||||
$traits = [];
|
||||
$class = $this;
|
||||
|
|
Loading…
Reference in New Issue