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 22ee4364daSGreg Roachuse Aura\Router\RouterContainer; 23d403609dSGreg Roachuse Fig\Http\Message\RequestMethodInterface; 24bd1e4e13SGreg Roachuse Fisharebest\Localization\Locale\LocaleEnUs; 25bd1e4e13SGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface; 26e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 27126654d7SGreg Roachuse Fisharebest\Webtrees\Http\Controllers\GedcomFileController; 288136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface; 298136679eSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme; 308c3e1068SGreg Roachuse Fisharebest\Webtrees\Services\MigrationService; 3171378461SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 32126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService; 33*1e653452SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 34e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 357fef9211SGreg Roachuse Illuminate\Cache\NullStore; 368b67c11aSGreg Roachuse Illuminate\Cache\Repository; 370115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 38e16a1bfdSGreg Roachuse Illuminate\Database\Query\Builder; 397def76c7SGreg Roachuse League\Flysystem\Filesystem; 4057ab2231SGreg Roachuse League\Flysystem\FilesystemInterface; 417def76c7SGreg Roachuse League\Flysystem\Memory\MemoryAdapter; 426ccdf4f0SGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory; 436ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 446ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface; 456ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 466ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 476ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface; 486ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileInterface; 496ccdf4f0SGreg Roachuse Psr\Http\Message\UriFactoryInterface; 5071378461SGreg Roach 516ccdf4f0SGreg Roachuse function app; 527def76c7SGreg Roachuse function basename; 536ccdf4f0SGreg Roachuse function define; 546ccdf4f0SGreg Roachuse function defined; 556ccdf4f0SGreg Roachuse function filesize; 566ccdf4f0SGreg Roachuse function http_build_query; 5757ab2231SGreg Roachuse function microtime; 5871378461SGreg Roach 596ccdf4f0SGreg Roachuse const UPLOAD_ERR_OK; 600115bc16SGreg Roach 6184e2cf4eSGreg Roach/** 6284e2cf4eSGreg Roach * Base class for unit tests 6384e2cf4eSGreg Roach */ 6471378461SGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase 6584e2cf4eSGreg Roach{ 6674d6dc0eSGreg Roach /** @var object */ 6774d6dc0eSGreg Roach public static $mock_functions; 6857ab2231SGreg Roach /** @var bool */ 6957ab2231SGreg Roach protected static $uses_database = false; 7074d6dc0eSGreg Roach 71061b43d7SGreg Roach /** 72061b43d7SGreg Roach * Things to run once, before all the tests. 73061b43d7SGreg Roach */ 74061b43d7SGreg Roach public static function setUpBeforeClass() 75061b43d7SGreg Roach { 76061b43d7SGreg Roach parent::setUpBeforeClass(); 77061b43d7SGreg Roach 786ccdf4f0SGreg Roach // Use nyholm as our PSR7 factory 796ccdf4f0SGreg Roach app()->bind(ResponseFactoryInterface::class, Psr17Factory::class); 806ccdf4f0SGreg Roach app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class); 816ccdf4f0SGreg Roach app()->bind(StreamFactoryInterface::class, Psr17Factory::class); 826ccdf4f0SGreg Roach app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class); 836ccdf4f0SGreg Roach app()->bind(UriFactoryInterface::class, Psr17Factory::class); 846ccdf4f0SGreg Roach 857fef9211SGreg Roach // Disable the cache. 867fef9211SGreg Roach app()->instance('cache.array', new Repository(new NullStore())); 876ccdf4f0SGreg Roach 8857ab2231SGreg Roach app()->instance(FilesystemInterface::class, new Filesystem(new MemoryAdapter())); 896ccdf4f0SGreg Roach app()->bind(LocaleInterface::class, LocaleEnUs::class); 9057ab2231SGreg Roach app()->bind(ModuleThemeInterface::class, WebtreesTheme::class); 916ccdf4f0SGreg Roach 92ee4364daSGreg Roach // Need the routing table, to generate URLs. 93ee4364daSGreg Roach app()->instance(RouterContainer::class, new RouterContainer()); 94ee4364daSGreg Roach require __DIR__ . '/../routes/web.php'; 95ee4364daSGreg Roach 96f397d0fdSGreg Roach defined('WT_DATA_DIR') || define('WT_DATA_DIR', Webtrees::ROOT_DIR . 'data/'); 976ccdf4f0SGreg Roach defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US', null, true)); 986ccdf4f0SGreg Roach 99061b43d7SGreg Roach if (static::$uses_database) { 100061b43d7SGreg Roach static::createTestDatabase(); 10171378461SGreg Roach 10271378461SGreg Roach // Boot modules 10371378461SGreg Roach (new ModuleService())->bootModules(new WebtreesTheme()); 104061b43d7SGreg Roach } 105061b43d7SGreg Roach } 106061b43d7SGreg Roach 107061b43d7SGreg Roach /** 10871378461SGreg Roach * Things to run once, AFTER all the tests. 10971378461SGreg Roach */ 11071378461SGreg Roach public static function tearDownAfterClass() 11171378461SGreg Roach { 11271378461SGreg Roach if (static::$uses_database) { 11371378461SGreg Roach $pdo = DB::connection()->getPdo(); 11471378461SGreg Roach unset($pdo); 11571378461SGreg Roach } 11671378461SGreg Roach 11771378461SGreg Roach parent::tearDownAfterClass(); 11871378461SGreg Roach } 11971378461SGreg Roach 12071378461SGreg Roach /** 1216ccdf4f0SGreg Roach * Create an SQLite in-memory database for testing 1226ccdf4f0SGreg Roach */ 1236ccdf4f0SGreg Roach protected static function createTestDatabase(): void 1246ccdf4f0SGreg Roach { 1256ccdf4f0SGreg Roach $capsule = new DB(); 1266ccdf4f0SGreg Roach $capsule->addConnection([ 1276ccdf4f0SGreg Roach 'driver' => 'sqlite', 1286ccdf4f0SGreg Roach 'database' => ':memory:', 1296ccdf4f0SGreg Roach ]); 1306ccdf4f0SGreg Roach $capsule->setAsGlobal(); 131e16a1bfdSGreg Roach 132e16a1bfdSGreg Roach Builder::macro('whereContains', function ($column, string $search, string $boolean = 'and'): Builder { 133e16a1bfdSGreg Roach $search = strtr($search, ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); 134e16a1bfdSGreg Roach 135e16a1bfdSGreg Roach return $this->where($column, 'LIKE', '%' . $search . '%', $boolean); 136e16a1bfdSGreg Roach }); 1376ccdf4f0SGreg Roach 1386ccdf4f0SGreg Roach // Migrations create logs, which requires an IP address, which requires a request 1396ccdf4f0SGreg Roach self::createRequest(); 1406ccdf4f0SGreg Roach 1416ccdf4f0SGreg Roach // Create tables 1423cfcc809SGreg Roach $migration_service = new MigrationService(); 1436ccdf4f0SGreg Roach $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 1446ccdf4f0SGreg Roach 1456ccdf4f0SGreg Roach // Create config data 1466ccdf4f0SGreg Roach $migration_service->seedDatabase(); 1476ccdf4f0SGreg Roach } 1486ccdf4f0SGreg Roach 1496ccdf4f0SGreg Roach /** 15057ab2231SGreg Roach * Create a request and bind it into the container. 15157ab2231SGreg Roach * 15257ab2231SGreg Roach * @param string $method 15357ab2231SGreg Roach * @param string[] $query 15457ab2231SGreg Roach * @param string[] $params 15557ab2231SGreg Roach * @param UploadedFileInterface[] $files 15657ab2231SGreg Roach * 15757ab2231SGreg Roach * @return ServerRequestInterface 15857ab2231SGreg Roach */ 1591a218474SGreg Roach protected static function createRequest( 1601a218474SGreg Roach string $method = RequestMethodInterface::METHOD_GET, 1611a218474SGreg Roach array $query = [], 1621a218474SGreg Roach array $params = [], 1631a218474SGreg Roach array $files = [] 1641a218474SGreg Roach ): ServerRequestInterface { 16557ab2231SGreg Roach /** @var ServerRequestFactoryInterface */ 16657ab2231SGreg Roach $server_request_factory = app(ServerRequestFactoryInterface::class); 16757ab2231SGreg Roach 16857ab2231SGreg Roach $uri = 'https://webtrees.test/index.php?' . http_build_query($query); 16957ab2231SGreg Roach 17057ab2231SGreg Roach /** @var ServerRequestInterface $request */ 17157ab2231SGreg Roach $request = $server_request_factory 17257ab2231SGreg Roach ->createServerRequest($method, $uri) 17357ab2231SGreg Roach ->withQueryParams($query) 17457ab2231SGreg Roach ->withParsedBody($params) 17557ab2231SGreg Roach ->withUploadedFiles($files) 17657ab2231SGreg Roach ->withAttribute('base_url', 'https://webtrees.test') 1774874f72dSGreg Roach ->withAttribute('client-ip', '127.0.0.1'); 17857ab2231SGreg Roach 17957ab2231SGreg Roach app()->instance(ServerRequestInterface::class, $request); 180a992e8c1SGreg Roach View::share('request', $request); 18157ab2231SGreg Roach 18257ab2231SGreg Roach return $request; 18357ab2231SGreg Roach } 18457ab2231SGreg Roach 18557ab2231SGreg Roach /** 186061b43d7SGreg Roach * Things to run before every test. 187061b43d7SGreg Roach */ 1885c48bcd6SGreg Roach protected function setUp(): void 1890115bc16SGreg Roach { 1900115bc16SGreg Roach parent::setUp(); 1910115bc16SGreg Roach 192061b43d7SGreg Roach if (static::$uses_database) { 193061b43d7SGreg Roach DB::connection()->beginTransaction(); 194061b43d7SGreg Roach } 195061b43d7SGreg Roach } 196061b43d7SGreg Roach 197061b43d7SGreg Roach /** 198061b43d7SGreg Roach * Things to run after every test 199061b43d7SGreg Roach */ 200a49feabaSGreg Roach protected function tearDown() 201a49feabaSGreg Roach { 20232f20c14SGreg Roach if (static::$uses_database) { 203061b43d7SGreg Roach DB::connection()->rollBack(); 204061b43d7SGreg Roach } 20532f20c14SGreg Roach 20632f20c14SGreg Roach Site::$preferences = []; 207bec87e94SGreg Roach GedcomRecord::$gedcom_record_cache = null; 208bec87e94SGreg Roach GedcomRecord::$pending_record_cache = null; 20932f20c14SGreg Roach 21032f20c14SGreg Roach Auth::logout(); 2110115bc16SGreg Roach } 2120115bc16SGreg Roach 2130115bc16SGreg Roach /** 2140115bc16SGreg Roach * Import a GEDCOM file into the test database. 2150115bc16SGreg Roach * 2160115bc16SGreg Roach * @param string $gedcom_file 217061b43d7SGreg Roach * 218061b43d7SGreg Roach * @return Tree 2190115bc16SGreg Roach */ 220061b43d7SGreg Roach protected function importTree(string $gedcom_file): Tree 2210115bc16SGreg Roach { 222*1e653452SGreg Roach $tree_service = new TreeService(); 223*1e653452SGreg Roach $tree = $tree_service->create(basename($gedcom_file), basename($gedcom_file)); 2246ccdf4f0SGreg Roach $stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file); 225*1e653452SGreg Roach 2266ccdf4f0SGreg Roach $tree->importGedcomFile($stream, $gedcom_file); 2270115bc16SGreg Roach 2281ad2dde6SGreg Roach View::share('tree', $tree); 22957ab2231SGreg Roach 23057ab2231SGreg Roach $timeout_service = new TimeoutService(microtime(true)); 23157ab2231SGreg Roach $controller = new GedcomFileController($timeout_service); 23257ab2231SGreg Roach $request = self::createRequest()->withAttribute('tree', $tree); 233126654d7SGreg Roach 234126654d7SGreg Roach do { 23557ab2231SGreg Roach $controller->import($request); 236126654d7SGreg Roach 237126654d7SGreg Roach $imported = $tree->getPreference('imported'); 238126654d7SGreg Roach } while (!$imported); 239061b43d7SGreg Roach 240061b43d7SGreg Roach return $tree; 2410115bc16SGreg Roach } 2426ccdf4f0SGreg Roach 2436ccdf4f0SGreg Roach /** 2446ccdf4f0SGreg Roach * Create an uploaded file for a request. 2456ccdf4f0SGreg Roach * 2466ccdf4f0SGreg Roach * @param string $filename 2476ccdf4f0SGreg Roach * @param string $mime_type 2486ccdf4f0SGreg Roach * 2496ccdf4f0SGreg Roach * @return UploadedFileInterface 2506ccdf4f0SGreg Roach */ 2516ccdf4f0SGreg Roach protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface 2526ccdf4f0SGreg Roach { 2536ccdf4f0SGreg Roach /** @var StreamFactoryInterface */ 2546ccdf4f0SGreg Roach $stream_factory = app(StreamFactoryInterface::class); 2556ccdf4f0SGreg Roach 2566ccdf4f0SGreg Roach /** @var UploadedFileFactoryInterface */ 2576ccdf4f0SGreg Roach $uploaded_file_factory = app(UploadedFileFactoryInterface::class); 2586ccdf4f0SGreg Roach 2596ccdf4f0SGreg Roach $stream = $stream_factory->createStreamFromFile($filename); 2606ccdf4f0SGreg Roach $size = filesize($filename); 2616ccdf4f0SGreg Roach $status = UPLOAD_ERR_OK; 2626ccdf4f0SGreg Roach $client_name = basename($filename); 2636ccdf4f0SGreg Roach 2646ccdf4f0SGreg Roach return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type); 2656ccdf4f0SGreg Roach } 26684e2cf4eSGreg Roach} 267