xref: /webtrees/tests/feature/Privacy.php (revision 928e8f1ab8bf8a4eae9e8ec7a464d6b65ee174d5)
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\UserService;
24
25use function strip_tags;
26
27/**
28 * Test the privacy logic
29 */
30class Privacy extends TestCase
31{
32    protected static bool $uses_database = true;
33
34    public function testRecordAccess(): void
35    {
36        $tree = $this->importTree('demo.ged');
37
38        // Identify some individuals in the tree
39        $queen_elizabeth = Registry::individualFactory()->make('X1030', $tree);
40        self::assertInstanceOf(Individual::class, $queen_elizabeth);
41        self::assertSame('Queen Elizabeth II', strip_tags($queen_elizabeth->fullName()));
42
43        $prince_charles = Registry::individualFactory()->make('X1052', $tree);
44        self::assertInstanceOf(Individual::class, $prince_charles);
45        self::assertSame('Charles, Prince of Wales', strip_tags($prince_charles->fullName()));
46
47        $savannah = Registry::individualFactory()->make('X1044', $tree);
48        self::assertInstanceOf(Individual::class, $savannah);
49        self::assertSame('Savannah Anne Kathleen Phillips', strip_tags($savannah->fullName()));
50
51        $beatrice = Registry::individualFactory()->make('X1047', $tree);
52        self::assertInstanceOf(Individual::class, $beatrice);
53        self::assertSame('Princess Beatrice of York', strip_tags($beatrice->fullName()));
54
55        $user_service = new UserService();
56
57        $admin = $user_service->create('admin', 'admin', 'admin', '*');
58        $admin->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
59
60        $manager = $user_service->create('manager', 'manager', 'manager', '*');
61        $tree->setUserPreference($manager, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MANAGER);
62
63        $moderator = $user_service->create('moderator', 'moderator', 'moderator', '*');
64        $tree->setUserPreference($moderator, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MODERATOR);
65
66        $editor = $user_service->create('editor', 'editor', 'editor', '*');
67        $tree->setUserPreference($editor, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_EDITOR);
68
69        $member = $user_service->create('member', 'member', 'member', '*');
70        $tree->setUserPreference($member, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MEMBER);
71
72        $visitor = $user_service->create('visitor', 'visitor', 'visitor', '*');
73        $tree->setUserPreference($visitor, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_VISITOR);
74
75        // Enable privacy functions
76        $tree->setPreference('HIDE_LIVE_PEOPLE', '1');
77
78        Auth::login($admin);
79        self::assertTrue(Auth::isAdmin(), 'admin isAdmin()');
80        self::assertTrue(Auth::isManager($tree), 'admin isManager()');
81        self::assertTrue(Auth::isModerator($tree), 'admin isModerator()');
82        self::assertTrue(Auth::isEditor($tree), 'admin isEditor()');
83        self::assertTrue(Auth::isMember($tree), 'admin isMember()');
84
85        Auth::login($manager);
86        self::assertFalse(Auth::isAdmin(), 'manager NOT isAdmin()');
87        self::assertTrue(Auth::isManager($tree, $manager), 'manager isManager()');
88        self::assertTrue(Auth::isModerator($tree, $manager), 'manager isModerator()');
89        self::assertTrue(Auth::isEditor($tree, $manager), 'manager isEditor()');
90        self::assertTrue(Auth::isMember($tree, $manager), 'manager isMember()');
91
92        Auth::login($moderator);
93        self::assertFalse(Auth::isAdmin(), 'moderator NOT isAdmin()');
94        self::assertFalse(Auth::isManager($tree, $moderator), 'moderator NOT isManager()');
95        self::assertTrue(Auth::isModerator($tree, $moderator), 'moderator isModerator()');
96        self::assertTrue(Auth::isEditor($tree, $moderator), 'moderator isEditor()');
97        self::assertTrue(Auth::isMember($tree, $moderator), 'moderator isMember()');
98
99        Auth::login($editor);
100        self::assertFalse(Auth::isAdmin(), 'editor NOT isAdmin()');
101        self::assertFalse(Auth::isManager($tree, $editor), 'editor NOT isManager()');
102        self::assertFalse(Auth::isModerator($tree, $editor), 'editor isModerator()');
103        self::assertTrue(Auth::isEditor($tree, $editor), 'editor isEditor()');
104        self::assertTrue(Auth::isMember($tree, $editor), 'editor isMember()');
105
106        Auth::login($member);
107        self::assertFalse(Auth::isAdmin(), 'member NOT isAdmin()');
108        self::assertFalse(Auth::isManager($tree, $member), 'member NOT isManager()');
109        self::assertFalse(Auth::isModerator($tree, $member), 'member isModerator()');
110        self::assertFalse(Auth::isEditor($tree, $member), 'member isEditor()');
111        self::assertTrue(Auth::isMember($tree, $member), 'member isMember()');
112
113        Auth::login($visitor);
114        self::assertFalse(Auth::isAdmin(), 'visitor NOT isAdmin()');
115        self::assertFalse(Auth::isManager($tree, $visitor), 'visitor NOT isManager()');
116        self::assertFalse(Auth::isModerator($tree, $visitor), 'visitor isModerator()');
117        self::assertFalse(Auth::isEditor($tree, $visitor), 'visitor isEditor()');
118        self::assertFalse(Auth::isMember($tree, $visitor), 'visitor isMember()');
119
120        Auth::logout();
121
122
123
124        Auth::login($admin);
125        self::assertTrue($queen_elizabeth->canShow(), 'admin can see living individual with RESN=none');
126        self::assertTrue($prince_charles->canShow(), 'admin can see living individual');
127
128        Auth::login($manager);
129        self::assertTrue($queen_elizabeth->canShow(), 'manager can see living individual with RESN=none');
130        self::assertTrue($prince_charles->canShow(), 'manager can see living individual');
131
132        Auth::login($moderator);
133        self::assertTrue($queen_elizabeth->canShow(), 'moderator can see living individual with RESN=none');
134        self::assertTrue($prince_charles->canShow(), 'moderator can see living individual');
135
136        Auth::login($editor);
137        self::assertTrue($queen_elizabeth->canShow(), 'editor can see living individual with RESN=none');
138        self::assertTrue($prince_charles->canShow(), 'editor can see living individual');
139
140        Auth::login($member);
141        self::assertTrue($queen_elizabeth->canShow(), 'member can see living individual with RESN=none');
142        self::assertTrue($prince_charles->canShow(), 'member can see living individual');
143
144        Auth::login($visitor);
145        self::assertTrue($queen_elizabeth->canShow(), 'visitor can see living individual with RESN=none');
146        self::assertFalse($prince_charles->canShow(), 'visitor can not see living individual');
147
148        Auth::logout();
149        self::assertTrue($queen_elizabeth->canShow(), 'guest can see living individual with RESN=none');
150        self::assertFalse($prince_charles->canShow(), 'guest can not see living individual');
151
152        // Relationship privacy
153        Auth::login($member);
154        self::assertTrue($beatrice->canShow());
155        $tree->setUserPreference($member, UserInterface::PREF_TREE_ACCOUNT_XREF, $savannah->xref());
156        $tree->setUserPreference($member, UserInterface::PREF_TREE_PATH_LENGTH, '3');
157        self::assertFalse($beatrice->canShow());
158        $tree->setUserPreference($member, UserInterface::PREF_TREE_PATH_LENGTH, '4');
159        self::assertTrue($beatrice->canShow());
160    }
161}
162