diff options
author | Morris Jobke <hey@morrisjobke.de> | 2016-11-25 11:40:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-25 11:40:20 +0100 |
commit | 64fb0fb3dd02fe9fd5f4543e7248137b7e9fcbcd (patch) | |
tree | 03c70a33e480b312451a72a08cdb286fcfdb2216 /tests | |
parent | 1967b9112c237f8ed825d3fc664edf96e97f9514 (diff) | |
parent | 0de685c5629deb66984e536e5fd20f140228aa3f (diff) | |
download | nextcloud-server-64fb0fb3dd02fe9fd5f4543e7248137b7e9fcbcd.tar.gz nextcloud-server-64fb0fb3dd02fe9fd5f4543e7248137b7e9fcbcd.zip |
Merge pull request #2276 from nextcloud/update-email-address
Update email address
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Settings/Controller/UsersControllerTest.php | 323 | ||||
-rw-r--r-- | tests/lib/Accounts/AccountsManagerTest.php | 39 | ||||
-rw-r--r-- | tests/lib/Accounts/HooksTest.php | 157 |
3 files changed, 484 insertions, 35 deletions
diff --git a/tests/Settings/Controller/UsersControllerTest.php b/tests/Settings/Controller/UsersControllerTest.php index ec92479b40e..69082a7929c 100644 --- a/tests/Settings/Controller/UsersControllerTest.php +++ b/tests/Settings/Controller/UsersControllerTest.php @@ -103,27 +103,51 @@ class UsersControllerTest extends \Test\TestCase { /** * @param bool $isAdmin - * @return UsersController + * @return UsersController | \PHPUnit_Framework_MockObject_MockObject */ - protected function getController($isAdmin) { - return new UsersController( - 'settings', - $this->createMock(IRequest::class), - $this->userManager, - $this->groupManager, - $this->userSession, - $this->config, - $isAdmin, - $this->l, - $this->logger, - $this->defaults, - $this->mailer, - 'no-reply@owncloud.com', - $this->urlGenerator, - $this->appManager, - $this->avatarManager, - $this->accountManager - ); + protected function getController($isAdmin = false, $mockedMethods = []) { + if (empty($mockedMethods)) { + return new UsersController( + 'settings', + $this->createMock(IRequest::class), + $this->userManager, + $this->groupManager, + $this->userSession, + $this->config, + $isAdmin, + $this->l, + $this->logger, + $this->defaults, + $this->mailer, + 'no-reply@owncloud.com', + $this->urlGenerator, + $this->appManager, + $this->avatarManager, + $this->accountManager + ); + } else { + return $this->getMockBuilder(UsersController::class) + ->setConstructorArgs( + [ + 'settings', + $this->createMock(IRequest::class), + $this->userManager, + $this->groupManager, + $this->userSession, + $this->config, + $isAdmin, + $this->l, + $this->logger, + $this->defaults, + $this->mailer, + 'no-reply@owncloud.com', + $this->urlGenerator, + $this->appManager, + $this->avatarManager, + $this->accountManager + ] + )->setMethods($mockedMethods)->getMock(); + } } public function testIndexAdmin() { @@ -2010,4 +2034,263 @@ class UsersControllerTest extends \Test\TestCase { $response = $controller->setDisplayName($user->getUID(), 'newDisplayName'); $this->assertEquals($expectedResponse, $response); } + + /** + * @dataProvider dataTestSetUserSettings + * + * @param string $email + * @param bool $validEmail + * @param $expectedStatus + */ + public function testSetUserSettings($email, $validEmail, $expectedStatus) { + $controller = $this->getController(false, ['saveUserSettings']); + $user = $this->createMock(IUser::class); + + $this->userSession->method('getUser')->willReturn($user); + + if (!empty($email) && $validEmail) { + $this->mailer->expects($this->once())->method('validateMailAddress') + ->willReturn($validEmail); + } + + $saveData = (!empty($email) && $validEmail) || empty($email); + + if ($saveData) { + $controller->expects($this->once())->method('saveUserSettings'); + } else { + $controller->expects($this->never())->method('saveUserSettings'); + } + + $result = $controller->setUserSettings( + AccountManager::VISIBILITY_CONTACTS_ONLY, + 'displayName', + AccountManager::VISIBILITY_CONTACTS_ONLY, + '47658468', + AccountManager::VISIBILITY_CONTACTS_ONLY, + $email, + AccountManager::VISIBILITY_CONTACTS_ONLY, + 'nextcloud.com', + AccountManager::VISIBILITY_CONTACTS_ONLY, + 'street and city', + AccountManager::VISIBILITY_CONTACTS_ONLY, + '@nextclouders', + AccountManager::VISIBILITY_CONTACTS_ONLY + ); + + $this->assertSame($expectedStatus, $result->getStatus()); + } + + public function dataTestSetUserSettings() { + return [ + ['', true, Http::STATUS_OK], + ['', false, Http::STATUS_OK], + ['example.com', false, Http::STATUS_UNPROCESSABLE_ENTITY], + ['john@example.com', true, Http::STATUS_OK], + ]; + } + + /** + * @dataProvider dataTestSaveUserSettings + * + * @param array $data + * @param string $oldEmailAddress + * @param string $oldDisplayName + */ + public function testSaveUserSettings($data, + $oldEmailAddress, + $oldDisplayName + ) { + $controller = $this->getController(); + $user = $this->createMock(IUser::class); + + $user->method('getDisplayName')->willReturn($oldDisplayName); + $user->method('getEMailAddress')->willReturn($oldEmailAddress); + $user->method('canChangeDisplayName')->willReturn(true); + + if ($data[AccountManager::PROPERTY_EMAIL]['value'] !== $oldEmailAddress) { + $user->expects($this->once())->method('setEMailAddress') + ->with($data[AccountManager::PROPERTY_EMAIL]['value']) + ->willReturn(true); + } else { + $user->expects($this->never())->method('setEMailAddress'); + } + + if ($data[AccountManager::PROPERTY_DISPLAYNAME]['value'] !== $oldDisplayName) { + $user->expects($this->once())->method('setDisplayName') + ->with($data[AccountManager::PROPERTY_DISPLAYNAME]['value']) + ->willReturn(true); + } else { + $user->expects($this->never())->method('setDisplayName'); + } + + $this->accountManager->expects($this->once())->method('updateUser') + ->with($user, $data); + + $this->invokePrivate($controller, 'saveUserSettings', [$user, $data]); + } + + public function dataTestSaveUserSettings() { + return [ + [ + [ + AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'], + ], + 'john@example.com', + 'john doe' + ], + [ + [ + AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'], + ], + 'johnNew@example.com', + 'john New doe' + ], + [ + [ + AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'], + ], + 'johnNew@example.com', + 'john doe' + ], + [ + [ + AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'], + ], + 'john@example.com', + 'john New doe' + ] + + ]; + } + + /** + * @dataProvider dataTestSaveUserSettingsException + * + * @param array $data + * @param string $oldEmailAddress + * @param string $oldDisplayName + * @param bool $setDisplayNameResult + * @param bool $canChangeEmail + * + * @expectedException \OC\ForbiddenException + */ + public function testSaveUserSettingsException($data, + $oldEmailAddress, + $oldDisplayName, + $setDisplayNameResult, + $canChangeEmail + ) { + $controller = $this->getController(); + $user = $this->createMock(IUser::class); + + $user->method('getDisplayName')->willReturn($oldDisplayName); + $user->method('getEMailAddress')->willReturn($oldEmailAddress); + + if ($data[AccountManager::PROPERTY_EMAIL]['value'] !== $oldEmailAddress) { + $user->method('canChangeDisplayName') + ->willReturn($canChangeEmail); + } + + if ($data[AccountManager::PROPERTY_DISPLAYNAME]['value'] !== $oldDisplayName) { + $user->method('setDisplayName') + ->with($data[AccountManager::PROPERTY_DISPLAYNAME]['value']) + ->willReturn($setDisplayNameResult); + } + + $this->invokePrivate($controller, 'saveUserSettings', [$user, $data]); + } + + + public function dataTestSaveUserSettingsException() { + return [ + [ + [ + AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'], + ], + 'johnNew@example.com', + 'john New doe', + true, + false + ], + [ + [ + AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'], + ], + 'johnNew@example.com', + 'john New doe', + false, + true + ], + [ + [ + AccountManager::PROPERTY_EMAIL => ['value' => 'john@example.com'], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'john doe'], + ], + 'johnNew@example.com', + 'john New doe', + false, + false + ], + + ]; + } + + /** + * @return array + */ + public function setEmailAddressData() { + return [ + /* mailAddress, isValid, expectsUpdate, canChangeDisplayName, responseCode */ + [ '', true, true, true, Http::STATUS_OK ], + [ 'foo@local', true, true, true, Http::STATUS_OK], + [ 'foo@bar@local', false, false, true, Http::STATUS_UNPROCESSABLE_ENTITY], + [ 'foo@local', true, false, false, Http::STATUS_FORBIDDEN], + ]; + } + /** + * @dataProvider setEmailAddressData + * + */ + public function testSetEMailAddress($mailAddress, $isValid, $expectsUpdate, $canChangeDisplayName, $responseCode) { + $user = $this->getMockBuilder('\OC\User\User') + ->disableOriginalConstructor()->getMock(); + $user + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('foo')); + $user + ->expects($this->any()) + ->method('canChangeDisplayName') + ->will($this->returnValue($canChangeDisplayName)); + $user + ->expects($expectsUpdate ? $this->once() : $this->never()) + ->method('setEMailAddress') + ->with( + $this->equalTo($mailAddress) + ); + $this->mailer + ->expects($this->any()) + ->method('validateMailAddress') + ->with($mailAddress) + ->willReturn($isValid); + if ($isValid) { + $user->expects($this->atLeastOnce()) + ->method('canChangeDisplayName') + ->willReturn(true); + $this->userManager + ->expects($this->atLeastOnce()) + ->method('get') + ->with('foo') + ->will($this->returnValue($user)); + } + $controller = $this->getController(true); + $response = $controller->setEMailAddress($user->getUID(), $mailAddress); + $this->assertSame($responseCode, $response->getStatus()); + } } diff --git a/tests/lib/Accounts/AccountsManagerTest.php b/tests/lib/Accounts/AccountsManagerTest.php index de88fbcab97..60811140e72 100644 --- a/tests/lib/Accounts/AccountsManagerTest.php +++ b/tests/lib/Accounts/AccountsManagerTest.php @@ -78,36 +78,45 @@ class AccountsManagerTest extends TestCase { * * @param bool $userAlreadyExists */ - public function testUpdateUser($userAlreadyExists) { + public function testUpdateUser($newData, $oldData, $insertNew, $updateExisitng) { $accountManager = $this->getInstance(['getUser', 'insertNewUser', 'updateExistingUser']); $user = $this->getMockBuilder('OCP\IUser')->getMock(); - $accountManager->expects($this->once())->method('getUser')->with($user)->willReturn($userAlreadyExists); + $accountManager->expects($this->once())->method('getUser')->with($user)->willReturn($oldData); - if ($userAlreadyExists) { + if ($updateExisitng) { $accountManager->expects($this->once())->method('updateExistingUser') - ->with($user, 'data'); + ->with($user, $newData); $accountManager->expects($this->never())->method('insertNewUser'); - } else { + } + if ($insertNew) { $accountManager->expects($this->once())->method('insertNewUser') - ->with($user, 'data'); + ->with($user, $newData); $accountManager->expects($this->never())->method('updateExistingUser'); } - $this->eventDispatcher->expects($this->once())->method('dispatch') - ->willReturnCallback(function($eventName, $event) use ($user) { - $this->assertSame('OC\AccountManager::userUpdated', $eventName); - $this->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event); - } - ); + if (!$insertNew && !$updateExisitng) { + $accountManager->expects($this->never())->method('updateExistingUser'); + $accountManager->expects($this->never())->method('insertNewUser'); + $this->eventDispatcher->expects($this->never())->method('dispatch'); + } else { + $this->eventDispatcher->expects($this->once())->method('dispatch') + ->willReturnCallback( + function ($eventName, $event) use ($user) { + $this->assertSame('OC\AccountManager::userUpdated', $eventName); + $this->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event); + } + ); + } - $accountManager->updateUser($user, 'data'); + $accountManager->updateUser($user, $newData); } public function dataTrueFalse() { return [ - [true], - [false] + [['newData'], ['oldData'], false, true], + [['newData'], [], true, false], + [['oldData'], ['oldData'], false, false] ]; } diff --git a/tests/lib/Accounts/HooksTest.php b/tests/lib/Accounts/HooksTest.php new file mode 100644 index 00000000000..071e78146ea --- /dev/null +++ b/tests/lib/Accounts/HooksTest.php @@ -0,0 +1,157 @@ +<?php +/** + * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + + +namespace Test\Accounts; + + +use OC\Accounts\AccountManager; +use OC\Accounts\Hooks; +use OCP\ILogger; +use OCP\IUser; +use Test\TestCase; + +/** + * Class HooksTest + * + * @package Test\Accounts + * @group DB + */ +class HooksTest extends TestCase { + + /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject */ + private $logger; + + /** @var AccountManager | \PHPUnit_Framework_MockObject_MockObject */ + private $accountManager; + + /** @var Hooks | \PHPUnit_Framework_MockObject_MockObject */ + private $hooks; + + public function setUp() { + parent::setUp(); + + $this->logger = $this->createMock(ILogger::class); + $this->accountManager = $this->getMockBuilder(AccountManager::class) + ->disableOriginalConstructor()->getMock(); + + $this->hooks = $this->getMockBuilder(Hooks::class) + ->setConstructorArgs([$this->logger]) + ->setMethods(['getAccountManager']) + ->getMock(); + + $this->hooks->method('getAccountManager')->willReturn($this->accountManager); + } + + /** + * @dataProvider dataTestChangeUserHook + * + * @param $params + * @param $data + * @param $setEmail + * @param $setDisplayName + * @param $error + */ + public function testChangeUserHook($params, $data, $setEmail, $setDisplayName, $error) { + if ($error) { + $this->accountManager->expects($this->never())->method('getUser'); + $this->accountManager->expects($this->never())->method('updateUser'); + } else { + $this->accountManager->expects($this->once())->method('getUser')->willReturn($data); + $newData = $data; + if ($setEmail) { + $newData[AccountManager::PROPERTY_EMAIL]['value'] = $params['value']; + $this->accountManager->expects($this->once())->method('updateUser') + ->with($params['user'], $newData); + } elseif ($setDisplayName) { + $newData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $params['value']; + $this->accountManager->expects($this->once())->method('updateUser') + ->with($params['user'], $newData); + } else { + $this->accountManager->expects($this->never())->method('updateUser'); + } + } + + $this->hooks->changeUserHook($params); + + } + + public function dataTestChangeUserHook() { + $user = $this->createMock(IUser::class); + return [ + [ + ['feature' => '', 'value' => ''], + [ + AccountManager::PROPERTY_EMAIL => ['value' => ''], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => ''] + ], + false, false, true + ], + [ + ['user' => $user, 'value' => ''], + [ + AccountManager::PROPERTY_EMAIL => ['value' => ''], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => ''] + ], + false, false, true + ], + [ + ['user' => $user, 'feature' => ''], + [ + AccountManager::PROPERTY_EMAIL => ['value' => ''], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => ''] + ], + false, false, true + ], + [ + ['user' => $user, 'feature' => 'foo', 'value' => 'bar'], + [ + AccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName'] + ], + false, false, false + ], + [ + ['user' => $user, 'feature' => 'eMailAddress', 'value' => 'newMail@example.com'], + [ + AccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName'] + ], + true, false, false + ], + [ + ['user' => $user, 'feature' => 'displayName', 'value' => 'newDisplayName'], + [ + AccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'], + AccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName'] + ], + false, true, false + ], + ]; + } + + public function testGetAccountManager() { + $hooks = new Hooks($this->logger); + $result = $this->invokePrivate($hooks, 'getAccountManager'); + $this->assertInstanceOf(AccountManager::class, $result); + } + +} |