2014-03-14 13:03:18 +01:00
|
|
|
<?php
|
2025-04-27 14:38:18 +02:00
|
|
|
|
2014-03-14 13:03:18 +01:00
|
|
|
/**
|
2024-05-10 15:09:14 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2014-03-14 13:03:18 +01:00
|
|
|
*/
|
|
|
|
|
2016-05-19 09:38:52 +02:00
|
|
|
namespace Test;
|
|
|
|
|
2014-03-14 13:03:18 +01:00
|
|
|
/**
|
|
|
|
* Tests for server check functions
|
2015-11-25 16:58:54 +01:00
|
|
|
*
|
|
|
|
* @group DB
|
2014-03-14 13:03:18 +01:00
|
|
|
*/
|
2016-05-19 09:38:52 +02:00
|
|
|
class UtilCheckServerTest extends \Test\TestCase {
|
2014-03-14 13:03:18 +01:00
|
|
|
private $datadir;
|
|
|
|
|
2014-09-08 14:56:11 +02:00
|
|
|
/**
|
|
|
|
* @param array $systemOptions
|
2020-08-11 21:32:18 +02:00
|
|
|
* @return \OC\SystemConfig | \PHPUnit\Framework\MockObject\MockObject
|
2014-09-08 14:56:11 +02:00
|
|
|
*/
|
|
|
|
protected function getConfig($systemOptions) {
|
|
|
|
$systemOptions['datadirectory'] = $this->datadir;
|
2014-09-08 15:05:57 +02:00
|
|
|
$systemOptions['appstoreenabled'] = false; //it's likely that there is no app folder we can write in
|
2017-03-17 16:37:48 -06:00
|
|
|
$config = $this->getMockBuilder('\OC\SystemConfig')
|
2014-09-08 14:56:11 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$config->expects($this->any())
|
2017-03-17 16:37:48 -06:00
|
|
|
->method('getValue')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturnCallback(function ($key, $default) use ($systemOptions) {
|
2023-07-07 14:15:12 +03:30
|
|
|
return $systemOptions[$key] ?? $default;
|
2020-03-25 22:21:27 +01:00
|
|
|
});
|
2014-09-08 14:56:11 +02:00
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:40:38 +01:00
|
|
|
protected function setUp(): void {
|
2014-11-10 22:59:50 +01:00
|
|
|
parent::setUp();
|
|
|
|
|
2015-12-18 11:19:53 +01:00
|
|
|
$this->datadir = \OC::$server->getTempManager()->getTemporaryFolder();
|
2014-03-14 13:03:18 +01:00
|
|
|
|
2024-07-11 20:53:37 +02:00
|
|
|
file_put_contents($this->datadir . '/.ncdata', '# Nextcloud data directory');
|
2014-09-08 15:05:57 +02:00
|
|
|
\OC::$server->getSession()->set('checkServer_succeeded', false);
|
2014-03-14 13:03:18 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 16:40:38 +01:00
|
|
|
protected function tearDown(): void {
|
2014-03-14 13:03:18 +01:00
|
|
|
// clean up
|
2024-07-11 20:53:37 +02:00
|
|
|
@unlink($this->datadir . '/.ncdata');
|
2014-11-10 22:59:50 +01:00
|
|
|
parent::tearDown();
|
2014-03-14 13:03:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that checkServer() returns no errors in the regular case.
|
|
|
|
*/
|
|
|
|
public function testCheckServer() {
|
2020-03-26 09:30:18 +01:00
|
|
|
$result = \OC_Util::checkServer($this->getConfig([
|
2014-09-08 14:56:11 +02:00
|
|
|
'installed' => true
|
2020-03-26 09:30:18 +01:00
|
|
|
]));
|
2014-03-14 13:03:18 +01:00
|
|
|
$this->assertEmpty($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that checkServer() does not check the data dir validity
|
|
|
|
* when the server is not installed yet (else the setup cannot
|
|
|
|
* be run...)
|
|
|
|
*/
|
|
|
|
public function testCheckServerSkipDataDirValidityOnSetup() {
|
|
|
|
// simulate old version that didn't have it
|
2024-07-11 20:53:37 +02:00
|
|
|
unlink($this->datadir . '/.ncdata');
|
2014-03-14 13:03:18 +01:00
|
|
|
|
2024-07-11 20:53:37 +02:00
|
|
|
// even though ".ncdata" is missing, the error isn't
|
2014-03-14 13:03:18 +01:00
|
|
|
// triggered to allow setup to run
|
2020-03-26 09:30:18 +01:00
|
|
|
$result = \OC_Util::checkServer($this->getConfig([
|
2014-09-08 14:56:11 +02:00
|
|
|
'installed' => false
|
2020-03-26 09:30:18 +01:00
|
|
|
]));
|
2014-03-14 13:03:18 +01:00
|
|
|
$this->assertEmpty($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that checkServer() does not check the data dir validity
|
|
|
|
* when an upgrade is required (else the upgrade cannot be
|
|
|
|
* performed...)
|
|
|
|
*/
|
|
|
|
public function testCheckServerSkipDataDirValidityOnUpgrade() {
|
|
|
|
// simulate old version that didn't have it
|
2024-07-11 20:53:37 +02:00
|
|
|
unlink($this->datadir . '/.ncdata');
|
2014-03-14 13:03:18 +01:00
|
|
|
|
|
|
|
$session = \OC::$server->getSession();
|
|
|
|
$oldCurrentVersion = $session->get('OC_Version');
|
|
|
|
|
|
|
|
// upgrade condition to simulate needUpgrade() === true
|
2020-03-26 09:30:18 +01:00
|
|
|
$session->set('OC_Version', [6, 0, 0, 2]);
|
2014-03-14 13:03:18 +01:00
|
|
|
|
2024-07-11 20:53:37 +02:00
|
|
|
// even though ".ncdata" is missing, the error isn't
|
2014-03-14 13:03:18 +01:00
|
|
|
// triggered to allow for upgrade
|
2020-03-26 09:30:18 +01:00
|
|
|
$result = \OC_Util::checkServer($this->getConfig([
|
2014-09-08 14:56:11 +02:00
|
|
|
'installed' => true,
|
|
|
|
'version' => '6.0.0.1'
|
2020-03-26 09:30:18 +01:00
|
|
|
]));
|
2014-03-14 13:03:18 +01:00
|
|
|
$this->assertEmpty($result);
|
|
|
|
|
|
|
|
// restore versions
|
|
|
|
$session->set('OC_Version', $oldCurrentVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that checkDataDirectoryValidity returns no error
|
2024-07-11 20:53:37 +02:00
|
|
|
* when ".ncdata" is present.
|
2014-03-14 13:03:18 +01:00
|
|
|
*/
|
|
|
|
public function testCheckDataDirValidity() {
|
|
|
|
$result = \OC_Util::checkDataDirectoryValidity($this->datadir);
|
|
|
|
$this->assertEmpty($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-08 14:56:11 +02:00
|
|
|
* Test that checkDataDirectoryValidity and checkServer
|
2024-07-11 20:53:37 +02:00
|
|
|
* both return an error when ".ncdata" is missing.
|
2014-03-14 13:03:18 +01:00
|
|
|
*/
|
|
|
|
public function testCheckDataDirValidityWhenFileMissing() {
|
2024-07-11 20:53:37 +02:00
|
|
|
unlink($this->datadir . '/.ncdata');
|
2014-03-14 13:03:18 +01:00
|
|
|
$result = \OC_Util::checkDataDirectoryValidity($this->datadir);
|
|
|
|
$this->assertEquals(1, count($result));
|
|
|
|
|
2020-03-26 09:30:18 +01:00
|
|
|
$result = \OC_Util::checkServer($this->getConfig([
|
2014-09-08 14:56:11 +02:00
|
|
|
'installed' => true,
|
2015-12-18 15:26:54 +01:00
|
|
|
'version' => implode('.', \OCP\Util::getVersion())
|
2020-03-26 09:30:18 +01:00
|
|
|
]));
|
2014-09-08 14:56:11 +02:00
|
|
|
$this->assertCount(1, $result);
|
2014-03-14 13:03:18 +01:00
|
|
|
}
|
|
|
|
|
2014-09-08 15:05:57 +02:00
|
|
|
/**
|
|
|
|
* Tests that no error is given when the datadir is writable
|
|
|
|
*/
|
|
|
|
public function testDataDirWritable() {
|
2020-03-26 09:30:18 +01:00
|
|
|
$result = \OC_Util::checkServer($this->getConfig([
|
2014-09-08 15:05:57 +02:00
|
|
|
'installed' => true,
|
2015-12-18 15:26:54 +01:00
|
|
|
'version' => implode('.', \OCP\Util::getVersion())
|
2020-03-26 09:30:18 +01:00
|
|
|
]));
|
2014-09-08 15:05:57 +02:00
|
|
|
$this->assertEmpty($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests an error is given when the datadir is not writable
|
|
|
|
*/
|
|
|
|
public function testDataDirNotWritable() {
|
2016-06-07 16:20:07 +02:00
|
|
|
$this->markTestSkipped('TODO: Disable because fails on drone');
|
2014-11-05 16:47:27 +01:00
|
|
|
|
2014-09-08 15:05:57 +02:00
|
|
|
chmod($this->datadir, 0300);
|
2020-03-26 09:30:18 +01:00
|
|
|
$result = \OC_Util::checkServer($this->getConfig([
|
2014-09-08 15:05:57 +02:00
|
|
|
'installed' => true,
|
2015-12-18 15:26:54 +01:00
|
|
|
'version' => implode('.', \OCP\Util::getVersion())
|
2020-03-26 09:30:18 +01:00
|
|
|
]));
|
2014-09-08 15:05:57 +02:00
|
|
|
$this->assertCount(1, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests no error is given when the datadir is not writable during setup
|
|
|
|
*/
|
|
|
|
public function testDataDirNotWritableSetup() {
|
|
|
|
chmod($this->datadir, 0300);
|
2020-03-26 09:30:18 +01:00
|
|
|
$result = \OC_Util::checkServer($this->getConfig([
|
2014-09-08 15:05:57 +02:00
|
|
|
'installed' => false,
|
2015-12-18 15:26:54 +01:00
|
|
|
'version' => implode('.', \OCP\Util::getVersion())
|
2020-03-26 09:30:18 +01:00
|
|
|
]));
|
2014-09-08 15:05:57 +02:00
|
|
|
chmod($this->datadir, 0700); //needed for cleanup
|
|
|
|
$this->assertEmpty($result);
|
|
|
|
}
|
2014-03-14 13:03:18 +01:00
|
|
|
}
|