Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Apr 22, 2021
1 parent 58f4d46 commit 396ad3a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion apps/provisioning_api/lib/Controller/UsersController.php
Expand Up @@ -563,7 +563,7 @@ public function getEditableFields(?string $userId = null): DataResponse {
throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
}
} else {
$targetUser = $currentLoggedInUser->getUID();
$targetUser = $currentLoggedInUser;
}

// Editing self (display, email)
Expand Down
35 changes: 30 additions & 5 deletions apps/provisioning_api/tests/Controller/UsersControllerTest.php
Expand Up @@ -66,6 +66,8 @@
use OCP\Mail\IEMailTemplate;
use OCP\Security\Events\GenerateSecurePasswordEvent;
use OCP\Security\ISecureRandom;
use OCP\User\Backend\IGetDisplayNameBackend;
use OCP\User\Backend\ISetDisplayNameBackend;
use OCP\UserInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -1444,6 +1446,10 @@ public function testEditUserRegularUserSelfEditChangeDisplayName() {
->method('get')
->with('UserToEdit')
->willReturn($targetUser);
$targetUser
->expects($this->once())
->method('getBackend')
->willReturn($this->createMock(ISetDisplayNameBackend::class));
$targetUser
->expects($this->once())
->method('setDisplayName')
Expand Down Expand Up @@ -3717,39 +3723,58 @@ public function testResendWelcomeMessageFailed() {

public function dataGetEditableFields() {
return [
[false, [
[false, ISetDisplayNameBackend::class, [
IAccountManager::PROPERTY_PHONE,
IAccountManager::PROPERTY_ADDRESS,
IAccountManager::PROPERTY_WEBSITE,
IAccountManager::PROPERTY_TWITTER,
]],
[ true, [
[true, ISetDisplayNameBackend::class, [
IAccountManager::PROPERTY_DISPLAYNAME,
IAccountManager::PROPERTY_EMAIL,
IAccountManager::PROPERTY_PHONE,
IAccountManager::PROPERTY_ADDRESS,
IAccountManager::PROPERTY_WEBSITE,
IAccountManager::PROPERTY_TWITTER,
]]
]],
[true, IGetDisplayNameBackend::class, [
IAccountManager::PROPERTY_EMAIL,
IAccountManager::PROPERTY_PHONE,
IAccountManager::PROPERTY_ADDRESS,
IAccountManager::PROPERTY_WEBSITE,
IAccountManager::PROPERTY_TWITTER,
]],
];
}

/**
* @dataProvider dataGetEditableFields
*
* @param bool $allowedToChangeDisplayName
* @param string $userBackend
* @param array $expected
*/
public function testGetEditableFields(bool $allowedToChangeDisplayName, array $expected) {
public function testGetEditableFields(bool $allowedToChangeDisplayName, string $userBackend, array $expected) {
$this->config
->method('getSystemValue')
->with(
$this->equalTo('allow_user_to_change_display_name'),
$this->anything()
)->willReturn($allowedToChangeDisplayName);

$user = $this->createMock(IUser::class);
$this->userSession->method('getUser')
->willReturn($user);

$backend = $this->createMock($userBackend);

$user->method('getUID')
->willReturn('userId');
$user->method('getBackend')
->willReturn($backend);

$expectedResp = new DataResponse($expected);
$this->assertEquals($expectedResp, $this->api->getEditableFields());
$this->assertEquals($expectedResp, $this->api->getEditableFields('userId'));
}

private function mockAccount($targetUser, $accountProperties) {
Expand Down

0 comments on commit 396ad3a

Please sign in to comment.