1111use OC \SystemTag \SystemTagManager ;
1212use OC \SystemTag \SystemTagObjectMapper ;
1313use OCP \EventDispatcher \IEventDispatcher ;
14+ use OCP \IAppConfig ;
1415use OCP \IDBConnection ;
1516use OCP \IGroupManager ;
1617use OCP \IUser ;
18+ use OCP \IUserSession ;
1719use OCP \SystemTag \ISystemTag ;
1820use OCP \SystemTag \ISystemTagManager ;
1921use Test \TestCase ;
@@ -40,6 +42,16 @@ class SystemTagManagerTest extends TestCase {
4042 */
4143 private $ groupManager ;
4244
45+ /**
46+ * @var IUserSession
47+ */
48+ private $ userSession ;
49+
50+ /**
51+ * @var IAppConfig
52+ */
53+ private $ appConfig ;
54+
4355 /**
4456 * @var IEventDispatcher
4557 */
@@ -52,11 +64,15 @@ protected function setUp(): void {
5264
5365 $ this ->dispatcher = $ this ->createMock (IEventDispatcher::class);
5466 $ this ->groupManager = $ this ->createMock (IGroupManager::class);
67+ $ this ->userSession = $ this ->createMock (IUserSession::class);
68+ $ this ->appConfig = $ this ->createMock (IAppConfig::class);
5569
5670 $ this ->tagManager = new SystemTagManager (
5771 $ this ->connection ,
5872 $ this ->groupManager ,
59- $ this ->dispatcher
73+ $ this ->dispatcher ,
74+ $ this ->userSession ,
75+ $ this ->appConfig ,
6076 );
6177 $ this ->pruneTagsTables ();
6278 }
@@ -535,6 +551,84 @@ public function testEmptyTagGroup(): void {
535551 $ this ->assertEquals ([], $ this ->tagManager ->getTagGroups ($ tag1 ));
536552 }
537553
554+ private function allowedToCreateProvider (): array {
555+ return [
556+ [true , null , true ],
557+ [true , null , false ],
558+ [false , true , true ],
559+ [false , true , false ],
560+ [false , false , false ],
561+ ];
562+ }
563+
564+ /**
565+ * @dataProvider allowedToCreateProvider
566+ */
567+ public function testAllowedToCreateTag (bool $ isCli , ?bool $ isAdmin , bool $ isRestricted ): void {
568+ $ oldCli = \OC ::$ CLI ;
569+ \OC ::$ CLI = $ isCli ;
570+
571+ $ user = $ this ->getMockBuilder (IUser::class)->getMock ();
572+ $ user ->expects ($ this ->any ())
573+ ->method ('getUID ' )
574+ ->willReturn ('test ' );
575+ $ this ->userSession ->expects ($ this ->any ())
576+ ->method ('getUser ' )
577+ ->willReturn ($ isAdmin === null ? null : $ user );
578+ $ this ->groupManager ->expects ($ this ->any ())
579+ ->method ('isAdmin ' )
580+ ->with ('test ' )
581+ ->willReturn ($ isAdmin );
582+ $ this ->appConfig ->expects ($ this ->any ())
583+ ->method ('getValueBool ' )
584+ ->with ('systemtags ' , 'only_admins_can_create ' )
585+ ->willReturn ($ isRestricted );
586+
587+ $ name = uniqid ('tag_ ' , true );
588+ $ tag = $ this ->tagManager ->createTag ($ name , true , true );
589+ $ this ->assertEquals ($ tag ->getName (), $ name );
590+ $ this ->tagManager ->deleteTags ($ tag ->getId ());
591+
592+ \OC ::$ CLI = $ oldCli ;
593+ }
594+
595+ private function disallowedToCreateProvider (): array {
596+ return [
597+ [false ],
598+ [null ],
599+ ];
600+ }
601+
602+ /**
603+ * @dataProvider disallowedToCreateProvider
604+ */
605+ public function testDisallowedToCreateTag (?bool $ isAdmin ): void {
606+ $ oldCli = \OC ::$ CLI ;
607+ \OC ::$ CLI = false ;
608+
609+ $ user = $ this ->getMockBuilder (IUser::class)->getMock ();
610+ $ user ->expects ($ this ->any ())
611+ ->method ('getUID ' )
612+ ->willReturn ('test ' );
613+ $ this ->userSession ->expects ($ this ->any ())
614+ ->method ('getUser ' )
615+ ->willReturn ($ isAdmin === null ? null : $ user );
616+ $ this ->groupManager ->expects ($ this ->any ())
617+ ->method ('isAdmin ' )
618+ ->with ('test ' )
619+ ->willReturn ($ isAdmin );
620+ $ this ->appConfig ->expects ($ this ->any ())
621+ ->method ('getValueBool ' )
622+ ->with ('systemtags ' , 'only_admins_can_create ' )
623+ ->willReturn (true );
624+
625+ $ this ->expectException (\Exception::class);
626+ $ tag = $ this ->tagManager ->createTag (uniqid ('tag_ ' , true ), true , true );
627+
628+ \OC ::$ CLI = $ oldCli ;
629+ }
630+
631+
538632 /**
539633 * @param ISystemTag $tag1
540634 * @param ISystemTag $tag2
0 commit comments