1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees; 19 20use Aura\Router\RouterContainer; 21use Fig\Http\Message\RequestMethodInterface; 22use Fisharebest\Localization\Locale\LocaleEnUs; 23use Fisharebest\Localization\Locale\LocaleInterface; 24use Fisharebest\Webtrees\Contracts\UserInterface; 25use Fisharebest\Webtrees\Http\Controllers\GedcomFileController; 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\UserService; 32use Illuminate\Cache\ArrayStore; 33use Illuminate\Cache\Repository; 34use Illuminate\Database\Capsule\Manager as DB; 35use Illuminate\Database\Query\Builder; 36use League\Flysystem\Filesystem; 37use League\Flysystem\FilesystemInterface; 38use League\Flysystem\Memory\MemoryAdapter; 39use Nyholm\Psr7\Factory\Psr17Factory; 40use Psr\Http\Message\ResponseFactoryInterface; 41use Psr\Http\Message\ServerRequestFactoryInterface; 42use Psr\Http\Message\ServerRequestInterface; 43use Psr\Http\Message\StreamFactoryInterface; 44use Psr\Http\Message\UploadedFileFactoryInterface; 45use Psr\Http\Message\UploadedFileInterface; 46use Psr\Http\Message\UriFactoryInterface; 47 48use function app; 49use function basename; 50use function define; 51use function defined; 52use function filesize; 53use function http_build_query; 54use function microtime; 55 56use const UPLOAD_ERR_OK; 57 58/** 59 * Base class for unit tests 60 */ 61class TestCase extends \PHPUnit\Framework\TestCase 62{ 63 /** @var object */ 64 public static $mock_functions; 65 /** @var bool */ 66 protected static $uses_database = false; 67 68 /** 69 * Things to run once, before all the tests. 70 */ 71 public static function setUpBeforeClass() 72 { 73 parent::setUpBeforeClass(); 74 75 // Use nyholm as our PSR7 factory 76 app()->bind(ResponseFactoryInterface::class, Psr17Factory::class); 77 app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class); 78 app()->bind(StreamFactoryInterface::class, Psr17Factory::class); 79 app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class); 80 app()->bind(UriFactoryInterface::class, Psr17Factory::class); 81 82 // Use an array cache for database calls, etc. 83 app()->instance('cache.array', new Repository(new ArrayStore())); 84 85 app()->instance(UserService::class, new UserService()); 86 app()->instance(FilesystemInterface::class, new Filesystem(new MemoryAdapter())); 87 app()->bind(LocaleInterface::class, LocaleEnUs::class); 88 app()->bind(ModuleThemeInterface::class, WebtreesTheme::class); 89 app()->bind(UserInterface::class, GuestUser::class); 90 91 // Need the routing table, to generate URLs. 92 app()->instance(RouterContainer::class, new RouterContainer()); 93 require __DIR__ . '/../routes/web.php'; 94 95 defined('WT_DATA_DIR') || define('WT_DATA_DIR', Webtrees::ROOT_DIR . 'data/'); 96 defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US', null, true)); 97 98 if (static::$uses_database) { 99 static::createTestDatabase(); 100 101 // Boot modules 102 (new ModuleService())->bootModules(new WebtreesTheme()); 103 } 104 } 105 106 /** 107 * Things to run once, AFTER all the tests. 108 */ 109 public static function tearDownAfterClass() 110 { 111 if (static::$uses_database) { 112 $pdo = DB::connection()->getPdo(); 113 unset($pdo); 114 } 115 116 parent::tearDownAfterClass(); 117 } 118 119 /** 120 * Create an SQLite in-memory database for testing 121 */ 122 protected static function createTestDatabase(): void 123 { 124 $capsule = new DB(); 125 $capsule->addConnection([ 126 'driver' => 'sqlite', 127 'database' => ':memory:', 128 ]); 129 $capsule->setAsGlobal(); 130 131 Builder::macro('whereContains', function ($column, string $search, string $boolean = 'and'): Builder { 132 $search = strtr($search, ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']); 133 134 return $this->where($column, 'LIKE', '%' . $search . '%', $boolean); 135 }); 136 137 // Migrations create logs, which requires an IP address, which requires a request 138 self::createRequest(); 139 140 // Create tables 141 $migration_service = new MigrationService; 142 $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 143 144 // Create config data 145 $migration_service->seedDatabase(); 146 } 147 148 /** 149 * Create a request and bind it into the container. 150 * 151 * @param string $method 152 * @param string[] $query 153 * @param string[] $params 154 * @param UploadedFileInterface[] $files 155 * 156 * @return ServerRequestInterface 157 */ 158 protected static function createRequest(string $method = RequestMethodInterface::METHOD_GET, array $query = [], array $params = [], array $files = []): ServerRequestInterface 159 { 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 174 app()->instance(ServerRequestInterface::class, $request); 175 View::share('request', $request); 176 177 return $request; 178 } 179 180 /** 181 * Things to run before every test. 182 */ 183 protected function setUp(): void 184 { 185 parent::setUp(); 186 187 if (static::$uses_database) { 188 DB::connection()->beginTransaction(); 189 } 190 } 191 192 /** 193 * Things to run after every test 194 */ 195 protected function tearDown() 196 { 197 if (static::$uses_database) { 198 DB::connection()->rollBack(); 199 } 200 201 app('cache.array')->flush(); 202 203 Site::$preferences = []; 204 Tree::$trees = []; 205 GedcomRecord::$gedcom_record_cache = null; 206 GedcomRecord::$pending_record_cache = null; 207 208 Auth::logout(); 209 } 210 211 /** 212 * Import a GEDCOM file into the test database. 213 * 214 * @param string $gedcom_file 215 * 216 * @return Tree 217 */ 218 protected function importTree(string $gedcom_file): Tree 219 { 220 $tree = Tree::create(basename($gedcom_file), basename($gedcom_file)); 221 222 $stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file); 223 $tree->importGedcomFile($stream, $gedcom_file); 224 225 View::share('tree', $tree); 226 227 $timeout_service = new TimeoutService(microtime(true)); 228 $controller = new GedcomFileController($timeout_service); 229 $request = self::createRequest()->withAttribute('tree', $tree); 230 231 do { 232 $controller->import($request); 233 234 $imported = $tree->getPreference('imported'); 235 } while (!$imported); 236 237 return $tree; 238 } 239 240 /** 241 * Create an uploaded file for a request. 242 * 243 * @param string $filename 244 * @param string $mime_type 245 * 246 * @return UploadedFileInterface 247 */ 248 protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface 249 { 250 /** @var StreamFactoryInterface */ 251 $stream_factory = app(StreamFactoryInterface::class); 252 253 /** @var UploadedFileFactoryInterface */ 254 $uploaded_file_factory = app(UploadedFileFactoryInterface::class); 255 256 $stream = $stream_factory->createStreamFromFile($filename); 257 $size = filesize($filename); 258 $status = UPLOAD_ERR_OK; 259 $client_name = basename($filename); 260 261 return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type); 262 } 263} 264