9
9
use Sonata \AdminBundle \Route \RouteCollection ;
10
10
use Sonata \AdminBundle \Translator \LabelTranslatorStrategyInterface ;
11
11
use Symfony \Component \Routing \Route ;
12
+ use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
12
13
use Symfony \Component \Workflow \Registry ;
13
14
use Symfony \Component \Workflow \StateMachine ;
14
15
use Yokai \SonataWorkflow \Admin \Extension \WorkflowExtension ;
@@ -36,7 +37,8 @@ public function testConfigureRoutes()
36
37
self ::assertSame ('/pull-request/{id}/workflow/transition/{transition}/apply ' , $ route ->getPath ());
37
38
self ::assertNotEmpty ($ defaults = $ route ->getDefaults ());
38
39
self ::assertArrayHasKey ('_controller ' , $ defaults );
39
- self ::assertSame (WorkflowController::class.'::workflowApplyTransitionAction ' , $ defaults ['_controller ' ]);
40
+ self ::assertStringStartsWith (WorkflowController::class, $ defaults ['_controller ' ]);
41
+ self ::assertStringEndsWith ('workflowApplyTransitionAction ' , $ defaults ['_controller ' ]);
40
42
self ::assertArrayHasKey ('_sonata_admin ' , $ defaults );
41
43
self ::assertSame ('pull_request ' , $ defaults ['_sonata_admin ' ]);
42
44
}
@@ -69,6 +71,18 @@ public function testAlterNewInstance()
69
71
self ::assertSame ('opened ' , $ pullRequest ->getMarking ());
70
72
}
71
73
74
+ public function testAccessMapping ()
75
+ {
76
+ /** @var AdminInterface|ObjectProphecy $admin */
77
+ $ admin = $ this ->prophesize (AdminInterface::class);
78
+
79
+ $ extension = new WorkflowExtension (new Registry ());
80
+ self ::assertSame (
81
+ ['viewTransitions ' => 'EDIT ' , 'applyTransitions ' => 'EDIT ' ],
82
+ $ extension ->getAccessMapping ($ admin ->reveal ())
83
+ );
84
+ }
85
+
72
86
public function testConfigureSideMenuWithoutSubject ()
73
87
{
74
88
/** @var AdminInterface|ObjectProphecy $admin */
@@ -81,11 +95,25 @@ public function testConfigureSideMenuWithoutSubject()
81
95
self ::assertFalse ($ menu ->hasChildren ());
82
96
}
83
97
98
+ public function testConfigureSideMenuWithoutPermission ()
99
+ {
100
+ /** @var AdminInterface|ObjectProphecy $admin */
101
+ $ admin = $ this ->prophesize (AdminInterface::class);
102
+ $ admin ->getSubject ()->willReturn ($ pullRequest = new PullRequest ());
103
+ $ admin ->checkAccess ('viewTransitions ' , $ pullRequest )->willThrow (new AccessDeniedException ());
104
+
105
+ $ extension = new WorkflowExtension (new Registry ());
106
+ $ extension ->configureSideMenu ($ admin ->reveal (), $ menu = new MenuItem ('root ' , new MenuFactory ()), 'edit ' );
107
+
108
+ self ::assertFalse ($ menu ->hasChildren ());
109
+ }
110
+
84
111
public function testConfigureSideMenuWithoutWorkflow ()
85
112
{
86
113
/** @var AdminInterface|ObjectProphecy $admin */
87
114
$ admin = $ this ->prophesize (AdminInterface::class);
88
- $ admin ->getSubject ()->willReturn (new PullRequest ());
115
+ $ admin ->getSubject ()->willReturn ($ pullRequest = new PullRequest ());
116
+ $ admin ->checkAccess ('viewTransitions ' , $ pullRequest )->shouldBeCalled ();
89
117
90
118
$ extension = new WorkflowExtension (new Registry ());
91
119
$ extension ->configureSideMenu ($ admin ->reveal (), $ menu = new MenuItem ('root ' , new MenuFactory ()), 'edit ' );
@@ -96,7 +124,7 @@ public function testConfigureSideMenuWithoutWorkflow()
96
124
/**
97
125
* @dataProvider markingToTransition
98
126
*/
99
- public function testConfigureSideMenu ($ marking , array $ transitions )
127
+ public function testConfigureSideMenu ($ marking , array $ transitions, $ grantedApply )
100
128
{
101
129
$ pullRequest = new PullRequest ();
102
130
$ pullRequest ->setMarking ($ marking );
@@ -109,14 +137,25 @@ public function testConfigureSideMenu($marking, array $transitions)
109
137
$ admin ->getTranslationDomain ()->willReturn ('admin ' );
110
138
$ admin ->getLabelTranslatorStrategy ()->willReturn ($ labelStrategy ->reveal ());
111
139
$ admin ->getSubject ()->willReturn ($ pullRequest );
140
+ $ admin ->checkAccess ('viewTransitions ' , $ pullRequest )->shouldBeCalled ();
141
+ if ($ grantedApply ) {
142
+ $ admin ->checkAccess ('applyTransitions ' , $ pullRequest )->shouldBeCalledTimes (count ($ transitions ));
143
+ } else {
144
+ $ admin ->checkAccess ('applyTransitions ' , $ pullRequest )->willThrow (new AccessDeniedException ());
145
+ }
112
146
113
147
foreach ($ transitions as $ transition ) {
114
148
$ labelStrategy ->getLabel ($ transition , 'workflow ' , 'transition ' )
115
149
->shouldBeCalledTimes (1 )
116
150
->willReturn ('workflow.transition. ' .$ transition );
117
- $ admin ->generateObjectUrl ('workflow_apply_transition ' , $ pullRequest , ['transition ' => $ transition ])
118
- ->shouldBeCalledTimes (1 )
119
- ->willReturn ('/pull-request/42/workflow/transition/ ' .$ transition .'/apply ' );
151
+ if ($ grantedApply ) {
152
+ $ admin ->generateObjectUrl ('workflow_apply_transition ' , $ pullRequest , ['transition ' => $ transition ])
153
+ ->shouldBeCalledTimes (1 )
154
+ ->willReturn ('/pull-request/42/workflow/transition/ ' .$ transition .'/apply ' );
155
+ } else {
156
+ $ admin ->generateObjectUrl ('workflow_apply_transition ' , $ pullRequest , ['transition ' => $ transition ])
157
+ ->shouldNotBeCalled ();
158
+ }
120
159
}
121
160
122
161
$ registry = new LegacyWorkflowRegistry ();
@@ -154,7 +193,11 @@ public function testConfigureSideMenu($marking, array $transitions)
154
193
}
155
194
156
195
self ::assertNotNull ($ item = $ child ->getChild ('workflow.transition. ' .$ transition ));
157
- self ::assertSame ('/pull-request/42/workflow/transition/ ' .$ transition .'/apply ' , $ item ->getUri ());
196
+ if ($ grantedApply ) {
197
+ self ::assertSame ('/pull-request/42/workflow/transition/ ' .$ transition .'/apply ' , $ item ->getUri ());
198
+ } else {
199
+ self ::assertNull ($ item ->getUri ());
200
+ }
158
201
self ::assertSame ('admin ' , $ item ->getExtra ('translation_domain ' ));
159
202
self ::assertSame ($ icon , $ item ->getAttribute ('icon ' ));
160
203
}
@@ -163,10 +206,12 @@ public function testConfigureSideMenu($marking, array $transitions)
163
206
164
207
public function markingToTransition ()
165
208
{
166
- return [
167
- 'opened ' => ['opened ' , ['start_review ' ]],
168
- 'pending_review ' => ['pending_review ' , ['merge ' , 'close ' ]],
169
- 'closed ' => ['closed ' , []],
170
- ];
209
+ foreach ([true , false ] as $ grantedApply ) {
210
+ $ grantedApplyStr = $ grantedApply ? 'with links ' : 'without links ' ;
211
+
212
+ yield 'opened ' .$ grantedApplyStr => ['opened ' , ['start_review ' ], $ grantedApply ];
213
+ yield 'pending_review ' .$ grantedApplyStr => ['pending_review ' , ['merge ' , 'close ' ], $ grantedApply ];
214
+ yield 'closed ' .$ grantedApplyStr => ['closed ' , [], $ grantedApply ];
215
+ }
171
216
}
172
217
}
0 commit comments