1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package framework |
5
|
|
|
* @subpackage tests |
6
|
|
|
*/ |
7
|
|
|
class PermissionTest extends SapphireTest { |
8
|
|
|
|
9
|
|
|
protected static $fixture_file = 'PermissionTest.yml'; |
10
|
|
|
|
11
|
|
|
public function testGetCodesGrouped() { |
12
|
|
|
$codes = Permission::get_codes(); |
13
|
|
|
$this->assertArrayNotHasKey('SITETREE_VIEW_ALL', $codes); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function testGetCodesUngrouped() { |
17
|
|
|
$codes = Permission::get_codes(false); |
18
|
|
|
$this->assertArrayHasKey('SITETREE_VIEW_ALL', $codes); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testDirectlyAppliedPermissions() { |
22
|
|
|
$member = $this->objFromFixture('Member', 'author'); |
23
|
|
|
$this->assertTrue(Permission::checkMember($member, "SITETREE_VIEW_ALL")); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testCMSAccess() { |
27
|
|
|
$members = Member::get()->byIDs($this->allFixtureIDs('Member')); |
28
|
|
|
foreach ($members as $member) { |
29
|
|
|
$this->assertTrue(Permission::checkMember($member, 'CMS_ACCESS')); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$member = new Member(); |
33
|
|
|
$member->update(array( |
34
|
|
|
'FirstName' => 'No CMS', |
35
|
|
|
'Surname' => 'Access', |
36
|
|
|
'Email' => '[email protected]', |
37
|
|
|
)); |
38
|
|
|
$member->write(); |
39
|
|
|
$this->assertFalse(Permission::checkMember($member, 'CMS_ACCESS')); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testLeftAndMainAccessAll() { |
43
|
|
|
//add user and group |
44
|
|
|
$member = $this->objFromFixture('Member', 'leftandmain'); |
45
|
|
|
|
46
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin")); |
47
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin")); |
48
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin")); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testPermissionAreInheritedFromOneRole() { |
52
|
|
|
$member = $this->objFromFixture('Member', 'author'); |
53
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin")); |
54
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin")); |
55
|
|
|
$this->assertFalse(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin")); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testPermissionAreInheritedFromMultipleRoles() { |
59
|
|
|
$member = $this->objFromFixture('Member', 'access'); |
60
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin")); |
61
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin")); |
62
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin")); |
63
|
|
|
$this->assertTrue(Permission::checkMember($member, "EDIT_PERMISSIONS")); |
64
|
|
|
$this->assertFalse(Permission::checkMember($member, "SITETREE_VIEW_ALL")); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testPermissionsForMember() { |
68
|
|
|
$member = $this->objFromFixture('Member', 'access'); |
69
|
|
|
$permissions = Permission::permissions_for_member($member->ID); |
70
|
|
|
$this->assertEquals(4, count($permissions)); |
71
|
|
|
$this->assertTrue(in_array('CMS_ACCESS_MyAdmin', $permissions)); |
72
|
|
|
$this->assertTrue(in_array('CMS_ACCESS_AssetAdmin', $permissions)); |
73
|
|
|
$this->assertTrue(in_array('CMS_ACCESS_SecurityAdmin', $permissions)); |
74
|
|
|
$this->assertTrue(in_array('EDIT_PERMISSIONS', $permissions)); |
75
|
|
|
|
76
|
|
|
$group = $this->objFromFixture("Group", "access"); |
77
|
|
|
|
78
|
|
|
Permission::deny($group->ID, "CMS_ACCESS_MyAdmin"); |
79
|
|
|
$permissions = Permission::permissions_for_member($member->ID); |
80
|
|
|
$this->assertEquals(3, count($permissions)); |
81
|
|
|
$this->assertFalse(in_array('CMS_ACCESS_MyAdmin', $permissions)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testRolesAndPermissionsFromParentGroupsAreInherited() { |
85
|
|
|
$member = $this->objFromFixture('Member', 'globalauthor'); |
86
|
|
|
|
87
|
|
|
// Check that permissions applied to the group are there |
88
|
|
|
$this->assertTrue(Permission::checkMember($member, "SITETREE_EDIT_ALL")); |
89
|
|
|
|
90
|
|
|
// Check that roles from parent groups are there |
91
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin")); |
92
|
|
|
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin")); |
93
|
|
|
|
94
|
|
|
// Check that permissions from parent groups are there |
95
|
|
|
$this->assertTrue(Permission::checkMember($member, "SITETREE_VIEW_ALL")); |
96
|
|
|
|
97
|
|
|
// Check that a random permission that shouldn't be there isn't |
98
|
|
|
$this->assertFalse(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin")); |
99
|
|
|
} |
100
|
|
|
/** |
101
|
|
|
* Ensure the the get_*_by_permission functions are permission role aware |
102
|
|
|
*/ |
103
|
|
|
public function testGettingMembersByPermission() { |
104
|
|
|
$accessMember = $this->objFromFixture('Member', 'access'); |
105
|
|
|
$accessAuthor = $this->objFromFixture('Member', 'author'); |
106
|
|
|
|
107
|
|
|
$result = Permission::get_members_by_permission(array('CMS_ACCESS_SecurityAdmin')); |
108
|
|
|
$resultIDs = $result ? $result->column() : array(); |
109
|
|
|
|
110
|
|
|
$this->assertContains($accessMember->ID, $resultIDs, |
111
|
|
|
'Member is found via a permission attached to a role'); |
112
|
|
|
$this->assertNotContains($accessAuthor->ID, $resultIDs); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
public function testHiddenPermissions(){ |
117
|
|
|
$permissionCheckboxSet = new PermissionCheckboxSetField('Permissions','Permissions','Permission','GroupID'); |
118
|
|
|
$this->assertContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field()); |
119
|
|
|
|
120
|
|
|
Config::inst()->update('Permission', 'hidden_permissions', array('CMS_ACCESS_LeftAndMain')); |
121
|
|
|
|
122
|
|
|
$this->assertNotContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field()); |
123
|
|
|
|
124
|
|
|
Config::inst()->remove('Permission', 'hidden_permissions'); |
125
|
|
|
$this->assertContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testEmptyMemberFails() { |
129
|
|
|
$member = new Member(); |
130
|
|
|
$this->assertFalse($member->exists()); |
131
|
|
|
|
132
|
|
|
$this->logInWithPermission('ADMIN'); |
133
|
|
|
|
134
|
|
|
$this->assertFalse(Permission::checkMember($member, 'ADMIN')); |
135
|
|
|
$this->assertFalse(Permission::checkMember($member, 'CMS_ACCESS_LeftAndMain')); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|