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
47 changes: 46 additions & 1 deletion lib/private/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ public function installApp(string $appId, bool $forceEnable = false): string {
*/
public function updateAppstoreApp(string $appId, bool $allowUnstable = false): bool {
if ($this->isUpdateAvailable($appId, $allowUnstable) !== false) {
// Before downloading, check whether the app is currently disabled due to version
// incompatibility with this NC version. If so, re-enable it after a successful update.
$isDisabled = !$this->appManager->isEnabledForAnyone($appId);
$wasIncompatible = false;
if ($isDisabled) {
$currentInfo = $this->appManager->getAppInfo($appId);
$ncVersion = implode('.', Util::getVersion());
$wasIncompatible = $currentInfo !== null && !$this->appManager->isAppCompatible($ncVersion, $currentInfo);
$this->logger->debug('App {appId} is disabled; incompatible with NC {version}: {incompat}', [
'appId' => $appId,
'version' => $ncVersion,
'incompat' => $wasIncompatible ? 'yes' : 'no',
'app' => 'updater',
]);
}

try {
$this->downloadApp($appId, $allowUnstable);
} catch (\Exception $e) {
Expand All @@ -109,9 +125,29 @@ public function updateAppstoreApp(string $appId, bool $allowUnstable = false): b
]);
return false;
}
return $this->appManager->upgradeApp($appId);

$result = $this->appManager->upgradeApp($appId);

if ($result && $isDisabled && $wasIncompatible) {
$this->logger->info('Re-enabling {appId} after update: it was disabled due to version incompatibility', [
'appId' => $appId,
'app' => 'updater',
]);
try {
$this->appManager->enableApp($appId);
} catch (\Exception $e) {
$this->logger->warning('Could not re-enable {appId} after update: {error}', [
'appId' => $appId,
'error' => $e->getMessage(),
'app' => 'updater',
]);
}
}

return $result;
}

$this->logger->debug('No update available for {appId}, skipping', ['appId' => $appId, 'app' => 'updater']);
return false;
}

Expand Down Expand Up @@ -373,6 +409,7 @@ public function isUpdateAvailable($appId, $allowUnstable = false): string|false
}

if ($this->isInstalledFromGit($appId) === true) {
$this->logger->debug('App {appId} is installed from git, skipping update check', ['appId' => $appId, 'app' => 'updater']);
return false;
}

Expand All @@ -385,17 +422,25 @@ public function isUpdateAvailable($appId, $allowUnstable = false): string|false
$currentVersion = $this->appManager->getAppVersion($appId, true);

if (!isset($app['releases'][0]['version'])) {
$this->logger->debug('App {appId} has no release version in app store data', ['appId' => $appId, 'app' => 'updater']);
return false;
}
$newestVersion = $app['releases'][0]['version'];
if ($currentVersion !== '0' && version_compare($newestVersion, $currentVersion, '>')) {
return $newestVersion;
} else {
$this->logger->debug('No newer version available for {appId}: current={current}, newest={newest}', [
'appId' => $appId,
'current' => $currentVersion,
'newest' => $newestVersion,
'app' => 'updater',
]);
return false;
}
}
}

$this->logger->debug('App {appId} not found in app store', ['appId' => $appId, 'app' => 'updater']);
return false;
}

Expand Down
101 changes: 101 additions & 0 deletions tests/lib/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,107 @@ public function testDownloadAppSuccessful(): void {
}


public function testIsUpdateAvailableLogsDebugForGitInstall(): void {
$tmpDir = sys_get_temp_dir() . '/nc_test_git_' . uniqid();
mkdir($tmpDir . '/.git', 0700, true);

$this->appManager
->expects($this->once())
->method('getAppPath')
->with('myapp')
->willReturn($tmpDir);
$this->logger
->expects($this->once())
->method('debug')
->with(
'App {appId} is installed from git, skipping update check',
$this->callback(fn($ctx) => $ctx['appId'] === 'myapp')
);

$installer = $this->getInstaller();
$result = $installer->isUpdateAvailable('myapp');
$this->assertFalse($result);

rmdir($tmpDir . '/.git');
rmdir($tmpDir);
}

