184e2cf4eSGreg Roach<?php 23cfcc809SGreg Roach 384e2cf4eSGreg Roach/** 484e2cf4eSGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 22*de2aa325SGreg Roachuse Aura\Router\Route; 23ee4364daSGreg Roachuse Aura\Router\RouterContainer; 24d403609dSGreg Roachuse Fig\Http\Message\RequestMethodInterface; 25126654d7SGreg Roachuse Fisharebest\Webtrees\Http\Controllers\GedcomFileController; 262279b4f3SGreg Roachuse Fisharebest\Webtrees\Http\Routes\WebRoutes; 278136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface; 288136679eSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme; 298c3e1068SGreg Roachuse Fisharebest\Webtrees\Services\MigrationService; 3071378461SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 31126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService; 321e653452SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 330115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 34e16a1bfdSGreg Roachuse Illuminate\Database\Query\Builder; 35b5f5afdbSGreg Roachuse League\Flysystem\Filesystem; 36b5f5afdbSGreg Roachuse League\Flysystem\Memory\MemoryAdapter; 376ccdf4f0SGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory; 386ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 396ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface; 406ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 416ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 426ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface; 436ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileInterface; 446ccdf4f0SGreg Roachuse Psr\Http\Message\UriFactoryInterface; 45c692965aSGreg Roachuse Symfony\Component\Cache\Adapter\NullAdapter; 4671378461SGreg Roach 476ccdf4f0SGreg Roachuse function app; 487def76c7SGreg Roachuse function basename; 496ccdf4f0SGreg Roachuse function filesize; 506ccdf4f0SGreg Roachuse function http_build_query; 5157ab2231SGreg Roachuse function microtime; 5271378461SGreg Roach 536ccdf4f0SGreg Roachuse const UPLOAD_ERR_OK; 540115bc16SGreg Roach 5584e2cf4eSGreg Roach/** 5684e2cf4eSGreg Roach * Base class for unit tests 5784e2cf4eSGreg Roach */ 5871378461SGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase 5984e2cf4eSGreg Roach{ 6074d6dc0eSGreg Roach /** @var object */ 6174d6dc0eSGreg Roach public static $mock_functions; 6257ab2231SGreg Roach /** @var bool */ 6357ab2231SGreg Roach protected static $uses_database = false; 6474d6dc0eSGreg Roach 65061b43d7SGreg Roach /** 66061b43d7SGreg Roach * Things to run once, before all the tests. 67061b43d7SGreg Roach */ 68061b43d7SGreg Roach public static function setUpBeforeClass() 69061b43d7SGreg Roach { 70061b43d7SGreg Roach parent::setUpBeforeClass(); 71061b43d7SGreg Roach 726ccdf4f0SGreg Roach // Use nyholm as our PSR7 factory 736ccdf4f0SGreg Roach app()->bind(ResponseFactoryInterface::class, Psr17Factory::class); 746ccdf4f0SGreg Roach app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class); 756ccdf4f0SGreg Roach app()->bind(StreamFactoryInterface::class, Psr17Factory::class); 766ccdf4f0SGreg Roach app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class); 776ccdf4f0SGreg Roach app()->bind(UriFactoryInterface::class, Psr17Factory::class); 786ccdf4f0SGreg Roach 797fef9211SGreg Roach // Disable the cache. 80c692965aSGreg Roach app()->instance('cache.array', new Cache(new NullAdapter())); 816ccdf4f0SGreg Roach 8257ab2231SGreg Roach app()->bind(ModuleThemeInterface::class, WebtreesTheme::class); 836ccdf4f0SGreg Roach 84ee4364daSGreg Roach // Need the routing table, to generate URLs. 852279b4f3SGreg Roach $router_container = new RouterContainer('/'); 862279b4f3SGreg Roach (new WebRoutes())->load($router_container->getMap()); 872279b4f3SGreg Roach app()->instance(RouterContainer::class, $router_container); 88ee4364daSGreg Roach 89150f35adSGreg Roach I18N::init('en-US', true); 906ccdf4f0SGreg Roach 91061b43d7SGreg Roach if (static::$uses_database) { 92061b43d7SGreg Roach static::createTestDatabase(); 9371378461SGreg Roach 9471378461SGreg Roach // Boot modules 9571378461SGreg Roach (new ModuleService())->bootModules(new WebtreesTheme()); 96061b43d7SGreg Roach } 97061b43d7SGreg Roach } 98061b43d7SGreg Roach 99061b43d7SGreg Roach /** 10071378461SGreg Roach * Things to run once, AFTER all the tests. 10171378461SGreg Roach */ 10271378461SGreg Roach public static function tearDownAfterClass() 10371378461SGreg Roach { 10471378461SGreg Roach if (static::$uses_database) { 10571378461SGreg Roach $pdo = DB::connection()->getPdo(); 10671378461SGreg Roach unset($pdo); 10771378461SGreg Roach } 10871378461SGreg Roach 10971378461SGreg Roach parent::tearDownAfterClass(); 11071378461SGreg Roach } 11171378461SGreg Roach 11271378461SGreg Roach /** 1136ccdf4f0SGreg Roach * Create an SQLite in-memory database for testing 1146ccdf4f0SGreg Roach */ 1156ccdf4f0SGreg Roach protected static function createTestDatabase(): void 1166ccdf4f0SGreg Roach { 1176ccdf4f0SGreg Roach $capsule = new DB(); 1186ccdf4f0SGreg Roach $capsule->addConnection([ 1196ccdf4f0SGreg Roach 'driver' => 'sqlite', 1206ccdf4f0SGreg Roach 'database' => ':memory:', 1216ccdf4f0SGreg Roach ]); 1226ccdf4f0SGreg Roach $capsule->setAsGlobal(); 123e16a1bfdSGreg Roach 124e16a1bfdSGreg Roach Builder::macro('whereContains', function ($column, string $search, string $boolean = 'and'): Builder { 125e16a1bfdSGreg Roach $search = strtr($search, ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); 126e16a1bfdSGreg Roach 127e16a1bfdSGreg Roach return $this->where($column, 'LIKE', '%' . $search . '%', $boolean); 128e16a1bfdSGreg Roach }); 1296ccdf4f0SGreg Roach 1306ccdf4f0SGreg Roach // Migrations create logs, which requires an IP address, which requires a request 1316ccdf4f0SGreg Roach self::createRequest(); 1326ccdf4f0SGreg Roach 1336ccdf4f0SGreg Roach // Create tables 1343cfcc809SGreg Roach $migration_service = new MigrationService(); 1356ccdf4f0SGreg Roach $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 1366ccdf4f0SGreg Roach 1376ccdf4f0SGreg Roach // Create config data 1386ccdf4f0SGreg Roach $migration_service->seedDatabase(); 1396ccdf4f0SGreg Roach } 1406ccdf4f0SGreg Roach 1416ccdf4f0SGreg Roach /** 14257ab2231SGreg Roach * Create a request and bind it into the container. 14357ab2231SGreg Roach * 14457ab2231SGreg Roach * @param string $method 14557ab2231SGreg Roach * @param string[] $query 14657ab2231SGreg Roach * @param string[] $params 14757ab2231SGreg Roach * @param UploadedFileInterface[] $files 148b3a775f6SGreg Roach * @param string[] $attributes 14957ab2231SGreg Roach * 15057ab2231SGreg Roach * @return ServerRequestInterface 15157ab2231SGreg Roach */ 1521a218474SGreg Roach protected static function createRequest( 1531a218474SGreg Roach string $method = RequestMethodInterface::METHOD_GET, 1541a218474SGreg Roach array $query = [], 1551a218474SGreg Roach array $params = [], 156b3a775f6SGreg Roach array $files = [], 157b3a775f6SGreg Roach array $attributes = [] 1581a218474SGreg Roach ): ServerRequestInterface { 15957ab2231SGreg Roach /** @var ServerRequestFactoryInterface */ 16057ab2231SGreg Roach $server_request_factory = app(ServerRequestFactoryInterface::class); 16157ab2231SGreg Roach 16257ab2231SGreg Roach $uri = 'https://webtrees.test/index.php?' . http_build_query($query); 16357ab2231SGreg Roach 16457ab2231SGreg Roach /** @var ServerRequestInterface $request */ 16557ab2231SGreg Roach $request = $server_request_factory 16657ab2231SGreg Roach ->createServerRequest($method, $uri) 16757ab2231SGreg Roach ->withQueryParams($query) 16857ab2231SGreg Roach ->withParsedBody($params) 16957ab2231SGreg Roach ->withUploadedFiles($files) 17057ab2231SGreg Roach ->withAttribute('base_url', 'https://webtrees.test') 17190a2f718SGreg Roach ->withAttribute('client-ip', '127.0.0.1') 172b5f5afdbSGreg Roach ->withAttribute('user', new GuestUser()) 173b5f5afdbSGreg Roach ->withAttribute('filesystem.data', new Filesystem(new MemoryAdapter())) 174*de2aa325SGreg Roach ->withAttribute('filesystem.data.name', 'data/') 175*de2aa325SGreg Roach ->withAttribute('route', new Route()); 176b3a775f6SGreg Roach 177b3a775f6SGreg Roach foreach ($attributes as $key => $value) { 178b3a775f6SGreg Roach $request = $request->withAttribute($key, $value); 179b3a775f6SGreg Roach 180b3a775f6SGreg Roach if ($key === 'tree') { 181b3a775f6SGreg Roach app()->instance(Tree::class, $value); 182b3a775f6SGreg Roach } 183b3a775f6SGreg Roach } 18457ab2231SGreg Roach 18557ab2231SGreg Roach app()->instance(ServerRequestInterface::class, $request); 18657ab2231SGreg Roach 18757ab2231SGreg Roach return $request; 18857ab2231SGreg Roach } 18957ab2231SGreg Roach 19057ab2231SGreg Roach /** 191061b43d7SGreg Roach * Things to run before every test. 192061b43d7SGreg Roach */ 1935c48bcd6SGreg Roach protected function setUp(): void 1940115bc16SGreg Roach { 1950115bc16SGreg Roach parent::setUp(); 1960115bc16SGreg Roach 197061b43d7SGreg Roach if (static::$uses_database) { 198061b43d7SGreg Roach DB::connection()->beginTransaction(); 199061b43d7SGreg Roach } 200061b43d7SGreg Roach } 201061b43d7SGreg Roach 202061b43d7SGreg Roach /** 203061b43d7SGreg Roach * Things to run after every test 204061b43d7SGreg Roach */ 205a49feabaSGreg Roach protected function tearDown() 206a49feabaSGreg Roach { 20732f20c14SGreg Roach if (static::$uses_database) { 208061b43d7SGreg Roach DB::connection()->rollBack(); 209061b43d7SGreg Roach } 21032f20c14SGreg Roach 21132f20c14SGreg Roach Site::$preferences = []; 212bec87e94SGreg Roach GedcomRecord::$gedcom_record_cache = null; 213bec87e94SGreg Roach GedcomRecord::$pending_record_cache = null; 21432f20c14SGreg Roach 21532f20c14SGreg Roach Auth::logout(); 2160115bc16SGreg Roach } 2170115bc16SGreg Roach 2180115bc16SGreg Roach /** 2190115bc16SGreg Roach * Import a GEDCOM file into the test database. 2200115bc16SGreg Roach * 2210115bc16SGreg Roach * @param string $gedcom_file 222061b43d7SGreg Roach * 223061b43d7SGreg Roach * @return Tree 2240115bc16SGreg Roach */ 225061b43d7SGreg Roach protected function importTree(string $gedcom_file): Tree 2260115bc16SGreg Roach { 2271e653452SGreg Roach $tree_service = new TreeService(); 2281e653452SGreg Roach $tree = $tree_service->create(basename($gedcom_file), basename($gedcom_file)); 2296ccdf4f0SGreg Roach $stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file); 2301e653452SGreg Roach 2316ccdf4f0SGreg Roach $tree->importGedcomFile($stream, $gedcom_file); 2320115bc16SGreg Roach 23357ab2231SGreg Roach $timeout_service = new TimeoutService(microtime(true)); 23457ab2231SGreg Roach $controller = new GedcomFileController($timeout_service); 23557ab2231SGreg Roach $request = self::createRequest()->withAttribute('tree', $tree); 236126654d7SGreg Roach 237126654d7SGreg Roach do { 23857ab2231SGreg Roach $controller->import($request); 239126654d7SGreg Roach 240126654d7SGreg Roach $imported = $tree->getPreference('imported'); 241126654d7SGreg Roach } while (!$imported); 242061b43d7SGreg Roach 243061b43d7SGreg Roach return $tree; 2440115bc16SGreg Roach } 2456ccdf4f0SGreg Roach 2466ccdf4f0SGreg Roach /** 2476ccdf4f0SGreg Roach * Create an uploaded file for a request. 2486ccdf4f0SGreg Roach * 2496ccdf4f0SGreg Roach * @param string $filename 2506ccdf4f0SGreg Roach * @param string $mime_type 2516ccdf4f0SGreg Roach * 2526ccdf4f0SGreg Roach * @return UploadedFileInterface 2536ccdf4f0SGreg Roach */ 2546ccdf4f0SGreg Roach protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface 2556ccdf4f0SGreg Roach { 2566ccdf4f0SGreg Roach /** @var StreamFactoryInterface */ 2576ccdf4f0SGreg Roach $stream_factory = app(StreamFactoryInterface::class); 2586ccdf4f0SGreg Roach 2596ccdf4f0SGreg Roach /** @var UploadedFileFactoryInterface */ 2606ccdf4f0SGreg Roach $uploaded_file_factory = app(UploadedFileFactoryInterface::class); 2616ccdf4f0SGreg Roach 2626ccdf4f0SGreg Roach $stream = $stream_factory->createStreamFromFile($filename); 2636ccdf4f0SGreg Roach $size = filesize($filename); 2646ccdf4f0SGreg Roach $status = UPLOAD_ERR_OK; 2656ccdf4f0SGreg Roach $client_name = basename($filename); 2666ccdf4f0SGreg Roach 2676ccdf4f0SGreg Roach return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type); 2686ccdf4f0SGreg Roach } 26984e2cf4eSGreg Roach} 270