|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Tests\Core\Command\Maintenance; |
| 10 | + |
| 11 | +use bantu\IniGetWrapper\IniGetWrapper; |
| 12 | +use OC\Core\Command\Maintenance\Install; |
| 13 | +use OC\SystemConfig; |
| 14 | +use Symfony\Component\Console\Input\ArrayInput; |
| 15 | +use Symfony\Component\Console\Output\NullOutput; |
| 16 | +use Test\TestCase; |
| 17 | + |
| 18 | +class InstallTest extends TestCase { |
| 19 | + private Install $command; |
| 20 | + |
| 21 | + #[\Override] |
| 22 | + protected function setUp(): void { |
| 23 | + parent::setUp(); |
| 24 | + |
| 25 | + $this->command = new Install( |
| 26 | + $this->createMock(SystemConfig::class), |
| 27 | + $this->createMock(IniGetWrapper::class), |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @param array<string, string|bool> $parameters |
| 33 | + * @return array The installer options built from the command line input |
| 34 | + */ |
| 35 | + private function validateInput(array $parameters): array { |
| 36 | + $input = new ArrayInput(array_merge([ |
| 37 | + '--database-name' => 'nextcloud', |
| 38 | + '--database-user' => 'admin', |
| 39 | + '--database-pass' => 'admin-password', |
| 40 | + '--admin-pass' => 'admin-password', |
| 41 | + ], $parameters), $this->command->getDefinition()); |
| 42 | + |
| 43 | + return self::invokePrivate($this->command, 'validateInput', [$input, new NullOutput(), ['sqlite', 'mysql', 'pgsql', 'oci']]); |
| 44 | + } |
| 45 | + |
| 46 | + public static function encryptionOptions(): array { |
| 47 | + return [ |
| 48 | + '--database-ssl-mode' => ['--database-ssl-mode', 'verify-full', 'dbsslmode', 'verify-full'], |
| 49 | + '--database-ssl-ca' => ['--database-ssl-ca', '/ca.pem', 'dbsslca', '/ca.pem'], |
| 50 | + '--database-ssl-cert' => ['--database-ssl-cert', '/client.crt', 'dbsslcert', '/client.crt'], |
| 51 | + '--database-ssl-key' => ['--database-ssl-key', '/client.key', 'dbsslkey', '/client.key'], |
| 52 | + '--database-ssl-crl' => ['--database-ssl-crl', '/crl.pem', 'dbsslcrl', '/crl.pem'], |
| 53 | + '--database-ssl-no-verify' => ['--database-ssl-no-verify', true, 'dbsslnoverify', true], |
| 54 | + ]; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * The command only forwards the options, the database setup translates them into the |
| 59 | + * system config values and rejects the ones it does not support. |
| 60 | + */ |
| 61 | + #[\PHPUnit\Framework\Attributes\DataProvider('encryptionOptions')] |
| 62 | + public function testForwardsEncryptionOptions(string $parameter, string|bool $value, string $option, string|bool $expected): void { |
| 63 | + $options = $this->validateInput([ |
| 64 | + '--database' => 'pgsql', |
| 65 | + $parameter => $value, |
| 66 | + ]); |
| 67 | + |
| 68 | + $this->assertSame($expected, $options[$option]); |
| 69 | + } |
| 70 | + |
| 71 | + public function testNoEncryptionOptions(): void { |
| 72 | + $options = $this->validateInput(['--database' => 'mysql']); |
| 73 | + |
| 74 | + foreach (['dbsslmode', 'dbsslca', 'dbsslcert', 'dbsslkey', 'dbsslcrl', 'dbsslnoverify'] as $option) { |
| 75 | + $this->assertArrayNotHasKey($option, $options); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * An option that does not apply to the chosen database is not filtered out here, it |
| 81 | + * has to be reported by the database setup instead of being silently dropped. |
| 82 | + */ |
| 83 | + public function testForwardsEncryptionOptionsRegardlessOfDatabase(): void { |
| 84 | + $options = $this->validateInput([ |
| 85 | + '--database' => 'sqlite', |
| 86 | + '--database-ssl-ca' => '/ca.pem', |
| 87 | + ]); |
| 88 | + |
| 89 | + $this->assertSame('/ca.pem', $options['dbsslca']); |
| 90 | + } |
| 91 | +} |
0 commit comments