1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees; 21 22use Aura\Router\Route; 23use Aura\Router\RouterContainer; 24use Fig\Http\Message\RequestMethodInterface; 25use Fisharebest\Webtrees\Http\RequestHandlers\GedcomLoad; 26use Fisharebest\Webtrees\Http\Routes\WebRoutes; 27use Fisharebest\Webtrees\Module\ModuleThemeInterface; 28use Fisharebest\Webtrees\Module\WebtreesTheme; 29use Fisharebest\Webtrees\Services\MigrationService; 30use Fisharebest\Webtrees\Services\ModuleService; 31use Fisharebest\Webtrees\Services\TimeoutService; 32use Fisharebest\Webtrees\Services\TreeService; 33use Illuminate\Database\Capsule\Manager as DB; 34use Nyholm\Psr7\Factory\Psr17Factory; 35use Psr\Http\Message\ResponseFactoryInterface; 36use Psr\Http\Message\ServerRequestFactoryInterface; 37use Psr\Http\Message\ServerRequestInterface; 38use Psr\Http\Message\StreamFactoryInterface; 39use Psr\Http\Message\UploadedFileFactoryInterface; 40use Psr\Http\Message\UploadedFileInterface; 41use Psr\Http\Message\UriFactoryInterface; 42 43use function app; 44use function basename; 45use function filesize; 46use function http_build_query; 47 48use const UPLOAD_ERR_OK; 49 50/** 51 * Base class for unit tests 52 */ 53class TestCase extends \PHPUnit\Framework\TestCase 54{ 55 /** @var object */ 56 public static $mock_functions; 57 /** @var bool */ 58 protected static $uses_database = false; 59 60 /** 61 * Things to run once, before all the tests. 62 */ 63 public static function setUpBeforeClass(): void 64 { 65 parent::setUpBeforeClass(); 66 67 $webtrees = new Webtrees(); 68 $webtrees->bootstrap(); 69 70 // PSR7 messages and PSR17 message-factories 71 Webtrees::set(ResponseFactoryInterface::class, Psr17Factory::class); 72 Webtrees::set(ServerRequestFactoryInterface::class, Psr17Factory::class); 73 Webtrees::set(StreamFactoryInterface::class, Psr17Factory::class); 74 Webtrees::set(UploadedFileFactoryInterface::class, Psr17Factory::class); 75 Webtrees::set(UriFactoryInterface::class, Psr17Factory::class); 76 77 // This is normally set in middleware. 78 Webtrees::set(ModuleThemeInterface::class, WebtreesTheme::class); 79 80 // Need the routing table, to generate URLs. 81 $router_container = new RouterContainer('/'); 82 (new WebRoutes())->load($router_container->getMap()); 83 Webtrees::set(RouterContainer::class, $router_container); 84 85 I18N::init('en-US', true); 86 87 if (static::$uses_database) { 88 static::createTestDatabase(); 89 90 // Boot modules 91 (new ModuleService())->bootModules(new WebtreesTheme()); 92 } 93 } 94 95 /** 96 * Things to run once, AFTER all the tests. 97 */ 98 public static function tearDownAfterClass(): void 99 { 100 if (static::$uses_database) { 101 $pdo = DB::connection()->getPdo(); 102 unset($pdo); 103 } 104 105 parent::tearDownAfterClass(); 106 } 107 108 /** 109 * Create an SQLite in-memory database for testing 110 */ 111 protected static function createTestDatabase(): void 112 { 113 $capsule = new DB(); 114 $capsule->addConnection([ 115 'driver' => 'sqlite', 116 'database' => ':memory:', 117 ]); 118 $capsule->setAsGlobal(); 119 120 // Migrations create logs, which requires an IP address, which requires a request 121 self::createRequest(); 122 123 // Create tables 124 $migration_service = new MigrationService(); 125 $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 126 127 // Create config data 128 $migration_service->seedDatabase(); 129 } 130 131 /** 132 * Create a request and bind it into the container. 133 * 134 * @param string $method 135 * @param string[] $query 136 * @param string[] $params 137 * @param UploadedFileInterface[] $files 138 * @param string[] $attributes 139 * 140 * @return ServerRequestInterface 141 */ 142 protected static function createRequest( 143 string $method = RequestMethodInterface::METHOD_GET, 144 array $query = [], 145 array $params = [], 146 array $files = [], 147 array $attributes = [] 148 ): ServerRequestInterface { 149 /** @var ServerRequestFactoryInterface */ 150 $server_request_factory = app(ServerRequestFactoryInterface::class); 151 152 $uri = 'https://webtrees.test/index.php?' . http_build_query($query); 153 154 /** @var ServerRequestInterface $request */ 155 $request = $server_request_factory 156 ->createServerRequest($method, $uri) 157 ->withQueryParams($query) 158 ->withParsedBody($params) 159 ->withUploadedFiles($files) 160 ->withAttribute('base_url', 'https://webtrees.test') 161 ->withAttribute('client-ip', '127.0.0.1') 162 ->withAttribute('user', new GuestUser()) 163 ->withAttribute('route', new Route()); 164 165 foreach ($attributes as $key => $value) { 166 $request = $request->withAttribute($key, $value); 167 168 if ($key === 'tree') { 169 app()->instance(Tree::class, $value); 170 } 171 } 172 173 app()->instance(ServerRequestInterface::class, $request); 174 175 return $request; 176 } 177 178 /** 179 * Things to run before every test. 180 */ 181 protected function setUp(): void 182 { 183 parent::setUp(); 184 185 if (static::$uses_database) { 186 DB::connection()->beginTransaction(); 187 } 188 } 189 190 /** 191 * Things to run after every test 192 */ 193 protected function tearDown(): void 194 { 195 if (static::$uses_database) { 196 DB::connection()->rollBack(); 197 } 198 199 Site::$preferences = []; 200 201 Auth::logout(); 202 } 203 204 /** 205 * Import a GEDCOM file into the test database. 206 * 207 * @param string $gedcom_file 208 * 209 * @return Tree 210 */ 211 protected function importTree(string $gedcom_file): Tree 212 { 213 $tree_service = new TreeService(); 214 $tree = $tree_service->create(basename($gedcom_file), basename($gedcom_file)); 215 $stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file); 216 217 $tree_service->importGedcomFile($tree, $stream, $gedcom_file); 218 219 $timeout_service = new TimeoutService(); 220 $controller = new GedcomLoad($timeout_service, $tree_service); 221 $request = self::createRequest()->withAttribute('tree', $tree); 222 223 do { 224 $controller->handle($request); 225 226 $imported = $tree->getPreference('imported'); 227 } while (!$imported); 228 229 return $tree; 230 } 231 232 /** 233 * Create an uploaded file for a request. 234 * 235 * @param string $filename 236 * @param string $mime_type 237 * 238 * @return UploadedFileInterface 239 */ 240 protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface 241 { 242 /** @var StreamFactoryInterface */ 243 $stream_factory = app(StreamFactoryInterface::class); 244 245 /** @var UploadedFileFactoryInterface */ 246 $uploaded_file_factory = app(UploadedFileFactoryInterface::class); 247 248 $stream = $stream_factory->createStreamFromFile($filename); 249 $size = filesize($filename); 250 $status = UPLOAD_ERR_OK; 251 $client_name = basename($filename); 252 253 return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type); 254 } 255} 256