xref: /webtrees/tests/TestCase.php (revision a992e8c1f5cff3fba48e8b69ec698e4e21142dc8)
184e2cf4eSGreg Roach<?php
284e2cf4eSGreg Roach/**
384e2cf4eSGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
584e2cf4eSGreg Roach * This program is free software: you can redistribute it and/or modify
684e2cf4eSGreg Roach * it under the terms of the GNU General Public License as published by
784e2cf4eSGreg Roach * the Free Software Foundation, either version 3 of the License, or
884e2cf4eSGreg Roach * (at your option) any later version.
984e2cf4eSGreg Roach * This program is distributed in the hope that it will be useful,
1084e2cf4eSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1184e2cf4eSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1284e2cf4eSGreg Roach * GNU General Public License for more details.
1384e2cf4eSGreg Roach * You should have received a copy of the GNU General Public License
1484e2cf4eSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1584e2cf4eSGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1884e2cf4eSGreg Roachnamespace Fisharebest\Webtrees;
1984e2cf4eSGreg Roach
20d403609dSGreg Roachuse Fig\Http\Message\RequestMethodInterface;
216ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
22bd1e4e13SGreg Roachuse Fisharebest\Localization\Locale\LocaleEnUs;
23bd1e4e13SGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface;
24e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
25126654d7SGreg Roachuse Fisharebest\Webtrees\Http\Controllers\GedcomFileController;
268136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
278136679eSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme;
288c3e1068SGreg Roachuse Fisharebest\Webtrees\Services\MigrationService;
29126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService;
30e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
318b67c11aSGreg Roachuse Illuminate\Cache\ArrayStore;
328b67c11aSGreg Roachuse Illuminate\Cache\Repository;
330115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
34e16a1bfdSGreg Roachuse Illuminate\Database\Query\Builder;
357def76c7SGreg Roachuse League\Flysystem\Filesystem;
3657ab2231SGreg Roachuse League\Flysystem\FilesystemInterface;
377def76c7SGreg Roachuse League\Flysystem\Memory\MemoryAdapter;
386ccdf4f0SGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory;
396ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
406ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface;
416ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
426ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
436ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface;
446ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileInterface;
456ccdf4f0SGreg Roachuse Psr\Http\Message\UriFactoryInterface;
466ccdf4f0SGreg Roachuse function app;
477def76c7SGreg Roachuse function basename;
486ccdf4f0SGreg Roachuse function define;
496ccdf4f0SGreg Roachuse function defined;
506ccdf4f0SGreg Roachuse function filesize;
516ccdf4f0SGreg Roachuse function http_build_query;
5257ab2231SGreg Roachuse function microtime;
536ccdf4f0SGreg Roachuse const UPLOAD_ERR_OK;
540115bc16SGreg Roach
5584e2cf4eSGreg Roach/**
5684e2cf4eSGreg Roach * Base class for unit tests
5784e2cf4eSGreg Roach */
58d403609dSGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase implements StatusCodeInterface, RequestMethodInterface
5984e2cf4eSGreg Roach{
6074d6dc0eSGreg Roach    /** @var object */
6174d6dc0eSGreg Roach    public static $mock_functions;
6257ab2231SGreg Roach    /** @var bool */
6357ab2231SGreg Roach    protected static $uses_database = false;
6474d6dc0eSGreg Roach
65061b43d7SGreg Roach    /**
66061b43d7SGreg Roach     * Things to run once, before all the tests.
67061b43d7SGreg Roach     */
68061b43d7SGreg Roach    public static function setUpBeforeClass()
69061b43d7SGreg Roach    {
70061b43d7SGreg Roach        parent::setUpBeforeClass();
71061b43d7SGreg Roach
726ccdf4f0SGreg Roach        // Use nyholm as our PSR7 factory
736ccdf4f0SGreg Roach        app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
746ccdf4f0SGreg Roach        app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
756ccdf4f0SGreg Roach        app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
766ccdf4f0SGreg Roach        app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
776ccdf4f0SGreg Roach        app()->bind(UriFactoryInterface::class, Psr17Factory::class);
786ccdf4f0SGreg Roach
796ccdf4f0SGreg Roach        // Use an array cache for database calls, etc.
806ccdf4f0SGreg Roach        app()->instance('cache.array', new Repository(new ArrayStore()));
816ccdf4f0SGreg Roach
826ccdf4f0SGreg Roach        app()->instance(UserService::class, new UserService());
8357ab2231SGreg Roach        app()->instance(FilesystemInterface::class, new Filesystem(new MemoryAdapter()));
846ccdf4f0SGreg Roach        app()->bind(LocaleInterface::class, LocaleEnUs::class);
8557ab2231SGreg Roach        app()->bind(ModuleThemeInterface::class, WebtreesTheme::class);
8657ab2231SGreg Roach        app()->bind(UserInterface::class, GuestUser::class);
876ccdf4f0SGreg Roach
88f397d0fdSGreg Roach        defined('WT_DATA_DIR') || define('WT_DATA_DIR', Webtrees::ROOT_DIR . 'data/');
896ccdf4f0SGreg Roach        defined('WT_LOCALE') || define('WT_LOCALE', I18N::init('en-US', null, true));
906ccdf4f0SGreg Roach
91061b43d7SGreg Roach        if (static::$uses_database) {
92061b43d7SGreg Roach            static::createTestDatabase();
93061b43d7SGreg Roach        }
94061b43d7SGreg Roach    }
95061b43d7SGreg Roach
96061b43d7SGreg Roach    /**
976ccdf4f0SGreg Roach     * Create an SQLite in-memory database for testing
986ccdf4f0SGreg Roach     */
996ccdf4f0SGreg Roach    protected static function createTestDatabase(): void
1006ccdf4f0SGreg Roach    {
1016ccdf4f0SGreg Roach        $capsule = new DB();
1026ccdf4f0SGreg Roach        $capsule->addConnection([
1036ccdf4f0SGreg Roach            'driver'   => 'sqlite',
1046ccdf4f0SGreg Roach            'database' => ':memory:',
1056ccdf4f0SGreg Roach        ]);
1066ccdf4f0SGreg Roach        $capsule->setAsGlobal();
107e16a1bfdSGreg Roach
108e16a1bfdSGreg Roach        Builder::macro('whereContains', function ($column, string $search, string $boolean = 'and'): Builder {
109e16a1bfdSGreg Roach            $search = strtr($search, ['\\' => '\\\\', '%' => '\\%', '_' => '\\_', ' ' => '%']);
110e16a1bfdSGreg Roach
111e16a1bfdSGreg Roach            return $this->where($column, 'LIKE', '%' . $search . '%', $boolean);
112e16a1bfdSGreg Roach        });
1136ccdf4f0SGreg Roach
1146ccdf4f0SGreg Roach        // Migrations create logs, which requires an IP address, which requires a request
1156ccdf4f0SGreg Roach        self::createRequest();
1166ccdf4f0SGreg Roach
1176ccdf4f0SGreg Roach        // Create tables
1186ccdf4f0SGreg Roach        $migration_service = new MigrationService;
1196ccdf4f0SGreg Roach        $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
1206ccdf4f0SGreg Roach
1216ccdf4f0SGreg Roach        // Create config data
1226ccdf4f0SGreg Roach        $migration_service->seedDatabase();
1236ccdf4f0SGreg Roach    }
1246ccdf4f0SGreg Roach
1256ccdf4f0SGreg Roach    /**
12657ab2231SGreg Roach     * Create a request and bind it into the container.
12757ab2231SGreg Roach     *
12857ab2231SGreg Roach     * @param string                  $method
12957ab2231SGreg Roach     * @param string[]                $query
13057ab2231SGreg Roach     * @param string[]                $params
13157ab2231SGreg Roach     * @param UploadedFileInterface[] $files
13257ab2231SGreg Roach     *
13357ab2231SGreg Roach     * @return ServerRequestInterface
13457ab2231SGreg Roach     */
135d403609dSGreg Roach    protected static function createRequest(string $method = self::METHOD_GET, array $query = [], array $params = [], array $files = []): ServerRequestInterface
13657ab2231SGreg Roach    {
13757ab2231SGreg Roach        /** @var ServerRequestFactoryInterface */
13857ab2231SGreg Roach        $server_request_factory = app(ServerRequestFactoryInterface::class);
13957ab2231SGreg Roach
14057ab2231SGreg Roach        $uri = 'https://webtrees.test/index.php?' . http_build_query($query);
14157ab2231SGreg Roach
14257ab2231SGreg Roach        /** @var ServerRequestInterface $request */
14357ab2231SGreg Roach        $request = $server_request_factory
14457ab2231SGreg Roach            ->createServerRequest($method, $uri)
14557ab2231SGreg Roach            ->withQueryParams($query)
14657ab2231SGreg Roach            ->withParsedBody($params)
14757ab2231SGreg Roach            ->withUploadedFiles($files)
14857ab2231SGreg Roach            ->withAttribute('base_url', 'https://webtrees.test')
14957ab2231SGreg Roach            ->withAttribute('client_ip', '127.0.0.1');
15057ab2231SGreg Roach
15157ab2231SGreg Roach        app()->instance(ServerRequestInterface::class, $request);
152*a992e8c1SGreg Roach        View::share('request', $request);
15357ab2231SGreg Roach
15457ab2231SGreg Roach        return $request;
15557ab2231SGreg Roach    }
15657ab2231SGreg Roach
15757ab2231SGreg Roach    /**
158061b43d7SGreg Roach     * Things to run once, AFTER all the tests.
159061b43d7SGreg Roach     */
160061b43d7SGreg Roach    public static function tearDownAfterClass()
161061b43d7SGreg Roach    {
162061b43d7SGreg Roach        if (static::$uses_database) {
163061b43d7SGreg Roach            $pdo = DB::connection()->getPdo();
16432f20c14SGreg Roach            unset($pdo);
165061b43d7SGreg Roach        }
166061b43d7SGreg Roach
167061b43d7SGreg Roach        parent::tearDownAfterClass();
168061b43d7SGreg Roach    }
169061b43d7SGreg Roach
170061b43d7SGreg Roach    /**
171061b43d7SGreg Roach     * Things to run before every test.
172061b43d7SGreg Roach     */
1735c48bcd6SGreg Roach    protected function setUp(): void
1740115bc16SGreg Roach    {
1750115bc16SGreg Roach        parent::setUp();
1760115bc16SGreg Roach
177061b43d7SGreg Roach        if (static::$uses_database) {
178061b43d7SGreg Roach            DB::connection()->beginTransaction();
179061b43d7SGreg Roach        }
180061b43d7SGreg Roach    }
181061b43d7SGreg Roach
182061b43d7SGreg Roach    /**
183061b43d7SGreg Roach     * Things to run after every test
184061b43d7SGreg Roach     */
185a49feabaSGreg Roach    protected function tearDown()
186a49feabaSGreg Roach    {
18732f20c14SGreg Roach        if (static::$uses_database) {
188061b43d7SGreg Roach            DB::connection()->rollBack();
189061b43d7SGreg Roach        }
19032f20c14SGreg Roach
1918b67c11aSGreg Roach        app('cache.array')->flush();
1928b67c11aSGreg Roach
19332f20c14SGreg Roach        Site::$preferences                  = [];
19432f20c14SGreg Roach        Tree::$trees                        = [];
195bec87e94SGreg Roach        GedcomRecord::$gedcom_record_cache  = null;
196bec87e94SGreg Roach        GedcomRecord::$pending_record_cache = null;
19732f20c14SGreg Roach
19832f20c14SGreg Roach        Auth::logout();
1990115bc16SGreg Roach    }
2000115bc16SGreg Roach
2010115bc16SGreg Roach    /**
2020115bc16SGreg Roach     * Import a GEDCOM file into the test database.
2030115bc16SGreg Roach     *
2040115bc16SGreg Roach     * @param string $gedcom_file
205061b43d7SGreg Roach     *
206061b43d7SGreg Roach     * @return Tree
2070115bc16SGreg Roach     */
208061b43d7SGreg Roach    protected function importTree(string $gedcom_file): Tree
2090115bc16SGreg Roach    {
210061b43d7SGreg Roach        $tree = Tree::create(basename($gedcom_file), basename($gedcom_file));
2116ccdf4f0SGreg Roach
2126ccdf4f0SGreg Roach        $stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file);
2136ccdf4f0SGreg Roach        $tree->importGedcomFile($stream, $gedcom_file);
2140115bc16SGreg Roach
2151ad2dde6SGreg Roach        View::share('tree', $tree);
21657ab2231SGreg Roach
21757ab2231SGreg Roach        $timeout_service = new TimeoutService(microtime(true));
21857ab2231SGreg Roach        $controller      = new GedcomFileController($timeout_service);
21957ab2231SGreg Roach        $request         = self::createRequest()->withAttribute('tree', $tree);
220126654d7SGreg Roach
221126654d7SGreg Roach        do {
22257ab2231SGreg Roach            $controller->import($request);
223126654d7SGreg Roach
224126654d7SGreg Roach            $imported = $tree->getPreference('imported');
225126654d7SGreg Roach        } while (!$imported);
226061b43d7SGreg Roach
227061b43d7SGreg Roach        return $tree;
2280115bc16SGreg Roach    }
2296ccdf4f0SGreg Roach
2306ccdf4f0SGreg Roach    /**
2316ccdf4f0SGreg Roach     * Create an uploaded file for a request.
2326ccdf4f0SGreg Roach     *
2336ccdf4f0SGreg Roach     * @param string $filename
2346ccdf4f0SGreg Roach     * @param string $mime_type
2356ccdf4f0SGreg Roach     *
2366ccdf4f0SGreg Roach     * @return UploadedFileInterface
2376ccdf4f0SGreg Roach     */
2386ccdf4f0SGreg Roach    protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface
2396ccdf4f0SGreg Roach    {
2406ccdf4f0SGreg Roach        /** @var StreamFactoryInterface */
2416ccdf4f0SGreg Roach        $stream_factory = app(StreamFactoryInterface::class);
2426ccdf4f0SGreg Roach
2436ccdf4f0SGreg Roach        /** @var UploadedFileFactoryInterface */
2446ccdf4f0SGreg Roach        $uploaded_file_factory = app(UploadedFileFactoryInterface::class);
2456ccdf4f0SGreg Roach
2466ccdf4f0SGreg Roach        $stream      = $stream_factory->createStreamFromFile($filename);
2476ccdf4f0SGreg Roach        $size        = filesize($filename);
2486ccdf4f0SGreg Roach        $status      = UPLOAD_ERR_OK;
2496ccdf4f0SGreg Roach        $client_name = basename($filename);
2506ccdf4f0SGreg Roach
2516ccdf4f0SGreg Roach        return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type);
2526ccdf4f0SGreg Roach    }
25384e2cf4eSGreg Roach}
254