nextcloud/tests/lib/SetupTest.php

178 lines
5.5 KiB
PHP
Raw Normal View History

<?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;
use OC\Installer;
use OC\Setup;
use OC\SystemConfig;
use OCP\Defaults;
2016-09-07 21:22:02 +03:00
use OCP\IL10N;
use OCP\ILogger;
use OCP\Security\ISecureRandom;
2016-05-19 10:38:52 +03:00
class SetupTest extends \Test\TestCase {
/** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
protected $config;
/** @var \bantu\IniGetWrapper\IniGetWrapper|\PHPUnit_Framework_MockObject_MockObject */
private $iniWrapper;
/** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */
private $l10n;
/** @var Defaults|\PHPUnit_Framework_MockObject_MockObject */
private $defaults;
/** @var \OC\Setup|\PHPUnit_Framework_MockObject_MockObject */
protected $setupClass;
/** @var \OCP\ILogger|\PHPUnit_Framework_MockObject_MockObject */
protected $logger;
/** @var \OCP\Security\ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
protected $random;
/** @var Installer|\PHPUnit_Framework_MockObject_MockObject */
protected $installer;
protected function setUp(): void {
parent::setUp();
$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);
$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);
$this->installer = $this->createMock(Installer::class);
$this->setupClass = $this->getMockBuilder(Setup::class)
2016-09-07 21:22:02 +03:00
->setMethods(['class_exists', 'is_callable', 'getAvailableDbDriversForPdo'])
->setConstructorArgs([$this->config, $this->iniWrapper, $this->l10n, $this->defaults, $this->logger, $this->random, $this->installer])
2016-09-07 21:22:02 +03:00
->getMock();
}
public function testGetSupportedDatabasesWithOneWorking() {
$this->config
->expects($this->once())
->method('getValue')
->will($this->returnValue(
['sqlite', 'mysql', 'oci']
));
$this->setupClass
->expects($this->once())
->method('is_callable')
->will($this->returnValue(false));
$this->setupClass
2016-07-13 13:23:44 +03:00
->expects($this->any())
->method('getAvailableDbDriversForPdo')
->will($this->returnValue(['sqlite']));
$result = $this->setupClass->getSupportedDatabases();
$expectedResult = [
'sqlite' => 'SQLite'
];
$this->assertSame($expectedResult, $result);
}
public function testGetSupportedDatabasesWithNoWorking() {
$this->config
->expects($this->once())
->method('getValue')
->will($this->returnValue(
['sqlite', 'mysql', 'oci', 'pgsql']
));
$this->setupClass
2016-07-13 13:23:44 +03:00
->expects($this->any())
->method('is_callable')
->will($this->returnValue(false));
$this->setupClass
2016-07-13 13:23:44 +03:00
->expects($this->any())
->method('getAvailableDbDriversForPdo')
->will($this->returnValue([]));
$result = $this->setupClass->getSupportedDatabases();
$this->assertSame([], $result);
}
2015-07-29 21:14:28 +03:00
public function testGetSupportedDatabasesWithAllWorking() {
$this->config
->expects($this->once())
->method('getValue')
->will($this->returnValue(
['sqlite', 'mysql', 'pgsql', 'oci']
));
$this->setupClass
2016-07-13 13:23:44 +03:00
->expects($this->any())
->method('is_callable')
->will($this->returnValue(true));
$this->setupClass
2016-07-13 13:23:44 +03:00
->expects($this->any())
->method('getAvailableDbDriversForPdo')
->will($this->returnValue(['sqlite', 'mysql', 'pgsql']));
$result = $this->setupClass->getSupportedDatabases();
$expectedResult = [
'sqlite' => 'SQLite',
'mysql' => 'MySQL/MariaDB',
'pgsql' => 'PostgreSQL',
2015-07-29 19:19:31 +03:00
'oci' => 'Oracle'
];
$this->assertSame($expectedResult, $result);
}
public function testGetSupportedDatabaseException() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Supported databases are not properly configured.');
$this->config
->expects($this->once())
->method('getValue')
->will($this->returnValue('NotAnArray'));
$this->setupClass->getSupportedDatabases();
}
/**
* @dataProvider findWebRootProvider
* @param $url
* @param $expected
*/
public function testFindWebRootCli($url, $expected) {
$cliState = \OC::$CLI;
$this->config
->expects($this->once())
->method('getValue')
->will($this->returnValue($url));
\OC::$CLI = true;
try {
$webRoot = self::invokePrivate($this->setupClass, 'findWebRoot', [$this->config]);
} catch (\InvalidArgumentException $e) {
$webRoot = false;
}
\OC::$CLI = $cliState;
$this->assertSame($webRoot, $expected);
}
public function findWebRootProvider(): array {
return [
'https://www.example.com/nextcloud/' => ['https://www.example.com/nextcloud/', '/nextcloud'],
'https://www.example.com/nextcloud' => ['https://www.example.com/nextcloud', '/nextcloud'],
'https://www.example.com/' => ['https://www.example.com/', ''],
'https://www.example.com' => ['https://www.example.com', ''],
'https://nctest13pgsql.lan/test123/' => ['https://nctest13pgsql.lan/test123/', '/test123'],
'https://nctest13pgsql.lan/test123' => ['https://nctest13pgsql.lan/test123', '/test123'],
'https://nctest13pgsql.lan/' => ['https://nctest13pgsql.lan/', ''],
'https://nctest13pgsql.lan' => ['https://nctest13pgsql.lan', ''],
'https://192.168.10.10/nc/' => ['https://192.168.10.10/nc/', '/nc'],
'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],
'empty' => ['', false],
];
}
2015-02-21 22:51:50 +03:00
}