99use Sonata \AdminBundle \Route \RouteCollection ;
1010use Sonata \AdminBundle \Translator \LabelTranslatorStrategyInterface ;
1111use Symfony \Component \Routing \Route ;
12+ use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
1213use Symfony \Component \Workflow \Registry ;
1314use Symfony \Component \Workflow \StateMachine ;
1415use Yokai \SonataWorkflow \Admin \Extension \WorkflowExtension ;
@@ -36,7 +37,8 @@ public function testConfigureRoutes()
3637 self ::assertSame ('/pull-request/{id}/workflow/transition/{transition}/apply ' , $ route ->getPath ());
3738 self ::assertNotEmpty ($ defaults = $ route ->getDefaults ());
3839 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 ' ]);
4042 self ::assertArrayHasKey ('_sonata_admin ' , $ defaults );
4143 self ::assertSame ('pull_request ' , $ defaults ['_sonata_admin ' ]);
4244 }
@@ -69,6 +71,18 @@ public function testAlterNewInstance()
6971 self ::assertSame ('opened ' , $ pullRequest ->getMarking ());
7072 }
7173
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+
7286 public function testConfigureSideMenuWithoutSubject ()
7387 {
7488 /** @var AdminInterface|ObjectProphecy $admin */
@@ -81,11 +95,25 @@ public function testConfigureSideMenuWithoutSubject()
8195 self ::assertFalse ($ menu ->hasChildren ());
8296 }
8397
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+
84111 public function testConfigureSideMenuWithoutWorkflow ()
85112 {
86113 /** @var AdminInterface|ObjectProphecy $admin */
87114 $ admin = $ this ->prophesize (AdminInterface::class);
88- $ admin ->getSubject ()->willReturn (new PullRequest ());
115+ $ admin ->getSubject ()->willReturn ($ pullRequest = new PullRequest ());
116+ $ admin ->checkAccess ('viewTransitions ' , $ pullRequest )->shouldBeCalled ();
89117
90118 $ extension = new WorkflowExtension (new Registry ());
91119 $ extension ->configureSideMenu ($ admin ->reveal (), $ menu = new MenuItem ('root ' , new MenuFactory ()), 'edit ' );
@@ -96,7 +124,7 @@ public function testConfigureSideMenuWithoutWorkflow()
96124 /**
97125 * @dataProvider markingToTransition
98126 */
99- public function testConfigureSideMenu ($ marking , array $ transitions )
127+ public function testConfigureSideMenu ($ marking , array $ transitions, $ grantedApply )
100128 {
101129 $ pullRequest = new PullRequest ();
102130 $ pullRequest ->setMarking ($ marking );
@@ -109,14 +137,25 @@ public function testConfigureSideMenu($marking, array $transitions)
109137 $ admin ->getTranslationDomain ()->willReturn ('admin ' );
110138 $ admin ->getLabelTranslatorStrategy ()->willReturn ($ labelStrategy ->reveal ());
111139 $ 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+ }
112146
113147 foreach ($ transitions as $ transition ) {
114148 $ labelStrategy ->getLabel ($ transition , 'workflow ' , 'transition ' )
115149 ->shouldBeCalledTimes (1 )
116150 ->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+ }
120159 }
121160
122161 $ registry = new LegacyWorkflowRegistry ();
@@ -154,7 +193,11 @@ public function testConfigureSideMenu($marking, array $transitions)
154193 }
155194
156195 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+ }
158201 self ::assertSame ('admin ' , $ item ->getExtra ('translation_domain ' ));
159202 self ::assertSame ($ icon , $ item ->getAttribute ('icon ' ));
160203 }
@@ -163,10 +206,12 @@ public function testConfigureSideMenu($marking, array $transitions)
163206
164207 public function markingToTransition ()
165208 {
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+ }
171216 }
172217}
0 commit comments