1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace Test\Updater;
use OC\Updater\ChangesCheck;
use OC\Updater\ChangesMapper;
use OC\Updater\ChangesResult;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
use OCP\ILogger;
use const Solarium\QueryType\Select\Query\Component\Facet\INCLUDE_LOWER;
use Test\TestCase;
class ChangesCheckTest extends TestCase {
/** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */
protected $clientService;
/** @var ChangesCheck */
protected $checker;
/** @var ChangesMapper|\PHPUnit_Framework_MockObject_MockObject */
protected $mapper;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
protected $logger;
public function setUp() {
parent::setUp();
$this->clientService = $this->createMock(IClientService::class);
$this->mapper = $this->createMock(ChangesMapper::class);
$this->logger = $this->createMock(ILogger::class);
$this->checker = new ChangesCheck($this->clientService, $this->mapper, $this->logger);
}
public function statusCodeProvider():array {
return [
[200, ChangesCheck::RESPONSE_HAS_CONTENT],
[304, ChangesCheck::RESPONSE_USE_CACHE],
[404, ChangesCheck::RESPONSE_NO_CONTENT],
[418, ChangesCheck::RESPONSE_NO_CONTENT],
];
}
/**
* @dataProvider statusCodeProvider
*/
public function testEvaluateResponse(int $statusCode, int $expected) {
$response = $this->createMock(IResponse::class);
$response->expects($this->atLeastOnce())
->method('getStatusCode')
->willReturn($statusCode);
if(!in_array($statusCode, [200, 304, 404])) {
$this->logger->expects($this->once())
->method('debug');
}
$evaluation = $this->invokePrivate($this->checker, 'evaluateResponse', [$response]);
$this->assertSame($expected, $evaluation);
}
public function testCacheResultInsert() {
$version = '13.0.4';
$entry = $this->createMock(ChangesResult::class);
$entry->expects($this->exactly(2))
->method('__call')
->withConsecutive(['getVersion'], ['setVersion', [$version]])
->willReturnOnConsecutiveCalls('', null);
$this->mapper->expects($this->once())
->method('insert');
$this->mapper->expects($this->never())
->method('update');
$this->invokePrivate($this->checker, 'cacheResult', [$entry, $version]);
}
public function testCacheResultUpdate() {
$version = '13.0.4';
$entry = $this->createMock(ChangesResult::class);
$entry->expects($this->once())
->method('__call')
->willReturn($version);
$this->mapper->expects($this->never())
->method('insert');
$this->mapper->expects($this->once())
->method('update');
$this->invokePrivate($this->checker, 'cacheResult', [$entry, $version]);
}
public function changesXMLProvider(): array {
return [
[ # 0 - full example
'<?xml version="1.0" encoding="utf-8" ?>
<release xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://updates.nextcloud.com/changelog_server/schema.xsd"
version="13.0.0">
<changelog href="https://nextcloud.com/changelog/#13-0-0"/>
<whatsNew lang="en">
<regular>
<item>Refined user interface</item>
<item>End-to-end Encryption</item>
<item>Video and Text Chat</item>
</regular>
<admin>
<item>Changes to the Nginx configuration</item>
<item>Theming: CSS files were consolidated</item>
</admin>
</whatsNew>
<whatsNew lang="de">
<regular>
<item>Überarbeitete Benutzerschnittstelle</item>
<item>Ende-zu-Ende Verschlüsselung</item>
<item>Video- und Text-Chat</item>
</regular>
<admin>
<item>Änderungen an der Nginx Konfiguration</item>
<item>Theming: CSS Dateien wurden konsolidiert</item>
</admin>
</whatsNew>
</release>',
[
'changelogURL' => 'https://nextcloud.com/changelog/#13-0-0',
'whatsNew' => [
'en' => [
'regular' => [
'Refined user interface',
'End-to-end Encryption',
'Video and Text Chat'
],
'admin' => [
'Changes to the Nginx configuration',
'Theming: CSS files were consolidated'
],
],
'de' => [
'regular' => [
'Überarbeitete Benutzerschnittstelle',
'Ende-zu-Ende Verschlüsselung',
'Video- und Text-Chat'
],
'admin' => [
'Änderungen an der Nginx Konfiguration',
'Theming: CSS Dateien wurden konsolidiert'
],
],
],
]
],
[ # 1- admin part not translated
'<?xml version="1.0" encoding="utf-8" ?>
<release xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://updates.nextcloud.com/changelog_server/schema.xsd"
version="13.0.0">
<changelog href="https://nextcloud.com/changelog/#13-0-0"/>
<whatsNew lang="en">
<regular>
<item>Refined user interface</item>
<item>End-to-end Encryption</item>
<item>Video and Text Chat</item>
</regular>
<admin>
<item>Changes to the Nginx configuration</item>
<item>Theming: CSS files were consolidated</item>
</admin>
</whatsNew>
<whatsNew lang="de">
<regular>
<item>Überarbeitete Benutzerschnittstelle</item>
<item>Ende-zu-Ende Verschlüsselung</item>
<item>Video- und Text-Chat</item>
</regular>
</whatsNew>
</release>',
[
'changelogURL' => 'https://nextcloud.com/changelog/#13-0-0',
'whatsNew' => [
'en' => [
'regular' => [
'Refined user interface',
'End-to-end Encryption',
'Video and Text Chat'
],
'admin' => [
'Changes to the Nginx configuration',
'Theming: CSS files were consolidated'
],
],
'de' => [
'regular' => [
'Überarbeitete Benutzerschnittstelle',
'Ende-zu-Ende Verschlüsselung',
'Video- und Text-Chat'
],
'admin' => [
],
],
],
]
],
[ # 2 - minimal set
'<?xml version="1.0" encoding="utf-8" ?>
<release xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://updates.nextcloud.com/changelog_server/schema.xsd"
version="13.0.0">
<changelog href="https://nextcloud.com/changelog/#13-0-0"/>
<whatsNew lang="en">
<regular>
<item>Refined user interface</item>
<item>End-to-end Encryption</item>
<item>Video and Text Chat</item>
</regular>
</whatsNew>
</release>',
[
'changelogURL' => 'https://nextcloud.com/changelog/#13-0-0',
'whatsNew' => [
'en' => [
'regular' => [
'Refined user interface',
'End-to-end Encryption',
'Video and Text Chat'
],
'admin' => [],
],
],
]
],
[ # 3 - minimal set (procrastinator edition)
'<?xml version="1.0" encoding="utf-8" ?>
<release xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://updates.nextcloud.com/changelog_server/schema.xsd"
version="13.0.0">
<changelog href="https://nextcloud.com/changelog/#13-0-0"/>
<whatsNew lang="en">
<regular>
<item>Write this tomorrow</item>
</regular>
</whatsNew>
</release>',
[
'changelogURL' => 'https://nextcloud.com/changelog/#13-0-0',
'whatsNew' => [
'en' => [
'regular' => [
'Write this tomorrow',
],
'admin' => [],
],
],
]
],
];
}
/**
* @dataProvider changesXMLProvider
*/
public function testExtractData(string $body, array $expected) {
$actual = $this->invokePrivate($this->checker, 'extractData', [$body]);
$this->assertSame($expected, $actual);
}
public function etagProvider() {
return [
[''],
['a27aab83d8205d73978435076e53d143']
];
}
/**
* @dataProvider etagProvider
*/
public function testQueryChangesServer(string $etag) {
$uri = 'https://changes.nextcloud.server/?13.0.5';
$entry = $this->createMock(ChangesResult::class);
$entry->expects($this->any())
->method('__call')
->willReturn($etag);
$expectedHeaders = $etag === '' ? [] : ['If-None-Match' => [$etag]];
$client = $this->createMock(IClient::class);
$client->expects($this->once())
->method('get')
->with($uri, ['headers' => $expectedHeaders])
->willReturn($this->createMock(IResponse::class));
$this->clientService->expects($this->once())
->method('newClient')
->willReturn($client);
$response = $this->invokePrivate($this->checker, 'queryChangesServer', [$uri, $entry]);
$this->assertInstanceOf(IResponse::class, $response);
}
public function versionProvider(): array {
return [
['13.0.7', '13.0.7'],
['13.0.7.3', '13.0.7'],
['13.0.7.3.42', '13.0.7'],
['13.0', '13.0.0'],
['13', '13.0.0'],
['', '0.0.0'],
];
}
/**
* @dataProvider versionProvider
*/
public function testNormalizeVersion(string $input, string $expected) {
$normalized = $this->invokePrivate($this->checker, 'normalizeVersion', [$input]);
$this->assertSame($expected, $normalized);
}
}
|