xref: /webtrees/tests/feature/Privacy.php (revision d11be7027e34e3121be11cc025421873364403f9)
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 Roach/**
28bb88f16bSGreg Roach * Test the privacy logic
29bb88f16bSGreg Roach */
30bb88f16bSGreg Roachclass Privacy extends TestCase
31bb88f16bSGreg Roach{
32cd94ca66SGreg Roach    protected static bool $uses_database = true;
33bb88f16bSGreg Roach
34bb88f16bSGreg Roach    /**
35bb88f16bSGreg Roach     * @return void
36bb88f16bSGreg Roach     */
37bb88f16bSGreg Roach    public function testRecordAccess(): void
38bb88f16bSGreg Roach    {
39bb88f16bSGreg Roach        $tree = $this->importTree('demo.ged');
40bb88f16bSGreg Roach
41bb88f16bSGreg Roach        // Identify some individuals in the tree
426b9cb339SGreg Roach        $queen_elizabeth = Registry::individualFactory()->make('X1030', $tree);
435e933c21SGreg Roach        self::assertInstanceOf(Individual::class, $queen_elizabeth);
445e933c21SGreg Roach        self::assertSame('Queen Elizabeth II', strip_tags($queen_elizabeth->fullName()));
45bb88f16bSGreg Roach
466b9cb339SGreg Roach        $prince_charles = Registry::individualFactory()->make('X1052', $tree);
475e933c21SGreg Roach        self::assertInstanceOf(Individual::class, $prince_charles);
485e933c21SGreg Roach        self::assertSame('Charles, Prince of Wales', strip_tags($prince_charles->fullName()));
49bb88f16bSGreg Roach
506b9cb339SGreg Roach        $savannah = Registry::individualFactory()->make('X1044', $tree);
515e933c21SGreg Roach        self::assertInstanceOf(Individual::class, $savannah);
525e933c21SGreg Roach        self::assertSame('Savannah Anne Kathleen Phillips', strip_tags($savannah->fullName()));
53bb88f16bSGreg Roach
546b9cb339SGreg Roach        $beatrice = Registry::individualFactory()->make('X1047', $tree);
555e933c21SGreg Roach        self::assertInstanceOf(Individual::class, $beatrice);
565e933c21SGreg Roach        self::assertSame('Princess Beatrice of York', strip_tags($beatrice->fullName()));
57bb88f16bSGreg Roach
58bb88f16bSGreg Roach        $user_service = new UserService();
59bb88f16bSGreg Roach
60bb88f16bSGreg Roach        $admin = $user_service->create('admin', 'admin', 'admin', '*');
611fe542e9SGreg Roach        $admin->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, '1');
62bb88f16bSGreg Roach
63bb88f16bSGreg Roach        $manager = $user_service->create('manager', 'manager', 'manager', '*');
641fe542e9SGreg Roach        $tree->setUserPreference($manager, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MANAGER);
65bb88f16bSGreg Roach
66bb88f16bSGreg Roach        $moderator = $user_service->create('moderator', 'moderator', 'moderator', '*');
671fe542e9SGreg Roach        $tree->setUserPreference($moderator, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MODERATOR);
68bb88f16bSGreg Roach
69bb88f16bSGreg Roach        $editor = $user_service->create('editor', 'editor', 'editor', '*');
701fe542e9SGreg Roach        $tree->setUserPreference($editor, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_EDITOR);
71bb88f16bSGreg Roach
72bb88f16bSGreg Roach        $member = $user_service->create('member', 'member', 'member', '*');
731fe542e9SGreg Roach        $tree->setUserPreference($member, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_MEMBER);
74bb88f16bSGreg Roach
75bb88f16bSGreg Roach        $visitor = $user_service->create('visitor', 'visitor', 'visitor', '*');
761fe542e9SGreg Roach        $tree->setUserPreference($visitor, UserInterface::PREF_TREE_ROLE, UserInterface::ROLE_VISITOR);
77bb88f16bSGreg Roach
78bb88f16bSGreg Roach        // Enable privacy functions
79bb88f16bSGreg Roach        $tree->setPreference('HIDE_LIVE_PEOPLE', '1');
80bb88f16bSGreg Roach
81bb88f16bSGreg Roach        Auth::login($admin);
825e933c21SGreg Roach        self::assertTrue(Auth::isAdmin(), 'admin isAdmin()');
835e933c21SGreg Roach        self::assertTrue(Auth::isManager($tree), 'admin isManager()');
845e933c21SGreg Roach        self::assertTrue(Auth::isModerator($tree), 'admin isModerator()');
855e933c21SGreg Roach        self::assertTrue(Auth::isEditor($tree), 'admin isEditor()');
865e933c21SGreg Roach        self::assertTrue(Auth::isMember($tree), 'admin isMember()');
87bb88f16bSGreg Roach
88bb88f16bSGreg Roach        Auth::login($manager);
895e933c21SGreg Roach        self::assertFalse(Auth::isAdmin(), 'manager NOT isAdmin()');
905e933c21SGreg Roach        self::assertTrue(Auth::isManager($tree, $manager), 'manager isManager()');
915e933c21SGreg Roach        self::assertTrue(Auth::isModerator($tree, $manager), 'manager isModerator()');
925e933c21SGreg Roach        self::assertTrue(Auth::isEditor($tree, $manager), 'manager isEditor()');
935e933c21SGreg Roach        self::assertTrue(Auth::isMember($tree, $manager), 'manager isMember()');
94bb88f16bSGreg Roach
95bb88f16bSGreg Roach        Auth::login($moderator);
965e933c21SGreg Roach        self::assertFalse(Auth::isAdmin(), 'moderator NOT isAdmin()');
975e933c21SGreg Roach        self::assertFalse(Auth::isManager($tree, $moderator), 'moderator NOT isManager()');
985e933c21SGreg Roach        self::assertTrue(Auth::isModerator($tree, $moderator), 'moderator isModerator()');
995e933c21SGreg Roach        self::assertTrue(Auth::isEditor($tree, $moderator), 'moderator isEditor()');
1005e933c21SGreg Roach        self::assertTrue(Auth::isMember($tree, $moderator), 'moderator isMember()');
101bb88f16bSGreg Roach
102bb88f16bSGreg Roach        Auth::login($editor);
1035e933c21SGreg Roach        self::assertFalse(Auth::isAdmin(), 'editor NOT isAdmin()');
1045e933c21SGreg Roach        self::assertFalse(Auth::isManager($tree, $editor), 'editor NOT isManager()');
1055e933c21SGreg Roach        self::assertFalse(Auth::isModerator($tree, $editor), 'editor isModerator()');
1065e933c21SGreg Roach        self::assertTrue(Auth::isEditor($tree, $editor), 'editor isEditor()');
1075e933c21SGreg Roach        self::assertTrue(Auth::isMember($tree, $editor), 'editor isMember()');
108bb88f16bSGreg Roach
109bb88f16bSGreg Roach        Auth::login($member);
1105e933c21SGreg Roach        self::assertFalse(Auth::isAdmin(), 'member NOT isAdmin()');
1115e933c21SGreg Roach        self::assertFalse(Auth::isManager($tree, $member), 'member NOT isManager()');
1125e933c21SGreg Roach        self::assertFalse(Auth::isModerator($tree, $member), 'member isModerator()');
1135e933c21SGreg Roach        self::assertFalse(Auth::isEditor($tree, $member), 'member isEditor()');
1145e933c21SGreg Roach        self::assertTrue(Auth::isMember($tree, $member), 'member isMember()');
115bb88f16bSGreg Roach
116bb88f16bSGreg Roach        Auth::login($visitor);
1175e933c21SGreg Roach        self::assertFalse(Auth::isAdmin(), 'visitor NOT isAdmin()');
1185e933c21SGreg Roach        self::assertFalse(Auth::isManager($tree, $visitor), 'visitor NOT isManager()');
1195e933c21SGreg Roach        self::assertFalse(Auth::isModerator($tree, $visitor), 'visitor isModerator()');
1205e933c21SGreg Roach        self::assertFalse(Auth::isEditor($tree, $visitor), 'visitor isEditor()');
1215e933c21SGreg Roach        self::assertFalse(Auth::isMember($tree, $visitor), 'visitor isMember()');
122bb88f16bSGreg Roach
123bb88f16bSGreg Roach        Auth::logout();
124bb88f16bSGreg Roach
125bb88f16bSGreg Roach
126bb88f16bSGreg Roach
127bb88f16bSGreg Roach        Auth::login($admin);
1285e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'admin can see living individual with RESN=none');
1295e933c21SGreg Roach        self::assertTrue($prince_charles->canShow(), 'admin can see living individual');
130bb88f16bSGreg Roach
131bb88f16bSGreg Roach        Auth::login($manager);
1325e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'manager can see living individual with RESN=none');
1335e933c21SGreg Roach        self::assertTrue($prince_charles->canShow(), 'manager can see living individual');
134bb88f16bSGreg Roach
135bb88f16bSGreg Roach        Auth::login($moderator);
1365e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'moderator can see living individual with RESN=none');
1375e933c21SGreg Roach        self::assertTrue($prince_charles->canShow(), 'moderator can see living individual');
138bb88f16bSGreg Roach
139bb88f16bSGreg Roach        Auth::login($editor);
1405e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'editor can see living individual with RESN=none');
1415e933c21SGreg Roach        self::assertTrue($prince_charles->canShow(), 'editor can see living individual');
142bb88f16bSGreg Roach
143bb88f16bSGreg Roach        Auth::login($member);
1445e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'member can see living individual with RESN=none');
1455e933c21SGreg Roach        self::assertTrue($prince_charles->canShow(), 'member can see living individual');
146bb88f16bSGreg Roach
147bb88f16bSGreg Roach        Auth::login($visitor);
1485e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'visitor can see living individual with RESN=none');
1495e933c21SGreg Roach        self::assertFalse($prince_charles->canShow(), 'visitor can not see living individual');
150bb88f16bSGreg Roach
151bb88f16bSGreg Roach        Auth::logout();
1525e933c21SGreg Roach        self::assertTrue($queen_elizabeth->canShow(), 'guest can see living individual with RESN=none');
1535e933c21SGreg Roach        self::assertFalse($prince_charles->canShow(), 'guest can not see living individual');
154bb88f16bSGreg Roach
155bb88f16bSGreg Roach        // Relationship privacy
156bb88f16bSGreg Roach        Auth::login($member);
1575e933c21SGreg Roach        self::assertTrue($beatrice->canShow());
1581fe542e9SGreg Roach        $tree->setUserPreference($member, UserInterface::PREF_TREE_ACCOUNT_XREF, $savannah->xref());
1591fe542e9SGreg Roach        $tree->setUserPreference($member, UserInterface::PREF_TREE_PATH_LENGTH, '3');
1605e933c21SGreg Roach        self::assertFalse($beatrice->canShow());
1611fe542e9SGreg Roach        $tree->setUserPreference($member, UserInterface::PREF_TREE_PATH_LENGTH, '4');
1625e933c21SGreg Roach        self::assertTrue($beatrice->canShow());
163bb88f16bSGreg Roach    }
164bb88f16bSGreg Roach}
165