184e2cf4eSGreg Roach<?php 23cfcc809SGreg Roach 384e2cf4eSGreg Roach/** 484e2cf4eSGreg Roach * webtrees: online genealogy 5a091ac74SGreg Roach * Copyright (C) 2020 webtrees development team 684e2cf4eSGreg Roach * This program is free software: you can redistribute it and/or modify 784e2cf4eSGreg Roach * it under the terms of the GNU General Public License as published by 884e2cf4eSGreg Roach * the Free Software Foundation, either version 3 of the License, or 984e2cf4eSGreg Roach * (at your option) any later version. 1084e2cf4eSGreg Roach * This program is distributed in the hope that it will be useful, 1184e2cf4eSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1284e2cf4eSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1384e2cf4eSGreg Roach * GNU General Public License for more details. 1484e2cf4eSGreg Roach * You should have received a copy of the GNU General Public License 1584e2cf4eSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 1684e2cf4eSGreg Roach */ 173cfcc809SGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2084e2cf4eSGreg Roachnamespace Fisharebest\Webtrees; 2184e2cf4eSGreg Roach 22de2aa325SGreg Roachuse Aura\Router\Route; 23ee4364daSGreg Roachuse Aura\Router\RouterContainer; 24d403609dSGreg Roachuse Fig\Http\Message\RequestMethodInterface; 2569675509SGreg Roachuse Fisharebest\Webtrees\Contracts\CacheFactoryInterface; 2669675509SGreg Roachuse Fisharebest\Webtrees\Factories\CacheFactory; 27bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\FamilyFactory; 2869675509SGreg Roachuse Fisharebest\Webtrees\Factories\FilesystemFactory; 29bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\GedcomRecordFactory; 30bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\HeaderFactory; 31bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\IndividualFactory; 329574834fSGreg Roachuse Fisharebest\Webtrees\Factories\LocationFactory; 33bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\MediaFactory; 34bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\NoteFactory; 35bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\RepositoryFactory; 36bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\SourceFactory; 37bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\SubmissionFactory; 38bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\SubmitterFactory; 39b4a2f885SGreg Roachuse Fisharebest\Webtrees\Factories\XrefFactory; 40*6fd01894SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\GedcomLoad; 412279b4f3SGreg Roachuse Fisharebest\Webtrees\Http\Routes\WebRoutes; 428136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface; 438136679eSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme; 448c3e1068SGreg Roachuse Fisharebest\Webtrees\Services\MigrationService; 4571378461SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 46126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService; 471e653452SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 480115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 49b5f5afdbSGreg Roachuse League\Flysystem\Filesystem; 50b5f5afdbSGreg Roachuse League\Flysystem\Memory\MemoryAdapter; 516ccdf4f0SGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory; 526ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 536ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface; 546ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 556ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 566ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface; 576ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileInterface; 586ccdf4f0SGreg Roachuse Psr\Http\Message\UriFactoryInterface; 59c692965aSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter; 6071378461SGreg Roach 616ccdf4f0SGreg Roachuse function app; 627def76c7SGreg Roachuse function basename; 636ccdf4f0SGreg Roachuse function filesize; 646ccdf4f0SGreg Roachuse function http_build_query; 6557ab2231SGreg Roachuse function microtime; 6671378461SGreg Roach 676ccdf4f0SGreg Roachuse const UPLOAD_ERR_OK; 680115bc16SGreg Roach 6984e2cf4eSGreg Roach/** 7084e2cf4eSGreg Roach * Base class for unit tests 7184e2cf4eSGreg Roach */ 7271378461SGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase 7384e2cf4eSGreg Roach{ 7474d6dc0eSGreg Roach /** @var object */ 7574d6dc0eSGreg Roach public static $mock_functions; 7657ab2231SGreg Roach /** @var bool */ 7757ab2231SGreg Roach protected static $uses_database = false; 7874d6dc0eSGreg Roach 79061b43d7SGreg Roach /** 80061b43d7SGreg Roach * Things to run once, before all the tests. 81061b43d7SGreg Roach */ 82061b43d7SGreg Roach public static function setUpBeforeClass() 83061b43d7SGreg Roach { 84061b43d7SGreg Roach parent::setUpBeforeClass(); 85061b43d7SGreg Roach 866ccdf4f0SGreg Roach // Use nyholm as our PSR7 factory 876ccdf4f0SGreg Roach app()->bind(ResponseFactoryInterface::class, Psr17Factory::class); 886ccdf4f0SGreg Roach app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class); 896ccdf4f0SGreg Roach app()->bind(StreamFactoryInterface::class, Psr17Factory::class); 906ccdf4f0SGreg Roach app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class); 916ccdf4f0SGreg Roach app()->bind(UriFactoryInterface::class, Psr17Factory::class); 926ccdf4f0SGreg Roach 93bb03c9f0SGreg Roach // Register the factories 9469675509SGreg Roach Factory::cache(new CacheFactory()); 9569675509SGreg Roach Factory::family(new FamilyFactory()); 9669675509SGreg Roach Factory::filesystem(new FilesystemFactory()); 9769675509SGreg Roach Factory::gedcomRecord(new GedcomRecordFactory()); 9869675509SGreg Roach Factory::header(new HeaderFactory()); 9969675509SGreg Roach Factory::individual(new IndividualFactory()); 10069675509SGreg Roach Factory::location(new LocationFactory()); 10169675509SGreg Roach Factory::media(new MediaFactory()); 10269675509SGreg Roach Factory::note(new NoteFactory()); 10369675509SGreg Roach Factory::repository(new RepositoryFactory()); 10469675509SGreg Roach Factory::source(new SourceFactory()); 10569675509SGreg Roach Factory::submission(new SubmissionFactory()); 10669675509SGreg Roach Factory::submitter(new SubmitterFactory()); 107b4a2f885SGreg Roach Factory::xref(new XrefFactory()); 1086ccdf4f0SGreg Roach 10957ab2231SGreg Roach app()->bind(ModuleThemeInterface::class, WebtreesTheme::class); 1106ccdf4f0SGreg Roach 111ee4364daSGreg Roach // Need the routing table, to generate URLs. 1122279b4f3SGreg Roach $router_container = new RouterContainer('/'); 1132279b4f3SGreg Roach (new WebRoutes())->load($router_container->getMap()); 1142279b4f3SGreg Roach app()->instance(RouterContainer::class, $router_container); 115ee4364daSGreg Roach 116150f35adSGreg Roach I18N::init('en-US', true); 1176ccdf4f0SGreg Roach 118061b43d7SGreg Roach if (static::$uses_database) { 119061b43d7SGreg Roach static::createTestDatabase(); 12071378461SGreg Roach 12171378461SGreg Roach // Boot modules 12271378461SGreg Roach (new ModuleService())->bootModules(new WebtreesTheme()); 123061b43d7SGreg Roach } 124061b43d7SGreg Roach } 125061b43d7SGreg Roach 126061b43d7SGreg Roach /** 12771378461SGreg Roach * Things to run once, AFTER all the tests. 12871378461SGreg Roach */ 12971378461SGreg Roach public static function tearDownAfterClass() 13071378461SGreg Roach { 13171378461SGreg Roach if (static::$uses_database) { 13271378461SGreg Roach $pdo = DB::connection()->getPdo(); 13371378461SGreg Roach unset($pdo); 13471378461SGreg Roach } 13571378461SGreg Roach 13671378461SGreg Roach parent::tearDownAfterClass(); 13771378461SGreg Roach } 13871378461SGreg Roach 13971378461SGreg Roach /** 1406ccdf4f0SGreg Roach * Create an SQLite in-memory database for testing 1416ccdf4f0SGreg Roach */ 1426ccdf4f0SGreg Roach protected static function createTestDatabase(): void 1436ccdf4f0SGreg Roach { 1446ccdf4f0SGreg Roach $capsule = new DB(); 1456ccdf4f0SGreg Roach $capsule->addConnection([ 1466ccdf4f0SGreg Roach 'driver' => 'sqlite', 1476ccdf4f0SGreg Roach 'database' => ':memory:', 1486ccdf4f0SGreg Roach ]); 1496ccdf4f0SGreg Roach $capsule->setAsGlobal(); 150e16a1bfdSGreg Roach 1516ccdf4f0SGreg Roach // Migrations create logs, which requires an IP address, which requires a request 1526ccdf4f0SGreg Roach self::createRequest(); 1536ccdf4f0SGreg Roach 1546ccdf4f0SGreg Roach // Create tables 1553cfcc809SGreg Roach $migration_service = new MigrationService(); 1566ccdf4f0SGreg Roach $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 1576ccdf4f0SGreg Roach 1586ccdf4f0SGreg Roach // Create config data 1596ccdf4f0SGreg Roach $migration_service->seedDatabase(); 1606ccdf4f0SGreg Roach } 1616ccdf4f0SGreg Roach 1626ccdf4f0SGreg Roach /** 16357ab2231SGreg Roach * Create a request and bind it into the container. 16457ab2231SGreg Roach * 16557ab2231SGreg Roach * @param string $method 16657ab2231SGreg Roach * @param string[] $query 16757ab2231SGreg Roach * @param string[] $params 16857ab2231SGreg Roach * @param UploadedFileInterface[] $files 169b3a775f6SGreg Roach * @param string[] $attributes 17057ab2231SGreg Roach * 17157ab2231SGreg Roach * @return ServerRequestInterface 17257ab2231SGreg Roach */ 1731a218474SGreg Roach protected static function createRequest( 1741a218474SGreg Roach string $method = RequestMethodInterface::METHOD_GET, 1751a218474SGreg Roach array $query = [], 1761a218474SGreg Roach array $params = [], 177b3a775f6SGreg Roach array $files = [], 178b3a775f6SGreg Roach array $attributes = [] 1791a218474SGreg Roach ): ServerRequestInterface { 18057ab2231SGreg Roach /** @var ServerRequestFactoryInterface */ 18157ab2231SGreg Roach $server_request_factory = app(ServerRequestFactoryInterface::class); 18257ab2231SGreg Roach 18357ab2231SGreg Roach $uri = 'https://webtrees.test/index.php?' . http_build_query($query); 18457ab2231SGreg Roach 18557ab2231SGreg Roach /** @var ServerRequestInterface $request */ 18657ab2231SGreg Roach $request = $server_request_factory 18757ab2231SGreg Roach ->createServerRequest($method, $uri) 18857ab2231SGreg Roach ->withQueryParams($query) 18957ab2231SGreg Roach ->withParsedBody($params) 19057ab2231SGreg Roach ->withUploadedFiles($files) 19157ab2231SGreg Roach ->withAttribute('base_url', 'https://webtrees.test') 19290a2f718SGreg Roach ->withAttribute('client-ip', '127.0.0.1') 193b5f5afdbSGreg Roach ->withAttribute('user', new GuestUser()) 194de2aa325SGreg Roach ->withAttribute('route', new Route()); 195b3a775f6SGreg Roach 196b3a775f6SGreg Roach foreach ($attributes as $key => $value) { 197b3a775f6SGreg Roach $request = $request->withAttribute($key, $value); 198b3a775f6SGreg Roach 199b3a775f6SGreg Roach if ($key === 'tree') { 200b3a775f6SGreg Roach app()->instance(Tree::class, $value); 201b3a775f6SGreg Roach } 202b3a775f6SGreg Roach } 20357ab2231SGreg Roach 20457ab2231SGreg Roach app()->instance(ServerRequestInterface::class, $request); 20557ab2231SGreg Roach 20657ab2231SGreg Roach return $request; 20757ab2231SGreg Roach } 20857ab2231SGreg Roach 20957ab2231SGreg Roach /** 210061b43d7SGreg Roach * Things to run before every test. 211061b43d7SGreg Roach */ 2125c48bcd6SGreg Roach protected function setUp(): void 2130115bc16SGreg Roach { 2140115bc16SGreg Roach parent::setUp(); 2150115bc16SGreg Roach 216061b43d7SGreg Roach if (static::$uses_database) { 217061b43d7SGreg Roach DB::connection()->beginTransaction(); 218061b43d7SGreg Roach } 219061b43d7SGreg Roach } 220061b43d7SGreg Roach 221061b43d7SGreg Roach /** 222061b43d7SGreg Roach * Things to run after every test 223061b43d7SGreg Roach */ 224a49feabaSGreg Roach protected function tearDown() 225a49feabaSGreg Roach { 22632f20c14SGreg Roach if (static::$uses_database) { 227061b43d7SGreg Roach DB::connection()->rollBack(); 228061b43d7SGreg Roach } 22932f20c14SGreg Roach 23032f20c14SGreg Roach Site::$preferences = []; 23132f20c14SGreg Roach 23232f20c14SGreg Roach Auth::logout(); 2330115bc16SGreg Roach } 2340115bc16SGreg Roach 2350115bc16SGreg Roach /** 2360115bc16SGreg Roach * Import a GEDCOM file into the test database. 2370115bc16SGreg Roach * 2380115bc16SGreg Roach * @param string $gedcom_file 239061b43d7SGreg Roach * 240061b43d7SGreg Roach * @return Tree 2410115bc16SGreg Roach */ 242061b43d7SGreg Roach protected function importTree(string $gedcom_file): Tree 2430115bc16SGreg Roach { 2441e653452SGreg Roach $tree_service = new TreeService(); 2451e653452SGreg Roach $tree = $tree_service->create(basename($gedcom_file), basename($gedcom_file)); 2466ccdf4f0SGreg Roach $stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file); 2471e653452SGreg Roach 2486ccdf4f0SGreg Roach $tree->importGedcomFile($stream, $gedcom_file); 2490115bc16SGreg Roach 25057ab2231SGreg Roach $timeout_service = new TimeoutService(microtime(true)); 251*6fd01894SGreg Roach $controller = new GedcomLoad($timeout_service); 25257ab2231SGreg Roach $request = self::createRequest()->withAttribute('tree', $tree); 253126654d7SGreg Roach 254126654d7SGreg Roach do { 255*6fd01894SGreg Roach $controller->handle($request); 256126654d7SGreg Roach 257126654d7SGreg Roach $imported = $tree->getPreference('imported'); 258126654d7SGreg Roach } while (!$imported); 259061b43d7SGreg Roach 260061b43d7SGreg Roach return $tree; 2610115bc16SGreg Roach } 2626ccdf4f0SGreg Roach 2636ccdf4f0SGreg Roach /** 2646ccdf4f0SGreg Roach * Create an uploaded file for a request. 2656ccdf4f0SGreg Roach * 2666ccdf4f0SGreg Roach * @param string $filename 2676ccdf4f0SGreg Roach * @param string $mime_type 2686ccdf4f0SGreg Roach * 2696ccdf4f0SGreg Roach * @return UploadedFileInterface 2706ccdf4f0SGreg Roach */ 2716ccdf4f0SGreg Roach protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface 2726ccdf4f0SGreg Roach { 2736ccdf4f0SGreg Roach /** @var StreamFactoryInterface */ 2746ccdf4f0SGreg Roach $stream_factory = app(StreamFactoryInterface::class); 2756ccdf4f0SGreg Roach 2766ccdf4f0SGreg Roach /** @var UploadedFileFactoryInterface */ 2776ccdf4f0SGreg Roach $uploaded_file_factory = app(UploadedFileFactoryInterface::class); 2786ccdf4f0SGreg Roach 2796ccdf4f0SGreg Roach $stream = $stream_factory->createStreamFromFile($filename); 2806ccdf4f0SGreg Roach $size = filesize($filename); 2816ccdf4f0SGreg Roach $status = UPLOAD_ERR_OK; 2826ccdf4f0SGreg Roach $client_name = basename($filename); 2836ccdf4f0SGreg Roach 2846ccdf4f0SGreg Roach return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type); 2856ccdf4f0SGreg Roach } 28684e2cf4eSGreg Roach} 287