1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bundle\OrganizationBundle\Tests\Unit\Provider; |
4
|
|
|
|
5
|
|
|
use Oro\Bundle\SecurityBundle\Acl\AccessLevel; |
6
|
|
|
use Oro\Bundle\OrganizationBundle\Provider\BusinessUnitAclProvider; |
7
|
|
|
|
8
|
|
|
class BusinessUnitAclProviderTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
const ENTITY_NAME='test'; |
11
|
|
|
const PERMISSION='VIEW'; |
12
|
|
|
|
13
|
|
|
/** @var BusinessUnitAclProvider */ |
14
|
|
|
protected $provider; |
15
|
|
|
|
16
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
17
|
|
|
protected $securityFacade; |
18
|
|
|
|
19
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
20
|
|
|
protected $aclVoter; |
21
|
|
|
|
22
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
23
|
|
|
protected $treeProvider; |
24
|
|
|
|
25
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
26
|
|
|
protected $observer; |
27
|
|
|
|
28
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
29
|
|
|
protected $user; |
30
|
|
|
|
31
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
32
|
|
|
protected $organization; |
33
|
|
|
|
34
|
|
|
protected function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->securityFacade = $this->getMockBuilder('Oro\Bundle\SecurityBundle\SecurityFacade') |
37
|
|
|
->disableOriginalConstructor() |
38
|
|
|
->getMock(); |
39
|
|
|
|
40
|
|
|
$this->aclVoter = $this->getMockBuilder('Oro\Bundle\SecurityBundle\Acl\Voter\AclVoter') |
41
|
|
|
->disableOriginalConstructor() |
42
|
|
|
->getMock(); |
43
|
|
|
|
44
|
|
|
$this->treeProvider = $this->getMockBuilder('Oro\Bundle\SecurityBundle\Owner\OwnerTreeProvider') |
45
|
|
|
->setMethods([ |
46
|
|
|
'getTree', |
47
|
|
|
'getUserBusinessUnitIds', |
48
|
|
|
'getUserSubordinateBusinessUnitIds', |
49
|
|
|
'getAllBusinessUnitIds', |
50
|
|
|
'getOrganizationBusinessUnitIds' |
51
|
|
|
]) |
52
|
|
|
->disableOriginalConstructor() |
53
|
|
|
->getMock(); |
54
|
|
|
|
55
|
|
|
$this->observer = $this->getMockBuilder('Oro\Bundle\SecurityBundle\Acl\Domain\OneShotIsGrantedObserver') |
56
|
|
|
->disableOriginalConstructor() |
57
|
|
|
->getMock(); |
58
|
|
|
|
59
|
|
|
$this->user = $this->getMockBuilder('Oro\Bundle\UserBundle\Entity\User') |
60
|
|
|
->setMethods(['getId']) |
61
|
|
|
->disableOriginalConstructor() |
62
|
|
|
->getMock(); |
63
|
|
|
|
64
|
|
|
$this->organization = $this->getMockBuilder('Oro\Bundle\OrganizationBundle\Entity\Organization') |
65
|
|
|
->setMethods(['getId']) |
66
|
|
|
->disableOriginalConstructor() |
67
|
|
|
->getMock(); |
68
|
|
|
|
69
|
|
|
$this->securityFacade->expects($this->any()) |
70
|
|
|
->method('getLoggedUser') |
71
|
|
|
->will($this->returnValue($this->user)); |
72
|
|
|
|
73
|
|
|
$this->securityFacade->expects($this->any()) |
74
|
|
|
->method('getOrganization') |
75
|
|
|
->will($this->returnValue($this->organization)); |
76
|
|
|
|
77
|
|
|
$this->provider = $this->getMockBuilder('Oro\Bundle\OrganizationBundle\Provider\BusinessUnitAclProvider') |
78
|
|
|
->setMethods(['getAccessLevel']) |
79
|
|
|
->setConstructorArgs([ |
80
|
|
|
$this->securityFacade, |
81
|
|
|
$this->aclVoter, |
82
|
|
|
$this->treeProvider |
83
|
|
|
]) |
84
|
|
|
->getMock(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
public function testSystemLevel() |
88
|
|
|
{ |
89
|
|
|
$this->provider->expects($this->once()) |
90
|
|
|
->method('getAccessLevel') |
91
|
|
|
->with(self::PERMISSION, 'entity:'.self::ENTITY_NAME) |
92
|
|
|
->will($this->returnValue(AccessLevel::SYSTEM_LEVEL)); |
93
|
|
|
|
94
|
|
|
$this->treeProvider->expects($this->exactly(1)) |
95
|
|
|
->method('getTree') |
96
|
|
|
->will($this->returnValue($this->treeProvider)); |
97
|
|
|
|
98
|
|
|
$this->treeProvider->expects($this->exactly(1)) |
99
|
|
|
->method('getAllBusinessUnitIds') |
100
|
|
|
->will($this->returnValue($this->treeProvider)); |
101
|
|
|
|
102
|
|
|
$this->provider->getBusinessUnitIds(self::ENTITY_NAME, self::PERMISSION); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
public function testLocalLevel() |
106
|
|
|
{ |
107
|
|
|
$this->provider->expects($this->once()) |
108
|
|
|
->method('getAccessLevel') |
109
|
|
|
->with(self::PERMISSION, 'entity:'.self::ENTITY_NAME) |
110
|
|
|
->will($this->returnValue(AccessLevel::LOCAL_LEVEL)); |
111
|
|
|
|
112
|
|
|
$this->treeProvider->expects($this->exactly(1)) |
113
|
|
|
->method('getTree') |
114
|
|
|
->will($this->returnValue($this->treeProvider)); |
115
|
|
|
|
116
|
|
|
$this->treeProvider->expects($this->exactly(1)) |
117
|
|
|
->method('getUserBusinessUnitIds') |
118
|
|
|
->will($this->returnValue($this->treeProvider)); |
119
|
|
|
|
120
|
|
|
$this->provider->getBusinessUnitIds(self::ENTITY_NAME, self::PERMISSION); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
View Code Duplication |
public function testDeepLevel() |
124
|
|
|
{ |
125
|
|
|
$this->provider->expects($this->once()) |
126
|
|
|
->method('getAccessLevel') |
127
|
|
|
->with(self::PERMISSION, 'entity:'.self::ENTITY_NAME) |
128
|
|
|
->will($this->returnValue(AccessLevel::DEEP_LEVEL)); |
129
|
|
|
|
130
|
|
|
$this->treeProvider->expects($this->exactly(1)) |
131
|
|
|
->method('getTree') |
132
|
|
|
->will($this->returnValue($this->treeProvider)); |
133
|
|
|
|
134
|
|
|
$this->treeProvider->expects($this->exactly(1)) |
135
|
|
|
->method('getUserSubordinateBusinessUnitIds') |
136
|
|
|
->will($this->returnValue($this->treeProvider)); |
137
|
|
|
|
138
|
|
|
$this->provider->getBusinessUnitIds(self::ENTITY_NAME, self::PERMISSION); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
View Code Duplication |
public function testGlobalLevel() |
142
|
|
|
{ |
143
|
|
|
$this->provider->expects($this->once()) |
144
|
|
|
->method('getAccessLevel') |
145
|
|
|
->with(self::PERMISSION, 'entity:'.self::ENTITY_NAME) |
146
|
|
|
->will($this->returnValue(AccessLevel::GLOBAL_LEVEL)); |
147
|
|
|
|
148
|
|
|
$this->treeProvider->expects($this->exactly(1)) |
149
|
|
|
->method('getTree') |
150
|
|
|
->will($this->returnValue($this->treeProvider)); |
151
|
|
|
|
152
|
|
|
$this->treeProvider->expects($this->exactly(1)) |
153
|
|
|
->method('getOrganizationBusinessUnitIds') |
154
|
|
|
->will($this->returnValue($this->treeProvider)); |
155
|
|
|
|
156
|
|
|
$this->provider->getBusinessUnitIds(self::ENTITY_NAME, self::PERMISSION); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testAccessNotGranted() |
160
|
|
|
{ |
161
|
|
|
$this->treeProvider->expects($this->exactly(0)) |
162
|
|
|
->method('getTree'); |
163
|
|
|
|
164
|
|
|
$this->assertEquals([], $this->provider->getBusinessUnitIds(self::ENTITY_NAME, self::PERMISSION)); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|