Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
232 / 232
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
1 / 1
phpbb_update_controller_test
100.00% covered (success)
100.00%
232 / 232
100.00% covered (success)
100.00%
12 / 12
12
100.00% covered (success)
100.00%
1 / 1
 setUp
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 tearDown
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 test_download_fails
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 test_download_success
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 test_download_signature_fails
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
1
 test_download_signature_success
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
1
 test_signature_validation_fails
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
1
 test_extract_fails
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
1
 test_extract_success
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
1
 test_copy_fails
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
1
 test_copy_success
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
1
 test_successful_update_process
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14use phpbb\update\controller;
15use phpbb\update\get_updates;
16use phpbb\filesystem\filesystem;
17use phpbb\language\language;
18
19class phpbb_update_controller_test extends \phpbb_test_case
20{
21    private $filesystem;
22    private $filesystem_mock;
23    private $updater_mock;
24    private $language_mock;
25    private $phpbb_root_path;
26
27    protected function setUp(): void
28    {
29        global $phpbb_root_path;
30
31        $this->filesystem = new filesystem();
32        $this->filesystem_mock = $this->createMock(filesystem::class);
33        $this->updater_mock = $this->createMock(get_updates::class);
34        $this->language_mock = $this->createMock(language::class);
35        $this->phpbb_root_path = $phpbb_root_path;
36    }
37
38    protected function tearDown(): void
39    {
40        $this->filesystem->remove([
41            $this->phpbb_root_path . 'store/update.zip',
42            $this->phpbb_root_path . 'store/update.zip.sig',
43            $this->phpbb_root_path . 'store/update',
44        ]);
45    }
46
47    public function test_download_fails(): void
48    {
49        $this->updater_mock->expects($this->once())
50            ->method('download')
51            ->willReturn(false);
52
53        $this->language_mock->expects($this->once())
54            ->method('lang')
55            ->with('UPDATE_PACKAGE_DOWNLOAD_FAILURE')
56            ->willReturnArgument(0);
57
58        $controller = new controller(
59            $this->filesystem_mock,
60            $this->updater_mock,
61            $this->language_mock,
62            $this->phpbb_root_path
63        );
64
65        $response = $controller->handle('https://example.com/update.zip');
66        $this->assertEquals(['status' => 'error', 'error' => 'UPDATE_PACKAGE_DOWNLOAD_FAILURE'], $response);
67    }
68
69    public function test_download_success(): void
70    {
71        $this->updater_mock->expects($this->once())
72            ->method('download')
73            ->willReturn(true);
74
75        $controller = new controller(
76            $this->filesystem_mock,
77            $this->updater_mock,
78            $this->language_mock,
79            $this->phpbb_root_path
80        );
81
82        $response = $controller->handle('https://example.com/update.zip');
83        $this->assertEquals(['status' => 'continue'], $response);
84    }
85
86    public function test_download_signature_fails(): void
87    {
88        $update_path = $this->phpbb_root_path . 'store/update.zip';
89
90        $this->filesystem_mock->expects($this->any())
91            ->method('exists')
92            ->willReturnMap([
93                [$update_path, true],
94                [$update_path . '.sig', false],
95                [$this->phpbb_root_path . 'store/update', false],
96                [$this->phpbb_root_path . 'install', false],
97            ]);
98
99        $this->updater_mock->expects($this->once())
100            ->method('download')
101            ->with('https://example.com/update.zip.sig', $update_path . '.sig')
102            ->willReturn(false);
103
104        $this->language_mock->expects($this->once())
105            ->method('lang')
106            ->with('UPDATE_SIGNATURE_DOWNLOAD_FAILURE')
107            ->willReturnArgument(0);
108
109        $controller = new controller(
110            $this->filesystem_mock,
111            $this->updater_mock,
112            $this->language_mock,
113            $this->phpbb_root_path
114        );
115
116        $response = $controller->handle('https://example.com/update.zip');
117        $this->assertEquals(['status' => 'error', 'error' => 'UPDATE_SIGNATURE_DOWNLOAD_FAILURE'], $response);
118    }
119
120    public function test_download_signature_success(): void
121    {
122        $update_path = $this->phpbb_root_path . 'store/update.zip';
123
124        $this->filesystem_mock->expects($this->any())
125            ->method('exists')
126            ->willReturnMap([
127                [$update_path, true],
128                [$update_path . '.sig', false],
129                [$this->phpbb_root_path . 'store/update', false],
130                [$this->phpbb_root_path . 'install', false],
131            ]);
132
133        $this->updater_mock->expects($this->once())
134            ->method('download')
135            ->with('https://example.com/update.zip.sig', $update_path . '.sig')
136            ->willReturn(true);
137
138        $controller = new controller(
139            $this->filesystem_mock,
140            $this->updater_mock,
141            $this->language_mock,
142            $this->phpbb_root_path
143        );
144
145        $response = $controller->handle('https://example.com/update.zip');
146        $this->assertEquals(['status' => 'continue'], $response);
147    }
148
149    public function test_signature_validation_fails(): void
150    {
151        $update_path = $this->phpbb_root_path . 'store/update.zip';
152
153        $this->filesystem_mock->expects($this->any())
154            ->method('exists')
155            ->willReturnMap([
156                [$update_path, true],
157                [$update_path . '.sig', true],
158                [$this->phpbb_root_path . 'store/update', false],
159                [$this->phpbb_root_path . 'install', false],
160            ]);
161
162        $this->updater_mock->expects($this->once())
163            ->method('validate')
164            ->willReturn(false);
165
166        $this->language_mock->expects($this->once())
167            ->method('lang')
168            ->with('UPDATE_SIGNATURE_INVALID')
169            ->willReturnArgument(0);
170
171        $controller = new controller(
172            $this->filesystem_mock,
173            $this->updater_mock,
174            $this->language_mock,
175            $this->phpbb_root_path
176        );
177
178        $response = $controller->handle('https://example.com/update.zip');
179        $this->assertEquals(['status' => 'error', 'error' => 'UPDATE_SIGNATURE_INVALID'], $response);
180    }
181
182    public function test_extract_fails(): void
183    {
184        $update_path = $this->phpbb_root_path . 'store/update.zip';
185
186        $this->filesystem_mock->expects($this->any())
187            ->method('exists')
188            ->willReturnMap([
189                [$update_path, true],
190                [$update_path . '.sig', true],
191                [$this->phpbb_root_path . 'store/update', false],
192                [$this->phpbb_root_path . 'install', false],
193            ]);
194
195        $this->updater_mock->expects($this->once())
196            ->method('validate')
197            ->willReturn(true);
198
199        $this->updater_mock->expects($this->once())
200            ->method('extract')
201            ->willReturn(false);
202
203        $this->language_mock->expects($this->once())
204            ->method('lang')
205            ->with('UPDATE_PACKAGE_EXTRACT_FAILURE')
206            ->willReturnArgument(0);
207
208        $controller = new controller(
209            $this->filesystem_mock,
210            $this->updater_mock,
211            $this->language_mock,
212            $this->phpbb_root_path
213        );
214
215        $response = $controller->handle('https://example.com/update.zip');
216        $this->assertEquals(['status' => 'error', 'error' => 'UPDATE_PACKAGE_EXTRACT_FAILURE'], $response);
217    }
218
219    public function test_extract_success(): void
220    {
221        $update_path = $this->phpbb_root_path . 'store/update.zip';
222
223        $this->filesystem_mock->expects($this->any())
224            ->method('exists')
225            ->willReturnMap([
226                [$update_path, true],
227                [$update_path . '.sig', true],
228                [$this->phpbb_root_path . 'store/update', false],
229                [$this->phpbb_root_path . 'install', false],
230            ]);
231
232        $this->updater_mock->expects($this->once())
233            ->method('validate')
234            ->willReturn(true);
235
236        $this->updater_mock->expects($this->once())
237            ->method('extract')
238            ->willReturn(true);
239
240        $controller = new controller(
241            $this->filesystem_mock,
242            $this->updater_mock,
243            $this->language_mock,
244            $this->phpbb_root_path
245        );
246
247        $response = $controller->handle('https://example.com/update.zip');
248        $this->assertEquals(['status' => 'continue'], $response);
249    }
250
251    public function test_copy_fails(): void
252    {
253        $update_path = $this->phpbb_root_path . 'store/update.zip';
254        $this->filesystem->touch($update_path); // Simulate existing update file
255        $this->filesystem->touch($update_path . '.sig'); // Simulate existing signature file
256        $this->filesystem->mkdir($this->phpbb_root_path . 'store/update');
257
258        $this->filesystem_mock->expects($this->any())
259            ->method('exists')
260            ->willReturnMap([
261                [$update_path, true],
262                [$update_path . '.sig', true],
263                [$this->phpbb_root_path . 'store/update', true],
264                [$this->phpbb_root_path . 'install', false],
265            ]);
266
267        $this->updater_mock->expects($this->once())
268            ->method('copy')
269            ->willReturn(false);
270
271        $this->language_mock->expects($this->once())
272            ->method('lang')
273            ->with('UPDATE_FILES_COPY_FAILURE')
274            ->willReturnArgument(0);
275
276        $controller = new controller(
277            $this->filesystem_mock,
278            $this->updater_mock,
279            $this->language_mock,
280            $this->phpbb_root_path
281        );
282
283        $response = $controller->handle('https://example.com/update.zip');
284        $this->assertEquals(['status' => 'error', 'error' => 'UPDATE_FILES_COPY_FAILURE'], $response);
285    }
286
287    public function test_copy_success(): void
288    {
289        $update_path = $this->phpbb_root_path . 'store/update.zip';
290        $this->filesystem->touch($update_path); // Simulate existing update file
291        $this->filesystem->touch($update_path . '.sig'); // Simulate existing signature file
292        $this->filesystem->mkdir($this->phpbb_root_path . 'store/update');
293
294        $this->filesystem_mock->expects($this->any())
295            ->method('exists')
296            ->willReturnMap([
297                [$update_path, true],
298                [$update_path . '.sig', true],
299                [$this->phpbb_root_path . 'store/update', true],
300                [$this->phpbb_root_path . 'install', false],
301            ]);
302
303        $this->updater_mock->expects($this->once())
304            ->method('copy')
305            ->willReturn(true);
306
307        $controller = new controller(
308            $this->filesystem_mock,
309            $this->updater_mock,
310            $this->language_mock,
311            $this->phpbb_root_path
312        );
313
314        $response = $controller->handle('https://example.com/update.zip');
315        $this->assertEquals(['status' => 'continue'], $response);
316    }
317
318    public function test_successful_update_process(): void
319    {
320        $update_path = $this->phpbb_root_path . 'store/update.zip';
321        $signature_path = $update_path . '.sig';
322        $update_dir = $this->phpbb_root_path . 'store/update';
323
324        $this->filesystem->touch($update_path);
325        $this->filesystem->touch($signature_path);
326        $this->filesystem->mkdir($update_dir);
327
328        $this->filesystem_mock->expects($this->any())
329            ->method('exists')
330            ->willReturnMap([
331                [$update_path, true],
332                [$update_path . '.sig', true],
333                [$this->phpbb_root_path . 'store/update', true],
334                [$this->phpbb_root_path . 'install', true],
335            ]);
336
337        $this->filesystem_mock->expects($this->once())
338            ->method('remove')
339            ->with([$update_dir, $update_path, $signature_path]);
340
341        $controller = new controller(
342            $this->filesystem_mock,
343            $this->updater_mock,
344            $this->language_mock,
345            $this->phpbb_root_path
346        );
347
348        $response = $controller->handle('https://example.com/update.zip');
349        $this->assertEquals(['status' => 'done'], $response);
350    }
351}