2014-10-27 14:51:26 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 10:38:52 +03:00
|
|
|
namespace Test;
|
|
|
|
|
2016-09-07 21:22:02 +03:00
|
|
|
use bantu\IniGetWrapper\IniGetWrapper;
|
2017-11-24 12:27:58 +03:00
|
|
|
use OC\Installer;
|
2018-09-10 23:33:35 +03:00
|
|
|
use OC\Setup;
|
2017-03-18 01:37:48 +03:00
|
|
|
use OC\SystemConfig;
|
2017-04-07 23:42:43 +03:00
|
|
|
use OCP\Defaults;
|
2016-09-07 21:22:02 +03:00
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\Security\ISecureRandom;
|
2014-10-27 14:51:26 +03:00
|
|
|
|
2016-05-19 10:38:52 +03:00
|
|
|
class SetupTest extends \Test\TestCase {
|
2014-10-27 14:51:26 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2014-10-27 14:51:26 +03:00
|
|
|
protected $config;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \bantu\IniGetWrapper\IniGetWrapper|\PHPUnit\Framework\MockObject\MockObject */
|
2015-03-11 01:44:29 +03:00
|
|
|
private $iniWrapper;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\IL10N|\PHPUnit\Framework\MockObject\MockObject */
|
2015-03-11 01:44:29 +03:00
|
|
|
private $l10n;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Defaults|\PHPUnit\Framework\MockObject\MockObject */
|
2015-03-11 01:44:29 +03:00
|
|
|
private $defaults;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OC\Setup|\PHPUnit\Framework\MockObject\MockObject */
|
2014-10-27 14:51:26 +03:00
|
|
|
protected $setupClass;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\ILogger|\PHPUnit\Framework\MockObject\MockObject */
|
2015-07-30 10:02:35 +03:00
|
|
|
protected $logger;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\Security\ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */
|
2015-07-30 10:02:35 +03:00
|
|
|
protected $random;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Installer|\PHPUnit\Framework\MockObject\MockObject */
|
2017-11-24 12:27:58 +03:00
|
|
|
protected $installer;
|
2014-10-27 14:51:26 +03:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-11 00:59:50 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-03-18 01:37:48 +03:00
|
|
|
$this->config = $this->createMock(SystemConfig::class);
|
2016-09-07 21:22:02 +03:00
|
|
|
$this->iniWrapper = $this->createMock(IniGetWrapper::class);
|
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
2017-04-07 23:42:43 +03:00
|
|
|
$this->defaults = $this->createMock(Defaults::class);
|
2016-09-07 21:22:02 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
$this->random = $this->createMock(ISecureRandom::class);
|
2017-11-24 12:27:58 +03:00
|
|
|
$this->installer = $this->createMock(Installer::class);
|
2018-09-10 23:33:35 +03:00
|
|
|
$this->setupClass = $this->getMockBuilder(Setup::class)
|
2016-09-07 21:22:02 +03:00
|
|
|
->setMethods(['class_exists', 'is_callable', 'getAvailableDbDriversForPdo'])
|
2017-11-24 12:27:58 +03:00
|
|
|
->setConstructorArgs([$this->config, $this->iniWrapper, $this->l10n, $this->defaults, $this->logger, $this->random, $this->installer])
|
2016-09-07 21:22:02 +03:00
|
|
|
->getMock();
|
2014-10-27 14:51:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSupportedDatabasesWithOneWorking() {
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
2017-03-18 01:37:48 +03:00
|
|
|
->method('getValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(
|
2018-09-10 23:33:35 +03:00
|
|
|
['sqlite', 'mysql', 'oci']
|
2020-03-26 00:21:27 +03:00
|
|
|
);
|
2014-10-27 14:51:26 +03:00
|
|
|
$this->setupClass
|
2015-07-30 13:32:22 +03:00
|
|
|
->expects($this->once())
|
2014-10-27 14:51:26 +03:00
|
|
|
->method('is_callable')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2015-07-30 13:32:22 +03:00
|
|
|
$this->setupClass
|
2016-07-13 13:23:44 +03:00
|
|
|
->expects($this->any())
|
2015-07-30 13:32:22 +03:00
|
|
|
->method('getAvailableDbDriversForPdo')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(['sqlite']);
|
2014-10-27 14:51:26 +03:00
|
|
|
$result = $this->setupClass->getSupportedDatabases();
|
2018-09-10 23:33:35 +03:00
|
|
|
$expectedResult = [
|
2014-10-27 14:51:26 +03:00
|
|
|
'sqlite' => 'SQLite'
|
2018-09-10 23:33:35 +03:00
|
|
|
];
|
2014-10-27 14:51:26 +03:00
|
|
|
|
|
|
|
$this->assertSame($expectedResult, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSupportedDatabasesWithNoWorking() {
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
2017-03-18 01:37:48 +03:00
|
|
|
->method('getValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(
|
2018-09-10 23:33:35 +03:00
|
|
|
['sqlite', 'mysql', 'oci', 'pgsql']
|
2020-03-26 00:21:27 +03:00
|
|
|
);
|
2014-10-27 14:51:26 +03:00
|
|
|
$this->setupClass
|
2016-07-13 13:23:44 +03:00
|
|
|
->expects($this->any())
|
2014-10-27 14:51:26 +03:00
|
|
|
->method('is_callable')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2015-07-30 13:32:22 +03:00
|
|
|
$this->setupClass
|
2016-07-13 13:23:44 +03:00
|
|
|
->expects($this->any())
|
2015-07-30 13:32:22 +03:00
|
|
|
->method('getAvailableDbDriversForPdo')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([]);
|
2014-10-27 14:51:26 +03:00
|
|
|
$result = $this->setupClass->getSupportedDatabases();
|
|
|
|
|
2018-09-10 23:33:35 +03:00
|
|
|
$this->assertSame([], $result);
|
2014-10-27 14:51:26 +03:00
|
|
|
}
|
|
|
|
|
2015-07-29 21:14:28 +03:00
|
|
|
public function testGetSupportedDatabasesWithAllWorking() {
|
2014-10-27 14:51:26 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
2017-03-18 01:37:48 +03:00
|
|
|
->method('getValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(
|
2018-09-10 23:33:35 +03:00
|
|
|
['sqlite', 'mysql', 'pgsql', 'oci']
|
2020-03-26 00:21:27 +03:00
|
|
|
);
|
2014-10-27 14:51:26 +03:00
|
|
|
$this->setupClass
|
2016-07-13 13:23:44 +03:00
|
|
|
->expects($this->any())
|
2014-10-27 14:51:26 +03:00
|
|
|
->method('is_callable')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2015-07-30 13:32:22 +03:00
|
|
|
$this->setupClass
|
2016-07-13 13:23:44 +03:00
|
|
|
->expects($this->any())
|
2015-07-30 13:32:22 +03:00
|
|
|
->method('getAvailableDbDriversForPdo')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(['sqlite', 'mysql', 'pgsql']);
|
2014-10-27 14:51:26 +03:00
|
|
|
$result = $this->setupClass->getSupportedDatabases();
|
2018-09-10 23:33:35 +03:00
|
|
|
$expectedResult = [
|
2014-10-27 14:51:26 +03:00
|
|
|
'sqlite' => 'SQLite',
|
|
|
|
'mysql' => 'MySQL/MariaDB',
|
|
|
|
'pgsql' => 'PostgreSQL',
|
2015-07-29 19:19:31 +03:00
|
|
|
'oci' => 'Oracle'
|
2018-09-10 23:33:35 +03:00
|
|
|
];
|
2014-10-27 14:51:26 +03:00
|
|
|
$this->assertSame($expectedResult, $result);
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2014-10-27 14:51:26 +03:00
|
|
|
public function testGetSupportedDatabaseException() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('Supported databases are not properly configured.');
|
|
|
|
|
2014-10-27 14:51:26 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
2017-03-18 01:37:48 +03:00
|
|
|
->method('getValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('NotAnArray');
|
2014-10-27 14:51:26 +03:00
|
|
|
$this->setupClass->getSupportedDatabases();
|
|
|
|
}
|
2018-09-10 23:33:35 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider findWebRootProvider
|
2018-09-10 23:45:40 +03:00
|
|
|
* @param $url
|
|
|
|
* @param $expected
|
2018-09-10 23:33:35 +03:00
|
|
|
*/
|
2018-09-10 23:45:40 +03:00
|
|
|
public function testFindWebRootCli($url, $expected) {
|
2018-09-11 18:22:10 +03:00
|
|
|
$cliState = \OC::$CLI;
|
|
|
|
|
2018-09-10 23:33:35 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($url);
|
2018-09-10 23:33:35 +03:00
|
|
|
\OC::$CLI = true;
|
|
|
|
|
2018-09-10 23:45:40 +03:00
|
|
|
try {
|
2018-09-13 13:24:06 +03:00
|
|
|
$webRoot = self::invokePrivate($this->setupClass, 'findWebRoot', [$this->config]);
|
2018-09-10 23:45:40 +03:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
$webRoot = false;
|
|
|
|
}
|
|
|
|
|
2018-09-11 18:22:10 +03:00
|
|
|
\OC::$CLI = $cliState;
|
2018-09-28 23:32:19 +03:00
|
|
|
$this->assertSame($webRoot, $expected);
|
2018-09-10 23:33:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function findWebRootProvider(): array {
|
|
|
|
return [
|
2018-09-30 13:24:38 +03:00
|
|
|
'https://www.example.com/nextcloud/' => ['https://www.example.com/nextcloud/', '/nextcloud'],
|
2018-09-10 23:33:35 +03:00
|
|
|
'https://www.example.com/nextcloud' => ['https://www.example.com/nextcloud', '/nextcloud'],
|
|
|
|
'https://www.example.com/' => ['https://www.example.com/', ''],
|
2018-09-28 23:32:19 +03:00
|
|
|
'https://www.example.com' => ['https://www.example.com', ''],
|
2018-09-30 13:24:38 +03:00
|
|
|
'https://nctest13pgsql.lan/test123/' => ['https://nctest13pgsql.lan/test123/', '/test123'],
|
|
|
|
'https://nctest13pgsql.lan/test123' => ['https://nctest13pgsql.lan/test123', '/test123'],
|
2018-09-28 23:32:19 +03:00
|
|
|
'https://nctest13pgsql.lan/' => ['https://nctest13pgsql.lan/', ''],
|
|
|
|
'https://nctest13pgsql.lan' => ['https://nctest13pgsql.lan', ''],
|
2018-09-30 13:24:38 +03:00
|
|
|
'https://192.168.10.10/nc/' => ['https://192.168.10.10/nc/', '/nc'],
|
2018-09-28 23:32:19 +03:00
|
|
|
'https://192.168.10.10/nc' => ['https://192.168.10.10/nc', '/nc'],
|
|
|
|
'https://192.168.10.10/' => ['https://192.168.10.10/', ''],
|
|
|
|
'https://192.168.10.10' => ['https://192.168.10.10', ''],
|
|
|
|
'invalid' => ['invalid', false],
|
2018-09-10 23:33:35 +03:00
|
|
|
'empty' => ['', false],
|
|
|
|
];
|
|
|
|
}
|
2015-02-21 22:51:50 +03:00
|
|
|
}
|