Check whether ownCloud is installed

ownCloud might not yet be setup. This causes an issue as the user config requires a setup ownCloud. Thus this needs a block whether ownCloud is installed or not.

Fixes https://github.com/owncloud/core/issues/21955
This commit is contained in:
Lukas Reschke 2016-01-27 15:54:57 +01:00
parent 816c23c17a
commit cb1a64b949
5 changed files with 231 additions and 88 deletions

View file

@ -27,6 +27,7 @@ namespace OC\L10N;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\L10N\IFactory;
/**
@ -59,13 +60,20 @@ class Factory implements IFactory {
/** @var IRequest */
protected $request;
/** @var IUserSession */
protected $userSession;
/**
* @param IConfig $config
* @param IRequest $request
* @param IUserSession $userSession
*/
public function __construct(IConfig $config, IRequest $request) {
public function __construct(IConfig $config,
IRequest $request,
IUserSession $userSession) {
$this->config = $config;
$this->request = $request;
$this->userSession = $userSession;
}
/**
@ -107,9 +115,25 @@ class Factory implements IFactory {
return $this->requestLanguage;
}
$userId = \OC_User::getUser(); // FIXME not available in non-static?
/**
* At this point ownCloud might not yet be installed and thus the lookup
* in the preferences table might fail. For this reason we need to check
* whether the instance has already been installed
*
* @link https://github.com/owncloud/core/issues/21955
*/
if($this->config->getSystemValue('installed', false)) {
$userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null;
if(!is_null($userId)) {
$userLang = $this->config->getUserValue($userId, 'core', 'lang', null);
} else {
$userLang = null;
}
} else {
$userId = null;
$userLang = null;
}
$userLang = $userId !== false ? $this->config->getUserValue($userId, 'core', 'lang') : null;
if ($userLang) {
$this->requestLanguage = $userLang;
if ($this->languageExists($app, $userLang)) {
@ -124,7 +148,7 @@ class Factory implements IFactory {
}
$lang = $this->setLanguageFromRequest($app);
if ($userId !== false && $app === null && !$userLang) {
if ($userId !== null && $app === null && !$userLang) {
$this->config->setUserValue($userId, 'core', 'lang', $lang);
}

View file

@ -265,7 +265,8 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerService('L10NFactory', function (Server $c) {
return new \OC\L10N\Factory(
$c->getConfig(),
$c->getRequest()
$c->getRequest(),
$c->getUserSession()
);
});
$this->registerService('URLGenerator', function (Server $c) {

View file

@ -8,7 +8,6 @@
namespace Test\L10N;
use OC\L10N\Factory;
use Test\TestCase;
@ -26,6 +25,9 @@ class FactoryTest extends TestCase {
/** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
protected $request;
/** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */
protected $userSession;
public function setUp() {
parent::setUp();
@ -38,6 +40,8 @@ class FactoryTest extends TestCase {
$this->request = $this->getMockBuilder('OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->userSession = $this->getMock('\OCP\IUserSession');
}
/**
@ -50,93 +54,15 @@ class FactoryTest extends TestCase {
->setConstructorArgs([
$this->config,
$this->request,
$this->userSession
])
->setMethods($methods)
->getMock();
} else {
return new Factory($this->config, $this->request);
return new Factory($this->config, $this->request, $this->userSession);
}
}
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 dataFindAvailableLanguages() {
return [
[null],
@ -144,6 +70,195 @@ class FactoryTest extends TestCase {
];
}
public function testFindLanguageWithExistingRequestLanguageAndNoApp() {
$factory = $this->getFactory(['languageExists']);
$this->invokePrivate($factory, 'requestLanguage', ['de']);
$factory->expects($this->once())
->method('languageExists')
->with(null, 'de')
->willReturn(true);
$this->assertSame('de', $factory->findLanguage());
}
public function testFindLanguageWithExistingRequestLanguageAndApp() {
$factory = $this->getFactory(['languageExists']);
$this->invokePrivate($factory, 'requestLanguage', ['de']);
$factory->expects($this->once())
->method('languageExists')
->with('MyApp', 'de')
->willReturn(true);
$this->assertSame('de', $factory->findLanguage('MyApp'));
}
public function testFindLanguageWithNotExistingRequestLanguageAndExistingStoredUserLanguage() {
$factory = $this->getFactory(['languageExists']);
$this->invokePrivate($factory, 'requestLanguage', ['de']);
$factory->expects($this->at(0))
->method('languageExists')
->with('MyApp', 'de')
->willReturn(false);
$this->config
->expects($this->once())
->method('getSystemValue')
->with('installed', false)
->willReturn(true);
$user = $this->getMock('\OCP\IUser');
$user->expects($this->once())
->method('getUID')
->willReturn('MyUserUid');
$this->userSession
->expects($this->exactly(2))
->method('getUser')
->willReturn($user);
$this->config
->expects($this->once())
->method('getUserValue')
->with('MyUserUid', 'core', 'lang', null)
->willReturn('jp');
$factory->expects($this->at(1))
->method('languageExists')
->with('MyApp', 'jp')
->willReturn(true);
$this->assertSame('jp', $factory->findLanguage('MyApp'));
}
public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguage() {
$factory = $this->getFactory(['languageExists']);
$this->invokePrivate($factory, 'requestLanguage', ['de']);
$factory->expects($this->at(0))
->method('languageExists')
->with('MyApp', 'de')
->willReturn(false);
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('installed', false)
->willReturn(true);
$user = $this->getMock('\OCP\IUser');
$user->expects($this->once())
->method('getUID')
->willReturn('MyUserUid');
$this->userSession
->expects($this->exactly(2))
->method('getUser')
->willReturn($user);
$this->config
->expects($this->once())
->method('getUserValue')
->with('MyUserUid', 'core', 'lang', null)
->willReturn('jp');
$factory->expects($this->at(1))
->method('languageExists')
->with('MyApp', 'jp')
->willReturn(false);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with('default_language', false)
->willReturn('es');
$factory->expects($this->at(2))
->method('languageExists')
->with('MyApp', 'es')
->willReturn(true);
$this->assertSame('es', $factory->findLanguage('MyApp'));
}
public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguageAndNotExistingDefault() {
$factory = $this->getFactory(['languageExists']);
$this->invokePrivate($factory, 'requestLanguage', ['de']);
$factory->expects($this->at(0))
->method('languageExists')
->with('MyApp', 'de')
->willReturn(false);
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('installed', false)
->willReturn(true);
$user = $this->getMock('\OCP\IUser');
$user->expects($this->once())
->method('getUID')
->willReturn('MyUserUid');
$this->userSession
->expects($this->exactly(2))
->method('getUser')
->willReturn($user);
$this->config
->expects($this->once())
->method('getUserValue')
->with('MyUserUid', 'core', 'lang', null)
->willReturn('jp');
$factory->expects($this->at(1))
->method('languageExists')
->with('MyApp', 'jp')
->willReturn(false);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with('default_language', false)
->willReturn('es');
$factory->expects($this->at(2))
->method('languageExists')
->with('MyApp', 'es')
->willReturn(false);
$this->config
->expects($this->never())
->method('setUserValue');
$this->assertSame('en', $factory->findLanguage('MyApp'));
}
public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguageAndNotExistingDefaultAndNoAppInScope() {
$factory = $this->getFactory(['languageExists']);
$this->invokePrivate($factory, 'requestLanguage', ['de']);
$factory->expects($this->at(0))
->method('languageExists')
->with('MyApp', 'de')
->willReturn(false);
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('installed', false)
->willReturn(true);
$user = $this->getMock('\OCP\IUser');
$user->expects($this->once())
->method('getUID')
->willReturn('MyUserUid');
$this->userSession
->expects($this->exactly(2))
->method('getUser')
->willReturn($user);
$this->config
->expects($this->once())
->method('getUserValue')
->with('MyUserUid', 'core', 'lang', null)
->willReturn('jp');
$factory->expects($this->at(1))
->method('languageExists')
->with('MyApp', 'jp')
->willReturn(false);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with('default_language', false)
->willReturn('es');
$factory->expects($this->at(2))
->method('languageExists')
->with('MyApp', 'es')
->willReturn(false);
$this->config
->expects($this->never())
->method('setUserValue')
->with('MyUserUid', 'core', 'lang', 'en');
$this->assertSame('en', $factory->findLanguage('MyApp'));
}
/**
* @dataProvider dataFindAvailableLanguages
*

View file

@ -124,7 +124,7 @@ class L10nLegacyTest extends \Test\TestCase {
}
public function testFactoryGetLanguageCode() {
$factory = new \OC\L10N\Factory($this->getMock('OCP\IConfig'), $this->getMock('OCP\IRequest'));
$factory = new \OC\L10N\Factory($this->getMock('OCP\IConfig'), $this->getMock('OCP\IRequest'), $this->getMock('OCP\IUserSession'));
$l = $factory->get('lib', 'de');
$this->assertEquals('de', $l->getLanguageCode());
}

View file

@ -12,6 +12,7 @@ namespace Test\L10N;
use DateTime;
use OC\L10N\Factory;
use OC\L10N\L10N;
use OCP\IUserSession;
use Test\TestCase;
/**
@ -28,7 +29,9 @@ class L10nTest extends TestCase {
$config = $this->getMock('OCP\IConfig');
/** @var \OCP\IRequest $request */
$request = $this->getMock('OCP\IRequest');
return new Factory($config, $request);
/** @var IUserSession $userSession */
$userSession = $this->getMock('OCP\IUserSession');
return new Factory($config, $request, $userSession);
}
public function testGermanPluralTranslations() {