1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2020 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 <http://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\Factories\CacheFactory; 26use Fisharebest\Webtrees\Factories\FamilyFactory; 27use Fisharebest\Webtrees\Factories\FilesystemFactory; 28use Fisharebest\Webtrees\Factories\GedcomRecordFactory; 29use Fisharebest\Webtrees\Factories\HeaderFactory; 30use Fisharebest\Webtrees\Factories\IndividualFactory; 31use Fisharebest\Webtrees\Factories\LocationFactory; 32use Fisharebest\Webtrees\Factories\MediaFactory; 33use Fisharebest\Webtrees\Factories\NoteFactory; 34use Fisharebest\Webtrees\Factories\RepositoryFactory; 35use Fisharebest\Webtrees\Factories\SourceFactory; 36use Fisharebest\Webtrees\Factories\SubmissionFactory; 37use Fisharebest\Webtrees\Factories\SubmitterFactory; 38use Fisharebest\Webtrees\Factories\XrefFactory; 39use Fisharebest\Webtrees\Http\RequestHandlers\GedcomLoad; 40use Fisharebest\Webtrees\Http\Routes\WebRoutes; 41use Fisharebest\Webtrees\Module\ModuleThemeInterface; 42use Fisharebest\Webtrees\Module\WebtreesTheme; 43use Fisharebest\Webtrees\Services\MigrationService; 44use Fisharebest\Webtrees\Services\ModuleService; 45use Fisharebest\Webtrees\Services\TimeoutService; 46use Fisharebest\Webtrees\Services\TreeService; 47use Illuminate\Database\Capsule\Manager as DB; 48use Nyholm\Psr7\Factory\Psr17Factory; 49use Psr\Http\Message\ResponseFactoryInterface; 50use Psr\Http\Message\ServerRequestFactoryInterface; 51use Psr\Http\Message\ServerRequestInterface; 52use Psr\Http\Message\StreamFactoryInterface; 53use Psr\Http\Message\UploadedFileFactoryInterface; 54use Psr\Http\Message\UploadedFileInterface; 55use Psr\Http\Message\UriFactoryInterface; 56 57use function app; 58use function basename; 59use function filesize; 60use function http_build_query; 61use function microtime; 62 63use const UPLOAD_ERR_OK; 64 65/** 66 * Base class for unit tests 67 */ 68class TestCase extends \PHPUnit\Framework\TestCase 69{ 70 /** @var object */ 71 public static $mock_functions; 72 /** @var bool */ 73 protected static $uses_database = false; 74 75 /** 76 * Things to run once, before all the tests. 77 */ 78 public static function setUpBeforeClass(): void 79 { 80 parent::setUpBeforeClass(); 81 82 // Use nyholm as our PSR7 factory 83 app()->bind(ResponseFactoryInterface::class, Psr17Factory::class); 84 app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class); 85 app()->bind(StreamFactoryInterface::class, Psr17Factory::class); 86 app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class); 87 app()->bind(UriFactoryInterface::class, Psr17Factory::class); 88 89 // Register the factories 90 Registry::cache(new CacheFactory()); 91 Registry::familyFactory(new FamilyFactory()); 92 Registry::filesystem(new FilesystemFactory()); 93 Registry::gedcomRecordFactory(new GedcomRecordFactory()); 94 Registry::headerFactory(new HeaderFactory()); 95 Registry::individualFactory(new IndividualFactory()); 96 Registry::locationFactory(new LocationFactory()); 97 Registry::mediaFactory(new MediaFactory()); 98 Registry::noteFactory(new NoteFactory()); 99 Registry::repositoryFactory(new RepositoryFactory()); 100 Registry::sourceFactory(new SourceFactory()); 101 Registry::submissionFactory(new SubmissionFactory()); 102 Registry::submitterFactory(new SubmitterFactory()); 103 Registry::xrefFactory(new XrefFactory()); 104 105 app()->bind(ModuleThemeInterface::class, WebtreesTheme::class); 106 107 // Need the routing table, to generate URLs. 108 $router_container = new RouterContainer('/'); 109 (new WebRoutes())->load($router_container->getMap()); 110 app()->instance(RouterContainer::class, $router_container); 111 112 I18N::init('en-US', true); 113 114 if (static::$uses_database) { 115 static::createTestDatabase(); 116 117 // Boot modules 118 (new ModuleService())->bootModules(new WebtreesTheme()); 119 } 120 } 121 122 /** 123 * Things to run once, AFTER all the tests. 124 */ 125 public static function tearDownAfterClass(): void 126 { 127 if (static::$uses_database) { 128 $pdo = DB::connection()->getPdo(); 129 unset($pdo); 130 } 131 132 parent::tearDownAfterClass(); 133 } 134 135 /** 136 * Create an SQLite in-memory database for testing 137 */ 138 protected static function createTestDatabase(): void 139 { 140 $capsule = new DB(); 141 $capsule->addConnection([ 142 'driver' => 'sqlite', 143 'database' => ':memory:', 144 ]); 145 $capsule->setAsGlobal(); 146 147 // Migrations create logs, which requires an IP address, which requires a request 148 self::createRequest(); 149 150 // Create tables 151 $migration_service = new MigrationService(); 152 $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 153 154 // Create config data 155 $migration_service->seedDatabase(); 156 } 157 158 /** 159 * Create a request and bind it into the container. 160 * 161 * @param string $method 162 * @param string[] $query 163 * @param string[] $params 164 * @param UploadedFileInterface[] $files 165 * @param string[] $attributes 166 * 167 * @return ServerRequestInterface 168 */ 169 protected static function createRequest( 170 string $method = RequestMethodInterface::METHOD_GET, 171 array $query = [], 172 array $params = [], 173 array $files = [], 174 array $attributes = [] 175 ): ServerRequestInterface { 176 /** @var ServerRequestFactoryInterface */ 177 $server_request_factory = app(ServerRequestFactoryInterface::class); 178 179 $uri = 'https://webtrees.test/index.php?' . http_build_query($query); 180 181 /** @var ServerRequestInterface $request */ 182 $request = $server_request_factory 183 ->createServerRequest($method, $uri) 184 ->withQueryParams($query) 185 ->withParsedBody($params) 186 ->withUploadedFiles($files) 187 ->withAttribute('base_url', 'https://webtrees.test') 188 ->withAttribute('client-ip', '127.0.0.1') 189 ->withAttribute('user', new GuestUser()) 190 ->withAttribute('route', new Route()); 191 192 foreach ($attributes as $key => $value) { 193 $request = $request->withAttribute($key, $value); 194 195 if ($key === 'tree') { 196 app()->instance(Tree::class, $value); 197 } 198 } 199 200 app()->instance(ServerRequestInterface::class, $request); 201 202 return $request; 203 } 204 205 /** 206 * Things to run before every test. 207 */ 208 protected function setUp(): void 209 { 210 parent::setUp(); 211 212 if (static::$uses_database) { 213 DB::connection()->beginTransaction(); 214 } 215 } 216 217 /** 218 * Things to run after every test 219 */ 220 protected function tearDown(): void 221 { 222 if (static::$uses_database) { 223 DB::connection()->rollBack(); 224 } 225 226 Site::$preferences = []; 227 228 Auth::logout(); 229 } 230 231 /** 232 * Import a GEDCOM file into the test database. 233 * 234 * @param string $gedcom_file 235 * 236 * @return Tree 237 */ 238 protected function importTree(string $gedcom_file): Tree 239 { 240 $tree_service = new TreeService(); 241 $tree = $tree_service->create(basename($gedcom_file), basename($gedcom_file)); 242 $stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file); 243 244 $tree->importGedcomFile($stream, $gedcom_file); 245 246 $timeout_service = new TimeoutService(microtime(true)); 247 $controller = new GedcomLoad($timeout_service); 248 $request = self::createRequest()->withAttribute('tree', $tree); 249 250 do { 251 $controller->handle($request); 252 253 $imported = $tree->getPreference('imported'); 254 } while (!$imported); 255 256 return $tree; 257 } 258 259 /** 260 * Create an uploaded file for a request. 261 * 262 * @param string $filename 263 * @param string $mime_type 264 * 265 * @return UploadedFileInterface 266 */ 267 protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface 268 { 269 /** @var StreamFactoryInterface */ 270 $stream_factory = app(StreamFactoryInterface::class); 271 272 /** @var UploadedFileFactoryInterface */ 273 $uploaded_file_factory = app(UploadedFileFactoryInterface::class); 274 275 $stream = $stream_factory->createStreamFromFile($filename); 276 $size = filesize($filename); 277 $status = UPLOAD_ERR_OK; 278 $client_name = basename($filename); 279 280 return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type); 281 } 282} 283