xref: /webtrees/tests/TestCase.php (revision 00c45d2300a5375d4779bf9dbe157d4ef56b9443)
184e2cf4eSGreg Roach<?php
23cfcc809SGreg Roach
384e2cf4eSGreg Roach/**
484e2cf4eSGreg Roach * webtrees: online genealogy
589f7189bSGreg 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
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;
25*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\CacheFactory;
26*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\FamilyFactory;
27*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\FilesystemFactory;
28*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\ElementFactory;
29*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\GedcomRecordFactory;
30*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\HeaderFactory;
31*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\IndividualFactory;
32*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\LocationFactory;
33*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\MediaFactory;
34*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\NoteFactory;
35*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\RepositoryFactory;
36*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\SlugFactory;
37*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\SourceFactory;
38*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\SubmissionFactory;
39*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\SubmitterFactory;
40*00c45d23SGreg Roachuse Fisharebest\Webtrees\Factories\XrefFactory;
416fd01894SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\GedcomLoad;
422279b4f3SGreg Roachuse Fisharebest\Webtrees\Http\Routes\WebRoutes;
438136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
448136679eSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme;
458c3e1068SGreg Roachuse Fisharebest\Webtrees\Services\MigrationService;
4671378461SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
47126654d7SGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService;
481e653452SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
490115bc16SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
50*00c45d23SGreg Roachuse Nyholm\Psr7\Factory\Psr17Factory;
51*00c45d23SGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
52*00c45d23SGreg Roachuse Psr\Http\Message\ServerRequestFactoryInterface;
536ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
546ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
556ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileFactoryInterface;
566ccdf4f0SGreg Roachuse Psr\Http\Message\UploadedFileInterface;
57*00c45d23SGreg Roachuse Psr\Http\Message\UriFactoryInterface;
5871378461SGreg Roach
596ccdf4f0SGreg Roachuse function app;
607def76c7SGreg Roachuse function basename;
616ccdf4f0SGreg Roachuse function filesize;
626ccdf4f0SGreg Roachuse function http_build_query;
6357ab2231SGreg Roachuse function microtime;
6471378461SGreg Roach
656ccdf4f0SGreg Roachuse const UPLOAD_ERR_OK;
660115bc16SGreg Roach
6784e2cf4eSGreg Roach/**
6884e2cf4eSGreg Roach * Base class for unit tests
6984e2cf4eSGreg Roach */
7071378461SGreg Roachclass TestCase extends \PHPUnit\Framework\TestCase
7184e2cf4eSGreg Roach{
7274d6dc0eSGreg Roach    /** @var object */
7374d6dc0eSGreg Roach    public static $mock_functions;
7457ab2231SGreg Roach    /** @var bool */
7557ab2231SGreg Roach    protected static $uses_database = false;
7674d6dc0eSGreg Roach
77061b43d7SGreg Roach    /**
78061b43d7SGreg Roach     * Things to run once, before all the tests.
79061b43d7SGreg Roach     */
805e933c21SGreg Roach    public static function setUpBeforeClass(): void
81061b43d7SGreg Roach    {
82061b43d7SGreg Roach        parent::setUpBeforeClass();
83061b43d7SGreg Roach
84*00c45d23SGreg Roach        // Use nyholm as our PSR7 factory
85*00c45d23SGreg Roach        app()->bind(ResponseFactoryInterface::class, Psr17Factory::class);
86*00c45d23SGreg Roach        app()->bind(ServerRequestFactoryInterface::class, Psr17Factory::class);
87*00c45d23SGreg Roach        app()->bind(StreamFactoryInterface::class, Psr17Factory::class);
88*00c45d23SGreg Roach        app()->bind(UploadedFileFactoryInterface::class, Psr17Factory::class);
89*00c45d23SGreg Roach        app()->bind(UriFactoryInterface::class, Psr17Factory::class);
90*00c45d23SGreg Roach
91*00c45d23SGreg Roach        // Register the factories
92*00c45d23SGreg Roach        Registry::cache(new CacheFactory());
93*00c45d23SGreg Roach        Registry::familyFactory(new FamilyFactory());
94*00c45d23SGreg Roach        Registry::filesystem(new FilesystemFactory());
95*00c45d23SGreg Roach        Registry::elementFactory(new ElementFactory());
96*00c45d23SGreg Roach        Registry::gedcomRecordFactory(new GedcomRecordFactory());
97*00c45d23SGreg Roach        Registry::headerFactory(new HeaderFactory());
98*00c45d23SGreg Roach        Registry::individualFactory(new IndividualFactory());
99*00c45d23SGreg Roach        Registry::locationFactory(new LocationFactory());
100*00c45d23SGreg Roach        Registry::mediaFactory(new MediaFactory());
101*00c45d23SGreg Roach        Registry::noteFactory(new NoteFactory());
102*00c45d23SGreg Roach        Registry::repositoryFactory(new RepositoryFactory());
103*00c45d23SGreg Roach        Registry::slugFactory(new SlugFactory());
104*00c45d23SGreg Roach        Registry::sourceFactory(new SourceFactory());
105*00c45d23SGreg Roach        Registry::submissionFactory(new SubmissionFactory());
106*00c45d23SGreg Roach        Registry::submitterFactory(new SubmitterFactory());
107*00c45d23SGreg Roach        Registry::xrefFactory(new XrefFactory());
1086ccdf4f0SGreg Roach
10957ab2231SGreg Roach        app()->bind(ModuleThemeInterface::class, WebtreesTheme::class);
1106ccdf4f0SGreg Roach
111ee4364daSGreg Roach        // Need the routing table, to generate URLs.
1122279b4f3SGreg Roach        $router_container = new RouterContainer('/');
1132279b4f3SGreg Roach        (new WebRoutes())->load($router_container->getMap());
1142279b4f3SGreg Roach        app()->instance(RouterContainer::class, $router_container);
115ee4364daSGreg Roach
116150f35adSGreg Roach        I18N::init('en-US', true);
1176ccdf4f0SGreg Roach
118061b43d7SGreg Roach        if (static::$uses_database) {
119061b43d7SGreg Roach            static::createTestDatabase();
12071378461SGreg Roach
12171378461SGreg Roach            // Boot modules
12271378461SGreg Roach            (new ModuleService())->bootModules(new WebtreesTheme());
123061b43d7SGreg Roach        }
124061b43d7SGreg Roach    }
125061b43d7SGreg Roach
126061b43d7SGreg Roach    /**
12771378461SGreg Roach     * Things to run once, AFTER all the tests.
12871378461SGreg Roach     */
1295e933c21SGreg Roach    public static function tearDownAfterClass(): void
13071378461SGreg Roach    {
13171378461SGreg Roach        if (static::$uses_database) {
13271378461SGreg Roach            $pdo = DB::connection()->getPdo();
13371378461SGreg Roach            unset($pdo);
13471378461SGreg Roach        }
13571378461SGreg Roach
13671378461SGreg Roach        parent::tearDownAfterClass();
13771378461SGreg Roach    }
13871378461SGreg Roach
13971378461SGreg Roach    /**
1406ccdf4f0SGreg Roach     * Create an SQLite in-memory database for testing
1416ccdf4f0SGreg Roach     */
1426ccdf4f0SGreg Roach    protected static function createTestDatabase(): void
1436ccdf4f0SGreg Roach    {
1446ccdf4f0SGreg Roach        $capsule = new DB();
1456ccdf4f0SGreg Roach        $capsule->addConnection([
1466ccdf4f0SGreg Roach            'driver'   => 'sqlite',
1476ccdf4f0SGreg Roach            'database' => ':memory:',
1486ccdf4f0SGreg Roach        ]);
1496ccdf4f0SGreg Roach        $capsule->setAsGlobal();
150e16a1bfdSGreg Roach
1516ccdf4f0SGreg Roach        // Migrations create logs, which requires an IP address, which requires a request
1526ccdf4f0SGreg Roach        self::createRequest();
1536ccdf4f0SGreg Roach
1546ccdf4f0SGreg Roach        // Create tables
1553cfcc809SGreg Roach        $migration_service = new MigrationService();
1566ccdf4f0SGreg Roach        $migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
1576ccdf4f0SGreg Roach
1586ccdf4f0SGreg Roach        // Create config data
1596ccdf4f0SGreg Roach        $migration_service->seedDatabase();
1606ccdf4f0SGreg Roach    }
1616ccdf4f0SGreg Roach
1626ccdf4f0SGreg Roach    /**
16357ab2231SGreg Roach     * Create a request and bind it into the container.
16457ab2231SGreg Roach     *
16557ab2231SGreg Roach     * @param string                  $method
16657ab2231SGreg Roach     * @param string[]                $query
16757ab2231SGreg Roach     * @param string[]                $params
16857ab2231SGreg Roach     * @param UploadedFileInterface[] $files
169b3a775f6SGreg Roach     * @param string[]                $attributes
17057ab2231SGreg Roach     *
17157ab2231SGreg Roach     * @return ServerRequestInterface
17257ab2231SGreg Roach     */
1731a218474SGreg Roach    protected static function createRequest(
1741a218474SGreg Roach        string $method = RequestMethodInterface::METHOD_GET,
1751a218474SGreg Roach        array $query = [],
1761a218474SGreg Roach        array $params = [],
177b3a775f6SGreg Roach        array $files = [],
178b3a775f6SGreg Roach        array $attributes = []
1791a218474SGreg Roach    ): ServerRequestInterface {
180*00c45d23SGreg Roach        /** @var ServerRequestFactoryInterface */
181*00c45d23SGreg Roach        $server_request_factory = app(ServerRequestFactoryInterface::class);
182*00c45d23SGreg Roach
18357ab2231SGreg Roach        $uri = 'https://webtrees.test/index.php?' . http_build_query($query);
18457ab2231SGreg Roach
185*00c45d23SGreg Roach        /** @var ServerRequestInterface $request */
186*00c45d23SGreg Roach        $request = $server_request_factory
18757ab2231SGreg Roach            ->createServerRequest($method, $uri)
18857ab2231SGreg Roach            ->withQueryParams($query)
18957ab2231SGreg Roach            ->withParsedBody($params)
19057ab2231SGreg Roach            ->withUploadedFiles($files)
19157ab2231SGreg Roach            ->withAttribute('base_url', 'https://webtrees.test')
19290a2f718SGreg Roach            ->withAttribute('client-ip', '127.0.0.1')
193b5f5afdbSGreg Roach            ->withAttribute('user', new GuestUser())
194de2aa325SGreg Roach            ->withAttribute('route', new Route());
195b3a775f6SGreg Roach
196b3a775f6SGreg Roach        foreach ($attributes as $key => $value) {
197b3a775f6SGreg Roach            $request = $request->withAttribute($key, $value);
198b3a775f6SGreg Roach
199b3a775f6SGreg Roach            if ($key === 'tree') {
200b3a775f6SGreg Roach                app()->instance(Tree::class, $value);
201b3a775f6SGreg Roach            }
202b3a775f6SGreg Roach        }
20357ab2231SGreg Roach
20457ab2231SGreg Roach        app()->instance(ServerRequestInterface::class, $request);
20557ab2231SGreg Roach
20657ab2231SGreg Roach        return $request;
20757ab2231SGreg Roach    }
20857ab2231SGreg Roach
20957ab2231SGreg Roach    /**
210061b43d7SGreg Roach     * Things to run before every test.
211061b43d7SGreg Roach     */
2125c48bcd6SGreg Roach    protected function setUp(): void
2130115bc16SGreg Roach    {
2140115bc16SGreg Roach        parent::setUp();
2150115bc16SGreg Roach
216061b43d7SGreg Roach        if (static::$uses_database) {
217061b43d7SGreg Roach            DB::connection()->beginTransaction();
218061b43d7SGreg Roach        }
219061b43d7SGreg Roach    }
220061b43d7SGreg Roach
221061b43d7SGreg Roach    /**
222061b43d7SGreg Roach     * Things to run after every test
223061b43d7SGreg Roach     */
2245e933c21SGreg Roach    protected function tearDown(): void
225a49feabaSGreg Roach    {
22632f20c14SGreg Roach        if (static::$uses_database) {
227061b43d7SGreg Roach            DB::connection()->rollBack();
228061b43d7SGreg Roach        }
22932f20c14SGreg Roach
23032f20c14SGreg Roach        Site::$preferences = [];
23132f20c14SGreg Roach
23232f20c14SGreg Roach        Auth::logout();
2330115bc16SGreg Roach    }
2340115bc16SGreg Roach
2350115bc16SGreg Roach    /**
2360115bc16SGreg Roach     * Import a GEDCOM file into the test database.
2370115bc16SGreg Roach     *
2380115bc16SGreg Roach     * @param string $gedcom_file
239061b43d7SGreg Roach     *
240061b43d7SGreg Roach     * @return Tree
2410115bc16SGreg Roach     */
242061b43d7SGreg Roach    protected function importTree(string $gedcom_file): Tree
2430115bc16SGreg Roach    {
2441e653452SGreg Roach        $tree_service = new TreeService();
2451e653452SGreg Roach        $tree         = $tree_service->create(basename($gedcom_file), basename($gedcom_file));
246*00c45d23SGreg Roach        $stream       = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file);
2471e653452SGreg Roach
2485cd281f4SGreg Roach        $tree_service->importGedcomFile($tree, $stream, $gedcom_file);
2490115bc16SGreg Roach
25057ab2231SGreg Roach        $timeout_service = new TimeoutService(microtime(true));
2515cd281f4SGreg Roach        $controller      = new GedcomLoad($timeout_service, $tree_service);
25257ab2231SGreg Roach        $request         = self::createRequest()->withAttribute('tree', $tree);
253126654d7SGreg Roach
254126654d7SGreg Roach        do {
2556fd01894SGreg Roach            $controller->handle($request);
256126654d7SGreg Roach
257126654d7SGreg Roach            $imported = $tree->getPreference('imported');
258126654d7SGreg Roach        } while (!$imported);
259061b43d7SGreg Roach
260061b43d7SGreg Roach        return $tree;
2610115bc16SGreg Roach    }
2626ccdf4f0SGreg Roach
2636ccdf4f0SGreg Roach    /**
2646ccdf4f0SGreg Roach     * Create an uploaded file for a request.
2656ccdf4f0SGreg Roach     *
2666ccdf4f0SGreg Roach     * @param string $filename
2676ccdf4f0SGreg Roach     * @param string $mime_type
2686ccdf4f0SGreg Roach     *
2696ccdf4f0SGreg Roach     * @return UploadedFileInterface
2706ccdf4f0SGreg Roach     */
2716ccdf4f0SGreg Roach    protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface
2726ccdf4f0SGreg Roach    {
273*00c45d23SGreg Roach        /** @var StreamFactoryInterface */
274*00c45d23SGreg Roach        $stream_factory = app(StreamFactoryInterface::class);
275*00c45d23SGreg Roach
276*00c45d23SGreg Roach        /** @var UploadedFileFactoryInterface */
277*00c45d23SGreg Roach        $uploaded_file_factory = app(UploadedFileFactoryInterface::class);
278*00c45d23SGreg Roach
279*00c45d23SGreg Roach        $stream      = $stream_factory->createStreamFromFile($filename);
2806ccdf4f0SGreg Roach        $size        = filesize($filename);
2816ccdf4f0SGreg Roach        $status      = UPLOAD_ERR_OK;
2826ccdf4f0SGreg Roach        $client_name = basename($filename);
2836ccdf4f0SGreg Roach
284*00c45d23SGreg Roach        return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type);
2856ccdf4f0SGreg Roach    }
28684e2cf4eSGreg Roach}
287