xref: /webtrees/tests/feature/EmbeddedVariablesTest.php (revision a2e04925bc20e9db3cb1f993405c8db01f5fb296)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Fisharebest\Webtrees\Contracts\UserInterface;
23use Fisharebest\Webtrees\Services\GedcomImportService;
24use Fisharebest\Webtrees\Services\ModuleService;
25use Fisharebest\Webtrees\Services\TreeService;
26use Fisharebest\Webtrees\Services\UserService;
27use Fisharebest\Webtrees\Statistics\Service\CenturyService;
28use Fisharebest\Webtrees\Statistics\Service\ColorService;
29use Fisharebest\Webtrees\Statistics\Service\CountryService;
30use Psr\Http\Message\ServerRequestInterface;
31
32/**
33 * Test the user functions
34 *
35 * @covers \Fisharebest\Webtrees\Statistics
36 * @covers \Fisharebest\Webtrees\Statistics\Repository\BrowserRepository
37 * @covers \Fisharebest\Webtrees\Statistics\Repository\ServerRepository
38 * @covers \Fisharebest\Webtrees\Statistics\Repository\LatestUserRepository
39 * @covers \Fisharebest\Webtrees\Statistics\Repository\FamilyDatesRepository
40 * @covers \Fisharebest\Webtrees\Statistics\Repository\HitCountRepository
41 * @covers \Fisharebest\Webtrees\Statistics\Repository\NewsRepository
42 * @covers \Fisharebest\Webtrees\Statistics\Repository\FavoritesRepository
43 * @covers \Fisharebest\Webtrees\Statistics\Repository\IndividualRepository
44 * @covers \Fisharebest\Webtrees\Statistics\Repository\MediaRepository
45 * @covers \Fisharebest\Webtrees\Statistics\Repository\MessageRepository
46 * @covers \Fisharebest\Webtrees\Statistics\Repository\ContactRepository
47 * @covers \Fisharebest\Webtrees\Statistics\Repository\GedcomRepository
48 * @covers \Fisharebest\Webtrees\Statistics\Repository\FamilyRepository
49 * @covers \Fisharebest\Webtrees\Statistics\Repository\EventRepository
50 * @covers \Fisharebest\Webtrees\Statistics\Repository\PlaceRepository
51 * @covers \Fisharebest\Webtrees\Statistics\Repository\UserRepository
52 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartChildren
53 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartAge
54 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartCommonGiven
55 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartMarriageAge
56 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartCommonSurname
57 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartDistribution
58 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartFamilyLargest
59 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartNoChildrenFamilies
60 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartSex
61 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartMedia
62 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartMarriage
63 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartFamilyWithSources
64 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartMortality
65 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartDeath
66 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartIndividualWithSources
67 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartBirth
68 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartDivorce
69 * @covers \Fisharebest\Webtrees\Statistics\Service\CountryService
70 * @covers \Fisharebest\Webtrees\Statistics\Service\CenturyService
71 */
72class EmbeddedVariablesTest extends TestCase
73{
74    protected static bool $uses_database = true;
75
76    public function testAllEmbeddedVariables(): void
77    {
78        $user_service = new UserService();
79
80        $user = $user_service->create('user', 'User', 'user@example.com', 'secret');
81        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
82        Auth::login($user);
83
84        $tree       = $this->importTree('demo.ged');
85        $request    = self::createRequest()->withAttribute('tree', $tree);
86        Registry::container()->set(ServerRequestInterface::class, $request);
87
88        $statistics = new Statistics(
89            new CenturyService(),
90            new ColorService(),
91            new CountryService(),
92            new ModuleService(),
93            $tree,
94            $user_service
95        );
96
97        // As member
98        $text = $statistics->embedTags('#getAllTagsTable#');
99        self::assertNotEquals('#getAllTagsTable#', $text);
100
101        // As visitor
102        $text = $statistics->embedTags('#getAllTagsTable#');
103        self::assertNotEquals('#getAllTagsTable#', $text);
104    }
105
106    public function testAllEmbeddedVariablesWithEmptyTree(): void
107    {
108        $gedcom_import_service = new GedcomImportService();
109        $tree_service          = new TreeService($gedcom_import_service);
110        $tree                  = $tree_service->create('name', 'title');
111        $statistics            = new Statistics(
112            new CenturyService(),
113            new ColorService(),
114            new CountryService(),
115            new ModuleService(),
116            $tree,
117            new UserService()
118        );
119
120        // As visitor
121        $text = $statistics->embedTags('#getAllTagsTable#');
122        self::assertNotEquals('#getAllTagsTable#', $text);
123
124        // As member
125        $user = (new UserService())->create('user', 'User', 'user@example.com', 'secret');
126        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
127        Auth::login($user);
128
129        $text = $statistics->embedTags('#getAllTagsTable#');
130        self::assertNotEquals('#getAllTagsTable#', $text);
131    }
132}
133