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