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 }
@@ -527,6 +543,84 @@ public function testEmptyTagGroup(): void {
527543 $ this ->assertEquals ([], $ this ->tagManager ->getTagGroups ($ tag1 ));
528544 }
529545
546+ private function allowedToCreateProvider (): array {
547+ return [
548+ [true , null , true ],
549+ [true , null , false ],
550+ [false , true , true ],
551+ [false , true , false ],
552+ [false , false , false ],
553+ ];
554+ }
555+
556+ /**
557+ * @dataProvider allowedToCreateProvider
558+ */
559+ public function testAllowedToCreateTag (bool $ isCli , ?bool $ isAdmin , bool $ isRestricted ): void {
560+ $ oldCli = \OC ::$ CLI ;
561+ \OC ::$ CLI = $ isCli ;
562+
563+ $ user = $ this ->getMockBuilder (IUser::class)->getMock ();
564+ $ user ->expects ($ this ->any ())
565+ ->method ('getUID ' )
566+ ->willReturn ('test ' );
567+ $ this ->userSession ->expects ($ this ->any ())
568+ ->method ('getUser ' )
569+ ->willReturn ($ isAdmin === null ? null : $ user );
570+ $ this ->groupManager ->expects ($ this ->any ())
571+ ->method ('isAdmin ' )
572+ ->with ('test ' )
573+ ->willReturn ($ isAdmin );
574+ $ this ->appConfig ->expects ($ this ->any ())
575+ ->method ('getValueBool ' )
576+ ->with ('systemtags ' , 'only_admins_can_create ' )
577+ ->willReturn ($ isRestricted );
578+
579+ $ name = uniqid ('tag_ ' , true );
580+ $ tag = $ this ->tagManager ->createTag ($ name , true , true );
581+ $ this ->assertEquals ($ tag ->getName (), $ name );
582+ $ this ->tagManager ->deleteTags ($ tag ->getId ());
583+
584+ \OC ::$ CLI = $ oldCli ;
585+ }
586+
587+ private function disallowedToCreateProvider (): array {
588+ return [
589+ [false ],
590+ [null ],
591+ ];
592+ }
593+
594+ /**
595+ * @dataProvider disallowedToCreateProvider
596+ */
597+ public function testDisallowedToCreateTag (?bool $ isAdmin ): void {
598+ $ oldCli = \OC ::$ CLI ;
599+ \OC ::$ CLI = false ;
600+
601+ $ user = $ this ->getMockBuilder (IUser::class)->getMock ();
602+ $ user ->expects ($ this ->any ())
603+ ->method ('getUID ' )
604+ ->willReturn ('test ' );
605+ $ this ->userSession ->expects ($ this ->any ())
606+ ->method ('getUser ' )
607+ ->willReturn ($ isAdmin === null ? null : $ user );
608+ $ this ->groupManager ->expects ($ this ->any ())
609+ ->method ('isAdmin ' )
610+ ->with ('test ' )
611+ ->willReturn ($ isAdmin );
612+ $ this ->appConfig ->expects ($ this ->any ())
613+ ->method ('getValueBool ' )
614+ ->with ('systemtags ' , 'only_admins_can_create ' )
615+ ->willReturn (true );
616+
617+ $ this ->expectException (\Exception::class);
618+ $ tag = $ this ->tagManager ->createTag (uniqid ('tag_ ' , true ), true , true );
619+
620+ \OC ::$ CLI = $ oldCli ;
621+ }
622+
623+
530624 /**
531625 * @param ISystemTag $tag1
532626 * @param ISystemTag $tag2
0 commit comments