xref: /webtrees/tests/feature/EmbeddedVariablesTest.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
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#[CoversClass(Statistics::class)]
67#[CoversClass(BrowserRepository::class)]
68#[CoversClass(ServerRepository::class)]
69#[CoversClass(LatestUserRepository::class)]
70#[CoversClass(FamilyDatesRepository::class)]
71#[CoversClass(HitCountRepository::class)]
72#[CoversClass(NewsRepository::class)]
73#[CoversClass(FavoritesRepository::class)]
74#[CoversClass(IndividualRepository::class)]
75#[CoversClass(MediaRepository::class)]
76#[CoversClass(MessageRepository::class)]
77#[CoversClass(ContactRepository::class)]
78#[CoversClass(GedcomRepository::class)]
79#[CoversClass(FamilyRepository::class)]
80#[CoversClass(EventRepository::class)]
81#[CoversClass(PlaceRepository::class)]
82#[CoversClass(UserRepository::class)]
83#[CoversClass(ChartChildren::class)]
84#[CoversClass(ChartAge::class)]
85#[CoversClass(ChartCommonGiven::class)]
86#[CoversClass(ChartMarriageAge::class)]
87#[CoversClass(ChartCommonSurname::class)]
88#[CoversClass(ChartDistribution::class)]
89#[CoversClass(ChartFamilyLargest::class)]
90#[CoversClass(ChartNoChildrenFamilies::class)]
91#[CoversClass(ChartSex::class)]
92#[CoversClass(ChartMedia::class)]
93#[CoversClass(ChartMarriage::class)]
94#[CoversClass(ChartFamilyWithSources::class)]
95#[CoversClass(ChartMortality::class)]
96#[CoversClass(ChartDeath::class)]
97#[CoversClass(ChartIndividualWithSources::class)]
98#[CoversClass(ChartBirth::class)]
99#[CoversClass(ChartDivorce::class)]
100#[CoversClass(CountryService::class)]
101#[CoversClass(CenturyService::class)]
102class EmbeddedVariablesTest extends TestCase
103{
104    protected static bool $uses_database = true;
105
106    public function testAllEmbeddedVariables(): void
107    {
108        $user_service = new UserService();
109
110        $user = $user_service->create('user', 'User', 'user@example.com', 'secret');
111        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
112        Auth::login($user);
113
114        $tree       = $this->importTree('demo.ged');
115        $request    = self::createRequest()->withAttribute('tree', $tree);
116        Registry::container()->set(ServerRequestInterface::class, $request);
117
118        $statistics = new Statistics(
119            new CenturyService(),
120            new ColorService(),
121            new CountryService(),
122            new ModuleService(),
123            $tree,
124            $user_service
125        );
126
127        // As member
128        $text = $statistics->embedTags('#getAllTagsTable#');
129        self::assertNotEquals('#getAllTagsTable#', $text);
130
131        // As visitor
132        $text = $statistics->embedTags('#getAllTagsTable#');
133        self::assertNotEquals('#getAllTagsTable#', $text);
134    }
135
136    public function testAllEmbeddedVariablesWithEmptyTree(): void
137    {
138        $gedcom_import_service = new GedcomImportService();
139        $tree_service          = new TreeService($gedcom_import_service);
140        $tree                  = $tree_service->create('name', 'title');
141        $statistics            = new Statistics(
142            new CenturyService(),
143            new ColorService(),
144            new CountryService(),
145            new ModuleService(),
146            $tree,
147            new UserService()
148        );
149
150        // As visitor
151        $text = $statistics->embedTags('#getAllTagsTable#');
152        self::assertNotEquals('#getAllTagsTable#', $text);
153
154        // As member
155        $user = (new UserService())->create('user', 'User', 'user@example.com', 'secret');
156        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
157        Auth::login($user);
158
159        $text = $statistics->embedTags('#getAllTagsTable#');
160        self::assertNotEquals('#getAllTagsTable#', $text);
161    }
162}
163