diff options
Diffstat (limited to 'apps/provisioning_api/tests/Controller/UsersControllerTest.php')
-rw-r--r-- | apps/provisioning_api/tests/Controller/UsersControllerTest.php | 100 |
1 files changed, 71 insertions, 29 deletions
diff --git a/apps/provisioning_api/tests/Controller/UsersControllerTest.php b/apps/provisioning_api/tests/Controller/UsersControllerTest.php index 80d6d0f6152..e838cd16633 100644 --- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php +++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php @@ -20,6 +20,7 @@ use OCP\Accounts\IAccount; use OCP\Accounts\IAccountManager; use OCP\Accounts\IAccountProperty; use OCP\Accounts\IAccountPropertyCollection; +use OCP\App\IAppManager; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCS\OCSException; use OCP\EventDispatcher\IEventDispatcher; @@ -64,6 +65,7 @@ class UsersControllerTest extends TestCase { private IEventDispatcher&MockObject $eventDispatcher; private IRootFolder $rootFolder; private IPhoneNumberUtil $phoneNumberUtil; + private IAppManager $appManager; protected function setUp(): void { parent::setUp(); @@ -84,6 +86,7 @@ class UsersControllerTest extends TestCase { $this->knownUserService = $this->createMock(KnownUserService::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->phoneNumberUtil = new PhoneNumberUtil(); + $this->appManager = $this->createMock(IAppManager::class); $this->rootFolder = $this->createMock(IRootFolder::class); $l10n = $this->createMock(IL10N::class); @@ -110,6 +113,7 @@ class UsersControllerTest extends TestCase { $this->knownUserService, $this->eventDispatcher, $this->phoneNumberUtil, + $this->appManager, ]) ->onlyMethods(['fillStorageInfo']) ->getMock(); @@ -195,8 +199,7 @@ class UsersControllerTest extends TestCase { ->willReturn($subAdminManager); $this->groupManager ->expects($this->any()) - ->method('displayNamesInGroup') - ->will($this->onConsecutiveCalls(['AnotherUserInTheFirstGroup' => []], ['UserInTheSecondGroup' => []])); + ->method('displayNamesInGroup')->willReturnOnConsecutiveCalls(['AnotherUserInTheFirstGroup' => []], ['UserInTheSecondGroup' => []]); $expected = [ 'users' => [ @@ -449,7 +452,8 @@ class UsersControllerTest extends TestCase { $this->userManager ->expects($this->once()) ->method('createUser') - ->with('NewUser', 'PasswordOfTheNewUser'); + ->with('NewUser', 'PasswordOfTheNewUser') + ->willReturn($this->createMock(IUser::class)); $this->logger ->expects($this->once()) ->method('info') @@ -501,6 +505,7 @@ class UsersControllerTest extends TestCase { $this->knownUserService, $this->eventDispatcher, $this->phoneNumberUtil, + $this->appManager, ]) ->onlyMethods(['editUser']) ->getMock(); @@ -513,7 +518,8 @@ class UsersControllerTest extends TestCase { $this->userManager ->expects($this->once()) ->method('createUser') - ->with('NewUser', 'PasswordOfTheNewUser'); + ->with('NewUser', 'PasswordOfTheNewUser') + ->willReturn($this->createMock(IUser::class)); $this->logger ->expects($this->once()) ->method('info') @@ -563,7 +569,8 @@ class UsersControllerTest extends TestCase { $this->userManager ->expects($this->once()) ->method('createUser') - ->with($this->anything(), 'PasswordOfTheNewUser'); + ->with($this->anything(), 'PasswordOfTheNewUser') + ->willReturn($this->createMock(IUser::class)); $this->logger ->expects($this->once()) ->method('info') @@ -605,7 +612,7 @@ class UsersControllerTest extends TestCase { ->willReturn(false); $newUser = $this->createMock(IUser::class); $newUser->expects($this->once()) - ->method('setEMailAddress'); + ->method('setSystemEMailAddress'); $this->userManager ->expects($this->once()) ->method('createUser') @@ -641,6 +648,51 @@ class UsersControllerTest extends TestCase { )); } + public function testAddUserSuccessfulLowercaseEmail(): void { + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('NewUser') + ->willReturn(false); + $newUser = $this->createMock(IUser::class); + $newUser->expects($this->once()) + ->method('setSystemEMailAddress') + ->with('foo@bar.com'); + $this->userManager + ->expects($this->once()) + ->method('createUser') + ->willReturn($newUser); + $this->logger + ->expects($this->once()) + ->method('info') + ->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']); + $loggedInUser = $this->getMockBuilder(IUser::class) + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->exactly(2)) + ->method('getUID') + ->willReturn('adminUser'); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->willReturn($loggedInUser); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('adminUser') + ->willReturn(true); + $this->eventDispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with(new GenerateSecurePasswordEvent()); + + $this->assertTrue(key_exists( + 'id', + $this->api->addUser('NewUser', '', '', 'fOo@BaR.CoM')->getData() + )); + } + public function testAddUserFailedToGenerateUserID(): void { $this->expectException(OCSException::class); @@ -785,7 +837,7 @@ class UsersControllerTest extends TestCase { $this->logger ->expects($this->exactly(2)) ->method('info') - ->willReturnCallback(function () use (&$calls) { + ->willReturnCallback(function () use (&$calls): void { $expected = array_shift($calls); $this->assertEquals($expected, func_get_args()); }); @@ -809,7 +861,7 @@ class UsersControllerTest extends TestCase { ->expects($this->once()) ->method('createUser') ->with('NewUser', 'PasswordOfTheNewUser') - ->will($this->throwException($exception)); + ->willThrowException($exception); $this->logger ->expects($this->once()) ->method('error') @@ -989,7 +1041,7 @@ class UsersControllerTest extends TestCase { $this->logger ->expects($this->exactly(3)) ->method('info') - ->willReturnCallback(function () use (&$calls) { + ->willReturnCallback(function () use (&$calls): void { $expected = array_shift($calls); $this->assertEquals($expected, func_get_args()); }); @@ -1529,9 +1581,7 @@ class UsersControllerTest extends TestCase { ]; } - /** - * @dataProvider dataSearchByPhoneNumbers - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataSearchByPhoneNumbers')] public function testSearchByPhoneNumbers(string $location, array $search, int $status, ?array $searchUsers, ?array $userMatches, array $expected): void { $knownTo = 'knownTo'; $user = $this->createMock(IUser::class); @@ -1627,7 +1677,7 @@ class UsersControllerTest extends TestCase { ->willReturn($targetUser); $targetUser ->expects($this->once()) - ->method('setEMailAddress') + ->method('setSystemEMailAddress') ->with('demo@nextcloud.com'); $targetUser ->expects($this->any()) @@ -1857,9 +1907,7 @@ class UsersControllerTest extends TestCase { ]; } - /** - * @dataProvider selfEditChangePropertyProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('selfEditChangePropertyProvider')] public function testEditUserRegularUserSelfEditChangeProperty($propertyName, $oldValue, $newValue): void { $loggedInUser = $this->getMockBuilder(IUser::class) ->disableOriginalConstructor() @@ -1935,9 +1983,7 @@ class UsersControllerTest extends TestCase { ]; } - /** - * @dataProvider selfEditChangePropertyProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('selfEditChangePropertyProvider')] public function testEditUserRegularUserSelfEditChangePropertyScope($propertyName, $oldScope, $newScope): void { $loggedInUser = $this->getMockBuilder(IUser::class) ->disableOriginalConstructor() @@ -2272,9 +2318,7 @@ class UsersControllerTest extends TestCase { ]; } - /** - * @dataProvider dataEditUserSelfEditChangeLanguageButForced - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataEditUserSelfEditChangeLanguageButForced')] public function testEditUserSelfEditChangeLanguageButForced($forced): void { $this->expectException(OCSException::class); @@ -2368,9 +2412,7 @@ class UsersControllerTest extends TestCase { $this->assertEquals([], $this->api->editUser('UserToEdit', 'language', 'de')->getData()); } - /** - * @dataProvider dataEditUserSelfEditChangeLanguageButForced - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataEditUserSelfEditChangeLanguageButForced')] public function testEditUserAdminEditChangeLanguageInvalidLanguage(): void { $this->expectException(OCSException::class); @@ -3796,6 +3838,7 @@ class UsersControllerTest extends TestCase { $this->knownUserService, $this->eventDispatcher, $this->phoneNumberUtil, + $this->appManager, ]) ->onlyMethods(['getUserData']) ->getMock(); @@ -3887,6 +3930,7 @@ class UsersControllerTest extends TestCase { $this->knownUserService, $this->eventDispatcher, $this->phoneNumberUtil, + $this->appManager, ]) ->onlyMethods(['getUserData']) ->getMock(); @@ -4356,9 +4400,7 @@ class UsersControllerTest extends TestCase { ]; } - /** - * @dataProvider dataGetEditableFields - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetEditableFields')] public function testGetEditableFields(bool $allowedToChangeDisplayName, bool $allowedToChangeEmail, string $userBackend, array $expected): void { $this->config->method('getSystemValue')->willReturnCallback(fn (string $key, mixed $default) => match ($key) { 'allow_user_to_change_display_name' => $allowedToChangeDisplayName, @@ -4393,7 +4435,7 @@ class UsersControllerTest extends TestCase { $account = $this->createMock(IAccount::class); $account->method('getProperty') - ->will($this->returnValueMap($mockedProperties)); + ->willReturnMap($mockedProperties); $this->accountManager->expects($this->any())->method('getAccount') ->with($targetUser) |