xref: /webtrees/tests/feature/EmbeddedVariablesTest.php (revision 2ebcf907ed34213f816592af04e6c160335d6311)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\ModuleService;
24use Fisharebest\Webtrees\Services\TreeService;
25use Fisharebest\Webtrees\Services\UserService;
26use Fisharebest\Webtrees\Statistics\Service\CenturyService;
27use Fisharebest\Webtrees\Statistics\Service\ColorService;
28use Fisharebest\Webtrees\Statistics\Service\CountryService;
29use Psr\Http\Message\ServerRequestInterface;
30
31/**
32 * Test the user functions
33 *
34 * @covers \Fisharebest\Webtrees\Statistics
35 * @covers \Fisharebest\Webtrees\Statistics\Repository\BrowserRepository
36 * @covers \Fisharebest\Webtrees\Statistics\Repository\ServerRepository
37 * @covers \Fisharebest\Webtrees\Statistics\Repository\LatestUserRepository
38 * @covers \Fisharebest\Webtrees\Statistics\Repository\FamilyDatesRepository
39 * @covers \Fisharebest\Webtrees\Statistics\Repository\HitCountRepository
40 * @covers \Fisharebest\Webtrees\Statistics\Repository\NewsRepository
41 * @covers \Fisharebest\Webtrees\Statistics\Repository\FavoritesRepository
42 * @covers \Fisharebest\Webtrees\Statistics\Repository\IndividualRepository
43 * @covers \Fisharebest\Webtrees\Statistics\Repository\MediaRepository
44 * @covers \Fisharebest\Webtrees\Statistics\Repository\MessageRepository
45 * @covers \Fisharebest\Webtrees\Statistics\Repository\ContactRepository
46 * @covers \Fisharebest\Webtrees\Statistics\Repository\GedcomRepository
47 * @covers \Fisharebest\Webtrees\Statistics\Repository\FamilyRepository
48 * @covers \Fisharebest\Webtrees\Statistics\Repository\EventRepository
49 * @covers \Fisharebest\Webtrees\Statistics\Repository\PlaceRepository
50 * @covers \Fisharebest\Webtrees\Statistics\Repository\UserRepository
51 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartChildren
52 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartAge
53 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartCommonGiven
54 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartMarriageAge
55 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartCommonSurname
56 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartDistribution
57 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartFamilyLargest
58 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartNoChildrenFamilies
59 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartSex
60 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartMedia
61 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartMarriage
62 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartFamilyWithSources
63 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartMortality
64 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartDeath
65 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartIndividualWithSources
66 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartBirth
67 * @covers \Fisharebest\Webtrees\Statistics\Google\ChartDivorce
68 * @covers \Fisharebest\Webtrees\Statistics\Service\CountryService
69 * @covers \Fisharebest\Webtrees\Statistics\Service\CenturyService
70 */
71class EmbeddedVariablesTest extends TestCase
72{
73    protected static bool $uses_database = true;
74
75    /**
76     * @return void
77     */
78    public function testAllEmbeddedVariables(): void
79    {
80        $user_service = new UserService();
81
82        $user = $user_service->create('user', 'User', 'user@example.com', 'secret');
83        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
84        Auth::login($user);
85
86        $tree       = $this->importTree('demo.ged');
87        $request    = self::createRequest()->withAttribute('tree', $tree);
88        Webtrees::set(ServerRequestInterface::class, $request);
89
90        $statistics = new Statistics(
91            new CenturyService(),
92            new ColorService(),
93            new CountryService(),
94            new ModuleService(),
95            $tree,
96            $user_service
97        );
98
99        // As member
100        $text = $statistics->embedTags('#getAllTagsTable#');
101        self::assertNotEquals('#getAllTagsTable#', $text);
102
103        // As visitor
104        $text = $statistics->embedTags('#getAllTagsTable#');
105        self::assertNotEquals('#getAllTagsTable#', $text);
106    }
107
108    /**
109     * @return void
110     */
111    public function testAllEmbeddedVariablesWithEmptyTree(): void
112    {
113        $tree_service = new TreeService();
114        $tree         = $tree_service->create('name', 'title');
115        $statistics   = new Statistics(
116            new CenturyService(),
117            new ColorService(),
118            new CountryService(),
119            new ModuleService(),
120            $tree,
121            new UserService()
122        );
123
124        // As visitor
125        $text = $statistics->embedTags('#getAllTagsTable#');
126        self::assertNotEquals('#getAllTagsTable#', $text);
127
128        // As member
129        $user = (new UserService())->create('user', 'User', 'user@example.com', 'secret');
130        $user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
131        Auth::login($user);
132
133        $text = $statistics->embedTags('#getAllTagsTable#');
134        self::assertNotEquals('#getAllTagsTable#', $text);
135    }
136}
137