xref: /webtrees/tests/feature/EmbeddedVariablesTest.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
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\Google\ChartAge;
28use Fisharebest\Webtrees\Statistics\Google\ChartBirth;
29use Fisharebest\Webtrees\Statistics\Google\ChartChildren;
30use Fisharebest\Webtrees\Statistics\Google\ChartCommonGiven;
31use Fisharebest\Webtrees\Statistics\Google\ChartCommonSurname;
32use Fisharebest\Webtrees\Statistics\Google\ChartDeath;
33use Fisharebest\Webtrees\Statistics\Google\ChartDistribution;
34use Fisharebest\Webtrees\Statistics\Google\ChartDivorce;
35use Fisharebest\Webtrees\Statistics\Google\ChartFamilyLargest;
36use Fisharebest\Webtrees\Statistics\Google\ChartFamilyWithSources;
37use Fisharebest\Webtrees\Statistics\Google\ChartIndividualWithSources;
38use Fisharebest\Webtrees\Statistics\Google\ChartMarriage;
39use Fisharebest\Webtrees\Statistics\Google\ChartMarriageAge;
40use Fisharebest\Webtrees\Statistics\Google\ChartMedia;
41use Fisharebest\Webtrees\Statistics\Google\ChartMortality;
42use Fisharebest\Webtrees\Statistics\Google\ChartNoChildrenFamilies;
43use Fisharebest\Webtrees\Statistics\Google\ChartSex;
44use Fisharebest\Webtrees\Statistics\Repository\BrowserRepository;
45use Fisharebest\Webtrees\Statistics\Repository\ContactRepository;
46use Fisharebest\Webtrees\Statistics\Repository\EventRepository;
47use Fisharebest\Webtrees\Statistics\Repository\FamilyDatesRepository;
48use Fisharebest\Webtrees\Statistics\Repository\FamilyRepository;
49use Fisharebest\Webtrees\Statistics\Repository\FavoritesRepository;
50use Fisharebest\Webtrees\Statistics\Repository\GedcomRepository;
51use Fisharebest\Webtrees\Statistics\Repository\HitCountRepository;
52use Fisharebest\Webtrees\Statistics\Repository\IndividualRepository;
53use Fisharebest\Webtrees\Statistics\Repository\LatestUserRepository;
54use Fisharebest\Webtrees\Statistics\Repository\MediaRepository;
55use Fisharebest\Webtrees\Statistics\Repository\MessageRepository;
56use Fisharebest\Webtrees\Statistics\Repository\NewsRepository;
57use Fisharebest\Webtrees\Statistics\Repository\PlaceRepository;
58use Fisharebest\Webtrees\Statistics\Repository\ServerRepository;
59use Fisharebest\Webtrees\Statistics\Repository\UserRepository;
60use Fisharebest\Webtrees\Statistics\Service\CenturyService;
61use Fisharebest\Webtrees\Statistics\Service\ColorService;
62use Fisharebest\Webtrees\Statistics\Service\CountryService;
63use PHPUnit\Framework\Attributes\CoversClass;
64use Psr\Http\Message\ServerRequestInterface;
65
66
67#[CoversClass(Statistics::class)]
68#[CoversClass(BrowserRepository::class)]
69#[CoversClass(ServerRepository::class)]
70#[CoversClass(LatestUserRepository::class)]
71#[CoversClass(FamilyDatesRepository::class)]
72#[CoversClass(HitCountRepository::class)]
73#[CoversClass(NewsRepository::class)]
74#[CoversClass(FavoritesRepository::class)]
75#[CoversClass(IndividualRepository::class)]
76#[CoversClass(MediaRepository::class)]
77#[CoversClass(MessageRepository::class)]
78#[CoversClass(ContactRepository::class)]
79#[CoversClass(GedcomRepository::class)]
80#[CoversClass(FamilyRepository::class)]
81#[CoversClass(EventRepository::class)]
82#[CoversClass(PlaceRepository::class)]
83#[CoversClass(UserRepository::class)]
84#[CoversClass(ChartChildren::class)]
85#[CoversClass(ChartAge::class)]
86#[CoversClass(ChartCommonGiven::class)]
87#[CoversClass(ChartMarriageAge::class)]
88#[CoversClass(ChartCommonSurname::class)]
89#[CoversClass(ChartDistribution::class)]
90#[CoversClass(ChartFamilyLargest::class)]
91#[CoversClass(ChartNoChildrenFamilies::class)]
92#[CoversClass(ChartSex::class)]
93#[CoversClass(ChartMedia::class)]
94#[CoversClass(ChartMarriage::class)]
95#[CoversClass(ChartFamilyWithSources::class)]
96#[CoversClass(ChartMortality::class)]
97#[CoversClass(ChartDeath::class)]
98#[CoversClass(ChartIndividualWithSources::class)]
99#[CoversClass(ChartBirth::class)]
100#[CoversClass(ChartDivorce::class)]
101#[CoversClass(CountryService::class)]
102#[CoversClass(CenturyService::class)]
103class EmbeddedVariablesTest extends TestCase
104{
105    protected static bool $uses_database = true;
106
107    public function testAllEmbeddedVariables(): void
108    {
109        $user_service = new UserService();
110
111        $user = $user_service->create('user', 'User', 'user@example.com', 'secret');
112        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
113        Auth::login($user);
114
115        $tree       = $this->importTree('demo.ged');
116        $request    = self::createRequest()->withAttribute('tree', $tree);
117        Registry::container()->set(ServerRequestInterface::class, $request);
118
119        $statistics = new Statistics(
120            new CenturyService(),
121            new ColorService(),
122            new CountryService(),
123            new ModuleService(),
124            $tree,
125            $user_service
126        );
127
128        // As member
129        $text = $statistics->embedTags('#getAllTagsTable#');
130        self::assertNotEquals('#getAllTagsTable#', $text);
131
132        // As visitor
133        $text = $statistics->embedTags('#getAllTagsTable#');
134        self::assertNotEquals('#getAllTagsTable#', $text);
135    }
136
137    public function testAllEmbeddedVariablesWithEmptyTree(): void
138    {
139        $gedcom_import_service = new GedcomImportService();
140        $tree_service          = new TreeService($gedcom_import_service);
141        $tree                  = $tree_service->create('name', 'title');
142        $statistics            = new Statistics(
143            new CenturyService(),
144            new ColorService(),
145            new CountryService(),
146            new ModuleService(),
147            $tree,
148            new UserService()
149        );
150
151        // As visitor
152        $text = $statistics->embedTags('#getAllTagsTable#');
153        self::assertNotEquals('#getAllTagsTable#', $text);
154
155        // As member
156        $user = (new UserService())->create('user', 'User', 'user@example.com', 'secret');
157        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
158        Auth::login($user);
159
160        $text = $statistics->embedTags('#getAllTagsTable#');
161        self::assertNotEquals('#getAllTagsTable#', $text);
162    }
163}
164