184e2cf4eSGreg Roach<?php 284e2cf4eSGreg Roach/** 384e2cf4eSGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 584e2cf4eSGreg Roach * This program is free software: you can redistribute it and/or modify 684e2cf4eSGreg Roach * it under the terms of the GNU General Public License as published by 784e2cf4eSGreg Roach * the Free Software Foundation, either version 3 of the License, or 884e2cf4eSGreg Roach * (at your option) any later version. 984e2cf4eSGreg Roach * This program is distributed in the hope that it will be useful, 1084e2cf4eSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1184e2cf4eSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1284e2cf4eSGreg Roach * GNU General Public License for more details. 1384e2cf4eSGreg Roach * You should have received a copy of the GNU General Public License 1484e2cf4eSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 1584e2cf4eSGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1884e2cf4eSGreg Roachnamespace Fisharebest\Webtrees; 1984e2cf4eSGreg Roach 206ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 21bd1e4e13SGreg Roachuse Fisharebest\Localization\Locale\LocaleEnUs; 22bd1e4e13SGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface; 23e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 24126654d7SGreg Roachuse Fisharebest\Webtrees\Http\Controllers\GedcomFileController; 256ccdf4f0SGreg Roachuse Fisharebest\Webtrees\Http\Request; 268136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface; 278136679eSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme; 288c3e1068SGreg Roachuse Fisharebest\Webtrees\Services\MigrationService; 29126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService; 30e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 318b67c11aSGreg Roachuse Illuminate\Cache\ArrayStore; 328b67c11aSGreg Roachuse Illuminate\Cache\Repository; 330115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 347def76c7SGreg Roachuse League\Flysystem\Filesystem; 357def76c7SGreg Roachuse League\Flysystem\Memory\MemoryAdapter; 366ccdf4f0SGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory; 376ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 386ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface; 396ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 406ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 416ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface; 426ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileInterface; 436ccdf4f0SGreg Roachuse Psr\Http\Message\UriFactoryInterface; 446ccdf4f0SGreg Roachuse function app; 457def76c7SGreg Roachuse function basename; 466ccdf4f0SGreg Roachuse function define; 476ccdf4f0SGreg Roachuse function defined; 486ccdf4f0SGreg Roachuse function filesize; 496ccdf4f0SGreg Roachuse function http_build_query; 506ccdf4f0SGreg Roachuse const UPLOAD_ERR_OK; 510115bc16SGreg Roach 5284e2cf4eSGreg Roach/** 5384e2cf4eSGreg Roach * Base class for unit tests 5484e2cf4eSGreg Roach */ 556ccdf4f0SGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase implements StatusCodeInterface 5684e2cf4eSGreg Roach{ 57*74d6dc0eSGreg Roach /** @var bool */ 58061b43d7SGreg Roach protected static $uses_database = false; 59061b43d7SGreg Roach 60*74d6dc0eSGreg Roach /** @var object */ 61*74d6dc0eSGreg Roach public static $mock_functions; 62*74d6dc0eSGreg Roach 63061b43d7SGreg Roach /** 64061b43d7SGreg Roach * Things to run once, before all the tests. 65061b43d7SGreg Roach */ 66061b43d7SGreg Roach public static function setUpBeforeClass() 67061b43d7SGreg Roach { 68061b43d7SGreg Roach parent::setUpBeforeClass(); 69061b43d7SGreg Roach 706ccdf4f0SGreg Roach // Use nyholm as our PSR7 factory 716ccdf4f0SGreg Roach app()->bind(ResponseFactoryInterface::class, Psr17Factory::class); 726ccdf4f0SGreg Roach app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class); 736ccdf4f0SGreg Roach app()->bind(StreamFactoryInterface::class, Psr17Factory::class); 746ccdf4f0SGreg Roach app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class); 756ccdf4f0SGreg Roach app()->bind(UriFactoryInterface::class, Psr17Factory::class); 766ccdf4f0SGreg Roach 776ccdf4f0SGreg Roach // Use an array cache for database calls, etc. 786ccdf4f0SGreg Roach app()->instance('cache.array', new Repository(new ArrayStore())); 796ccdf4f0SGreg Roach 806ccdf4f0SGreg Roach app()->bind(Tree::class, static function () { 816ccdf4f0SGreg Roach return null; 826ccdf4f0SGreg Roach }); 836ccdf4f0SGreg Roach 846ccdf4f0SGreg Roach app()->instance(UserService::class, new UserService()); 856ccdf4f0SGreg Roach app()->instance(UserInterface::class, new GuestUser()); 866ccdf4f0SGreg Roach 876ccdf4f0SGreg Roach app()->instance(ServerRequestInterface::class, Request::create('http://localhost/index.php')); 886ccdf4f0SGreg Roach app()->instance(Filesystem::class, new Filesystem(new MemoryAdapter())); 896ccdf4f0SGreg Roach 906ccdf4f0SGreg Roach app()->bind(ModuleThemeInterface::class, WebtreesTheme::class); 916ccdf4f0SGreg Roach app()->bind(LocaleInterface::class, LocaleEnUs::class); 926ccdf4f0SGreg Roach 936ccdf4f0SGreg Roach defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/'); 94f397d0fdSGreg Roach defined('WT_DATA_DIR') || define('WT_DATA_DIR', Webtrees::ROOT_DIR . 'data/'); 956ccdf4f0SGreg Roach defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US', null, true)); 966ccdf4f0SGreg Roach 97061b43d7SGreg Roach if (static::$uses_database) { 98061b43d7SGreg Roach static::createTestDatabase(); 99061b43d7SGreg Roach } 100061b43d7SGreg Roach } 101061b43d7SGreg Roach 102061b43d7SGreg Roach /** 1036ccdf4f0SGreg Roach * Create an SQLite in-memory database for testing 1046ccdf4f0SGreg Roach */ 1056ccdf4f0SGreg Roach protected static function createTestDatabase(): void 1066ccdf4f0SGreg Roach { 1076ccdf4f0SGreg Roach $capsule = new DB(); 1086ccdf4f0SGreg Roach $capsule->addConnection([ 1096ccdf4f0SGreg Roach 'driver' => 'sqlite', 1106ccdf4f0SGreg Roach 'database' => ':memory:', 1116ccdf4f0SGreg Roach ]); 1126ccdf4f0SGreg Roach $capsule->setAsGlobal(); 1136ccdf4f0SGreg Roach Database::registerMacros(); 1146ccdf4f0SGreg Roach 1156ccdf4f0SGreg Roach // Migrations create logs, which requires an IP address, which requires a request 1166ccdf4f0SGreg Roach self::createRequest(); 1176ccdf4f0SGreg Roach 1186ccdf4f0SGreg Roach // Create tables 1196ccdf4f0SGreg Roach $migration_service = new MigrationService; 1206ccdf4f0SGreg Roach $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 1216ccdf4f0SGreg Roach 1226ccdf4f0SGreg Roach // Create config data 1236ccdf4f0SGreg Roach $migration_service->seedDatabase(); 1246ccdf4f0SGreg Roach } 1256ccdf4f0SGreg Roach 1266ccdf4f0SGreg Roach /** 127061b43d7SGreg Roach * Things to run once, AFTER all the tests. 128061b43d7SGreg Roach */ 129061b43d7SGreg Roach public static function tearDownAfterClass() 130061b43d7SGreg Roach { 131061b43d7SGreg Roach if (static::$uses_database) { 132061b43d7SGreg Roach $pdo = DB::connection()->getPdo(); 13332f20c14SGreg Roach unset($pdo); 134061b43d7SGreg Roach } 135061b43d7SGreg Roach 136061b43d7SGreg Roach parent::tearDownAfterClass(); 137061b43d7SGreg Roach } 138061b43d7SGreg Roach 139061b43d7SGreg Roach /** 140061b43d7SGreg Roach * Things to run before every test. 141061b43d7SGreg Roach */ 1420115bc16SGreg Roach protected function setUp() 1430115bc16SGreg Roach { 1440115bc16SGreg Roach parent::setUp(); 1450115bc16SGreg Roach 146061b43d7SGreg Roach if (static::$uses_database) { 147061b43d7SGreg Roach DB::connection()->beginTransaction(); 148061b43d7SGreg Roach } 149061b43d7SGreg Roach } 150061b43d7SGreg Roach 151061b43d7SGreg Roach /** 152061b43d7SGreg Roach * Things to run after every test 153061b43d7SGreg Roach */ 154a49feabaSGreg Roach protected function tearDown() 155a49feabaSGreg Roach { 15632f20c14SGreg Roach if (static::$uses_database) { 157061b43d7SGreg Roach DB::connection()->rollBack(); 158061b43d7SGreg Roach } 15932f20c14SGreg Roach 1608b67c11aSGreg Roach app('cache.array')->flush(); 1618b67c11aSGreg Roach 16232f20c14SGreg Roach Site::$preferences = []; 16332f20c14SGreg Roach Tree::$trees = []; 164bec87e94SGreg Roach GedcomRecord::$gedcom_record_cache = null; 165bec87e94SGreg Roach GedcomRecord::$pending_record_cache = null; 16632f20c14SGreg Roach 16732f20c14SGreg Roach Auth::logout(); 1680115bc16SGreg Roach } 1690115bc16SGreg Roach 1700115bc16SGreg Roach /** 1710115bc16SGreg Roach * Import a GEDCOM file into the test database. 1720115bc16SGreg Roach * 1730115bc16SGreg Roach * @param string $gedcom_file 174061b43d7SGreg Roach * 175061b43d7SGreg Roach * @return Tree 1760115bc16SGreg Roach */ 177061b43d7SGreg Roach protected function importTree(string $gedcom_file): Tree 1780115bc16SGreg Roach { 179061b43d7SGreg Roach $tree = Tree::create(basename($gedcom_file), basename($gedcom_file)); 1806ccdf4f0SGreg Roach 1816ccdf4f0SGreg Roach $stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file); 1826ccdf4f0SGreg Roach $tree->importGedcomFile($stream, $gedcom_file); 1830115bc16SGreg Roach 1841ad2dde6SGreg Roach View::share('tree', $tree); 185126654d7SGreg Roach $gedcom_file_controller = new GedcomFileController(); 186126654d7SGreg Roach 187126654d7SGreg Roach do { 1881ad2dde6SGreg Roach $gedcom_file_controller->import(new TimeoutService(microtime(true)), $tree); 189126654d7SGreg Roach 190126654d7SGreg Roach $imported = $tree->getPreference('imported'); 191126654d7SGreg Roach } while (!$imported); 192061b43d7SGreg Roach 193061b43d7SGreg Roach return $tree; 1940115bc16SGreg Roach } 1956ccdf4f0SGreg Roach 1966ccdf4f0SGreg Roach /** 1976ccdf4f0SGreg Roach * Create a request and bind it into the container. 1986ccdf4f0SGreg Roach * 1996ccdf4f0SGreg Roach * @param string $method 2006ccdf4f0SGreg Roach * @param string[] $query 2016ccdf4f0SGreg Roach * @param string[] $params 2026ccdf4f0SGreg Roach * @param UploadedFileInterface[] $files 2036ccdf4f0SGreg Roach * 2046ccdf4f0SGreg Roach * @return ServerRequestInterface 2056ccdf4f0SGreg Roach */ 2066ccdf4f0SGreg Roach protected static function createRequest(string $method = 'GET', array $query = [], array $params = [], array $files = []): ServerRequestInterface 2076ccdf4f0SGreg Roach { 2086ccdf4f0SGreg Roach /** @var ServerRequestFactoryInterface */ 2096ccdf4f0SGreg Roach $server_request_factory = app(ServerRequestFactoryInterface::class); 2106ccdf4f0SGreg Roach 2116ccdf4f0SGreg Roach $uri = 'http://localhost/index.php?' . http_build_query($query); 2126ccdf4f0SGreg Roach 2136ccdf4f0SGreg Roach /** @var ServerRequestInterface $request */ 2146ccdf4f0SGreg Roach $request = $server_request_factory 2156ccdf4f0SGreg Roach ->createServerRequest($method, $uri) 2166ccdf4f0SGreg Roach ->withQueryParams($query) 2176ccdf4f0SGreg Roach ->withParsedBody($params) 2186ccdf4f0SGreg Roach ->withUploadedFiles($files); 2196ccdf4f0SGreg Roach 2206ccdf4f0SGreg Roach app()->instance(ServerRequestInterface::class, $request); 2216ccdf4f0SGreg Roach 2226ccdf4f0SGreg Roach return $request; 2236ccdf4f0SGreg Roach } 2246ccdf4f0SGreg Roach 2256ccdf4f0SGreg Roach /** 2266ccdf4f0SGreg Roach * Create an uploaded file for a request. 2276ccdf4f0SGreg Roach * 2286ccdf4f0SGreg Roach * @param string $filename 2296ccdf4f0SGreg Roach * @param string $mime_type 2306ccdf4f0SGreg Roach * 2316ccdf4f0SGreg Roach * @return UploadedFileInterface 2326ccdf4f0SGreg Roach */ 2336ccdf4f0SGreg Roach protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface 2346ccdf4f0SGreg Roach { 2356ccdf4f0SGreg Roach /** @var StreamFactoryInterface */ 2366ccdf4f0SGreg Roach $stream_factory = app(StreamFactoryInterface::class); 2376ccdf4f0SGreg Roach 2386ccdf4f0SGreg Roach /** @var UploadedFileFactoryInterface */ 2396ccdf4f0SGreg Roach $uploaded_file_factory = app(UploadedFileFactoryInterface::class); 2406ccdf4f0SGreg Roach 2416ccdf4f0SGreg Roach $stream = $stream_factory->createStreamFromFile($filename); 2426ccdf4f0SGreg Roach 2436ccdf4f0SGreg Roach $size = filesize($filename); 2446ccdf4f0SGreg Roach 2456ccdf4f0SGreg Roach $status = UPLOAD_ERR_OK; 2466ccdf4f0SGreg Roach 2476ccdf4f0SGreg Roach $client_name = basename($filename); 2486ccdf4f0SGreg Roach 2496ccdf4f0SGreg Roach return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type); 2506ccdf4f0SGreg Roach } 25184e2cf4eSGreg Roach} 252