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