Fix tests for setup checks

Had to remove tests for migrated checks, we should add tests for each
 SetupCheck class later on.

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2023-10-09 18:03:25 +02:00
parent efa2dfa641
commit 6bc3e008c7
No known key found for this signature in database
GPG key ID: A3E2F658B28C760A
4 changed files with 58 additions and 137 deletions

View file

@ -60,6 +60,7 @@ use OCP\IURLGenerator;
use OCP\Lock\ILockingProvider;
use OCP\Notification\IManager;
use OCP\Security\Bruteforce\IThrottler;
use OCP\SetupCheck\ISetupCheckManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
@ -113,6 +114,8 @@ class CheckSetupControllerTest extends TestCase {
private $appManager;
/** @var IServerContainer|MockObject */
private $serverContainer;
/** @var ISetupCheckManager|MockObject */
private $setupCheckManager;
/**
* Holds a list of directories created during tests.
@ -159,6 +162,7 @@ class CheckSetupControllerTest extends TestCase {
$this->notificationManager = $this->getMockBuilder(IManager::class)->getMock();
$this->appManager = $this->createMock(IAppManager::class);
$this->serverContainer = $this->createMock(IServerContainer::class);
$this->setupCheckManager = $this->createMock(ISetupCheckManager::class);
$this->checkSetupController = $this->getMockBuilder(CheckSetupController::class)
->setConstructorArgs([
'settings',
@ -182,6 +186,7 @@ class CheckSetupControllerTest extends TestCase {
$this->notificationManager,
$this->appManager,
$this->serverContainer,
$this->setupCheckManager,
])
->setMethods([
'isReadOnlyConfig',
@ -224,73 +229,6 @@ class CheckSetupControllerTest extends TestCase {
$this->dirsToRemove = [];
}
public function testIsInternetConnectionWorkingDisabledViaConfig() {
$this->config->expects($this->once())
->method('getSystemValue')
->with('has_internet_connection', true)
->willReturn(false);
$this->assertFalse(
self::invokePrivate(
$this->checkSetupController,
'hasInternetConnectivityProblems'
)
);
}
public function testIsInternetConnectionWorkingCorrectly() {
$this->config->expects($this->exactly(2))
->method('getSystemValue')
->withConsecutive(
['has_internet_connection', true],
['connectivity_check_domains', ['www.nextcloud.com', 'www.startpage.com', 'www.eff.org', 'www.edri.org']],
)->willReturnArgument(1);
$client = $this->getMockBuilder('\OCP\Http\Client\IClient')
->disableOriginalConstructor()->getMock();
$client->expects($this->any())
->method('get');
$this->clientService->expects($this->once())
->method('newClient')
->willReturn($client);
$this->assertFalse(
self::invokePrivate(
$this->checkSetupController,
'hasInternetConnectivityProblems'
)
);
}
public function testIsInternetConnectionFail() {
$this->config->expects($this->exactly(2))
->method('getSystemValue')
->withConsecutive(
['has_internet_connection', true],
['connectivity_check_domains', ['www.nextcloud.com', 'www.startpage.com', 'www.eff.org', 'www.edri.org']],
)->willReturnArgument(1);
$client = $this->getMockBuilder('\OCP\Http\Client\IClient')
->disableOriginalConstructor()->getMock();
$client->expects($this->any())
->method('get')
->will($this->throwException(new \Exception()));
$this->clientService->expects($this->exactly(4))
->method('newClient')
->willReturn($client);
$this->assertTrue(
self::invokePrivate(
$this->checkSetupController,
'hasInternetConnectivityProblems'
)
);
}
public function testIsMemcacheConfiguredFalse() {
$this->config->expects($this->once())
->method('getSystemValue')
@ -319,36 +257,6 @@ class CheckSetupControllerTest extends TestCase {
);
}
public function testIsPhpSupportedFalse() {
$this->checkSetupController
->expects($this->once())
->method('isPhpOutdated')
->willReturn(true);
$this->assertEquals(
['eol' => true, 'version' => PHP_VERSION],
self::invokePrivate($this->checkSetupController, 'isPhpSupported')
);
}
public function testIsPhpSupportedTrue() {
$this->checkSetupController
->expects($this->exactly(2))
->method('isPhpOutdated')
->willReturn(false);
$this->assertEquals(
['eol' => false, 'version' => PHP_VERSION],
self::invokePrivate($this->checkSetupController, 'isPhpSupported')
);
$this->assertEquals(
['eol' => false, 'version' => PHP_VERSION],
self::invokePrivate($this->checkSetupController, 'isPhpSupported')
);
}
/**
* @dataProvider dataForwardedForHeadersWorking
*
@ -454,23 +362,8 @@ class CheckSetupControllerTest extends TestCase {
['X-Forwarded-Host', '']
]);
$client = $this->getMockBuilder('\OCP\Http\Client\IClient')
->disableOriginalConstructor()->getMock();
$client->expects($this->exactly(4))
->method('get')
->withConsecutive(
['http://www.nextcloud.com/', []],
['http://www.startpage.com/', []],
['http://www.eff.org/', []],
['http://www.edri.org/', []]
)->will($this->throwException(new \Exception()));
$this->clientService->expects($this->exactly(4))
->method('newClient')
->willReturn($client);
$this->checkSetupController
->expects($this->once())
->method('isPhpOutdated')
->willReturn(true);
$this->clientService->expects($this->never())
->method('newClient');
$this->checkSetupController
->expects($this->once())
->method('getOpcacheSetupRecommendations')
@ -621,16 +514,11 @@ class CheckSetupControllerTest extends TestCase {
'backgroundJobsUrl' => 'https://example.org',
],
'cronErrors' => [],
'serverHasInternetConnectionProblems' => true,
'isMemcacheConfigured' => true,
'memcacheDocs' => 'http://docs.example.org/server/go.php?to=admin-performance',
'isRandomnessSecure' => self::invokePrivate($this->checkSetupController, 'isRandomnessSecure'),
'securityDocs' => 'https://docs.example.org/server/8.1/admin_manual/configuration_server/hardening.html',
'isUsedTlsLibOutdated' => '',
'phpSupported' => [
'eol' => true,
'version' => PHP_VERSION
],
'forwardedForHeadersWorking' => false,
'reverseProxyDocs' => 'reverse-proxy-doc-link',
'isCorrectMemcachedPHPModuleInstalled' => true,
@ -654,19 +542,12 @@ class CheckSetupControllerTest extends TestCase {
'isMysqlUsedWithoutUTF8MB4' => false,
'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed' => true,
'reverseProxyGeneratedURL' => 'https://server/index.php',
'OCA\Settings\SetupChecks\PhpDefaultCharset' => ['pass' => true, 'description' => 'PHP configuration option default_charset should be UTF-8', 'severity' => 'warning'],
'OCA\Settings\SetupChecks\PhpOutputBuffering' => ['pass' => true, 'description' => 'PHP configuration option output_buffering must be disabled', 'severity' => 'error'],
'OCA\Settings\SetupChecks\LegacySSEKeyFormat' => ['pass' => true, 'description' => 'The old server-side-encryption format is enabled. We recommend disabling this.', 'severity' => 'warning', 'linkToDocumentation' => ''],
'OCA\Settings\SetupChecks\CheckUserCertificates' => ['pass' => false, 'description' => 'There are some user imported SSL certificates present, that are not used anymore with Nextcloud 21. They can be imported on the command line via "occ security:certificates:import" command. Their paths inside the data directory are shown below.', 'severity' => 'warning', 'elements' => ['a', 'b']],
'imageMagickLacksSVGSupport' => false,
'isDefaultPhoneRegionSet' => false,
'OCA\Settings\SetupChecks\SupportedDatabase' => ['pass' => true, 'description' => '', 'severity' => 'info'],
'isFairUseOfFreePushService' => false,
'temporaryDirectoryWritable' => false,
\OCA\Settings\SetupChecks\LdapInvalidUuids::class => ['pass' => true, 'description' => 'Invalid UUIDs of LDAP users or groups have been found. Please review your "Override UUID detection" settings in the Expert part of the LDAP configuration and use "occ ldap:update-uuid" to update them.', 'severity' => 'warning'],
\OCA\Settings\SetupChecks\NeedsSystemAddressBookSync::class => ['pass' => true, 'description' => 'The DAV system address book sync has not run yet as your instance has more than 1000 users or because an error occurred. Please run it manually by calling "occ dav:sync-system-addressbook".', 'severity' => 'warning'],
'isBruteforceThrottled' => false,
'bruteforceRemoteAddress' => '',
'generic' => [],
]
);
$this->assertEquals($expected, $this->checkSetupController->check());
@ -695,7 +576,8 @@ class CheckSetupControllerTest extends TestCase {
$this->tempManager,
$this->notificationManager,
$this->appManager,
$this->serverContainer
$this->serverContainer,
$this->setupCheckManager,
])
->setMethods(null)->getMock();
@ -1423,7 +1305,8 @@ Array
$this->tempManager,
$this->notificationManager,
$this->appManager,
$this->serverContainer
$this->serverContainer,
$this->setupCheckManager,
);
$this->assertSame($expected, $this->invokePrivate($checkSetupController, 'isMysqlUsedWithoutUTF8MB4'));
@ -1478,7 +1361,8 @@ Array
$this->tempManager,
$this->notificationManager,
$this->appManager,
$this->serverContainer
$this->serverContainer,
$this->setupCheckManager,
);
$this->assertSame($expected, $this->invokePrivate($checkSetupController, 'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed'));

View file

@ -26,19 +26,37 @@ declare(strict_types=1);
namespace OCA\Settings\Tests;
use OCA\Settings\SetupChecks\PhpDefaultCharset;
use OCP\IL10N;
use OCP\SetupCheck\SetupResult;
use Test\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
class PhpDefaultCharsetTest extends TestCase {
/** @var IL10N|MockObject */
private $l10n;
protected function setUp(): void {
parent::setUp();
$this->l10n = $this->getMockBuilder(IL10N::class)
->disableOriginalConstructor()->getMock();
$this->l10n->expects($this->any())
->method('t')
->willReturnCallback(function ($message, array $replace) {
return vsprintf($message, $replace);
});
}
public function testPass(): void {
$check = new PhpDefaultCharset();
$this->assertTrue($check->run());
$check = new PhpDefaultCharset($this->l10n);
$this->assertEquals(SetupResult::SUCCESS, $check->run()->getSeverity());
}
public function testFail(): void {
ini_set('default_charset', 'ISO-8859-15');
$check = new PhpDefaultCharset();
$this->assertFalse($check->run());
$check = new PhpDefaultCharset($this->l10n);
$this->assertEquals(SetupResult::WARNING, $check->run()->getSeverity());
ini_restore('default_charset');
}

View file

@ -26,16 +26,34 @@ declare(strict_types=1);
namespace OCA\Settings\Tests;
use OCA\Settings\SetupChecks\PhpOutputBuffering;
use OCP\IL10N;
use OCP\SetupCheck\SetupResult;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class PhpOutputBufferingTest extends TestCase {
/** @var IL10N|MockObject */
private $l10n;
protected function setUp(): void {
parent::setUp();
$this->l10n = $this->getMockBuilder(IL10N::class)
->disableOriginalConstructor()->getMock();
$this->l10n->expects($this->any())
->method('t')
->willReturnCallback(function ($message, array $replace) {
return vsprintf($message, $replace);
});
}
/*
* output_buffer is PHP_INI_PERDIR and cannot changed at runtime.
* Run this test with -d output_buffering=1 to validate the fail case.
*/
public function testPass(): void {
$check = new PhpOutputBuffering();
$this->assertTrue($check->run());
$check = new PhpOutputBuffering($this->l10n);
$this->assertEquals(SetupResult::SUCCESS, $check->run()->getSeverity());
}
}

View file

@ -27,6 +27,7 @@ namespace OCA\Settings\Tests;
use OCA\Settings\SetupChecks\SupportedDatabase;
use OCP\IL10N;
use OCP\SetupCheck\SetupResult;
use Test\TestCase;
/**
@ -36,6 +37,6 @@ class SupportedDatabaseTest extends TestCase {
public function testPass(): void {
$l10n = $this->getMockBuilder(IL10N::class)->getMock();
$check = new SupportedDatabase($l10n, \OC::$server->getDatabaseConnection());
$this->assertTrue($check->run());
$this->assertEquals(SetupResult::SUCCESS, $check->run()->getSeverity());
}
}