xref: /webtrees/tests/feature/EmbeddedVariablesTest.php (revision 4c8fd9a21a2f83b2d6f6ff708f3d81a9938d02f3)
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    /**
77     * @return void
78     */
79    public function testAllEmbeddedVariables(): void
80    {
81        $user_service = new UserService();
82
83        $user = $user_service->create('user', 'User', 'user@example.com', 'secret');
84        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
85        Auth::login($user);
86
87        $tree       = $this->importTree('demo.ged');
88        $request    = self::createRequest()->withAttribute('tree', $tree);
89        Registry::container()->set(ServerRequestInterface::class, $request);
90
91        $statistics = new Statistics(
92            new CenturyService(),
93            new ColorService(),
94            new CountryService(),
95            new ModuleService(),
96            $tree,
97            $user_service
98        );
99
100        // As member
101        $text = $statistics->embedTags('#getAllTagsTable#');
102        self::assertNotEquals('#getAllTagsTable#', $text);
103
104        // As visitor
105        $text = $statistics->embedTags('#getAllTagsTable#');
106        self::assertNotEquals('#getAllTagsTable#', $text);
107    }
108
109    /**
110     * @return void
111     */
112    public function testAllEmbeddedVariablesWithEmptyTree(): void
113    {
114        $gedcom_import_service = new GedcomImportService();
115        $tree_service          = new TreeService($gedcom_import_service);
116        $tree                  = $tree_service->create('name', 'title');
117        $statistics            = new Statistics(
118            new CenturyService(),
119            new ColorService(),
120            new CountryService(),
121            new ModuleService(),
122            $tree,
123            new UserService()
124        );
125
126        // As visitor
127        $text = $statistics->embedTags('#getAllTagsTable#');
128        self::assertNotEquals('#getAllTagsTable#', $text);
129
130        // As member
131        $user = (new UserService())->create('user', 'User', 'user@example.com', 'secret');
132        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
133        Auth::login($user);
134
135        $text = $statistics->embedTags('#getAllTagsTable#');
136        self::assertNotEquals('#getAllTagsTable#', $text);
137    }
138}
139