xref: /webtrees/tests/feature/Privacy.php (revision 5a8afed46297e8105e3e5a33ce37e6a8e88bc79d)
1bb88f16bSGreg Roach<?php
2bb88f16bSGreg Roach
3bb88f16bSGreg Roach/**
4bb88f16bSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6bb88f16bSGreg Roach * This program is free software: you can redistribute it and/or modify
7bb88f16bSGreg Roach * it under the terms of the GNU General Public License as published by
8bb88f16bSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9bb88f16bSGreg Roach * (at your option) any later version.
10bb88f16bSGreg Roach * This program is distributed in the hope that it will be useful,
11bb88f16bSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12bb88f16bSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13bb88f16bSGreg Roach * GNU General Public License for more details.
14bb88f16bSGreg 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/>.
16bb88f16bSGreg Roach */
17bb88f16bSGreg Roach
18bb88f16bSGreg Roachdeclare(strict_types=1);
19bb88f16bSGreg Roach
20bb88f16bSGreg Roachnamespace Fisharebest\Webtrees;
21bb88f16bSGreg Roach
221fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
23bb88f16bSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
24bb88f16bSGreg Roach
25bb88f16bSGreg Roachuse function strip_tags;
26bb88f16bSGreg Roach
27bb88f16bSGreg Roachclass Privacy extends TestCase
28bb88f16bSGreg Roach{
29cd94ca66SGreg Roach    protected static bool $uses_database = true;
30bb88f16bSGreg Roach
31bb88f16bSGreg Roach    public function testRecordAccess(): void
32bb88f16bSGreg Roach    {
33bb88f16bSGreg Roach        $tree = $this->importTree('demo.ged');
34bb88f16bSGreg Roach
35bb88f16bSGreg Roach        // Identify some individuals in the tree
366b9cb339SGreg Roach        $queen_elizabeth = Registry::individualFactory()->make('X1030', $tree);
375e933c21SGreg Roach        self::assertInstanceOf(Individual::class, $queen_elizabeth);
385e933c21SGreg Roach        self::assertSame('Queen Elizabeth II', strip_tags($queen_elizabeth->fullName()));
39bb88f16bSGreg Roach
406b9cb339SGreg Roach        $prince_charles = Registry::individualFactory()->make('X1052', $tree);
415e933c21SGreg Roach        self::assertInstanceOf(Individual::class, $prince_charles);
425e933c21SGreg Roach        self::assertSame('Charles, Prince of Wales', strip_tags($prince_charles->fullName()));
43bb88f16bSGreg Roach
446b9cb339SGreg Roach        $savannah = Registry::individualFactory()->make('X1044', $tree);
455e933c21SGreg Roach        self::assertInstanceOf(Individual::class, $savannah);
465e933c21SGreg Roach        self::assertSame('Savannah Anne Kathleen Phillips', strip_tags($savannah->fullName()));
47bb88f16bSGreg Roach
486b9cb339SGreg Roach        $beatrice = Registry::individualFactory()->make('X1047', $tree);
495e933c21SGreg Roach        self::assertInstanceOf(Individual::class, $beatrice);
505e933c21SGreg Roach        self::assertSame('Princess Beatrice of York', strip_tags($beatrice->fullName()));
51bb88f16bSGreg Roach
52bb88f16bSGreg Roach        $user_service = new UserService();
53bb88f16bSGreg Roach
54bb88f16bSGreg Roach        $admin = $user_service->create('admin', 'admin', 'admin', '*');
551fe542e9SGreg Roach        $admin->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
56bb88f16bSGreg Roach
57bb88f16bSGreg Roach        $manager = $user_service->create('manager', 'manager', 'manager', '*');
581fe542e9SGreg Roach        $tree->setUserPreference($manager, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MANAGER);
59bb88f16bSGreg Roach
60bb88f16bSGreg Roach        $moderator = $user_service->create('moderator', 'moderator', 'moderator', '*');
611fe542e9SGreg Roach        $tree->setUserPreference($moderator, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MODERATOR);
62bb88f16bSGreg Roach
63bb88f16bSGreg Roach        $editor = $user_service->create('editor', 'editor', 'editor', '*');
641fe542e9SGreg Roach        $tree->setUserPreference($editor, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_EDITOR);
65bb88f16bSGreg Roach
66bb88f16bSGreg Roach        $member = $user_service->create('member', 'member', 'member', '*');
671fe542e9SGreg Roach        $tree->setUserPreference($member, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MEMBER);
68bb88f16bSGreg Roach
69bb88f16bSGreg Roach        $visitor = $user_service->create('visitor', 'visitor', 'visitor', '*');
701fe542e9SGreg Roach        $tree->setUserPreference($visitor, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_VISITOR);
71bb88f16bSGreg Roach
72bb88f16bSGreg Roach        // Enable privacy functions
73bb88f16bSGreg Roach        $tree->setPreference('HIDE_LIVE_PEOPLE', '1');
74bb88f16bSGreg Roach
75bb88f16bSGreg Roach        Auth::login($admin);
765e933c21SGreg Roach        self::assertTrue(Auth::isAdmin(), 'admin isAdmin()');
775e933c21SGreg Roach        self::assertTrue(Auth::isManager($tree), 'admin isManager()');
785e933c21SGreg Roach        self::assertTrue(Auth::isModerator($tree), 'admin isModerator()');
795e933c21SGreg Roach        self::assertTrue(Auth::isEditor($tree), 'admin isEditor()');
805e933c21SGreg Roach        self::assertTrue(Auth::isMember($tree), 'admin isMember()');
81bb88f16bSGreg Roach
82bb88f16bSGreg Roach        Auth::login($manager);
835e933c21SGreg Roach        self::assertFalse(Auth::isAdmin(), 'manager NOT isAdmin()');
845e933c21SGreg Roach        self::assertTrue(Auth::isManager($tree, $manager), 'manager isManager()');
855e933c21SGreg Roach        self::assertTrue(Auth::isModerator($tree, $manager), 'manager isModerator()');
865e933c21SGreg Roach        self::assertTrue(Auth::isEditor($tree, $manager), 'manager isEditor()');
875e933c21SGreg Roach        self::assertTrue(Auth::isMember($tree, $manager), 'manager isMember()');
88bb88f16bSGreg Roach
89bb88f16bSGreg Roach        Auth::login($moderator);
905e933c21SGreg Roach        self::assertFalse(Auth::isAdmin(), 'moderator NOT isAdmin()');
915e933c21SGreg Roach        self::assertFalse(Auth::isManager($tree, $moderator), 'moderator NOT isManager()');
925e933c21SGreg Roach        self::assertTrue(Auth::isModerator($tree, $moderator), 'moderator isModerator()');
935e933c21SGreg Roach        self::assertTrue(Auth::isEditor($tree, $moderator), 'moderator isEditor()');
945e933c21SGreg Roach        self::assertTrue(Auth::isMember($tree, $moderator), 'moderator isMember()');
95bb88f16bSGreg Roach
96bb88f16bSGreg Roach        Auth::login($editor);
975e933c21SGreg Roach        self::assertFalse(Auth::isAdmin(), 'editor NOT isAdmin()');
985e933c21SGreg Roach        self::assertFalse(Auth::isManager($tree, $editor), 'editor NOT isManager()');
995e933c21SGreg Roach        self::assertFalse(Auth::isModerator($tree, $editor), 'editor isModerator()');
1005e933c21SGreg Roach        self::assertTrue(Auth::isEditor($tree, $editor), 'editor isEditor()');
1015e933c21SGreg Roach        self::assertTrue(Auth::isMember($tree, $editor), 'editor isMember()');
102bb88f16bSGreg Roach
103bb88f16bSGreg Roach        Auth::login($member);
1045e933c21SGreg Roach        self::assertFalse(Auth::isAdmin(), 'member NOT isAdmin()');
1055e933c21SGreg Roach        self::assertFalse(Auth::isManager($tree, $member), 'member NOT isManager()');
1065e933c21SGreg Roach        self::assertFalse(Auth::isModerator($tree, $member), 'member isModerator()');
1075e933c21SGreg Roach        self::assertFalse(Auth::isEditor($tree, $member), 'member isEditor()');
1085e933c21SGreg Roach        self::assertTrue(Auth::isMember($tree, $member), 'member isMember()');
109bb88f16bSGreg Roach
110bb88f16bSGreg Roach        Auth::login($visitor);
1115e933c21SGreg Roach        self::assertFalse(Auth::isAdmin(), 'visitor NOT isAdmin()');
1125e933c21SGreg Roach        self::assertFalse(Auth::isManager($tree, $visitor), 'visitor NOT isManager()');
1135e933c21SGreg Roach        self::assertFalse(Auth::isModerator($tree, $visitor), 'visitor isModerator()');
1145e933c21SGreg Roach        self::assertFalse(Auth::isEditor($tree, $visitor), 'visitor isEditor()');
1155e933c21SGreg Roach        self::assertFalse(Auth::isMember($tree, $visitor), 'visitor isMember()');
116bb88f16bSGreg Roach
117bb88f16bSGreg Roach        Auth::logout();
118bb88f16bSGreg Roach
119bb88f16bSGreg Roach        Auth::login($admin);
1205e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'admin can see living individual with RESN=none');
1215e933c21SGreg Roach        self::assertTrue($prince_charles->canShow(), 'admin can see living individual');
122bb88f16bSGreg Roach
123bb88f16bSGreg Roach        Auth::login($manager);
1245e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'manager can see living individual with RESN=none');
1255e933c21SGreg Roach        self::assertTrue($prince_charles->canShow(), 'manager can see living individual');
126bb88f16bSGreg Roach
127bb88f16bSGreg Roach        Auth::login($moderator);
1285e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'moderator can see living individual with RESN=none');
1295e933c21SGreg Roach        self::assertTrue($prince_charles->canShow(), 'moderator can see living individual');
130bb88f16bSGreg Roach
131bb88f16bSGreg Roach        Auth::login($editor);
1325e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'editor can see living individual with RESN=none');
1335e933c21SGreg Roach        self::assertTrue($prince_charles->canShow(), 'editor can see living individual');
134bb88f16bSGreg Roach
135bb88f16bSGreg Roach        Auth::login($member);
1365e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'member can see living individual with RESN=none');
1375e933c21SGreg Roach        self::assertTrue($prince_charles->canShow(), 'member can see living individual');
138bb88f16bSGreg Roach
139bb88f16bSGreg Roach        Auth::login($visitor);
1405e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'visitor can see living individual with RESN=none');
1415e933c21SGreg Roach        self::assertFalse($prince_charles->canShow(), 'visitor can not see living individual');
142bb88f16bSGreg Roach
143bb88f16bSGreg Roach        Auth::logout();
1445e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'guest can see living individual with RESN=none');
1455e933c21SGreg Roach        self::assertFalse($prince_charles->canShow(), 'guest can not see living individual');
146bb88f16bSGreg Roach
147bb88f16bSGreg Roach        // Relationship privacy
148bb88f16bSGreg Roach        Auth::login($member);
1495e933c21SGreg Roach        self::assertTrue($beatrice->canShow());
1501fe542e9SGreg Roach        $tree->setUserPreference($member, UserInterface::PREF_TREE_ACCOUNT_XREF, $savannah->xref());
1511fe542e9SGreg Roach        $tree->setUserPreference($member, UserInterface::PREF_TREE_PATH_LENGTH, '3');
1525e933c21SGreg Roach        self::assertFalse($beatrice->canShow());
1531fe542e9SGreg Roach        $tree->setUserPreference($member, UserInterface::PREF_TREE_PATH_LENGTH, '4');
1545e933c21SGreg Roach        self::assertTrue($beatrice->canShow());
155bb88f16bSGreg Roach    }
156bb88f16bSGreg Roach}
157