xref: /webtrees/tests/app/Http/RequestHandlers/UpgradeWizardStepTest.php (revision 24f2a3af38709f9bf0a739b30264240d20ba34e8)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Exceptions\HttpServerErrorException;
27use Fisharebest\Webtrees\Services\GedcomExportService;
28use Fisharebest\Webtrees\Services\TimeoutService;
29use Fisharebest\Webtrees\Services\TreeService;
30use Fisharebest\Webtrees\Services\UpgradeService;
31use Fisharebest\Webtrees\Services\UserService;
32use Fisharebest\Webtrees\TestCase;
33use Illuminate\Support\Collection;
34
35/**
36 * Test UpgradeController class.
37 *
38 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\UpgradeWizardStep
39 */
40class UpgradeWizardStepTest extends TestCase
41{
42    protected static $uses_database = true;
43
44    /**
45     * @return void
46     */
47    public function testIgnoreStepInvalid(): void
48    {
49        $handler = new UpgradeWizardStep(
50            new GedcomExportService(),
51            new TreeService(),
52            new UpgradeService(new TimeoutService())
53        );
54
55        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Invalid']);
56
57        $response = $handler->handle($request);
58
59        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
60    }
61
62    /**
63     * @return void
64     */
65    public function testStepCheckOK(): void
66    {
67        $mock_upgrade_service = $this->createMock(UpgradeService::class);
68        $mock_upgrade_service->method('latestVersion')->willReturn('999.999.999');
69        $handler = new UpgradeWizardStep(
70            new GedcomExportService(),
71            new TreeService(),
72            $mock_upgrade_service
73        );
74
75        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Check']);
76        $response = $handler->handle($request);
77
78        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
79    }
80
81    /**
82     * @return void
83     */
84    public function testStepCheckUnavailable(): void
85    {
86        $this->expectException(HttpServerErrorException::class);
87
88        $mock_upgrade_service = $this->createMock(UpgradeService::class);
89        $mock_upgrade_service->method('latestVersion')->willReturn('');
90        $handler = new UpgradeWizardStep(
91            new GedcomExportService(),
92            new TreeService(),
93            $mock_upgrade_service
94        );
95
96        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Check']);
97        $handler->handle($request);
98    }
99
100    /**
101     * @return void
102     */
103    public function testStepCheckFail(): void
104    {
105        $this->expectException(HttpServerErrorException::class);
106
107        $mock_upgrade_service = $this->createMock(UpgradeService::class);
108        $mock_upgrade_service->method('latestVersion')->willReturn('0.0.0');
109        $handler = new UpgradeWizardStep(
110            new GedcomExportService(),
111            new TreeService(),
112            $mock_upgrade_service
113        );
114
115        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Check']);
116        $handler->handle($request);
117    }
118
119    /**
120     * @return void
121     */
122    public function testStepPrepare(): void
123    {
124        $handler = new UpgradeWizardStep(
125            new GedcomExportService(),
126            new TreeService(),
127            new UpgradeService(new TimeoutService())
128        );
129
130        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Prepare']);
131        $response = $handler->handle($request);
132
133        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
134    }
135
136    /**
137     * @return void
138     */
139    public function testStepPending(): void
140    {
141        $handler = new UpgradeWizardStep(
142            new GedcomExportService(),
143            new TreeService(),
144            new UpgradeService(new TimeoutService())
145        );
146
147        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Pending']);
148        $response = $handler->handle($request);
149
150        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
151    }
152
153    /**
154     * @return void
155     */
156    public function testStepPendingExist(): void
157    {
158        $this->expectException(HttpServerErrorException::class);
159
160        $tree_service = new TreeService();
161        $tree         = $tree_service->create('name', 'title');
162        $user         = (new UserService())->create('user', 'name', 'email', 'password');
163
164        Auth::login($user);
165        $tree->createIndividual("0 @@ INDI\n1 NAME Joe Bloggs");
166
167        $handler = new UpgradeWizardStep(
168            new GedcomExportService(),
169            new TreeService(),
170            new UpgradeService(new TimeoutService())
171        );
172
173        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Pending']);
174        $handler->handle($request);
175    }
176
177    /**
178     * @return void
179     */
180    public function testStepExport(): void
181    {
182        $tree            = $this->importTree('demo.ged');
183        $all_trees       = Collection::make([$tree->name() => $tree]);
184        $tree_service    = $this->createMock(TreeService::class);
185        $tree_service->method('all')->willReturn($all_trees);
186
187        $handler = new UpgradeWizardStep(
188            new GedcomExportService(),
189            $tree_service,
190            new UpgradeService(new TimeoutService())
191        );
192
193        $request  = self::createRequest()->withQueryParams(['step' => 'Export', 'tree' => $tree->name()]);
194        $response = $handler->handle($request);
195
196        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
197
198        // Now overwrite the file we just created
199        $response = $handler->handle($request);
200
201        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
202    }
203
204    /**
205     * @return void
206     */
207    public function testStepDownloadFails(): void
208    {
209        $this->expectException(HttpServerErrorException::class);
210
211        $mock_upgrade_service = $this->createMock(UpgradeService::class);
212        $mock_upgrade_service->method('downloadFile')->will(self::throwException(new Exception()));
213        $handler = new UpgradeWizardStep(
214            new GedcomExportService(),
215            new TreeService(),
216            $mock_upgrade_service
217        );
218
219        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Download']);
220        $handler->handle($request);
221    }
222
223    /**
224     * @return void
225     */
226    public function testStepDownload(): void
227    {
228        $mock_upgrade_service = $this->createMock(UpgradeService::class);
229        $mock_upgrade_service->method('downloadFile')->willReturn(123456);
230        $handler = new UpgradeWizardStep(
231            new GedcomExportService(),
232            new TreeService(),
233            $mock_upgrade_service
234        );
235
236        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Download']);
237        $response = $handler->handle($request);
238
239        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
240    }
241
242    /**
243     * @return void
244     */
245    public function testStepUnzip(): void
246    {
247        $mock_upgrade_service = $this->createMock(UpgradeService::class);
248        $mock_upgrade_service->method('webtreesZipContents')->willReturn(new Collection());
249        $handler = new UpgradeWizardStep(
250            new GedcomExportService(),
251            new TreeService(),
252            $mock_upgrade_service
253        );
254
255        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Unzip']);
256        $response = $handler->handle($request);
257
258        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
259    }
260}
261