184e2cf4eSGreg Roach<?php 23cfcc809SGreg Roach 384e2cf4eSGreg Roach/** 484e2cf4eSGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 684e2cf4eSGreg Roach * This program is free software: you can redistribute it and/or modify 784e2cf4eSGreg Roach * it under the terms of the GNU General Public License as published by 884e2cf4eSGreg Roach * the Free Software Foundation, either version 3 of the License, or 984e2cf4eSGreg Roach * (at your option) any later version. 1084e2cf4eSGreg Roach * This program is distributed in the hope that it will be useful, 1184e2cf4eSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1284e2cf4eSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1384e2cf4eSGreg Roach * GNU General Public License for more details. 1484e2cf4eSGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 1684e2cf4eSGreg Roach */ 173cfcc809SGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2084e2cf4eSGreg Roachnamespace Fisharebest\Webtrees; 2184e2cf4eSGreg Roach 22de2aa325SGreg Roachuse Aura\Router\Route; 23ee4364daSGreg Roachuse Aura\Router\RouterContainer; 24d403609dSGreg Roachuse Fig\Http\Message\RequestMethodInterface; 2512b5bef1SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 266fd01894SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\GedcomLoad; 272279b4f3SGreg Roachuse Fisharebest\Webtrees\Http\Routes\WebRoutes; 288136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface; 298136679eSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme; 302c685d76SGreg Roachuse Fisharebest\Webtrees\Services\GedcomImportService; 318c3e1068SGreg Roachuse Fisharebest\Webtrees\Services\MigrationService; 3271378461SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 33126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService; 341e653452SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 359aef375dSGreg Roachuse PHPUnit\Framework\Constraint\Callback; 36d4786c66SGreg Roachuse Psr\Http\Message\ResponseInterface; 3700c45d23SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface; 386ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 396ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 406ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface; 416ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileInterface; 4271378461SGreg Roach 439aef375dSGreg Roachuse function array_shift; 447def76c7SGreg Roachuse function basename; 456ccdf4f0SGreg Roachuse function filesize; 466ccdf4f0SGreg Roachuse function http_build_query; 47d4786c66SGreg Roachuse function implode; 48d4786c66SGreg Roachuse function preg_match; 49d4786c66SGreg Roachuse function str_starts_with; 50d4786c66SGreg Roachuse function strcspn; 51d4786c66SGreg Roachuse function strlen; 52d4786c66SGreg Roachuse function strpos; 53d4786c66SGreg Roachuse function substr; 54d4786c66SGreg Roach 556ccdf4f0SGreg Roachuse const UPLOAD_ERR_OK; 560115bc16SGreg Roach 5771378461SGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase 5884e2cf4eSGreg Roach{ 59cd94ca66SGreg Roach public static ?object $mock_functions = null; 60cd94ca66SGreg Roach 61cd94ca66SGreg Roach protected static bool $uses_database = false; 6274d6dc0eSGreg Roach 63061b43d7SGreg Roach /** 646ccdf4f0SGreg Roach * Create an SQLite in-memory database for testing 656ccdf4f0SGreg Roach */ 66a26ec5edSGreg Roach private static function createTestDatabase(): void 676ccdf4f0SGreg Roach { 68*52550490SGreg Roach DB::connect( 69*52550490SGreg Roach driver: DB::SQLITE, 70*52550490SGreg Roach host: '', 71*52550490SGreg Roach port: '', 72*52550490SGreg Roach database: ':memory:', 73*52550490SGreg Roach username: '', 74*52550490SGreg Roach password: '', 75*52550490SGreg Roach prefix: 'wt_', 76*52550490SGreg Roach key: '', 77*52550490SGreg Roach certificate: '', 78*52550490SGreg Roach ca: '', 79*52550490SGreg Roach verify_certificate: false, 80*52550490SGreg Roach ); 81e16a1bfdSGreg Roach 826ccdf4f0SGreg Roach // Create tables 833cfcc809SGreg Roach $migration_service = new MigrationService(); 846ccdf4f0SGreg Roach $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); 856ccdf4f0SGreg Roach 866ccdf4f0SGreg Roach // Create config data 876ccdf4f0SGreg Roach $migration_service->seedDatabase(); 886ccdf4f0SGreg Roach } 896ccdf4f0SGreg Roach 906ccdf4f0SGreg Roach /** 9157ab2231SGreg Roach * Create a request and bind it into the container. 9257ab2231SGreg Roach * 9309482a55SGreg Roach * @param array<string> $query 9409482a55SGreg Roach * @param array<string> $params 9509482a55SGreg Roach * @param array<UploadedFileInterface> $files 960acf1b4bSGreg Roach * @param array<string|Tree> $attributes 9757ab2231SGreg Roach */ 981a218474SGreg Roach protected static function createRequest( 991a218474SGreg Roach string $method = RequestMethodInterface::METHOD_GET, 1001a218474SGreg Roach array $query = [], 1011a218474SGreg Roach array $params = [], 102b3a775f6SGreg Roach array $files = [], 103b3a775f6SGreg Roach array $attributes = [] 1041a218474SGreg Roach ): ServerRequestInterface { 105d35568b4SGreg Roach $server_request_factory = Registry::container()->get(ServerRequestFactoryInterface::class); 10600c45d23SGreg Roach 10757ab2231SGreg Roach $uri = 'https://webtrees.test/index.php?' . http_build_query($query); 10857ab2231SGreg Roach 10900c45d23SGreg Roach $request = $server_request_factory 11057ab2231SGreg Roach ->createServerRequest($method, $uri) 11157ab2231SGreg Roach ->withQueryParams($query) 11257ab2231SGreg Roach ->withParsedBody($params) 11357ab2231SGreg Roach ->withUploadedFiles($files) 11457ab2231SGreg Roach ->withAttribute('base_url', 'https://webtrees.test') 11590a2f718SGreg Roach ->withAttribute('client-ip', '127.0.0.1') 116b5f5afdbSGreg Roach ->withAttribute('user', new GuestUser()) 117de2aa325SGreg Roach ->withAttribute('route', new Route()); 118b3a775f6SGreg Roach 119b3a775f6SGreg Roach foreach ($attributes as $key => $value) { 120b3a775f6SGreg Roach $request = $request->withAttribute($key, $value); 121b3a775f6SGreg Roach 122b3a775f6SGreg Roach if ($key === 'tree') { 123d35568b4SGreg Roach Registry::container()->set(Tree::class, $value); 124b3a775f6SGreg Roach } 125b3a775f6SGreg Roach } 12657ab2231SGreg Roach 127d35568b4SGreg Roach Registry::container()->set(ServerRequestInterface::class, $request); 12857ab2231SGreg Roach 12957ab2231SGreg Roach return $request; 13057ab2231SGreg Roach } 13157ab2231SGreg Roach 13257ab2231SGreg Roach /** 133061b43d7SGreg Roach * Things to run before every test. 134061b43d7SGreg Roach */ 1355c48bcd6SGreg Roach protected function setUp(): void 1360115bc16SGreg Roach { 1370115bc16SGreg Roach parent::setUp(); 1380115bc16SGreg Roach 139a26ec5edSGreg Roach $webtrees = new Webtrees(); 140a26ec5edSGreg Roach $webtrees->bootstrap(); 141a26ec5edSGreg Roach 142a26ec5edSGreg Roach // This is normally set in middleware. 143a26ec5edSGreg Roach Registry::container()->set(ModuleThemeInterface::class, new WebtreesTheme()); 144a26ec5edSGreg Roach 145a26ec5edSGreg Roach // Need the routing table, to generate URLs. 146a26ec5edSGreg Roach $router_container = new RouterContainer('/'); 147a26ec5edSGreg Roach (new WebRoutes())->load($router_container->getMap()); 148a26ec5edSGreg Roach Registry::container()->set(RouterContainer::class, $router_container); 149a26ec5edSGreg Roach 1509025445dSGreg Roach if (static::$uses_database) { 151f24a8589SGreg Roach self::createTestDatabase(); 152a26ec5edSGreg Roach 153a26ec5edSGreg Roach // This is normally set in middleware. 154a26ec5edSGreg Roach (new Gedcom())->registerTags(Registry::elementFactory(), true); 155a26ec5edSGreg Roach 156a26ec5edSGreg Roach // Boot modules 157a26ec5edSGreg Roach (new ModuleService())->bootModules(new WebtreesTheme()); 158a26ec5edSGreg Roach 159a26ec5edSGreg Roach I18N::init('en-US'); 160a26ec5edSGreg Roach } else { 161a26ec5edSGreg Roach I18N::init('en-US', true); 162061b43d7SGreg Roach } 163a26ec5edSGreg Roach 164a26ec5edSGreg Roach self::createRequest(); 165061b43d7SGreg Roach } 166061b43d7SGreg Roach 167061b43d7SGreg Roach /** 168061b43d7SGreg Roach * Things to run after every test 169061b43d7SGreg Roach */ 1705e933c21SGreg Roach protected function tearDown(): void 171a49feabaSGreg Roach { 17232f20c14SGreg Roach if (static::$uses_database) { 173a26ec5edSGreg Roach DB::connection()->disconnect(); 174061b43d7SGreg Roach } 17532f20c14SGreg Roach 176a26ec5edSGreg Roach Session::clear(); // Session data is stored in the super-global 177a26ec5edSGreg Roach Site::$preferences = []; // These are cached from the database 1780115bc16SGreg Roach } 1790115bc16SGreg Roach 180061b43d7SGreg Roach protected function importTree(string $gedcom_file): Tree 1810115bc16SGreg Roach { 1822c685d76SGreg Roach $gedcom_import_service = new GedcomImportService(); 1832c685d76SGreg Roach $tree_service = new TreeService($gedcom_import_service); 1841e653452SGreg Roach $tree = $tree_service->create(basename($gedcom_file), basename($gedcom_file)); 185d35568b4SGreg Roach $stream = Registry::container()->get(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file); 1861e653452SGreg Roach 1871c6adce8SGreg Roach $tree_service->importGedcomFile($tree, $stream, $gedcom_file, ''); 1880115bc16SGreg Roach 189c4943cffSGreg Roach $timeout_service = new TimeoutService(); 19010e06497SGreg Roach $controller = new GedcomLoad($gedcom_import_service, $timeout_service); 19157ab2231SGreg Roach $request = self::createRequest()->withAttribute('tree', $tree); 192126654d7SGreg Roach 193126654d7SGreg Roach do { 1946fd01894SGreg Roach $controller->handle($request); 195126654d7SGreg Roach 196126654d7SGreg Roach $imported = $tree->getPreference('imported'); 197126654d7SGreg Roach } while (!$imported); 198061b43d7SGreg Roach 199061b43d7SGreg Roach return $tree; 2000115bc16SGreg Roach } 2016ccdf4f0SGreg Roach 2026ccdf4f0SGreg Roach protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface 2036ccdf4f0SGreg Roach { 204d35568b4SGreg Roach $stream_factory = Registry::container()->get(StreamFactoryInterface::class); 205d35568b4SGreg Roach $uploaded_file_factory = Registry::container()->get(UploadedFileFactoryInterface::class); 20600c45d23SGreg Roach 20700c45d23SGreg Roach $stream = $stream_factory->createStreamFromFile($filename); 2086ccdf4f0SGreg Roach $size = filesize($filename); 2096ccdf4f0SGreg Roach $status = UPLOAD_ERR_OK; 2106ccdf4f0SGreg Roach $client_name = basename($filename); 2116ccdf4f0SGreg Roach 21200c45d23SGreg Roach return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type); 2136ccdf4f0SGreg Roach } 214d4786c66SGreg Roach 215d4786c66SGreg Roach protected function validateHtmlResponse(ResponseInterface $response): void 216d4786c66SGreg Roach { 21712b5bef1SGreg Roach self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); 21812b5bef1SGreg Roach 21912b5bef1SGreg Roach self::assertEquals('text/html; charset=UTF-8', $response->getHeaderLine('content-type')); 220d4786c66SGreg Roach 221d4786c66SGreg Roach $html = $response->getBody()->getContents(); 222d4786c66SGreg Roach 22312b5bef1SGreg Roach self::assertStringStartsWith('<DOCTYPE html>', $html); 224d4786c66SGreg Roach 225d4786c66SGreg Roach $this->validateHtml(substr($html, strlen('<DOCTYPE html>'))); 226d4786c66SGreg Roach } 227d4786c66SGreg Roach 228d4786c66SGreg Roach protected function validateHtml(string $html): void 229d4786c66SGreg Roach { 230d4786c66SGreg Roach $stack = []; 231d4786c66SGreg Roach 232d4786c66SGreg Roach do { 233b3027499SJonathan Jaubart $html = substr($html, strcspn($html, '<>')); 234d4786c66SGreg Roach 235d4786c66SGreg Roach if (str_starts_with($html, '>')) { 236f01ab4acSGreg Roach static::fail('Unescaped > found in HTML'); 237d4786c66SGreg Roach } 238d4786c66SGreg Roach 239d4786c66SGreg Roach if (str_starts_with($html, '<')) { 240d4786c66SGreg Roach if (preg_match('~^</([a-z]+)>~', $html, $match)) { 241d4786c66SGreg Roach if ($match[1] !== array_pop($stack)) { 242f01ab4acSGreg Roach static::fail('Closing tag matches nothing: ' . $match[0] . ' at ' . implode(':', $stack)); 243d4786c66SGreg Roach } 244d4786c66SGreg Roach $html = substr($html, strlen($match[0])); 245c3f581d9SGreg Roach } elseif (preg_match('~^<([a-z]+)(?:\s+[a-z_\-]+="[^">]*")*\s*(/?)>~', $html, $match)) { 246d4786c66SGreg Roach $tag = $match[1]; 247c3f581d9SGreg Roach $self_closing = $match[2] === '/'; 248d4786c66SGreg Roach 249d4786c66SGreg Roach $message = 'Tag ' . $tag . ' is not allowed at ' . implode(':', $stack) . '.'; 250d4786c66SGreg Roach 251d4786c66SGreg Roach switch ($tag) { 252d4786c66SGreg Roach case 'html': 253f01ab4acSGreg Roach static::assertSame([], $stack); 254d4786c66SGreg Roach break; 255d4786c66SGreg Roach case 'head': 256d4786c66SGreg Roach case 'body': 257f01ab4acSGreg Roach static::assertSame(['head'], $stack); 258d4786c66SGreg Roach break; 259d4786c66SGreg Roach case 'div': 260f01ab4acSGreg Roach static::assertNotContains('span', $stack, $message); 261d4786c66SGreg Roach break; 262d4786c66SGreg Roach } 263d4786c66SGreg Roach 264d4786c66SGreg Roach if (!$self_closing) { 265d4786c66SGreg Roach $stack[] = $tag; 266d4786c66SGreg Roach } 267d4786c66SGreg Roach 268b3027499SJonathan Jaubart if ($tag === 'script' && !$self_closing) { 269b3027499SJonathan Jaubart $html = substr($html, strpos($html, '</script>')); 270b3027499SJonathan Jaubart } else { 271d4786c66SGreg Roach $html = substr($html, strlen($match[0])); 272b3027499SJonathan Jaubart } 273d4786c66SGreg Roach } else { 274f01ab4acSGreg Roach static::fail('Unrecognised tag: ' . substr($html, 0, 40)); 275d4786c66SGreg Roach } 276d4786c66SGreg Roach } 277d4786c66SGreg Roach } while ($html !== ''); 278d4786c66SGreg Roach 279f01ab4acSGreg Roach static::assertSame([], $stack); 280d4786c66SGreg Roach } 2819aef375dSGreg Roach 2829aef375dSGreg Roach /** 2839aef375dSGreg Roach * Workaround for removal of withConsecutive in phpunit 10. 2849aef375dSGreg Roach * 2859aef375dSGreg Roach * @param array<int,mixed> $parameters 2869aef375dSGreg Roach */ 2879aef375dSGreg Roach protected static function withConsecutive(array $parameters): Callback 2889aef375dSGreg Roach { 2899aef375dSGreg Roach return self::callback(static function (mixed $parameter) use ($parameters): bool { 2909aef375dSGreg Roach static $array = null; 2919aef375dSGreg Roach 2929aef375dSGreg Roach $array ??= $parameters; 2939aef375dSGreg Roach 2949aef375dSGreg Roach return $parameter === array_shift($array); 2959aef375dSGreg Roach }); 2969aef375dSGreg Roach } 29784e2cf4eSGreg Roach} 298