1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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\FamilyFactory; 26use Fisharebest\Webtrees\Factories\GedcomRecordFactory; 27use Fisharebest\Webtrees\Factories\HeaderFactory; 28use Fisharebest\Webtrees\Factories\IndividualFactory; 29use Fisharebest\Webtrees\Factories\MediaFactory; 30use Fisharebest\Webtrees\Factories\NoteFactory; 31use Fisharebest\Webtrees\Factories\RepositoryFactory; 32use Fisharebest\Webtrees\Factories\SourceFactory; 33use Fisharebest\Webtrees\Factories\SubmissionFactory; 34use Fisharebest\Webtrees\Factories\SubmitterFactory; 35use Fisharebest\Webtrees\Http\Controllers\GedcomFileController; 36use Fisharebest\Webtrees\Http\Routes\WebRoutes; 37use Fisharebest\Webtrees\Module\ModuleThemeInterface; 38use Fisharebest\Webtrees\Module\WebtreesTheme; 39use Fisharebest\Webtrees\Services\MigrationService; 40use Fisharebest\Webtrees\Services\ModuleService; 41use Fisharebest\Webtrees\Services\TimeoutService; 42use Fisharebest\Webtrees\Services\TreeService; 43use Illuminate\Database\Capsule\Manager as DB; 44use Illuminate\Database\Query\Builder; 45use League\Flysystem\Filesystem; 46use League\Flysystem\Memory\MemoryAdapter; 47use Nyholm\Psr7\Factory\Psr17Factory; 48use Psr\Http\Message\ResponseFactoryInterface; 49use Psr\Http\Message\ServerRequestFactoryInterface; 50use Psr\Http\Message\ServerRequestInterface; 51use Psr\Http\Message\StreamFactoryInterface; 52use Psr\Http\Message\UploadedFileFactoryInterface; 53use Psr\Http\Message\UploadedFileInterface; 54use Psr\Http\Message\UriFactoryInterface; 55use Symfony\Component\Cache\Adapter\NullAdapter; 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() 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 // Disable the cache. 90 $cache = new Cache(new NullAdapter()); 91 app()->instance('cache.array', $cache); 92 93 // Register the factories 94 Factory::family(new FamilyFactory($cache)); 95 Factory::gedcomRecord(new GedcomRecordFactory($cache)); 96 Factory::header(new HeaderFactory($cache)); 97 Factory::individual(new IndividualFactory($cache)); 98 Factory::media(new MediaFactory($cache)); 99 Factory::note(new NoteFactory($cache)); 100 Factory::repository(new RepositoryFactory($cache)); 101 Factory::source(new SourceFactory($cache)); 102 Factory::submission(new SubmissionFactory($cache)); 103 Factory::submitter(new SubmitterFactory($cache)); 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() 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('filesystem.data', new Filesystem(new MemoryAdapter())) 191 ->withAttribute('filesystem.data.name', 'data/') 192 ->withAttribute('route', new Route()); 193 194 foreach ($attributes as $key => $value) { 195 $request = $request->withAttribute($key, $value); 196 197 if ($key === 'tree') { 198 app()->instance(Tree::class, $value); 199 } 200 } 201 202 app()->instance(ServerRequestInterface::class, $request); 203 204 return $request; 205 } 206 207 /** 208 * Things to run before every test. 209 */ 210 protected function setUp(): void 211 { 212 parent::setUp(); 213 214 if (static::$uses_database) { 215 DB::connection()->beginTransaction(); 216 } 217 } 218 219 /** 220 * Things to run after every test 221 */ 222 protected function tearDown() 223 { 224 if (static::$uses_database) { 225 DB::connection()->rollBack(); 226 } 227 228 Site::$preferences = []; 229 230 Auth::logout(); 231 } 232 233 /** 234 * Import a GEDCOM file into the test database. 235 * 236 * @param string $gedcom_file 237 * 238 * @return Tree 239 */ 240 protected function importTree(string $gedcom_file): Tree 241 { 242 $tree_service = new TreeService(); 243 $tree = $tree_service->create(basename($gedcom_file), basename($gedcom_file)); 244 $stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file); 245 246 $tree->importGedcomFile($stream, $gedcom_file); 247 248 $timeout_service = new TimeoutService(microtime(true)); 249 $controller = new GedcomFileController($timeout_service); 250 $request = self::createRequest()->withAttribute('tree', $tree); 251 252 do { 253 $controller->import($request); 254 255 $imported = $tree->getPreference('imported'); 256 } while (!$imported); 257 258 return $tree; 259 } 260 261 /** 262 * Create an uploaded file for a request. 263 * 264 * @param string $filename 265 * @param string $mime_type 266 * 267 * @return UploadedFileInterface 268 */ 269 protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface 270 { 271 /** @var StreamFactoryInterface */ 272 $stream_factory = app(StreamFactoryInterface::class); 273 274 /** @var UploadedFileFactoryInterface */ 275 $uploaded_file_factory = app(UploadedFileFactoryInterface::class); 276 277 $stream = $stream_factory->createStreamFromFile($filename); 278 $size = filesize($filename); 279 $status = UPLOAD_ERR_OK; 280 $client_name = basename($filename); 281 282 return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type); 283 } 284} 285