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