2222
2323namespace Tuleap \Gitlab \Artifact \Action ;
2424
25+ use GitPermissionsManager ;
26+ use GitUserNotAdminException ;
2527use Tuleap \Git \Branch \InvalidBranchNameException ;
2628use Tuleap \Gitlab \Repository \GitlabRepositoryIntegrationFactory ;
2729use Tuleap \Gitlab \Repository \GitlabRepositoryIntegrationNotFoundException ;
2830use Tuleap \Gitlab \Test \Builder \RepositoryIntegrationBuilder ;
2931use Tuleap \Gitlab \Test \Stubs \SaveIntegrationBranchPrefixStub ;
32+ use Tuleap \Test \Builders \UserTestBuilder ;
3033use Tuleap \Test \PHPUnit \TestCase ;
3134
3235final class CreateBranchPrefixUpdaterTest extends TestCase
@@ -38,11 +41,16 @@ final class CreateBranchPrefixUpdaterTest extends TestCase
3841 private $ integration_factory ;
3942 private SaveIntegrationBranchPrefixStub $ branch_prefix_saver ;
4043 private string $ branch_prefix ;
44+ /**
45+ * @var GitPermissionsManager&\PHPUnit\Framework\MockObject\MockObject
46+ */
47+ private $ git_permissions_manager ;
4148
4249 protected function setUp (): void
4350 {
44- $ this ->integration_factory = $ this ->createMock (GitlabRepositoryIntegrationFactory::class);
45- $ this ->branch_prefix_saver = SaveIntegrationBranchPrefixStub::withCallCount ();
51+ $ this ->integration_factory = $ this ->createMock (GitlabRepositoryIntegrationFactory::class);
52+ $ this ->git_permissions_manager = $ this ->createMock (GitPermissionsManager::class);
53+ $ this ->branch_prefix_saver = SaveIntegrationBranchPrefixStub::withCallCount ();
4654
4755 $ this ->branch_prefix = 'dev- ' ;
4856 }
@@ -51,10 +59,15 @@ private function updateBranchPrefix(): void
5159 {
5260 $ updater = new CreateBranchPrefixUpdater (
5361 $ this ->integration_factory ,
54- $ this ->branch_prefix_saver
62+ $ this ->git_permissions_manager ,
63+ $ this ->branch_prefix_saver ,
5564 );
5665
57- $ updater ->updateBranchPrefix (self ::INTEGRATION_ID , $ this ->branch_prefix );
66+ $ updater ->updateBranchPrefix (
67+ UserTestBuilder::anActiveUser ()->build (),
68+ self ::INTEGRATION_ID ,
69+ $ this ->branch_prefix ,
70+ );
5871 }
5972
6073 public function testItStoresTheBranchPrefix (): void
@@ -65,6 +78,11 @@ public function testItStoresTheBranchPrefix(): void
6578 ->with (self ::INTEGRATION_ID )
6679 ->willReturn (RepositoryIntegrationBuilder::aGitlabRepositoryIntegration (self ::INTEGRATION_ID )->build ());
6780
81+ $ this ->git_permissions_manager
82+ ->expects (self ::once ())
83+ ->method ('userIsGitAdmin ' )
84+ ->willReturn (true );
85+
6886 $ this ->updateBranchPrefix ();
6987
7088 self ::assertSame (1 , $ this ->branch_prefix_saver ->getCallCount ());
@@ -78,6 +96,11 @@ public function testItStoresTheBranchPrefixWithSomeSpecialChars(): void
7896 ->with (self ::INTEGRATION_ID )
7997 ->willReturn (RepositoryIntegrationBuilder::aGitlabRepositoryIntegration (self ::INTEGRATION_ID )->build ());
8098
99+ $ this ->git_permissions_manager
100+ ->expects (self ::once ())
101+ ->method ('userIsGitAdmin ' )
102+ ->willReturn (true );
103+
81104 $ this ->branch_prefix = 'dev/ ' ;
82105 $ this ->updateBranchPrefix ();
83106
@@ -98,6 +121,25 @@ public function testItThrowsAnExceptionIfIntegrationNotFoundTheBranchPrefix(): v
98121 self ::assertSame (0 , $ this ->branch_prefix_saver ->getCallCount ());
99122 }
100123
124+ public function testItThrowsAnExceptionIfUserIsNotGitAdministrator (): void
125+ {
126+ $ this ->integration_factory
127+ ->expects (self ::once ())
128+ ->method ('getIntegrationById ' )
129+ ->with (self ::INTEGRATION_ID )
130+ ->willReturn (RepositoryIntegrationBuilder::aGitlabRepositoryIntegration (self ::INTEGRATION_ID )->build ());
131+
132+ $ this ->git_permissions_manager
133+ ->expects (self ::once ())
134+ ->method ('userIsGitAdmin ' )
135+ ->willReturn (false );
136+
137+ $ this ->expectException (GitUserNotAdminException::class);
138+ $ this ->updateBranchPrefix ();
139+
140+ self ::assertSame (0 , $ this ->branch_prefix_saver ->getCallCount ());
141+ }
142+
101143 public function testItThrowsAnExceptionIfBranchPrefixIsNotValid (): void
102144 {
103145 $ this ->integration_factory
@@ -106,6 +148,11 @@ public function testItThrowsAnExceptionIfBranchPrefixIsNotValid(): void
106148 ->with (self ::INTEGRATION_ID )
107149 ->willReturn (RepositoryIntegrationBuilder::aGitlabRepositoryIntegration (self ::INTEGRATION_ID )->build ());
108150
151+ $ this ->git_permissions_manager
152+ ->expects (self ::once ())
153+ ->method ('userIsGitAdmin ' )
154+ ->willReturn (true );
155+
109156 $ this ->branch_prefix = 'dev: ' ;
110157
111158 $ this ->expectException (InvalidBranchNameException::class);
0 commit comments