Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(user_ldap): Fix user_ldap tests by mocking new method exists in m…
…anager

Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc authored and max-nextcloud committed Mar 5, 2025
commit 8717646cbfbe7c96e5e604430dfe801612c2d583
84 changes: 64 additions & 20 deletions apps/user_ldap/tests/User_LDAPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ public function testDeleteUserSuccess() {
$this->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($offlineUser);
$this->userManager->expects($this->once())
->method('exists')
->with($uid)
->willReturn(true);

$backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex);

Expand Down Expand Up @@ -486,9 +490,12 @@ public function testUserExists() {

$user = $this->createMock(User::class);

$this->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
$this->userManager->expects($this->never())
->method('get');
$this->userManager->expects($this->once())
->method('exists')
->with('gunslinger')
->willReturn(true);
$this->access->expects($this->any())
->method('getUserMapper')
->willReturn($this->createMock(UserMapping::class));
Expand All @@ -513,11 +520,12 @@ public function testUserExistsForDeleted() {
->method('getUserMapper')
->willReturn($mapper);

$user = $this->createMock(User::class);

$this->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
$this->userManager->expects($this->never())
->method('get');
$this->userManager->expects($this->once())
->method('exists')
->with('formerUser')
->willReturn(true);

//test for deleted user – always returns true as long as we have the user in DB
$this->assertTrue($backend->userExists('formerUser'));
Expand Down Expand Up @@ -560,9 +568,12 @@ public function testUserExistsPublicAPI() {
}
return false;
});
$this->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
$this->userManager->expects($this->never())
->method('get');
$this->userManager->expects($this->once())
->method('exists')
->with('gunslinger')
->willReturn(true);
$this->access->expects($this->any())
->method('getUserMapper')
->willReturn($this->createMock(UserMapping::class));
Expand Down Expand Up @@ -621,7 +632,12 @@ public function testGetHomeAbsolutePath() {

$this->userManager->expects($this->atLeastOnce())
->method('get')
->with('gunslinger')
->willReturn($user);
$this->userManager->expects($this->once())
->method('exists')
->with('gunslinger')
->willReturn(true);

//absolute path
/** @noinspection PhpUnhandledExceptionInspection */
Expand Down Expand Up @@ -674,6 +690,10 @@ public function testGetHomeRelative() {
$this->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
$this->userManager->expects($this->once())
->method('exists')
->with('ladyofshadows')
->willReturn(true);

/** @noinspection PhpUnhandledExceptionInspection */
$result = $backend->getHome('ladyofshadows');
Expand Down Expand Up @@ -703,14 +723,6 @@ public function testGetHomeNoPath() {
return false;
}
});
$this->access->connection->expects($this->any())
->method('getFromCache')
->willReturnCallback(function ($key) {
if ($key === 'userExistsnewyorker') {
return true;
}
return null;
});

$user = $this->createMock(User::class);
$user->expects($this->any())
Expand All @@ -722,7 +734,12 @@ public function testGetHomeNoPath() {

$this->userManager->expects($this->atLeastOnce())
->method('get')
->with('newyorker')
->willReturn($user);
$this->userManager->expects($this->once())
->method('exists')
->with('newyorker')
->willReturn(true);

//no path at all – triggers OC default behaviour
$result = $backend->getHome('newyorker');
Expand Down Expand Up @@ -761,7 +778,12 @@ public function testGetHomeDeletedUser() {

$this->userManager->expects($this->atLeastOnce())
->method('get')
->with($uid)
->willReturn($offlineUser);
$this->userManager->expects($this->once())
->method('exists')
->with($uid)
->willReturn(true);

$result = $backend->getHome($uid);
$this->assertFalse($result);
Expand Down Expand Up @@ -861,6 +883,16 @@ public function testGetDisplayName() {
}
return null;
});
$this->userManager->expects($this->any())
->method('exists')
->willReturnCallback(function ($uid) use ($user1, $user2) {
if ($uid === 'gunslinger') {
return true;
} elseif ($uid === 'newyorker') {
return true;
}
return false;
});
$this->access->expects($this->any())
->method('getUserMapper')
->willReturn($mapper);
Expand Down Expand Up @@ -944,6 +976,16 @@ public function testGetDisplayNamePublicAPI() {
}
return null;
});
$this->userManager->expects($this->any())
->method('exists')
->willReturnCallback(function ($uid) use ($user1, $user2) {
if ($uid === 'gunslinger') {
return true;
} elseif ($uid === 'newyorker') {
return true;
}
return false;
});
$this->access->expects($this->any())
->method('getUserMapper')
->willReturn($mapper);
Expand All @@ -954,7 +996,7 @@ public function testGetDisplayNamePublicAPI() {
});

//with displayName
$result = \OC::$server->getUserManager()->get('gunslinger')->getDisplayName();
$result = \OC::$server->getUserManager()->get('gunslinger')?->getDisplayName();
$this->assertEquals('Roland Deschain', $result);

//empty displayname retrieved
Expand Down Expand Up @@ -1048,6 +1090,8 @@ public function testLoginName2UserNameSuccess() {
->method('get')
->with($dn)
->willReturn($user);
$this->userManager->expects($this->never())
->method('exists');
$this->userManager->expects($this->any())
->method('getAttributes')
->willReturn(['dn', 'uid', 'mail', 'displayname']);
Expand Down