14b3ef6caSGreg Roach<?php 24b3ef6caSGreg Roach 34b3ef6caSGreg Roach/** 44b3ef6caSGreg Roach * webtrees: online genealogy 5d11be702SGreg 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; 36*202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass; 374b3ef6caSGreg Roach 38*202c018bSGreg Roach#[CoversClass(UpgradeWizardStep::class)] 394b3ef6caSGreg Roachclass UpgradeWizardStepTest extends TestCase 404b3ef6caSGreg Roach{ 41cd94ca66SGreg Roach protected static bool $uses_database = true; 424b3ef6caSGreg Roach 434b3ef6caSGreg Roach public function testIgnoreStepInvalid(): void 444b3ef6caSGreg Roach { 454b3ef6caSGreg Roach $handler = new UpgradeWizardStep( 4616ecfcafSGreg Roach new GedcomExportService(new Psr17Factory(), new Psr17Factory()), 472c685d76SGreg Roach new TreeService(new GedcomImportService()), 484b3ef6caSGreg Roach new UpgradeService(new TimeoutService()) 494b3ef6caSGreg Roach ); 504b3ef6caSGreg Roach 514b3ef6caSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Invalid']); 524b3ef6caSGreg Roach 534b3ef6caSGreg Roach $response = $handler->handle($request); 544b3ef6caSGreg Roach 554b3ef6caSGreg Roach self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode()); 564b3ef6caSGreg Roach } 574b3ef6caSGreg Roach 584b3ef6caSGreg Roach public function testStepCheckOK(): void 594b3ef6caSGreg Roach { 604b3ef6caSGreg Roach $mock_upgrade_service = $this->createMock(UpgradeService::class); 614b3ef6caSGreg Roach $mock_upgrade_service->method('latestVersion')->willReturn('999.999.999'); 624b3ef6caSGreg Roach $handler = new UpgradeWizardStep( 6316ecfcafSGreg Roach new GedcomExportService(new Psr17Factory(), new Psr17Factory()), 642c685d76SGreg Roach new TreeService(new GedcomImportService()), 654b3ef6caSGreg Roach $mock_upgrade_service 664b3ef6caSGreg Roach ); 674b3ef6caSGreg Roach 684b3ef6caSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Check']); 694b3ef6caSGreg Roach $response = $handler->handle($request); 704b3ef6caSGreg Roach 714b3ef6caSGreg Roach self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); 724b3ef6caSGreg Roach } 734b3ef6caSGreg Roach 744b3ef6caSGreg Roach public function testStepCheckUnavailable(): void 754b3ef6caSGreg Roach { 764b3ef6caSGreg Roach $this->expectException(HttpServerErrorException::class); 774b3ef6caSGreg Roach 784b3ef6caSGreg Roach $mock_upgrade_service = $this->createMock(UpgradeService::class); 794b3ef6caSGreg Roach $mock_upgrade_service->method('latestVersion')->willReturn(''); 804b3ef6caSGreg Roach $handler = new UpgradeWizardStep( 8116ecfcafSGreg Roach new GedcomExportService(new Psr17Factory(), new Psr17Factory()), 822c685d76SGreg Roach new TreeService(new GedcomImportService()), 834b3ef6caSGreg Roach $mock_upgrade_service 844b3ef6caSGreg Roach ); 854b3ef6caSGreg Roach 864b3ef6caSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Check']); 874b3ef6caSGreg Roach $handler->handle($request); 884b3ef6caSGreg Roach } 894b3ef6caSGreg Roach 904b3ef6caSGreg Roach public function testStepCheckFail(): void 914b3ef6caSGreg Roach { 924b3ef6caSGreg Roach $this->expectException(HttpServerErrorException::class); 934b3ef6caSGreg Roach 944b3ef6caSGreg Roach $mock_upgrade_service = $this->createMock(UpgradeService::class); 954b3ef6caSGreg Roach $mock_upgrade_service->method('latestVersion')->willReturn('0.0.0'); 964b3ef6caSGreg Roach $handler = new UpgradeWizardStep( 9716ecfcafSGreg Roach new GedcomExportService(new Psr17Factory(), new Psr17Factory()), 982c685d76SGreg Roach new TreeService(new GedcomImportService()), 994b3ef6caSGreg Roach $mock_upgrade_service 1004b3ef6caSGreg Roach ); 1014b3ef6caSGreg Roach 1024b3ef6caSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Check']); 1034b3ef6caSGreg Roach $handler->handle($request); 1044b3ef6caSGreg Roach } 1054b3ef6caSGreg Roach 1064b3ef6caSGreg Roach public function testStepPrepare(): void 1074b3ef6caSGreg Roach { 1084b3ef6caSGreg Roach $handler = new UpgradeWizardStep( 10916ecfcafSGreg Roach new GedcomExportService(new Psr17Factory(), new Psr17Factory()), 1102c685d76SGreg Roach new TreeService(new GedcomImportService()), 1114b3ef6caSGreg Roach new UpgradeService(new TimeoutService()) 1124b3ef6caSGreg Roach ); 1134b3ef6caSGreg Roach 1144b3ef6caSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Prepare']); 1154b3ef6caSGreg Roach $response = $handler->handle($request); 1164b3ef6caSGreg Roach 1174b3ef6caSGreg Roach self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); 1184b3ef6caSGreg Roach } 1194b3ef6caSGreg Roach 1204b3ef6caSGreg Roach public function testStepPending(): void 1214b3ef6caSGreg Roach { 1224b3ef6caSGreg Roach $handler = new UpgradeWizardStep( 12316ecfcafSGreg Roach new GedcomExportService(new Psr17Factory(), new Psr17Factory()), 1242c685d76SGreg Roach new TreeService(new GedcomImportService()), 1254b3ef6caSGreg Roach new UpgradeService(new TimeoutService()) 1264b3ef6caSGreg Roach ); 1274b3ef6caSGreg Roach 1284b3ef6caSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Pending']); 1294b3ef6caSGreg Roach $response = $handler->handle($request); 1304b3ef6caSGreg Roach 1314b3ef6caSGreg Roach self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); 1324b3ef6caSGreg Roach } 1334b3ef6caSGreg Roach 1344b3ef6caSGreg Roach public function testStepPendingExist(): void 1354b3ef6caSGreg Roach { 1362c685d76SGreg Roach $tree_service = new TreeService(new GedcomImportService()); 1374b3ef6caSGreg Roach $tree = $tree_service->create('name', 'title'); 1384b3ef6caSGreg Roach $user = (new UserService())->create('user', 'name', 'email', 'password'); 1394b3ef6caSGreg Roach 1404b3ef6caSGreg Roach Auth::login($user); 1414b3ef6caSGreg Roach $tree->createIndividual("0 @@ INDI\n1 NAME Joe Bloggs"); 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']); 1506fd7250fSGreg Roach $response = $handler->handle($request); 1516fd7250fSGreg Roach 1526fd7250fSGreg Roach self::assertSame(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR, $response->getStatusCode()); 1534b3ef6caSGreg Roach } 1544b3ef6caSGreg Roach 1554b3ef6caSGreg Roach public function testStepExport(): void 1564b3ef6caSGreg Roach { 1574b3ef6caSGreg Roach $tree = $this->importTree('demo.ged'); 1584b3ef6caSGreg Roach $all_trees = Collection::make([$tree->name() => $tree]); 1594b3ef6caSGreg Roach $tree_service = $this->createMock(TreeService::class); 1604b3ef6caSGreg Roach $tree_service->method('all')->willReturn($all_trees); 1614b3ef6caSGreg Roach 1624b3ef6caSGreg Roach $handler = new UpgradeWizardStep( 16316ecfcafSGreg Roach new GedcomExportService(new Psr17Factory(), new Psr17Factory()), 1644b3ef6caSGreg Roach $tree_service, 1654b3ef6caSGreg Roach new UpgradeService(new TimeoutService()) 1664b3ef6caSGreg Roach ); 1674b3ef6caSGreg Roach 1687b0fd466SGreg Roach $request = self::createRequest()->withQueryParams(['step' => 'Export', 'tree' => $tree->name()]); 1694b3ef6caSGreg Roach $response = $handler->handle($request); 1704b3ef6caSGreg Roach 1714b3ef6caSGreg Roach self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); 1724b3ef6caSGreg Roach 1734b3ef6caSGreg Roach // Now overwrite the file we just created 1744b3ef6caSGreg Roach $response = $handler->handle($request); 1754b3ef6caSGreg Roach 1764b3ef6caSGreg Roach self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); 1774b3ef6caSGreg Roach } 1784b3ef6caSGreg Roach 1794b3ef6caSGreg Roach public function testStepDownloadFails(): void 1804b3ef6caSGreg Roach { 1814b3ef6caSGreg Roach $this->expectException(HttpServerErrorException::class); 1824b3ef6caSGreg Roach 1834b3ef6caSGreg Roach $mock_upgrade_service = $this->createMock(UpgradeService::class); 1844b3ef6caSGreg Roach $mock_upgrade_service->method('downloadFile')->will(self::throwException(new Exception())); 1854b3ef6caSGreg Roach $handler = new UpgradeWizardStep( 18616ecfcafSGreg Roach new GedcomExportService(new Psr17Factory(), new Psr17Factory()), 1872c685d76SGreg Roach new TreeService(new GedcomImportService()), 1884b3ef6caSGreg Roach $mock_upgrade_service 1894b3ef6caSGreg Roach ); 1904b3ef6caSGreg Roach 1914b3ef6caSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Download']); 1924b3ef6caSGreg Roach $handler->handle($request); 1934b3ef6caSGreg Roach } 1944b3ef6caSGreg Roach 1954b3ef6caSGreg Roach public function testStepDownload(): void 1964b3ef6caSGreg Roach { 1974b3ef6caSGreg Roach $mock_upgrade_service = $this->createMock(UpgradeService::class); 1984b3ef6caSGreg Roach $mock_upgrade_service->method('downloadFile')->willReturn(123456); 1994b3ef6caSGreg Roach $handler = new UpgradeWizardStep( 20016ecfcafSGreg Roach new GedcomExportService(new Psr17Factory(), new Psr17Factory()), 2012c685d76SGreg Roach new TreeService(new GedcomImportService()), 2024b3ef6caSGreg Roach $mock_upgrade_service 2034b3ef6caSGreg Roach ); 2044b3ef6caSGreg Roach 2054b3ef6caSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_POST, ['step' => 'Download']); 2064b3ef6caSGreg Roach $response = $handler->handle($request); 2074b3ef6caSGreg Roach 2084b3ef6caSGreg Roach self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); 2094b3ef6caSGreg Roach } 2104b3ef6caSGreg Roach 2114b3ef6caSGreg Roach public function testStepUnzip(): void 2124b3ef6caSGreg Roach { 2134b3ef6caSGreg Roach $mock_upgrade_service = $this->createMock(UpgradeService::class); 2144b3ef6caSGreg Roach $mock_upgrade_service->method('webtreesZipContents')->willReturn(new Collection()); 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' => 'Unzip']); 2224b3ef6caSGreg Roach $response = $handler->handle($request); 2234b3ef6caSGreg Roach 2244b3ef6caSGreg Roach self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); 2254b3ef6caSGreg Roach } 2264b3ef6caSGreg Roach} 227