Skip to content

Commit 5c42326

Browse files
committed
fix(updatenotification): Parse pre-release version of apps
The notitication is correctly created, but the changelog is not show. We need to make sure the version passed to the manager is in the allowed format (major.minor.patch). Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 326120a commit 5c42326

2 files changed

Lines changed: 185 additions & 1 deletion

File tree

apps/updatenotification/lib/Controller/APIController.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,15 @@ protected function getAppDetails(string $appId): array {
160160
*/
161161
public function getAppChangelogEntry(string $appId, ?string $version = null): DataResponse {
162162
$version = $version ?? $this->appManager->getAppVersion($appId);
163-
$changes = $this->manager->getChangelog($appId, $version);
163+
// handle pre-release versions
164+
$matches = [];
165+
$result = preg_match('/^(\d+\.\d+(\.\d+)?)/', $version, $matches);
166+
if ($result === false || $result === 0) {
167+
return new DataResponse([], Http::STATUS_BAD_REQUEST);
168+
}
169+
$shortVersion = $matches[0];
170+
171+
$changes = $this->manager->getChangelog($appId, $shortVersion);
164172

165173
if ($changes === null) {
166174
return new DataResponse([], Http::STATUS_NOT_FOUND);
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
namespace OCA\UpdateNotification\Tests\Controller;
10+
11+
use OC\App\AppStore\Fetcher\AppFetcher;
12+
use OCA\UpdateNotification\AppInfo\Application;
13+
use OCA\UpdateNotification\BackgroundJob\ResetToken;
14+
use OCA\UpdateNotification\Controller\AdminController;
15+
use OCA\UpdateNotification\Controller\APIController;
16+
use OCA\UpdateNotification\Manager;
17+
use OCP\App\IAppManager;
18+
use OCP\AppFramework\Http;
19+
use OCP\AppFramework\Http\DataResponse;
20+
use OCP\AppFramework\Utility\ITimeFactory;
21+
use OCP\BackgroundJob\IJobList;
22+
use OCP\IAppConfig;
23+
use OCP\IConfig;
24+
use OCP\IL10N;
25+
use OCP\IRequest;
26+
use OCP\IUserSession;
27+
use OCP\L10N\IFactory;
28+
use OCP\Security\ISecureRandom;
29+
use PHPUnit\Framework\Exception;
30+
use PHPUnit\Framework\MockObject\MethodNameNotConfiguredException;
31+
use PHPUnit\Framework\MockObject\MethodParametersAlreadyConfiguredException;
32+
use PHPUnit\Framework\MockObject\IncompatibleReturnValueException;
33+
use PHPUnit\Framework\ExpectationFailedException;
34+
use PHPUnit\Framework\MockObject\MockObject;
35+
use SebastianBergmann\RecursionContext\InvalidArgumentException;
36+
use Test\TestCase;
37+
38+
class APIControllerTest extends TestCase {
39+
private IRequest&MockObject $request;
40+
private IConfig&MockObject $config;
41+
private IAppManager&MockObject $appManager;
42+
private AppFetcher&MockObject $appFetcher;
43+
private IFactory&MockObject $l10nFactory;
44+
private IUserSession&MockObject $userSession;
45+
private Manager&MockObject $manager;
46+
47+
private APIController $apiController;
48+
49+
protected function setUp(): void {
50+
parent::setUp();
51+
52+
$this->request = $this->createMock(IRequest::class);
53+
$this->config = $this->createMock(IConfig::class);
54+
$this->appManager = $this->createMock(IAppManager::class);
55+
$this->appFetcher = $this->createMock(AppFetcher::class);
56+
$this->l10nFactory = $this->createMock(IFactory::class);
57+
$this->userSession = $this->createMock(IUserSession::class);
58+
$this->manager = $this->createMock(Manager::class);
59+
60+
$this->apiController = new APIController(
61+
Application::APP_NAME,
62+
$this->request,
63+
$this->config,
64+
$this->appManager,
65+
$this->appFetcher,
66+
$this->l10nFactory,
67+
$this->userSession,
68+
$this->manager,
69+
);
70+
}
71+
72+
/**
73+
* @dataProvider dataGetAppChangelog
74+
*/
75+
public function testGetAppChangelogEntry(
76+
array $params,
77+
bool $hasChanges,
78+
array $appInfo,
79+
array $expected,
80+
): void {
81+
$this->appManager->method('getAppInfo')
82+
->with('the-app')
83+
->willReturn($appInfo);
84+
$this->appManager->method('getAppVersion')
85+
->with('the-app')
86+
->willReturn($appInfo['version']);
87+
$this->manager->method('getChangelog')
88+
->with('the-app', self::anything())
89+
->willReturnCallback(fn ($app, $version) => $hasChanges ? "$app v$version" : null);
90+
91+
$result = $this->apiController->getAppChangelogEntry(...$params);
92+
$this->assertEquals($result->getStatus(), $expected['status']);
93+
$this->assertEquals($result->getData(), $expected['data']);
94+
}
95+
96+
public static function dataGetAppChangelog(): array {
97+
return [
98+
'no changes found' => [
99+
['the-app', null],
100+
false,
101+
[
102+
'name' => 'Localized name',
103+
'version' => '1.0.0',
104+
],
105+
[
106+
'status' => Http::STATUS_NOT_FOUND,
107+
'data' => [],
108+
]
109+
],
110+
'changes with version parameter' => [
111+
['the-app', '1.0.0'],
112+
true,
113+
[
114+
'name' => 'Localized name',
115+
'version' => '1.2.0', // installed version
116+
],
117+
[
118+
'status' => Http::STATUS_OK,
119+
'data' => [
120+
'appName' => 'Localized name',
121+
'content' => 'the-app v1.0.0',
122+
'version' => '1.0.0',
123+
],
124+
]
125+
],
126+
'changes without version parameter' => [
127+
['the-app', null],
128+
true,
129+
[
130+
'name' => 'Localized name',
131+
'version' => '1.2.0',
132+
],
133+
[
134+
'status' => Http::STATUS_OK,
135+
'data' => [
136+
'appName' => 'Localized name',
137+
'content' => 'the-app v1.2.0',
138+
'version' => '1.2.0',
139+
],
140+
]
141+
],
142+
'changes of pre-release version' => [
143+
['the-app', null],
144+
true,
145+
[
146+
'name' => 'Localized name',
147+
'version' => '1.2.0-alpha.1',
148+
],
149+
[
150+
'status' => Http::STATUS_OK,
151+
'data' => [
152+
'appName' => 'Localized name',
153+
'content' => 'the-app v1.2.0',
154+
'version' => '1.2.0-alpha.1',
155+
],
156+
]
157+
],
158+
'changes of pre-release version as parameter' => [
159+
['the-app', '1.2.0-alpha.2'],
160+
true,
161+
[
162+
'name' => 'Localized name',
163+
'version' => '1.2.0-beta.3',
164+
],
165+
[
166+
'status' => Http::STATUS_OK,
167+
'data' => [
168+
'appName' => 'Localized name',
169+
'content' => 'the-app v1.2.0',
170+
'version' => '1.2.0-alpha.2',
171+
],
172+
]
173+
],
174+
];
175+
}
176+
}

0 commit comments

Comments
 (0)