protected function getPartialInstaller(array $onlyMethods): Installer&\PHPUnit\Framework\MockObject\MockObject {
return $this->getMockBuilder(Installer::class)
->setConstructorArgs([
$this->appFetcher,
$this->clientService,
$this->tempManager,
$this->logger,
$this->config,
$this->appManager,
$this->l10nFactory,
false,
])
->onlyMethods($onlyMethods)
->getMock();
}

public function testUpdateAppstoreAppReEnablesDisabledIncompatibleApp(): void {
$installer = $this->getPartialInstaller(['isUpdateAvailable', 'downloadApp']);
$installer->method('isUpdateAvailable')->willReturn('1.0.0');
$installer->method('downloadApp')->willReturn(null);

$this->appManager->method('isEnabledForAnyone')->with('myapp')->willReturn(false);
$this->appManager->method('getAppInfo')->with('myapp')->willReturn(['id' => 'myapp', 'version' => '0.0.1']);
$this->appManager->method('isAppCompatible')->willReturn(false);
$this->appManager->method('upgradeApp')->with('myapp')->willReturn(true);
$this->appManager->expects($this->once())->method('enableApp')->with('myapp');

$result = $installer->updateAppstoreApp('myapp');
$this->assertTrue($result);
}

public function testUpdateAppstoreAppDoesNotReEnableCompatibleButDisabledApp(): void {
$installer = $this->getPartialInstaller(['isUpdateAvailable', 'downloadApp']);
$installer->method('isUpdateAvailable')->willReturn('1.0.0');
$installer->method('downloadApp')->willReturn(null);

$this->appManager->method('isEnabledForAnyone')->with('myapp')->willReturn(false);
$this->appManager->method('getAppInfo')->with('myapp')->willReturn(['id' => 'myapp', 'version' => '1.0.0']);
$this->appManager->method('isAppCompatible')->willReturn(true);
$this->appManager->method('upgradeApp')->with('myapp')->willReturn(true);
$this->appManager->expects($this->never())->method('enableApp');

$result = $installer->updateAppstoreApp('myapp');
$this->assertTrue($result);
}

public function testUpdateAppstoreAppDoesNotReEnableAlreadyEnabledApp(): void {
$installer = $this->getPartialInstaller(['isUpdateAvailable', 'downloadApp']);
$installer->method('isUpdateAvailable')->willReturn('1.0.0');
$installer->method('downloadApp')->willReturn(null);

$this->appManager->method('isEnabledForAnyone')->with('myapp')->willReturn(true);
$this->appManager->method('upgradeApp')->with('myapp')->willReturn(true);
$this->appManager->expects($this->never())->method('enableApp');
$this->appManager->expects($this->never())->method('getAppInfo');
$this->appManager->expects($this->never())->method('isAppCompatible');

$result = $installer->updateAppstoreApp('myapp');
$this->assertTrue($result);
}

public function testUpdateAppstoreAppDoesNotReEnableWhenUpgradeFails(): void {
$installer = $this->getPartialInstaller(['isUpdateAvailable', 'downloadApp']);
$installer->method('isUpdateAvailable')->willReturn('1.0.0');
$installer->method('downloadApp')->willReturn(null);

$this->appManager->method('isEnabledForAnyone')->with('myapp')->willReturn(false);
$this->appManager->method('getAppInfo')->with('myapp')->willReturn(['id' => 'myapp', 'version' => '0.0.1']);
$this->appManager->method('isAppCompatible')->willReturn(false);
$this->appManager->method('upgradeApp')->with('myapp')->willReturn(false);
$this->appManager->expects($this->never())->method('enableApp');

$result = $installer->updateAppstoreApp('myapp');
$this->assertFalse($result);
}

public function testDownloadAppWithDowngrade(): void {
// Use previous test to download the application in version 0.9
$this->testDownloadAppSuccessful();
Expand Down
Loading