Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/private/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function is_callable(string $name): bool {
* Wrapper around \PDO::getAvailableDrivers
*/
protected function getAvailableDbDriversForPdo(): array {
if (class_exists(\PDO::class)) {
if ($this->class_exists(\PDO::class)) {
return \PDO::getAvailableDrivers();
}
return [];
Expand Down
153 changes: 133 additions & 20 deletions tests/lib/SetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,50 @@ protected function setUp(): void {

$this->config = $this->createMock(SystemConfig::class);
$this->iniWrapper = $this->createMock(IniGetWrapper::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->random = $this->createMock(ISecureRandom::class);

$this->l10n = $this->createMock(IL10N::class);
$this->l10nFactory = $this->createMock(IL10NFactory::class);
$this->l10nFactory->method('get')
->willReturn($this->l10n);

$this->defaults = $this->createMock(Defaults::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->random = $this->createMock(ISecureRandom::class);
$this->installer = $this->createMock(Installer::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);

$this->setupClass = $this->getMockBuilder(Setup::class)
->onlyMethods(['class_exists', 'is_callable', 'getAvailableDbDriversForPdo'])
->setConstructorArgs([$this->config, $this->iniWrapper, $this->l10nFactory, $this->defaults, $this->logger, $this->random, $this->installer, $this->eventDispatcher])
->setConstructorArgs([
$this->config,
$this->iniWrapper,
$this->l10nFactory,
$this->defaults,
$this->logger,
$this->random,
$this->installer,
$this->eventDispatcher,
])
->getMock();
}

public function testGetSupportedDatabasesWithOneWorking(): void {
$this->config
->expects($this->once())
->method('getValue')
->willReturn(
['sqlite', 'mysql', 'oci']
);
->willReturn(['sqlite', 'mysql', 'oci']);
$this->setupClass
->expects($this->once())
->method('is_callable')
->with('oci_connect')
->willReturn(false);
$this->setupClass
->expects($this->any())
->expects($this->exactly(2))
->method('getAvailableDbDriversForPdo')
->willReturn(['sqlite']);

$result = $this->setupClass->getSupportedDatabases();
$expectedResult = [
'sqlite' => 'SQLite'
];
$expectedResult = ['sqlite' => 'SQLite'];

$this->assertSame($expectedResult, $result);
}
Expand All @@ -79,17 +89,17 @@ public function testGetSupportedDatabasesWithNoWorking(): void {
$this->config
->expects($this->once())
->method('getValue')
->willReturn(
['sqlite', 'mysql', 'oci', 'pgsql']
);
->willReturn(['sqlite', 'mysql', 'oci', 'pgsql']);
$this->setupClass
->expects($this->any())
->expects($this->once())
->method('is_callable')
->with('oci_connect')
->willReturn(false);
$this->setupClass
->expects($this->any())
->expects($this->exactly(3))
->method('getAvailableDbDriversForPdo')
->willReturn([]);

$result = $this->setupClass->getSupportedDatabases();

$this->assertSame([], $result);
Expand All @@ -99,27 +109,71 @@ public function testGetSupportedDatabasesWithAllWorking(): void {
$this->config
->expects($this->once())
->method('getValue')
->willReturn(
['sqlite', 'mysql', 'pgsql', 'oci']
);
->willReturn(['sqlite', 'mysql', 'pgsql', 'oci']);
$this->setupClass
->expects($this->any())
->expects($this->once())
->method('is_callable')
->with('oci_connect')
->willReturn(true);
$this->setupClass
->expects($this->any())
->expects($this->exactly(3))
->method('getAvailableDbDriversForPdo')
->willReturn(['sqlite', 'mysql', 'pgsql']);

$result = $this->setupClass->getSupportedDatabases();
$expectedResult = [
'sqlite' => 'SQLite',
'mysql' => 'MySQL/MariaDB',
'pgsql' => 'PostgreSQL',
'oci' => 'Oracle'
];

$this->assertSame($expectedResult, $result);
}

public function testGetSupportedDatabasesAllowsAllKnownDatabasesWithoutConfiguration(): void {
$this->config
->expects($this->never())
->method('getValue');
$this->setupClass
->expects($this->once())
->method('is_callable')
->with('oci_connect')
->willReturn(true);
$this->setupClass
->expects($this->exactly(3))
->method('getAvailableDbDriversForPdo')
->willReturn(['sqlite', 'mysql', 'pgsql']);

$result = $this->setupClass->getSupportedDatabases(true);

$this->assertSame([
'sqlite' => 'SQLite',
'mysql' => 'MySQL/MariaDB',
'pgsql' => 'PostgreSQL',
'oci' => 'Oracle',
], $result);
}

public function testGetSupportedDatabasesIgnoresUnknownConfiguredDatabases(): void {
$this->config
->expects($this->once())
->method('getValue')
->with('supportedDatabases', ['sqlite', 'mysql', 'pgsql'])
->willReturn(['unknown', 'sqlite']);
$this->setupClass
->expects($this->never())
->method('is_callable');
$this->setupClass
->expects($this->once())
->method('getAvailableDbDriversForPdo')
->willReturn(['sqlite']);

$result = $this->setupClass->getSupportedDatabases();

$this->assertSame(['sqlite' => 'SQLite'], $result);
}

public function testGetSupportedDatabaseException(): void {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Supported databases are not properly configured.');
Expand All @@ -128,9 +182,37 @@ public function testGetSupportedDatabaseException(): void {
->expects($this->once())
->method('getValue')
->willReturn('NotAnArray');

$this->setupClass->getSupportedDatabases();
}

public function testGetAvailableDbDriversForPdoWhenPdoIsUnavailable(): void {
$setup = $this->getMockBuilder(Setup::class)
->onlyMethods(['class_exists'])
->setConstructorArgs([
$this->config,
$this->iniWrapper,
$this->l10nFactory,
$this->defaults,
$this->logger,
$this->random,
$this->installer,
$this->eventDispatcher,
])
->getMock();

$setup
->expects($this->once())
->method('class_exists')
->with(\PDO::class)
->willReturn(false);

$this->assertSame(
[],
self::invokePrivate($setup, 'getAvailableDbDriversForPdo'),
);
}

/**
* @param $url
* @param $expected
Expand Down Expand Up @@ -173,4 +255,35 @@ public static function findWebRootProvider(): array {
'empty' => ['', false],
];
}

/**
* @param string $configuredWebRoot
*/
#[\PHPUnit\Framework\Attributes\DataProvider('findWebRootWebProvider')]
public function testFindWebRootWeb(string $configuredWebRoot, string $expected): void {
$cliState = \OC::$CLI;
$webRootState = \OC::$WEBROOT;
$this->config
->expects($this->never())
->method('getValue');

try {
\OC::$CLI = false;
\OC::$WEBROOT = $configuredWebRoot;

$webRoot = self::invokePrivate($this->setupClass, 'findWebRoot', [$this->config]);
} finally {
\OC::$CLI = $cliState;
\OC::$WEBROOT = $webRootState;
}

$this->assertSame($expected, $webRoot);
}

public static function findWebRootWebProvider(): array {
return [
'configured web root' => ['/nextcloud', '/nextcloud'],
'empty web root defaults to slash' => ['', '/'],
];
}
}
Loading