1414use OCA \DAV \CalDAV \PublicCalendarRoot ;
1515use OCA \DAV \Connector \Sabre \Principal ;
1616use OCP \EventDispatcher \IEventDispatcher ;
17+ use OCP \IAppConfig ;
1718use OCP \ICacheFactory ;
1819use OCP \IConfig ;
1920use OCP \IDBConnection ;
2021use OCP \IGroupManager ;
2122use OCP \IL10N ;
23+ use OCP \IUser ;
2224use OCP \IUserManager ;
2325use OCP \Security \ISecureRandom ;
2426use OCP \Server ;
2527use PHPUnit \Framework \MockObject \MockObject ;
2628use Psr \Log \LoggerInterface ;
29+ use Sabre \DAV \Exception \NotFound ;
2730use Test \TestCase ;
2831
2932/**
3538#[\PHPUnit \Framework \Attributes \Group(name: 'DB ' )]
3639class PublicCalendarRootTest extends TestCase {
3740 public const UNIT_TEST_USER = '' ;
41+ private const DISABLED_USER_PRINCIPAL = 'principals/users/disabled-caldav-unit-test ' ;
3842 private CalDavBackend $ backend ;
3943 private PublicCalendarRoot $ publicCalendarRoot ;
4044 private IL10N &MockObject $ l10n ;
4145 private Principal &MockObject $ principal ;
4246 protected IUserManager &MockObject $ userManager ;
4347 protected IGroupManager &MockObject $ groupManager ;
4448 protected IConfig &MockObject $ config ;
49+ protected IAppConfig &MockObject $ appConfig ;
4550 private ISecureRandom $ random ;
4651 private LoggerInterface &MockObject $ logger ;
4752 protected ICacheFactory &MockObject $ cacheFactory ;
@@ -86,9 +91,10 @@ protected function setUp(): void {
8691 );
8792 $ this ->l10n = $ this ->createMock (IL10N ::class);
8893 $ this ->config = $ this ->createMock (IConfig::class);
94+ $ this ->appConfig = $ this ->createMock (IAppConfig::class);
8995
9096 $ this ->publicCalendarRoot = new PublicCalendarRoot ($ this ->backend ,
91- $ this ->l10n , $ this ->config , $ this ->logger );
97+ $ this ->l10n , $ this ->config , $ this ->appConfig , $ this -> logger , $ this -> userManager );
9298 }
9399
94100 protected function tearDown (): void {
@@ -105,7 +111,10 @@ protected function tearDown(): void {
105111 ->withAnyParameters ()
106112 ->willReturn ([]);
107113
108- $ books = $ this ->backend ->getCalendarsForUser (self ::UNIT_TEST_USER );
114+ $ books = array_merge (
115+ $ this ->backend ->getCalendarsForUser (self ::UNIT_TEST_USER ),
116+ $ this ->backend ->getCalendarsForUser (self ::DISABLED_USER_PRINCIPAL ),
117+ );
109118 foreach ($ books as $ book ) {
110119 $ this ->backend ->deleteCalendar ($ book ['id ' ], true );
111120 }
@@ -135,10 +144,32 @@ public function testGetChildren(): void {
135144 $ this ->assertSame ([], $ calendarResults );
136145 }
137146
138- protected function createPublicCalendar (): Calendar {
139- $ this ->backend ->createCalendar (self ::UNIT_TEST_USER , 'Example ' , []);
147+ public function testGetChildHidesCalendarOfDisabledUser (): void {
148+ $ calendar = $ this ->createPublicCalendar (self ::DISABLED_USER_PRINCIPAL );
149+ $ publicUri = $ calendar ->getPublishStatus ();
150+
151+ $ this ->mockDisabledOwner ();
152+ $ this ->setHideDisabledUserShares (true );
153+
154+ $ this ->expectException (NotFound::class);
155+ $ this ->publicCalendarRoot ->getChild ($ publicUri );
156+ }
157+
158+ public function testGetChildServesCalendarOfDisabledUserWhenHidingIsDisabled (): void {
159+ $ calendar = $ this ->createPublicCalendar (self ::DISABLED_USER_PRINCIPAL );
160+ $ publicUri = $ calendar ->getPublishStatus ();
161+
162+ $ this ->mockDisabledOwner ();
163+ $ this ->setHideDisabledUserShares (false );
164+
165+ $ calendarResult = $ this ->publicCalendarRoot ->getChild ($ publicUri );
166+ $ this ->assertEquals ($ calendar , $ calendarResult );
167+ }
168+
169+ protected function createPublicCalendar (string $ principal = self ::UNIT_TEST_USER ): Calendar {
170+ $ this ->backend ->createCalendar ($ principal , 'Example ' , []);
140171
141- $ calendarInfo = $ this ->backend ->getCalendarsForUser (self :: UNIT_TEST_USER )[0 ];
172+ $ calendarInfo = $ this ->backend ->getCalendarsForUser ($ principal )[0 ];
142173 $ calendar = new PublicCalendar ($ this ->backend , $ calendarInfo , $ this ->l10n , $ this ->config , $ this ->logger );
143174 $ publicUri = $ calendar ->setPublishStatus (true );
144175
@@ -147,4 +178,18 @@ protected function createPublicCalendar(): Calendar {
147178
148179 return $ calendar ;
149180 }
181+
182+ private function mockDisabledOwner (): void {
183+ $ disabledUser = $ this ->createMock (IUser::class);
184+ $ disabledUser ->method ('isEnabled ' )
185+ ->willReturn (false );
186+ $ this ->userManager ->method ('get ' )
187+ ->willReturn ($ disabledUser );
188+ }
189+
190+ private function setHideDisabledUserShares (bool $ hide ): void {
191+ $ this ->appConfig ->method ('getValueBool ' )
192+ ->with ('files_sharing ' , 'hide_disabled_user_shares ' , 'yes ' )
193+ ->willReturn ($ hide );
194+ }
150195}
0 commit comments