xref: /webtrees/tests/app/Http/RequestHandlers/UpgradeWizardStepTest.php (revision d11be7027e34e3121be11cc025421873364403f9)
14b3ef6caSGreg Roach<?php
24b3ef6caSGreg Roach
34b3ef6caSGreg Roach/**
44b3ef6caSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
64b3ef6caSGreg Roach * This program is free software: you can redistribute it and/or modify
74b3ef6caSGreg Roach * it under the terms of the GNU General Public License as published by
84b3ef6caSGreg Roach * the Free Software Foundation, either version 3 of the License, or
94b3ef6caSGreg Roach * (at your option) any later version.
104b3ef6caSGreg Roach * This program is distributed in the hope that it will be useful,
114b3ef6caSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
124b3ef6caSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134b3ef6caSGreg Roach * GNU General Public License for more details.
144b3ef6caSGreg Roach * You should have received a copy of the GNU General Public License
154b3ef6caSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
164b3ef6caSGreg Roach */
174b3ef6caSGreg Roach
184b3ef6caSGreg Roachdeclare(strict_types=1);
194b3ef6caSGreg Roach
204b3ef6caSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
214b3ef6caSGreg Roach
224b3ef6caSGreg Roachuse Exception;
234b3ef6caSGreg Roachuse Fig\Http\Message\RequestMethodInterface;
244b3ef6caSGreg Roachuse Fig\Http\Message\StatusCodeInterface;
254b3ef6caSGreg Roachuse Fisharebest\Webtrees\Auth;
2681b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpServerErrorException;
274b3ef6caSGreg Roachuse Fisharebest\Webtrees\Services\GedcomExportService;
282c685d76SGreg Roachuse Fisharebest\Webtrees\Services\GedcomImportService;
294b3ef6caSGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService;
304b3ef6caSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
314b3ef6caSGreg Roachuse Fisharebest\Webtrees\Services\UpgradeService;
324b3ef6caSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
334b3ef6caSGreg Roachuse Fisharebest\Webtrees\TestCase;
344b3ef6caSGreg Roachuse Illuminate\Support\Collection;
3516ecfcafSGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory;
364b3ef6caSGreg Roach
374b3ef6caSGreg Roach/**
384b3ef6caSGreg Roach * Test UpgradeController class.
394b3ef6caSGreg Roach *
404b3ef6caSGreg Roach * @covers \Fisharebest\Webtrees\Http\RequestHandlers\UpgradeWizardStep
414b3ef6caSGreg Roach */
424b3ef6caSGreg Roachclass UpgradeWizardStepTest extends TestCase
434b3ef6caSGreg Roach{
44cd94ca66SGreg Roach    protected static bool $uses_database = true;
454b3ef6caSGreg Roach
464b3ef6caSGreg Roach    /**
474b3ef6caSGreg Roach     * @return void
484b3ef6caSGreg Roach     */
494b3ef6caSGreg Roach    public function testIgnoreStepInvalid(): void
504b3ef6caSGreg Roach    {
514b3ef6caSGreg Roach        $handler = new UpgradeWizardStep(
5216ecfcafSGreg Roach            new GedcomExportService(new Psr17Factory(), new Psr17Factory()),
532c685d76SGreg Roach            new TreeService(new GedcomImportService()),
544b3ef6caSGreg Roach            new UpgradeService(new TimeoutService())
554b3ef6caSGreg Roach        );
564b3ef6caSGreg Roach
574b3ef6caSGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Invalid']);
584b3ef6caSGreg Roach
594b3ef6caSGreg Roach        $response = $handler->handle($request);
604b3ef6caSGreg Roach
614b3ef6caSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
624b3ef6caSGreg Roach    }
634b3ef6caSGreg Roach
644b3ef6caSGreg Roach    /**
654b3ef6caSGreg Roach     * @return void
664b3ef6caSGreg Roach     */
674b3ef6caSGreg Roach    public function testStepCheckOK(): void
684b3ef6caSGreg Roach    {
694b3ef6caSGreg Roach        $mock_upgrade_service = $this->createMock(UpgradeService::class);
704b3ef6caSGreg Roach        $mock_upgrade_service->method('latestVersion')->willReturn('999.999.999');
714b3ef6caSGreg Roach        $handler = new UpgradeWizardStep(
7216ecfcafSGreg Roach            new GedcomExportService(new Psr17Factory(), new Psr17Factory()),
732c685d76SGreg Roach            new TreeService(new GedcomImportService()),
744b3ef6caSGreg Roach            $mock_upgrade_service
754b3ef6caSGreg Roach        );
764b3ef6caSGreg Roach
774b3ef6caSGreg Roach        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Check']);
784b3ef6caSGreg Roach        $response = $handler->handle($request);
794b3ef6caSGreg Roach
804b3ef6caSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
814b3ef6caSGreg Roach    }
824b3ef6caSGreg Roach
834b3ef6caSGreg Roach    /**
844b3ef6caSGreg Roach     * @return void
854b3ef6caSGreg Roach     */
864b3ef6caSGreg Roach    public function testStepCheckUnavailable(): void
874b3ef6caSGreg Roach    {
884b3ef6caSGreg Roach        $this->expectException(HttpServerErrorException::class);
894b3ef6caSGreg Roach
904b3ef6caSGreg Roach        $mock_upgrade_service = $this->createMock(UpgradeService::class);
914b3ef6caSGreg Roach        $mock_upgrade_service->method('latestVersion')->willReturn('');
924b3ef6caSGreg Roach        $handler = new UpgradeWizardStep(
9316ecfcafSGreg Roach            new GedcomExportService(new Psr17Factory(), new Psr17Factory()),
942c685d76SGreg Roach            new TreeService(new GedcomImportService()),
954b3ef6caSGreg Roach            $mock_upgrade_service
964b3ef6caSGreg Roach        );
974b3ef6caSGreg Roach
984b3ef6caSGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Check']);
994b3ef6caSGreg Roach        $handler->handle($request);
1004b3ef6caSGreg Roach    }
1014b3ef6caSGreg Roach
1024b3ef6caSGreg Roach    /**
1034b3ef6caSGreg Roach     * @return void
1044b3ef6caSGreg Roach     */
1054b3ef6caSGreg Roach    public function testStepCheckFail(): void
1064b3ef6caSGreg Roach    {
1074b3ef6caSGreg Roach        $this->expectException(HttpServerErrorException::class);
1084b3ef6caSGreg Roach
1094b3ef6caSGreg Roach        $mock_upgrade_service = $this->createMock(UpgradeService::class);
1104b3ef6caSGreg Roach        $mock_upgrade_service->method('latestVersion')->willReturn('0.0.0');
1114b3ef6caSGreg Roach        $handler = new UpgradeWizardStep(
11216ecfcafSGreg Roach            new GedcomExportService(new Psr17Factory(), new Psr17Factory()),
1132c685d76SGreg Roach            new TreeService(new GedcomImportService()),
1144b3ef6caSGreg Roach            $mock_upgrade_service
1154b3ef6caSGreg Roach        );
1164b3ef6caSGreg Roach
1174b3ef6caSGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Check']);
1184b3ef6caSGreg Roach        $handler->handle($request);
1194b3ef6caSGreg Roach    }
1204b3ef6caSGreg Roach
1214b3ef6caSGreg Roach    /**
1224b3ef6caSGreg Roach     * @return void
1234b3ef6caSGreg Roach     */
1244b3ef6caSGreg Roach    public function testStepPrepare(): void
1254b3ef6caSGreg Roach    {
1264b3ef6caSGreg Roach        $handler = new UpgradeWizardStep(
12716ecfcafSGreg Roach            new GedcomExportService(new Psr17Factory(), new Psr17Factory()),
1282c685d76SGreg Roach            new TreeService(new GedcomImportService()),
1294b3ef6caSGreg Roach            new UpgradeService(new TimeoutService())
1304b3ef6caSGreg Roach        );
1314b3ef6caSGreg Roach
1324b3ef6caSGreg Roach        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Prepare']);
1334b3ef6caSGreg Roach        $response = $handler->handle($request);
1344b3ef6caSGreg Roach
1354b3ef6caSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
1364b3ef6caSGreg Roach    }
1374b3ef6caSGreg Roach
1384b3ef6caSGreg Roach    /**
1394b3ef6caSGreg Roach     * @return void
1404b3ef6caSGreg Roach     */
1414b3ef6caSGreg Roach    public function testStepPending(): void
1424b3ef6caSGreg Roach    {
1434b3ef6caSGreg Roach        $handler = new UpgradeWizardStep(
14416ecfcafSGreg Roach            new GedcomExportService(new Psr17Factory(), new Psr17Factory()),
1452c685d76SGreg Roach            new TreeService(new GedcomImportService()),
1464b3ef6caSGreg Roach            new UpgradeService(new TimeoutService())
1474b3ef6caSGreg Roach        );
1484b3ef6caSGreg Roach
1494b3ef6caSGreg Roach        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Pending']);
1504b3ef6caSGreg Roach        $response = $handler->handle($request);
1514b3ef6caSGreg Roach
1524b3ef6caSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
1534b3ef6caSGreg Roach    }
1544b3ef6caSGreg Roach
1554b3ef6caSGreg Roach    /**
1564b3ef6caSGreg Roach     * @return void
1574b3ef6caSGreg Roach     */
1584b3ef6caSGreg Roach    public function testStepPendingExist(): void
1594b3ef6caSGreg Roach    {
1602c685d76SGreg Roach        $tree_service = new TreeService(new GedcomImportService());
1614b3ef6caSGreg Roach        $tree         = $tree_service->create('name', 'title');
1624b3ef6caSGreg Roach        $user         = (new UserService())->create('user', 'name', 'email', 'password');
1634b3ef6caSGreg Roach
1644b3ef6caSGreg Roach        Auth::login($user);
1654b3ef6caSGreg Roach        $tree->createIndividual("0 @@ INDI\n1 NAME Joe Bloggs");
1664b3ef6caSGreg Roach
1674b3ef6caSGreg Roach        $handler = new UpgradeWizardStep(
16816ecfcafSGreg Roach            new GedcomExportService(new Psr17Factory(), new Psr17Factory()),
1692c685d76SGreg Roach            new TreeService(new GedcomImportService()),
1704b3ef6caSGreg Roach            new UpgradeService(new TimeoutService())
1714b3ef6caSGreg Roach        );
1724b3ef6caSGreg Roach
1734b3ef6caSGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Pending']);
1746fd7250fSGreg Roach        $response = $handler->handle($request);
1756fd7250fSGreg Roach
1766fd7250fSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR, $response->getStatusCode());
1774b3ef6caSGreg Roach    }
1784b3ef6caSGreg Roach
1794b3ef6caSGreg Roach    /**
1804b3ef6caSGreg Roach     * @return void
1814b3ef6caSGreg Roach     */
1824b3ef6caSGreg Roach    public function testStepExport(): void
1834b3ef6caSGreg Roach    {
1844b3ef6caSGreg Roach        $tree            = $this->importTree('demo.ged');
1854b3ef6caSGreg Roach        $all_trees       = Collection::make([$tree->name() => $tree]);
1864b3ef6caSGreg Roach        $tree_service    = $this->createMock(TreeService::class);
1874b3ef6caSGreg Roach        $tree_service->method('all')->willReturn($all_trees);
1884b3ef6caSGreg Roach
1894b3ef6caSGreg Roach        $handler = new UpgradeWizardStep(
19016ecfcafSGreg Roach            new GedcomExportService(new Psr17Factory(), new Psr17Factory()),
1914b3ef6caSGreg Roach            $tree_service,
1924b3ef6caSGreg Roach            new UpgradeService(new TimeoutService())
1934b3ef6caSGreg Roach        );
1944b3ef6caSGreg Roach
1957b0fd466SGreg Roach        $request  = self::createRequest()->withQueryParams(['step' => 'Export', 'tree' => $tree->name()]);
1964b3ef6caSGreg Roach        $response = $handler->handle($request);
1974b3ef6caSGreg Roach
1984b3ef6caSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
1994b3ef6caSGreg Roach
2004b3ef6caSGreg Roach        // Now overwrite the file we just created
2014b3ef6caSGreg Roach        $response = $handler->handle($request);
2024b3ef6caSGreg Roach
2034b3ef6caSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
2044b3ef6caSGreg Roach    }
2054b3ef6caSGreg Roach
2064b3ef6caSGreg Roach    /**
2074b3ef6caSGreg Roach     * @return void
2084b3ef6caSGreg Roach     */
2094b3ef6caSGreg Roach    public function testStepDownloadFails(): void
2104b3ef6caSGreg Roach    {
2114b3ef6caSGreg Roach        $this->expectException(HttpServerErrorException::class);
2124b3ef6caSGreg Roach
2134b3ef6caSGreg Roach        $mock_upgrade_service = $this->createMock(UpgradeService::class);
2144b3ef6caSGreg Roach        $mock_upgrade_service->method('downloadFile')->will(self::throwException(new Exception()));
2154b3ef6caSGreg Roach        $handler = new UpgradeWizardStep(
21616ecfcafSGreg Roach            new GedcomExportService(new Psr17Factory(), new Psr17Factory()),
2172c685d76SGreg Roach            new TreeService(new GedcomImportService()),
2184b3ef6caSGreg Roach            $mock_upgrade_service
2194b3ef6caSGreg Roach        );
2204b3ef6caSGreg Roach
2214b3ef6caSGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Download']);
2224b3ef6caSGreg Roach        $handler->handle($request);
2234b3ef6caSGreg Roach    }
2244b3ef6caSGreg Roach
2254b3ef6caSGreg Roach    /**
2264b3ef6caSGreg Roach     * @return void
2274b3ef6caSGreg Roach     */
2284b3ef6caSGreg Roach    public function testStepDownload(): void
2294b3ef6caSGreg Roach    {
2304b3ef6caSGreg Roach        $mock_upgrade_service = $this->createMock(UpgradeService::class);
2314b3ef6caSGreg Roach        $mock_upgrade_service->method('downloadFile')->willReturn(123456);
2324b3ef6caSGreg Roach        $handler = new UpgradeWizardStep(
23316ecfcafSGreg Roach            new GedcomExportService(new Psr17Factory(), new Psr17Factory()),
2342c685d76SGreg Roach            new TreeService(new GedcomImportService()),
2354b3ef6caSGreg Roach            $mock_upgrade_service
2364b3ef6caSGreg Roach        );
2374b3ef6caSGreg Roach
2384b3ef6caSGreg Roach        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Download']);
2394b3ef6caSGreg Roach        $response = $handler->handle($request);
2404b3ef6caSGreg Roach
2414b3ef6caSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
2424b3ef6caSGreg Roach    }
2434b3ef6caSGreg Roach
2444b3ef6caSGreg Roach    /**
2454b3ef6caSGreg Roach     * @return void
2464b3ef6caSGreg Roach     */
2474b3ef6caSGreg Roach    public function testStepUnzip(): void
2484b3ef6caSGreg Roach    {
2494b3ef6caSGreg Roach        $mock_upgrade_service = $this->createMock(UpgradeService::class);
2504b3ef6caSGreg Roach        $mock_upgrade_service->method('webtreesZipContents')->willReturn(new Collection());
2514b3ef6caSGreg Roach        $handler = new UpgradeWizardStep(
25216ecfcafSGreg Roach            new GedcomExportService(new Psr17Factory(), new Psr17Factory()),
2532c685d76SGreg Roach            new TreeService(new GedcomImportService()),
2544b3ef6caSGreg Roach            $mock_upgrade_service
2554b3ef6caSGreg Roach        );
2564b3ef6caSGreg Roach
2574b3ef6caSGreg Roach        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Unzip']);
2584b3ef6caSGreg Roach        $response = $handler->handle($request);
2594b3ef6caSGreg Roach
2604b3ef6caSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
2614b3ef6caSGreg Roach    }
2624b3ef6caSGreg Roach}
263