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