1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\IDEAnnotator\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injector; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverLeague\IDEAnnotator\DataObjectAnnotator; |
9
|
|
|
use SilverStripe\Assets\File; |
10
|
|
|
use SilverStripe\ORM\DataObject; |
11
|
|
|
use SilverLeague\IDEAnnotator\AnnotatePermissionChecker; |
12
|
|
|
use SilverStripe\Control\Director; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DataObjectAnnotatorTest |
16
|
|
|
* |
17
|
|
|
* @mixin \PHPUnit_Framework_TestCase |
18
|
|
|
*/ |
19
|
|
|
class AnnotatePermissionCheckerTest extends SapphireTest |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var AnnotatePermissionChecker $permissionChecker |
24
|
|
|
*/ |
25
|
|
|
private $permissionChecker = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var MockDataObjectAnnotator |
29
|
|
|
*/ |
30
|
|
|
private $annotator; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Setup Defaults |
35
|
|
|
*/ |
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
parent::setUp(); |
39
|
|
|
Config::modify()->set(Director::class, 'environment_type', 'dev'); |
40
|
|
|
Config::modify()->set(DataObjectAnnotator::class, 'enabled', true); |
41
|
|
|
Config::modify()->set(DataObjectAnnotator::class, 'enabled_modules', ['ideannotator', 'mysite']); |
42
|
|
|
|
43
|
|
|
Config::modify()->merge(Team::class, 'extensions', [Team_Extension::class]); |
44
|
|
|
|
45
|
|
|
$this->annotator = Injector::inst()->get(MockDataObjectAnnotator::class); |
46
|
|
|
$this->permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
public function testIsEnabled() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this->assertTrue($this->permissionChecker->isEnabled()); |
52
|
|
|
|
53
|
|
|
Config::modify()->set(DataObjectAnnotator::class, 'enabled', false); |
54
|
|
|
$this->assertFalse($this->permissionChecker->isEnabled()); |
55
|
|
|
// Set everything back to normal |
56
|
|
|
Config::modify()->set(DataObjectAnnotator::class, 'enabled', true); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function testAnnotatePermissionChecker() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
Config::modify()->set(DataObjectAnnotator::class, 'enabled', false); |
62
|
|
|
$this->assertFalse($this->permissionChecker->environmentIsAllowed()); |
63
|
|
|
Config::modify()->set(DataObjectAnnotator::class, 'enabled', true); |
64
|
|
|
$this->assertTrue($this->permissionChecker->environmentIsAllowed()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Test is a module name is in the @Config enabled_modules |
69
|
|
|
* and will be seen as allowed or disallowed correctly |
70
|
|
|
*/ |
71
|
|
|
public function testModuleIsAllowed() |
72
|
|
|
{ |
73
|
|
|
$this->assertFalse($this->permissionChecker->moduleIsAllowed('framework')); |
74
|
|
|
$this->assertTrue($this->permissionChecker->moduleIsAllowed('mysite')); |
75
|
|
|
$this->assertTrue($this->permissionChecker->moduleIsAllowed('ideannotator')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test if a DataObject is in an allowed module name |
80
|
|
|
* and will be seen as allowed or disallowed correctly |
81
|
|
|
*/ |
82
|
|
|
public function testDataObjectIsAllowed() |
83
|
|
|
{ |
84
|
|
|
$this->assertTrue($this->permissionChecker->classNameIsAllowed(Team::class)); |
85
|
|
|
$this->assertTrue($this->permissionChecker->classNameIsAllowed(Team_Extension::class)); |
86
|
|
|
|
87
|
|
|
$this->assertFalse($this->permissionChecker->classNameIsAllowed(DataObject::class)); |
88
|
|
|
$this->assertFalse($this->permissionChecker->classNameIsAllowed(File::class)); |
89
|
|
|
|
90
|
|
|
Config::inst()->remove(DataObjectAnnotator::class, 'enabled_modules'); |
|
|
|
|
91
|
|
|
Config::modify()->set(DataObjectAnnotator::class, 'enabled_modules', array('mysite')); |
92
|
|
|
|
93
|
|
|
$this->assertFalse($this->permissionChecker->classNameIsAllowed(Team::class)); |
94
|
|
|
} |
95
|
|
|
public function tearDown() |
96
|
|
|
{ |
97
|
|
|
parent::tearDown(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.