1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\SiteConfig\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\Security\Security; |
8
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @package siteconfig |
12
|
|
|
* @subpackage tests |
13
|
|
|
*/ |
14
|
|
|
class SiteConfigTest extends SapphireTest |
15
|
|
|
{ |
16
|
|
|
protected static $fixture_file = 'SiteConfigTest.yml'; |
17
|
|
|
|
18
|
|
|
protected static $illegal_extensions = array( |
19
|
|
|
SiteTree::class => ['SiteTreeSubsites'], |
20
|
|
|
); |
21
|
|
|
|
22
|
|
View Code Duplication |
public function testCanCreateRootPages() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** @var SiteConfig $config */ |
25
|
|
|
$config = $this->objFromFixture(SiteConfig::class, 'default'); |
26
|
|
|
|
27
|
|
|
// Admin trumps all |
28
|
|
|
$this->actWithPermission('ADMIN', function () use ($config) { |
29
|
|
|
$this->assertTrue($config->canCreateTopLevel()); |
30
|
|
|
}); |
31
|
|
|
|
32
|
|
|
// Log in without pages admin access |
33
|
|
|
$this->actWithPermission('CMS_ACCESS_AssetAdmin', function () use ($config) { |
34
|
|
|
$this->assertFalse($config->canCreateTopLevel()); |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
// Login with necessary edit permission |
38
|
|
|
$perms = SiteConfig::config()->get('required_permission'); |
39
|
|
|
$this->actWithPermission(reset($perms), function () use ($config) { |
40
|
|
|
$this->assertTrue($config->canCreateTopLevel()); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
// "OnlyTheseUsers" restricts to the correct groups |
44
|
|
|
$config->CanCreateTopLevelType = 'OnlyTheseUsers'; |
45
|
|
|
$this->actWithPermission('ADMIN', function () use ($config) { |
46
|
|
|
$this->assertTrue($config->canCreateTopLevel()); |
47
|
|
|
}); |
48
|
|
|
$this->actWithPermission('CMS_ACCESS_AssetAdmin', function () use ($config) { |
49
|
|
|
$this->assertFalse($config->canCreateTopLevel()); |
50
|
|
|
$config->CreateTopLevelGroups()->add(Security::getCurrentUser()->Groups()->First()); |
51
|
|
|
$this->assertTrue($config->canCreateTopLevel()); |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testCanViewPages() |
56
|
|
|
{ |
57
|
|
|
/** @var SiteConfig $config */ |
58
|
|
|
$config = $this->objFromFixture(SiteConfig::class, 'default'); |
59
|
|
|
|
60
|
|
|
// "Anyone" can view |
61
|
|
|
$this->actWithPermission('ADMIN', function () use ($config) { |
62
|
|
|
$this->assertTrue($config->canViewPages()); |
63
|
|
|
}); |
64
|
|
|
$this->actWithPermission('CMS_ACCESS_AssetAdmin', function () use ($config) { |
65
|
|
|
$this->assertTrue($config->canViewPages()); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
// "LoggedInUsers" can view |
69
|
|
|
$config->CanViewType = 'LoggedInUsers'; |
70
|
|
|
$this->logOut(); |
71
|
|
|
$this->assertFalse($config->canViewPages()); |
72
|
|
|
|
73
|
|
|
// "OnlyTheseUsers" restricts to the correct groups |
74
|
|
|
$config->CanViewType = 'OnlyTheseUsers'; |
75
|
|
|
$this->actWithPermission('ADMIN', function () use ($config) { |
76
|
|
|
$this->assertTrue($config->canViewPages()); |
77
|
|
|
}); |
78
|
|
|
$this->actWithPermission('CMS_ACCESS_AssetAdmin', function () use ($config) { |
79
|
|
|
$this->assertFalse($config->canViewPages()); |
80
|
|
|
$config->ViewerGroups()->add(Security::getCurrentUser()->Groups()->First()); |
81
|
|
|
$this->assertTrue($config->canViewPages()); |
82
|
|
|
}); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testCanEdit() |
86
|
|
|
{ |
87
|
|
|
$config = $this->objFromFixture(SiteConfig::class, 'default'); |
88
|
|
|
|
89
|
|
|
// Unrelated permissions don't allow siteconfig |
90
|
|
|
$this->actWithPermission('CMS_ACCESS_AssetAdmin', function () use ($config) { |
91
|
|
|
$this->assertFalse($config->canEdit()); |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
// Only those with edit permission can do this |
95
|
|
|
$this->actWithPermission('EDIT_SITECONFIG', function () use ($config) { |
96
|
|
|
$this->assertTrue($config->canEdit()); |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
public function testCanEditPages() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
/** @var SiteConfig $config */ |
103
|
|
|
$config = $this->objFromFixture(SiteConfig::class, 'default'); |
104
|
|
|
|
105
|
|
|
// Admin can always edit |
106
|
|
|
$this->actWithPermission('ADMIN', function () use ($config) { |
107
|
|
|
$this->assertTrue($config->canEditPages()); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
// Log in without pages admin access |
111
|
|
|
$this->actWithPermission('CMS_ACCESS_AssetAdmin', function () use ($config) { |
112
|
|
|
$this->assertFalse($config->canEditPages()); |
113
|
|
|
}); |
114
|
|
|
|
115
|
|
|
// Login with necessary edit permission |
116
|
|
|
$perms = SiteConfig::config()->get('required_permission'); |
117
|
|
|
$this->actWithPermission(reset($perms), function () use ($config) { |
118
|
|
|
$this->assertTrue($config->canEditPages()); |
119
|
|
|
}); |
120
|
|
|
|
121
|
|
|
// "OnlyTheseUsers" restricts to the correct groups |
122
|
|
|
$config->CanEditType = 'OnlyTheseUsers'; |
123
|
|
|
$this->actWithPermission('ADMIN', function () use ($config) { |
124
|
|
|
$this->assertTrue($config->canEditPages()); |
125
|
|
|
}); |
126
|
|
|
$this->actWithPermission('CMS_ACCESS_AssetAdmin', function () use ($config) { |
127
|
|
|
$this->assertFalse($config->canEditPages()); |
128
|
|
|
$config->EditorGroups()->add(Security::getCurrentUser()->Groups()->First()); |
129
|
|
|
$this->assertTrue($config->canEditPages()); |
130
|
|
|
}); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.