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