99namespace Test \User ;
1010
1111use OC \AllConfig ;
12+ use OC \Avatar \AvatarManager ;
1213use OC \Files \Mount \ObjectHomeMountProvider ;
1314use OC \Hooks \PublicEmitter ;
1415use OC \User \Database ;
1516use OC \User \User ;
1617use OCP \Comments \ICommentsManager ;
18+ use OCP \Config \IUserConfig ;
1719use OCP \EventDispatcher \IEventDispatcher ;
1820use OCP \Files \FileInfo ;
1921use OCP \Files \Storage \IStorageFactory ;
2022use OCP \IConfig ;
2123use OCP \IURLGenerator ;
2224use OCP \IUser ;
25+ use OCP \IUserManager ;
2326use OCP \Notification \IManager as INotificationManager ;
2427use OCP \Notification \INotification ;
28+ use OCP \Security \ICredentialsManager ;
2529use OCP \Server ;
2630use PHPUnit \Framework \Attributes \DataProvider ;
2731use PHPUnit \Framework \Attributes \Group ;
2832use PHPUnit \Framework \MockObject \MockObject ;
2933use Test \TestCase ;
34+ use Test \Util \User \Dummy ;
3035
3136#[Group('DB ' )]
3237class UserTest extends TestCase {
@@ -429,6 +434,8 @@ public function testDeleteHooks(bool $result, int $expectedHooks): void {
429434
430435 $ commentsManager = $ this ->createMock (ICommentsManager::class);
431436 $ notificationManager = $ this ->createMock (INotificationManager::class);
437+ $ avatarManager = $ this ->createMock (AvatarManager::class);
438+ $ credentialsManager = $ this ->createMock (ICredentialsManager::class);
432439
433440 if ($ result ) {
434441 $ config ->expects ($ this ->atLeastOnce ())
@@ -453,6 +460,13 @@ public function testDeleteHooks(bool $result, int $expectedHooks): void {
453460 $ notificationManager ->expects ($ this ->once ())
454461 ->method ('markProcessed ' )
455462 ->with ($ notification );
463+
464+ $ avatarManager ->expects ($ this ->once ())
465+ ->method ('deleteUserAvatar ' )
466+ ->with ('foo ' );
467+ $ credentialsManager ->expects ($ this ->once ())
468+ ->method ('erase ' )
469+ ->with ('foo ' );
456470 } else {
457471 $ config ->expects ($ this ->never ())
458472 ->method ('deleteAllUserValues ' );
@@ -466,20 +480,61 @@ public function testDeleteHooks(bool $result, int $expectedHooks): void {
466480 ->method ('createNotification ' );
467481 $ notificationManager ->expects ($ this ->never ())
468482 ->method ('markProcessed ' );
483+
484+ $ avatarManager ->expects ($ this ->never ())
485+ ->method ('deleteUserAvatar ' );
486+ $ credentialsManager ->expects ($ this ->never ())
487+ ->method ('erase ' );
469488 }
470489
471490 $ this ->overwriteService (INotificationManager::class, $ notificationManager );
491+ $ this ->overwriteService (AvatarManager::class, $ avatarManager );
492+ $ this ->overwriteService (ICredentialsManager::class, $ credentialsManager );
472493 $ this ->overwriteService (ICommentsManager::class, $ commentsManager );
473494
474495 $ this ->assertSame ($ result , $ user ->delete ());
475496
476497 $ this ->restoreService (AllConfig::class);
477498 $ this ->restoreService (ICommentsManager::class);
478499 $ this ->restoreService (INotificationManager::class);
500+ $ this ->restoreService (AvatarManager::class);
501+ $ this ->restoreService (ICredentialsManager::class);
479502
480503 $ this ->assertEquals ($ expectedHooks , $ hooksCalled );
481504 }
482505
506+ public function testDeleteRefusedByBackendKeepsAvatarAndCredentials (): void {
507+ $ backend = new class extends Dummy {
508+ #[\Override]
509+ public function deleteUser ($ uid ): bool {
510+ return false ;
511+ }
512+ };
513+ $ uid = $ this ->getUniqueID ('user ' );
514+ $ backend ->createUser ($ uid , 'password ' );
515+
516+ $ userManager = Server ::get (IUserManager::class);
517+ $ userManager ->registerBackend ($ backend );
518+ $ avatarManager = Server ::get (AvatarManager::class);
519+ $ credentialsManager = Server ::get (ICredentialsManager::class);
520+
521+ try {
522+ $ user = $ userManager ->get ($ uid );
523+ $ avatarManager ->getAvatar ($ uid )->set (file_get_contents (\OC ::$ SERVERROOT . '/tests/data/testavatar.png ' ));
524+ $ credentialsManager ->store ($ uid , 'test ' , ['password ' => 'secret ' ]);
525+
526+ $ this ->assertFalse ($ user ->delete ());
527+
528+ $ this ->assertTrue ($ avatarManager ->getAvatar ($ uid )->exists ());
529+ $ this ->assertSame (['password ' => 'secret ' ], $ credentialsManager ->retrieve ($ uid , 'test ' ));
530+ } finally {
531+ $ avatarManager ->deleteUserAvatar ($ uid );
532+ $ credentialsManager ->erase ($ uid );
533+ Server ::get (IUserConfig::class)->deleteAllUserConfig ($ uid );
534+ $ userManager ->removeBackend ($ backend );
535+ }
536+ }
537+
483538 public function testDeleteRecoverState () {
484539 $ backend = $ this ->createMock (\Test \Util \User \Dummy::class);
485540 $ backend ->method ('getBackendName ' )->willReturn ('foo ' );
0 commit comments