xref: /webtrees/tests/TestCase.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
184e2cf4eSGreg Roach<?php
23cfcc809SGreg Roach
384e2cf4eSGreg Roach/**
484e2cf4eSGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 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
15*89f7189bSGreg 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;
2569675509SGreg Roachuse Fisharebest\Webtrees\Factories\CacheFactory;
26bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\FamilyFactory;
2769675509SGreg Roachuse Fisharebest\Webtrees\Factories\FilesystemFactory;
28bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\GedcomRecordFactory;
29bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\HeaderFactory;
30bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\IndividualFactory;
319574834fSGreg Roachuse Fisharebest\Webtrees\Factories\LocationFactory;
32bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\MediaFactory;
33bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\NoteFactory;
34bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\RepositoryFactory;
35bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\SourceFactory;
36bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\SubmissionFactory;
37bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Factories\SubmitterFactory;
38b4a2f885SGreg Roachuse Fisharebest\Webtrees\Factories\XrefFactory;
396fd01894SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\GedcomLoad;
402279b4f3SGreg Roachuse Fisharebest\Webtrees\Http\Routes\WebRoutes;
418136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
428136679eSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme;
438c3e1068SGreg Roachuse Fisharebest\Webtrees\Services\MigrationService;
4471378461SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
45126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService;
461e653452SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
470115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
486ccdf4f0SGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory;
496ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
506ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface;
516ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
526ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
536ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface;
546ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileInterface;
556ccdf4f0SGreg Roachuse Psr\Http\Message\UriFactoryInterface;
5671378461SGreg Roach
576ccdf4f0SGreg Roachuse function app;
587def76c7SGreg Roachuse function basename;
596ccdf4f0SGreg Roachuse function filesize;
606ccdf4f0SGreg Roachuse function http_build_query;
6157ab2231SGreg Roachuse function microtime;
6271378461SGreg Roach
636ccdf4f0SGreg Roachuse const UPLOAD_ERR_OK;
640115bc16SGreg Roach
6584e2cf4eSGreg Roach/**
6684e2cf4eSGreg Roach * Base class for unit tests
6784e2cf4eSGreg Roach */
6871378461SGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase
6984e2cf4eSGreg Roach{
7074d6dc0eSGreg Roach    /** @var object */
7174d6dc0eSGreg Roach    public static $mock_functions;
7257ab2231SGreg Roach    /** @var bool */
7357ab2231SGreg Roach    protected static $uses_database = false;
7474d6dc0eSGreg Roach
75061b43d7SGreg Roach    /**
76061b43d7SGreg Roach     * Things to run once, before all the tests.
77061b43d7SGreg Roach     */
785e933c21SGreg Roach    public static function setUpBeforeClass(): void
79061b43d7SGreg Roach    {
80061b43d7SGreg Roach        parent::setUpBeforeClass();
81061b43d7SGreg Roach
826ccdf4f0SGreg Roach        // Use nyholm as our PSR7 factory
836ccdf4f0SGreg Roach        app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
846ccdf4f0SGreg Roach        app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
856ccdf4f0SGreg Roach        app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
866ccdf4f0SGreg Roach        app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
876ccdf4f0SGreg Roach        app()->bind(UriFactoryInterface::class, Psr17Factory::class);
886ccdf4f0SGreg Roach
89bb03c9f0SGreg Roach        // Register the factories
906b9cb339SGreg Roach        Registry::cache(new CacheFactory());
916b9cb339SGreg Roach        Registry::familyFactory(new FamilyFactory());
926b9cb339SGreg Roach        Registry::filesystem(new FilesystemFactory());
936b9cb339SGreg Roach        Registry::gedcomRecordFactory(new GedcomRecordFactory());
946b9cb339SGreg Roach        Registry::headerFactory(new HeaderFactory());
956b9cb339SGreg Roach        Registry::individualFactory(new IndividualFactory());
966b9cb339SGreg Roach        Registry::locationFactory(new LocationFactory());
976b9cb339SGreg Roach        Registry::mediaFactory(new MediaFactory());
986b9cb339SGreg Roach        Registry::noteFactory(new NoteFactory());
996b9cb339SGreg Roach        Registry::repositoryFactory(new RepositoryFactory());
1006b9cb339SGreg Roach        Registry::sourceFactory(new SourceFactory());
1016b9cb339SGreg Roach        Registry::submissionFactory(new SubmissionFactory());
1026b9cb339SGreg Roach        Registry::submitterFactory(new SubmitterFactory());
1036b9cb339SGreg Roach        Registry::xrefFactory(new XrefFactory());
1046ccdf4f0SGreg Roach
10557ab2231SGreg Roach        app()->bind(ModuleThemeInterface::class, WebtreesTheme::class);
1066ccdf4f0SGreg Roach
107ee4364daSGreg Roach        // Need the routing table, to generate URLs.
1082279b4f3SGreg Roach        $router_container = new RouterContainer('/');
1092279b4f3SGreg Roach        (new WebRoutes())->load($router_container->getMap());
1102279b4f3SGreg Roach        app()->instance(RouterContainer::class, $router_container);
111ee4364daSGreg Roach
112150f35adSGreg Roach        I18N::init('en-US', true);
1136ccdf4f0SGreg Roach
114061b43d7SGreg Roach        if (static::$uses_database) {
115061b43d7SGreg Roach            static::createTestDatabase();
11671378461SGreg Roach
11771378461SGreg Roach            // Boot modules
11871378461SGreg Roach            (new ModuleService())->bootModules(new WebtreesTheme());
119061b43d7SGreg Roach        }
120061b43d7SGreg Roach    }
121061b43d7SGreg Roach
122061b43d7SGreg Roach    /**
12371378461SGreg Roach     * Things to run once, AFTER all the tests.
12471378461SGreg Roach     */
1255e933c21SGreg Roach    public static function tearDownAfterClass(): void
12671378461SGreg Roach    {
12771378461SGreg Roach        if (static::$uses_database) {
12871378461SGreg Roach            $pdo = DB::connection()->getPdo();
12971378461SGreg Roach            unset($pdo);
13071378461SGreg Roach        }
13171378461SGreg Roach
13271378461SGreg Roach        parent::tearDownAfterClass();
13371378461SGreg Roach    }
13471378461SGreg Roach
13571378461SGreg Roach    /**
1366ccdf4f0SGreg Roach     * Create an SQLite in-memory database for testing
1376ccdf4f0SGreg Roach     */
1386ccdf4f0SGreg Roach    protected static function createTestDatabase(): void
1396ccdf4f0SGreg Roach    {
1406ccdf4f0SGreg Roach        $capsule = new DB();
1416ccdf4f0SGreg Roach        $capsule->addConnection([
1426ccdf4f0SGreg Roach            'driver'   => 'sqlite',
1436ccdf4f0SGreg Roach            'database' => ':memory:',
1446ccdf4f0SGreg Roach        ]);
1456ccdf4f0SGreg Roach        $capsule->setAsGlobal();
146e16a1bfdSGreg Roach
1476ccdf4f0SGreg Roach        // Migrations create logs, which requires an IP address, which requires a request
1486ccdf4f0SGreg Roach        self::createRequest();
1496ccdf4f0SGreg Roach
1506ccdf4f0SGreg Roach        // Create tables
1513cfcc809SGreg Roach        $migration_service = new MigrationService();
1526ccdf4f0SGreg Roach        $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
1536ccdf4f0SGreg Roach
1546ccdf4f0SGreg Roach        // Create config data
1556ccdf4f0SGreg Roach        $migration_service->seedDatabase();
1566ccdf4f0SGreg Roach    }
1576ccdf4f0SGreg Roach
1586ccdf4f0SGreg Roach    /**
15957ab2231SGreg Roach     * Create a request and bind it into the container.
16057ab2231SGreg Roach     *
16157ab2231SGreg Roach     * @param string                  $method
16257ab2231SGreg Roach     * @param string[]                $query
16357ab2231SGreg Roach     * @param string[]                $params
16457ab2231SGreg Roach     * @param UploadedFileInterface[] $files
165b3a775f6SGreg Roach     * @param string[]                $attributes
16657ab2231SGreg Roach     *
16757ab2231SGreg Roach     * @return ServerRequestInterface
16857ab2231SGreg Roach     */
1691a218474SGreg Roach    protected static function createRequest(
1701a218474SGreg Roach        string $method = RequestMethodInterface::METHOD_GET,
1711a218474SGreg Roach        array $query = [],
1721a218474SGreg Roach        array $params = [],
173b3a775f6SGreg Roach        array $files = [],
174b3a775f6SGreg Roach        array $attributes = []
1751a218474SGreg Roach    ): ServerRequestInterface {
17657ab2231SGreg Roach        /** @var ServerRequestFactoryInterface */
17757ab2231SGreg Roach        $server_request_factory = app(ServerRequestFactoryInterface::class);
17857ab2231SGreg Roach
17957ab2231SGreg Roach        $uri = 'https://webtrees.test/index.php?' . http_build_query($query);
18057ab2231SGreg Roach
18157ab2231SGreg Roach        /** @var ServerRequestInterface $request */
18257ab2231SGreg Roach        $request = $server_request_factory
18357ab2231SGreg Roach            ->createServerRequest($method, $uri)
18457ab2231SGreg Roach            ->withQueryParams($query)
18557ab2231SGreg Roach            ->withParsedBody($params)
18657ab2231SGreg Roach            ->withUploadedFiles($files)
18757ab2231SGreg Roach            ->withAttribute('base_url', 'https://webtrees.test')
18890a2f718SGreg Roach            ->withAttribute('client-ip', '127.0.0.1')
189b5f5afdbSGreg Roach            ->withAttribute('user', new GuestUser())
190de2aa325SGreg Roach            ->withAttribute('route', new Route());
191b3a775f6SGreg Roach
192b3a775f6SGreg Roach        foreach ($attributes as $key => $value) {
193b3a775f6SGreg Roach            $request = $request->withAttribute($key, $value);
194b3a775f6SGreg Roach
195b3a775f6SGreg Roach            if ($key === 'tree') {
196b3a775f6SGreg Roach                app()->instance(Tree::class, $value);
197b3a775f6SGreg Roach            }
198b3a775f6SGreg Roach        }
19957ab2231SGreg Roach
20057ab2231SGreg Roach        app()->instance(ServerRequestInterface::class, $request);
20157ab2231SGreg Roach
20257ab2231SGreg Roach        return $request;
20357ab2231SGreg Roach    }
20457ab2231SGreg Roach
20557ab2231SGreg Roach    /**
206061b43d7SGreg Roach     * Things to run before every test.
207061b43d7SGreg Roach     */
2085c48bcd6SGreg Roach    protected function setUp(): void
2090115bc16SGreg Roach    {
2100115bc16SGreg Roach        parent::setUp();
2110115bc16SGreg Roach
212061b43d7SGreg Roach        if (static::$uses_database) {
213061b43d7SGreg Roach            DB::connection()->beginTransaction();
214061b43d7SGreg Roach        }
215061b43d7SGreg Roach    }
216061b43d7SGreg Roach
217061b43d7SGreg Roach    /**
218061b43d7SGreg Roach     * Things to run after every test
219061b43d7SGreg Roach     */
2205e933c21SGreg Roach    protected function tearDown(): void
221a49feabaSGreg Roach    {
22232f20c14SGreg Roach        if (static::$uses_database) {
223061b43d7SGreg Roach            DB::connection()->rollBack();
224061b43d7SGreg Roach        }
22532f20c14SGreg Roach
22632f20c14SGreg Roach        Site::$preferences = [];
22732f20c14SGreg Roach
22832f20c14SGreg Roach        Auth::logout();
2290115bc16SGreg Roach    }
2300115bc16SGreg Roach
2310115bc16SGreg Roach    /**
2320115bc16SGreg Roach     * Import a GEDCOM file into the test database.
2330115bc16SGreg Roach     *
2340115bc16SGreg Roach     * @param string $gedcom_file
235061b43d7SGreg Roach     *
236061b43d7SGreg Roach     * @return Tree
2370115bc16SGreg Roach     */
238061b43d7SGreg Roach    protected function importTree(string $gedcom_file): Tree
2390115bc16SGreg Roach    {
2401e653452SGreg Roach        $tree_service = new TreeService();
2411e653452SGreg Roach        $tree         = $tree_service->create(basename($gedcom_file), basename($gedcom_file));
2426ccdf4f0SGreg Roach        $stream       = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file);
2431e653452SGreg Roach
2446ccdf4f0SGreg Roach        $tree->importGedcomFile($stream, $gedcom_file);
2450115bc16SGreg Roach
24657ab2231SGreg Roach        $timeout_service = new TimeoutService(microtime(true));
2476fd01894SGreg Roach        $controller      = new GedcomLoad($timeout_service);
24857ab2231SGreg Roach        $request         = self::createRequest()->withAttribute('tree', $tree);
249126654d7SGreg Roach
250126654d7SGreg Roach        do {
2516fd01894SGreg Roach            $controller->handle($request);
252126654d7SGreg Roach
253126654d7SGreg Roach            $imported = $tree->getPreference('imported');
254126654d7SGreg Roach        } while (!$imported);
255061b43d7SGreg Roach
256061b43d7SGreg Roach        return $tree;
2570115bc16SGreg Roach    }
2586ccdf4f0SGreg Roach
2596ccdf4f0SGreg Roach    /**
2606ccdf4f0SGreg Roach     * Create an uploaded file for a request.
2616ccdf4f0SGreg Roach     *
2626ccdf4f0SGreg Roach     * @param string $filename
2636ccdf4f0SGreg Roach     * @param string $mime_type
2646ccdf4f0SGreg Roach     *
2656ccdf4f0SGreg Roach     * @return UploadedFileInterface
2666ccdf4f0SGreg Roach     */
2676ccdf4f0SGreg Roach    protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface
2686ccdf4f0SGreg Roach    {
2696ccdf4f0SGreg Roach        /** @var StreamFactoryInterface */
2706ccdf4f0SGreg Roach        $stream_factory = app(StreamFactoryInterface::class);
2716ccdf4f0SGreg Roach
2726ccdf4f0SGreg Roach        /** @var UploadedFileFactoryInterface */
2736ccdf4f0SGreg Roach        $uploaded_file_factory = app(UploadedFileFactoryInterface::class);
2746ccdf4f0SGreg Roach
2756ccdf4f0SGreg Roach        $stream      = $stream_factory->createStreamFromFile($filename);
2766ccdf4f0SGreg Roach        $size        = filesize($filename);
2776ccdf4f0SGreg Roach        $status      = UPLOAD_ERR_OK;
2786ccdf4f0SGreg Roach        $client_name = basename($filename);
2796ccdf4f0SGreg Roach
2806ccdf4f0SGreg Roach        return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type);
2816ccdf4f0SGreg Roach    }
28284e2cf4eSGreg Roach}
283