xref: /webtrees/tests/feature/Privacy.php (revision bb88f16b1377db732afb12b8b709b3f5f899347a)
1*bb88f16bSGreg Roach<?php
2*bb88f16bSGreg Roach
3*bb88f16bSGreg Roach/**
4*bb88f16bSGreg Roach * webtrees: online genealogy
5*bb88f16bSGreg Roach * Copyright (C) 2019 webtrees development team
6*bb88f16bSGreg Roach * This program is free software: you can redistribute it and/or modify
7*bb88f16bSGreg Roach * it under the terms of the GNU General Public License as published by
8*bb88f16bSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*bb88f16bSGreg Roach * (at your option) any later version.
10*bb88f16bSGreg Roach * This program is distributed in the hope that it will be useful,
11*bb88f16bSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*bb88f16bSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*bb88f16bSGreg Roach * GNU General Public License for more details.
14*bb88f16bSGreg Roach * You should have received a copy of the GNU General Public License
15*bb88f16bSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*bb88f16bSGreg Roach */
17*bb88f16bSGreg Roach
18*bb88f16bSGreg Roachdeclare(strict_types=1);
19*bb88f16bSGreg Roach
20*bb88f16bSGreg Roachnamespace Fisharebest\Webtrees;
21*bb88f16bSGreg Roach
22*bb88f16bSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
23*bb88f16bSGreg Roach
24*bb88f16bSGreg Roachuse function strip_tags;
25*bb88f16bSGreg Roach
26*bb88f16bSGreg Roach/**
27*bb88f16bSGreg Roach * Test the privacy logic
28*bb88f16bSGreg Roach */
29*bb88f16bSGreg Roachclass Privacy extends TestCase
30*bb88f16bSGreg Roach{
31*bb88f16bSGreg Roach    protected static $uses_database = true;
32*bb88f16bSGreg Roach
33*bb88f16bSGreg Roach    /**
34*bb88f16bSGreg Roach     * @return void
35*bb88f16bSGreg Roach     */
36*bb88f16bSGreg Roach    public function testRecordAccess(): void
37*bb88f16bSGreg Roach    {
38*bb88f16bSGreg Roach        $tree = $this->importTree('demo.ged');
39*bb88f16bSGreg Roach
40*bb88f16bSGreg Roach        // Identify some individuals in the tree
41*bb88f16bSGreg Roach        $queen_elizabeth = Individual::getInstance('X1030', $tree);
42*bb88f16bSGreg Roach        $this->assertInstanceOf(Individual::class, $queen_elizabeth);
43*bb88f16bSGreg Roach        $this->assertSame('Queen Elizabeth II', strip_tags($queen_elizabeth->fullName()));
44*bb88f16bSGreg Roach
45*bb88f16bSGreg Roach        $prince_charles = Individual::getInstance('X1052', $tree);
46*bb88f16bSGreg Roach        $this->assertInstanceOf(Individual::class, $prince_charles);
47*bb88f16bSGreg Roach        $this->assertSame('Charles, Prince of Wales', strip_tags($prince_charles->fullName()));
48*bb88f16bSGreg Roach
49*bb88f16bSGreg Roach        $savannah = Individual::getInstance('X1044', $tree);
50*bb88f16bSGreg Roach        $this->assertInstanceOf(Individual::class, $savannah);
51*bb88f16bSGreg Roach        $this->assertSame('Savannah Anne Kathleen Phillips', strip_tags($savannah->fullName()));
52*bb88f16bSGreg Roach
53*bb88f16bSGreg Roach        $beatrice = Individual::getInstance('X1047', $tree);
54*bb88f16bSGreg Roach        $this->assertInstanceOf(Individual::class, $beatrice);
55*bb88f16bSGreg Roach        $this->assertSame('Princess Beatrice of York', strip_tags($beatrice->fullName()));
56*bb88f16bSGreg Roach
57*bb88f16bSGreg Roach        $user_service = new UserService();
58*bb88f16bSGreg Roach
59*bb88f16bSGreg Roach        $admin = $user_service->create('admin', 'admin', 'admin', '*');
60*bb88f16bSGreg Roach        $admin->setPreference(User::PREF_IS_ADMINISTRATOR, '1');
61*bb88f16bSGreg Roach
62*bb88f16bSGreg Roach        $manager = $user_service->create('manager', 'manager', 'manager', '*');
63*bb88f16bSGreg Roach        $tree->setUserPreference($manager, User::PREF_TREE_ROLE, User::ROLE_MANAGER);
64*bb88f16bSGreg Roach
65*bb88f16bSGreg Roach        $moderator = $user_service->create('moderator', 'moderator', 'moderator', '*');
66*bb88f16bSGreg Roach        $tree->setUserPreference($moderator, User::PREF_TREE_ROLE, User::ROLE_MODERATOR);
67*bb88f16bSGreg Roach
68*bb88f16bSGreg Roach        $editor = $user_service->create('editor', 'editor', 'editor', '*');
69*bb88f16bSGreg Roach        $tree->setUserPreference($editor, User::PREF_TREE_ROLE, User::ROLE_EDITOR);
70*bb88f16bSGreg Roach
71*bb88f16bSGreg Roach        $member = $user_service->create('member', 'member', 'member', '*');
72*bb88f16bSGreg Roach        $tree->setUserPreference($member, User::PREF_TREE_ROLE, User::ROLE_MEMBER);
73*bb88f16bSGreg Roach
74*bb88f16bSGreg Roach        $visitor = $user_service->create('visitor', 'visitor', 'visitor', '*');
75*bb88f16bSGreg Roach        $tree->setUserPreference($visitor, User::PREF_TREE_ROLE, User::ROLE_VISITOR);
76*bb88f16bSGreg Roach
77*bb88f16bSGreg Roach        // Enable privacy functions
78*bb88f16bSGreg Roach        $tree->setPreference('HIDE_LIVE_PEOPLE', '1');
79*bb88f16bSGreg Roach
80*bb88f16bSGreg Roach        Auth::login($admin);
81*bb88f16bSGreg Roach        $this->assertTrue(Auth::isAdmin(), 'admin isAdmin()');
82*bb88f16bSGreg Roach        $this->assertTrue(Auth::isManager($tree), 'admin isManager()');
83*bb88f16bSGreg Roach        $this->assertTrue(Auth::isModerator($tree), 'admin isModerator()');
84*bb88f16bSGreg Roach        $this->assertTrue(Auth::isEditor($tree), 'admin isEditor()');
85*bb88f16bSGreg Roach        $this->assertTrue(Auth::isMember($tree), 'admin isMember()');
86*bb88f16bSGreg Roach
87*bb88f16bSGreg Roach        Auth::login($manager);
88*bb88f16bSGreg Roach        $this->assertFalse(Auth::isAdmin(), 'manager NOT isAdmin()');
89*bb88f16bSGreg Roach        $this->assertTrue(Auth::isManager($tree, $manager), 'manager isManager()');
90*bb88f16bSGreg Roach        $this->assertTrue(Auth::isModerator($tree, $manager), 'manager isModerator()');
91*bb88f16bSGreg Roach        $this->assertTrue(Auth::isEditor($tree, $manager), 'manager isEditor()');
92*bb88f16bSGreg Roach        $this->assertTrue(Auth::isMember($tree, $manager), 'manager isMember()');
93*bb88f16bSGreg Roach
94*bb88f16bSGreg Roach        Auth::login($moderator);
95*bb88f16bSGreg Roach        $this->assertFalse(Auth::isAdmin(), 'moderator NOT isAdmin()');
96*bb88f16bSGreg Roach        $this->assertFalse(Auth::isManager($tree, $moderator), 'moderator NOT isManager()');
97*bb88f16bSGreg Roach        $this->assertTrue(Auth::isModerator($tree, $moderator), 'moderator isModerator()');
98*bb88f16bSGreg Roach        $this->assertTrue(Auth::isEditor($tree, $moderator), 'moderator isEditor()');
99*bb88f16bSGreg Roach        $this->assertTrue(Auth::isMember($tree, $moderator), 'moderator isMember()');
100*bb88f16bSGreg Roach
101*bb88f16bSGreg Roach        Auth::login($editor);
102*bb88f16bSGreg Roach        $this->assertFalse(Auth::isAdmin(), 'editor NOT isAdmin()');
103*bb88f16bSGreg Roach        $this->assertFalse(Auth::isManager($tree, $editor), 'editor NOT isManager()');
104*bb88f16bSGreg Roach        $this->assertFalse(Auth::isModerator($tree, $editor), 'editor isModerator()');
105*bb88f16bSGreg Roach        $this->assertTrue(Auth::isEditor($tree, $editor), 'editor isEditor()');
106*bb88f16bSGreg Roach        $this->assertTrue(Auth::isMember($tree, $editor), 'editor isMember()');
107*bb88f16bSGreg Roach
108*bb88f16bSGreg Roach        Auth::login($member);
109*bb88f16bSGreg Roach        $this->assertFalse(Auth::isAdmin(), 'member NOT isAdmin()');
110*bb88f16bSGreg Roach        $this->assertFalse(Auth::isManager($tree, $member), 'member NOT isManager()');
111*bb88f16bSGreg Roach        $this->assertFalse(Auth::isModerator($tree, $member), 'member isModerator()');
112*bb88f16bSGreg Roach        $this->assertFalse(Auth::isEditor($tree, $member), 'member isEditor()');
113*bb88f16bSGreg Roach        $this->assertTrue(Auth::isMember($tree, $member), 'member isMember()');
114*bb88f16bSGreg Roach
115*bb88f16bSGreg Roach        Auth::login($visitor);
116*bb88f16bSGreg Roach        $this->assertFalse(Auth::isAdmin(), 'visitor NOT isAdmin()');
117*bb88f16bSGreg Roach        $this->assertFalse(Auth::isManager($tree, $visitor), 'visitor NOT isManager()');
118*bb88f16bSGreg Roach        $this->assertFalse(Auth::isModerator($tree, $visitor), 'visitor isModerator()');
119*bb88f16bSGreg Roach        $this->assertFalse(Auth::isEditor($tree, $visitor), 'visitor isEditor()');
120*bb88f16bSGreg Roach        $this->assertFalse(Auth::isMember($tree, $visitor), 'visitor isMember()');
121*bb88f16bSGreg Roach
122*bb88f16bSGreg Roach        Auth::logout();
123*bb88f16bSGreg Roach
124*bb88f16bSGreg Roach
125*bb88f16bSGreg Roach
126*bb88f16bSGreg Roach        Auth::login($admin);
127*bb88f16bSGreg Roach        $this->assertTrue($queen_elizabeth->canShow(), 'admin can see living individual with RESN=none');
128*bb88f16bSGreg Roach        $this->assertTrue($prince_charles->canShow(), 'admin can see living individual');
129*bb88f16bSGreg Roach
130*bb88f16bSGreg Roach        Auth::login($manager);
131*bb88f16bSGreg Roach        $this->assertTrue($queen_elizabeth->canShow(), 'manager can see living individual with RESN=none');
132*bb88f16bSGreg Roach        $this->assertTrue($prince_charles->canShow(), 'manager can see living individual');
133*bb88f16bSGreg Roach
134*bb88f16bSGreg Roach        Auth::login($moderator);
135*bb88f16bSGreg Roach        $this->assertTrue($queen_elizabeth->canShow(), 'moderator can see living individual with RESN=none');
136*bb88f16bSGreg Roach        $this->assertTrue($prince_charles->canShow(), 'moderator can see living individual');
137*bb88f16bSGreg Roach
138*bb88f16bSGreg Roach        Auth::login($editor);
139*bb88f16bSGreg Roach        $this->assertTrue($queen_elizabeth->canShow(), 'editor can see living individual with RESN=none');
140*bb88f16bSGreg Roach        $this->assertTrue($prince_charles->canShow(), 'editor can see living individual');
141*bb88f16bSGreg Roach
142*bb88f16bSGreg Roach        Auth::login($member);
143*bb88f16bSGreg Roach        $this->assertTrue($queen_elizabeth->canShow(), 'member can see living individual with RESN=none');
144*bb88f16bSGreg Roach        $this->assertTrue($prince_charles->canShow(), 'member can see living individual');
145*bb88f16bSGreg Roach
146*bb88f16bSGreg Roach        Auth::login($visitor);
147*bb88f16bSGreg Roach        $this->assertTrue($queen_elizabeth->canShow(), 'visitor can see living individual with RESN=none');
148*bb88f16bSGreg Roach        $this->assertFalse($prince_charles->canShow(), 'visitor can not see living individual');
149*bb88f16bSGreg Roach
150*bb88f16bSGreg Roach        Auth::logout();
151*bb88f16bSGreg Roach        $this->assertTrue($queen_elizabeth->canShow(), 'guest can see living individual with RESN=none');
152*bb88f16bSGreg Roach        $this->assertFalse($prince_charles->canShow(), 'guest can not see living individual');
153*bb88f16bSGreg Roach
154*bb88f16bSGreg Roach        // Relationship privacy
155*bb88f16bSGreg Roach        Auth::login($member);
156*bb88f16bSGreg Roach        $this->assertTrue($beatrice->canShow());
157*bb88f16bSGreg Roach        $tree->setUserPreference($member, User::PREF_TREE_ACCOUNT_XREF, $savannah->xref());
158*bb88f16bSGreg Roach        $tree->setUserPreference($member, User::PREF_TREE_PATH_LENGTH, '3');
159*bb88f16bSGreg Roach        $this->assertFalse($beatrice->canShow());
160*bb88f16bSGreg Roach        $tree->setUserPreference($member, User::PREF_TREE_PATH_LENGTH, '4');
161*bb88f16bSGreg Roach        $this->assertTrue($beatrice->canShow());
162*bb88f16bSGreg Roach    }
163*bb88f16bSGreg Roach}
164