xref: /webtrees/tests/feature/EmbeddedVariablesTest.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
1fd9aba47SGreg Roach<?php
23976b470SGreg Roach
3fd9aba47SGreg Roach/**
4fd9aba47SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6fd9aba47SGreg Roach * This program is free software: you can redistribute it and/or modify
7fd9aba47SGreg Roach * it under the terms of the GNU General Public License as published by
8fd9aba47SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9fd9aba47SGreg Roach * (at your option) any later version.
10fd9aba47SGreg Roach * This program is distributed in the hope that it will be useful,
11fd9aba47SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12fd9aba47SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13fd9aba47SGreg Roach * GNU General Public License for more details.
14fd9aba47SGreg 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/>.
16fd9aba47SGreg Roach */
17fcfa147eSGreg Roach
18fd9aba47SGreg Roachdeclare(strict_types=1);
19fd9aba47SGreg Roach
20fd9aba47SGreg Roachnamespace Fisharebest\Webtrees;
21fd9aba47SGreg Roach
221fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
232c685d76SGreg Roachuse Fisharebest\Webtrees\Services\GedcomImportService;
24e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
251e653452SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
26e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
27*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartAge;
28*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartBirth;
29*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartChildren;
30*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartCommonGiven;
31*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartCommonSurname;
32*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartDeath;
33*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartDistribution;
34*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartDivorce;
35*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartFamilyLargest;
36*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartFamilyWithSources;
37*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartIndividualWithSources;
38*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartMarriage;
39*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartMarriageAge;
40*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartMedia;
41*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartMortality;
42*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartNoChildrenFamilies;
43*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Google\ChartSex;
44*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\BrowserRepository;
45*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\ContactRepository;
46*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\EventRepository;
47*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\FamilyDatesRepository;
48*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\FamilyRepository;
49*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\FavoritesRepository;
50*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\GedcomRepository;
51*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\HitCountRepository;
52*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\IndividualRepository;
53*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\LatestUserRepository;
54*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\MediaRepository;
55*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\MessageRepository;
56*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\NewsRepository;
57*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\PlaceRepository;
58*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\ServerRepository;
59*202c018bSGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\UserRepository;
60f78da678SGreg Roachuse Fisharebest\Webtrees\Statistics\Service\CenturyService;
61f78da678SGreg Roachuse Fisharebest\Webtrees\Statistics\Service\ColorService;
624c78e066SGreg Roachuse Fisharebest\Webtrees\Statistics\Service\CountryService;
63*202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
64cd94ca66SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
65e5a6b4d4SGreg Roach
66*202c018bSGreg Roach
67*202c018bSGreg Roach#[CoversClass(Statistics::class)]
68*202c018bSGreg Roach#[CoversClass(BrowserRepository::class)]
69*202c018bSGreg Roach#[CoversClass(ServerRepository::class)]
70*202c018bSGreg Roach#[CoversClass(LatestUserRepository::class)]
71*202c018bSGreg Roach#[CoversClass(FamilyDatesRepository::class)]
72*202c018bSGreg Roach#[CoversClass(HitCountRepository::class)]
73*202c018bSGreg Roach#[CoversClass(NewsRepository::class)]
74*202c018bSGreg Roach#[CoversClass(FavoritesRepository::class)]
75*202c018bSGreg Roach#[CoversClass(IndividualRepository::class)]
76*202c018bSGreg Roach#[CoversClass(MediaRepository::class)]
77*202c018bSGreg Roach#[CoversClass(MessageRepository::class)]
78*202c018bSGreg Roach#[CoversClass(ContactRepository::class)]
79*202c018bSGreg Roach#[CoversClass(GedcomRepository::class)]
80*202c018bSGreg Roach#[CoversClass(FamilyRepository::class)]
81*202c018bSGreg Roach#[CoversClass(EventRepository::class)]
82*202c018bSGreg Roach#[CoversClass(PlaceRepository::class)]
83*202c018bSGreg Roach#[CoversClass(UserRepository::class)]
84*202c018bSGreg Roach#[CoversClass(ChartChildren::class)]
85*202c018bSGreg Roach#[CoversClass(ChartAge::class)]
86*202c018bSGreg Roach#[CoversClass(ChartCommonGiven::class)]
87*202c018bSGreg Roach#[CoversClass(ChartMarriageAge::class)]
88*202c018bSGreg Roach#[CoversClass(ChartCommonSurname::class)]
89*202c018bSGreg Roach#[CoversClass(ChartDistribution::class)]
90*202c018bSGreg Roach#[CoversClass(ChartFamilyLargest::class)]
91*202c018bSGreg Roach#[CoversClass(ChartNoChildrenFamilies::class)]
92*202c018bSGreg Roach#[CoversClass(ChartSex::class)]
93*202c018bSGreg Roach#[CoversClass(ChartMedia::class)]
94*202c018bSGreg Roach#[CoversClass(ChartMarriage::class)]
95*202c018bSGreg Roach#[CoversClass(ChartFamilyWithSources::class)]
96*202c018bSGreg Roach#[CoversClass(ChartMortality::class)]
97*202c018bSGreg Roach#[CoversClass(ChartDeath::class)]
98*202c018bSGreg Roach#[CoversClass(ChartIndividualWithSources::class)]
99*202c018bSGreg Roach#[CoversClass(ChartBirth::class)]
100*202c018bSGreg Roach#[CoversClass(ChartDivorce::class)]
101*202c018bSGreg Roach#[CoversClass(CountryService::class)]
102*202c018bSGreg Roach#[CoversClass(CenturyService::class)]
10394caf646SGreg Roachclass EmbeddedVariablesTest extends TestCase
10494caf646SGreg Roach{
105cd94ca66SGreg Roach    protected static bool $uses_database = true;
10694caf646SGreg Roach
107e5a6b4d4SGreg Roach    public function testAllEmbeddedVariables(): void
108fd9aba47SGreg Roach    {
109cd94ca66SGreg Roach        $user_service = new UserService();
110e5a6b4d4SGreg Roach
111cd94ca66SGreg Roach        $user = $user_service->create('user', 'User', 'user@example.com', 'secret');
1121fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
113945057aeSGreg Roach        Auth::login($user);
114e5a6b4d4SGreg Roach
115cd94ca66SGreg Roach        $tree       = $this->importTree('demo.ged');
116cd94ca66SGreg Roach        $request    = self::createRequest()->withAttribute('tree', $tree);
117d35568b4SGreg Roach        Registry::container()->set(ServerRequestInterface::class, $request);
118cd94ca66SGreg Roach
119f78da678SGreg Roach        $statistics = new Statistics(
120f78da678SGreg Roach            new CenturyService(),
121f78da678SGreg Roach            new ColorService(),
122f78da678SGreg Roach            new CountryService(),
123f78da678SGreg Roach            new ModuleService(),
124f78da678SGreg Roach            $tree,
125f78da678SGreg Roach            $user_service
126f78da678SGreg Roach        );
127cd94ca66SGreg Roach
128cd94ca66SGreg Roach        // As member
129cd94ca66SGreg Roach        $text = $statistics->embedTags('#getAllTagsTable#');
130cd94ca66SGreg Roach        self::assertNotEquals('#getAllTagsTable#', $text);
131cd94ca66SGreg Roach
132cd94ca66SGreg Roach        // As visitor
13338ef8377SGreg Roach        $text = $statistics->embedTags('#getAllTagsTable#');
1345e933c21SGreg Roach        self::assertNotEquals('#getAllTagsTable#', $text);
135e5a6b4d4SGreg Roach    }
136e5a6b4d4SGreg Roach
137e5a6b4d4SGreg Roach    public function testAllEmbeddedVariablesWithEmptyTree(): void
138e5a6b4d4SGreg Roach    {
1392c685d76SGreg Roach        $gedcom_import_service = new GedcomImportService();
1402c685d76SGreg Roach        $tree_service          = new TreeService($gedcom_import_service);
1411e653452SGreg Roach        $tree                  = $tree_service->create('name', 'title');
142f78da678SGreg Roach        $statistics            = new Statistics(
143f78da678SGreg Roach            new CenturyService(),
144f78da678SGreg Roach            new ColorService(),
145f78da678SGreg Roach            new CountryService(),
146f78da678SGreg Roach            new ModuleService(),
147f78da678SGreg Roach            $tree,
148f78da678SGreg Roach            new UserService()
149f78da678SGreg Roach        );
150e5a6b4d4SGreg Roach
151945057aeSGreg Roach        // As visitor
1524a520496SGreg Roach        $text = $statistics->embedTags('#getAllTagsTable#');
1535e933c21SGreg Roach        self::assertNotEquals('#getAllTagsTable#', $text);
154945057aeSGreg Roach
155945057aeSGreg Roach        // As member
156945057aeSGreg Roach        $user = (new UserService())->create('user', 'User', 'user@example.com', 'secret');
1571fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
158945057aeSGreg Roach        Auth::login($user);
159e5a6b4d4SGreg Roach
16038ef8377SGreg Roach        $text = $statistics->embedTags('#getAllTagsTable#');
1615e933c21SGreg Roach        self::assertNotEquals('#getAllTagsTable#', $text);
162fd9aba47SGreg Roach    }
163fd9aba47SGreg Roach}
164