xref: /webtrees/tests/app/Http/RequestHandlers/UpgradeWizardStepTest.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Exception;
23use Fig\Http\Message\RequestMethodInterface;
24use Fig\Http\Message\StatusCodeInterface;
25use Fisharebest\Webtrees\Auth;
26use Fisharebest\Webtrees\Http\Exceptions\HttpServerErrorException;
27use Fisharebest\Webtrees\Services\GedcomExportService;
28use Fisharebest\Webtrees\Services\GedcomImportService;
29use Fisharebest\Webtrees\Services\TimeoutService;
30use Fisharebest\Webtrees\Services\TreeService;
31use Fisharebest\Webtrees\Services\UpgradeService;
32use Fisharebest\Webtrees\Services\UserService;
33use Fisharebest\Webtrees\TestCase;
34use Illuminate\Support\Collection;
35
36/**
37 * Test UpgradeController class.
38 *
39 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\UpgradeWizardStep
40 */
41class UpgradeWizardStepTest extends TestCase
42{
43    protected static bool $uses_database = true;
44
45    /**
46     * @return void
47     */
48    public function testIgnoreStepInvalid(): void
49    {
50        $handler = new UpgradeWizardStep(
51            new GedcomExportService(),
52            new TreeService(new GedcomImportService()),
53            new UpgradeService(new TimeoutService())
54        );
55
56        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Invalid']);
57
58        $response = $handler->handle($request);
59
60        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
61    }
62
63    /**
64     * @return void
65     */
66    public function testStepCheckOK(): void
67    {
68        $mock_upgrade_service = $this->createMock(UpgradeService::class);
69        $mock_upgrade_service->method('latestVersion')->willReturn('999.999.999');
70        $handler = new UpgradeWizardStep(
71            new GedcomExportService(),
72            new TreeService(new GedcomImportService()),
73            $mock_upgrade_service
74        );
75
76        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Check']);
77        $response = $handler->handle($request);
78
79        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
80    }
81
82    /**
83     * @return void
84     */
85    public function testStepCheckUnavailable(): void
86    {
87        $this->expectException(HttpServerErrorException::class);
88
89        $mock_upgrade_service = $this->createMock(UpgradeService::class);
90        $mock_upgrade_service->method('latestVersion')->willReturn('');
91        $handler = new UpgradeWizardStep(
92            new GedcomExportService(),
93            new TreeService(new GedcomImportService()),
94            $mock_upgrade_service
95        );
96
97        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Check']);
98        $handler->handle($request);
99    }
100
101    /**
102     * @return void
103     */
104    public function testStepCheckFail(): void
105    {
106        $this->expectException(HttpServerErrorException::class);
107
108        $mock_upgrade_service = $this->createMock(UpgradeService::class);
109        $mock_upgrade_service->method('latestVersion')->willReturn('0.0.0');
110        $handler = new UpgradeWizardStep(
111            new GedcomExportService(),
112            new TreeService(new GedcomImportService()),
113            $mock_upgrade_service
114        );
115
116        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Check']);
117        $handler->handle($request);
118    }
119
120    /**
121     * @return void
122     */
123    public function testStepPrepare(): void
124    {
125        $handler = new UpgradeWizardStep(
126            new GedcomExportService(),
127            new TreeService(new GedcomImportService()),
128            new UpgradeService(new TimeoutService())
129        );
130
131        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Prepare']);
132        $response = $handler->handle($request);
133
134        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
135    }
136
137    /**
138     * @return void
139     */
140    public function testStepPending(): void
141    {
142        $handler = new UpgradeWizardStep(
143            new GedcomExportService(),
144            new TreeService(new GedcomImportService()),
145            new UpgradeService(new TimeoutService())
146        );
147
148        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Pending']);
149        $response = $handler->handle($request);
150
151        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
152    }
153
154    /**
155     * @return void
156     */
157    public function testStepPendingExist(): void
158    {
159        $tree_service = new TreeService(new GedcomImportService());
160        $tree         = $tree_service->create('name', 'title');
161        $user         = (new UserService())->create('user', 'name', 'email', 'password');
162
163        Auth::login($user);
164        $tree->createIndividual("0 @@ INDI\n1 NAME Joe Bloggs");
165
166        $handler = new UpgradeWizardStep(
167            new GedcomExportService(),
168            new TreeService(new GedcomImportService()),
169            new UpgradeService(new TimeoutService())
170        );
171
172        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Pending']);
173        $response = $handler->handle($request);
174
175        self::assertSame(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR, $response->getStatusCode());
176    }
177
178    /**
179     * @return void
180     */
181    public function testStepExport(): void
182    {
183        $tree            = $this->importTree('demo.ged');
184        $all_trees       = Collection::make([$tree->name() => $tree]);
185        $tree_service    = $this->createMock(TreeService::class);
186        $tree_service->method('all')->willReturn($all_trees);
187
188        $handler = new UpgradeWizardStep(
189            new GedcomExportService(),
190            $tree_service,
191            new UpgradeService(new TimeoutService())
192        );
193
194        $request  = self::createRequest()->withQueryParams(['step' => 'Export', 'tree' => $tree->name()]);
195        $response = $handler->handle($request);
196
197        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
198
199        // Now overwrite the file we just created
200        $response = $handler->handle($request);
201
202        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
203    }
204
205    /**
206     * @return void
207     */
208    public function testStepDownloadFails(): void
209    {
210        $this->expectException(HttpServerErrorException::class);
211
212        $mock_upgrade_service = $this->createMock(UpgradeService::class);
213        $mock_upgrade_service->method('downloadFile')->will(self::throwException(new Exception()));
214        $handler = new UpgradeWizardStep(
215            new GedcomExportService(),
216            new TreeService(new GedcomImportService()),
217            $mock_upgrade_service
218        );
219
220        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Download']);
221        $handler->handle($request);
222    }
223
224    /**
225     * @return void
226     */
227    public function testStepDownload(): void
228    {
229        $mock_upgrade_service = $this->createMock(UpgradeService::class);
230        $mock_upgrade_service->method('downloadFile')->willReturn(123456);
231        $handler = new UpgradeWizardStep(
232            new GedcomExportService(),
233            new TreeService(new GedcomImportService()),
234            $mock_upgrade_service
235        );
236
237        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Download']);
238        $response = $handler->handle($request);
239
240        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
241    }
242
243    /**
244     * @return void
245     */
246    public function testStepUnzip(): void
247    {
248        $mock_upgrade_service = $this->createMock(UpgradeService::class);
249        $mock_upgrade_service->method('webtreesZipContents')->willReturn(new Collection());
250        $handler = new UpgradeWizardStep(
251            new GedcomExportService(),
252            new TreeService(new GedcomImportService()),
253            $mock_upgrade_service
254        );
255
256        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Unzip']);
257        $response = $handler->handle($request);
258
259        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
260    }
261}
262