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