public function testTagManagerWithoutUserReturnsNull() { $this->userSession = $this->getMock('\\OCP\\IUserSession'); $this->userSession->expects($this->any())->method('getUser')->will($this->returnValue(null)); $this->tagMgr = new OC\TagManager($this->tagMapper, $this->userSession); $this->assertNull($this->tagMgr->load($this->objectType)); }
/** * @dataProvider usersProvider * @param $query * @param $uid */ public function testImpersonate($query, $uid) { $user = $this->getMock('\\OCP\\IUser'); $user->expects($this->once())->method('getUID')->will($this->returnValue($uid)); $this->userManager->expects($this->once())->method('search')->with($query, 1, 0)->will($this->returnValue([$user])); $this->userSession->expects($this->once())->method('setUser')->with($user); $this->assertEquals(new JSONResponse(), $this->controller->impersonate($query)); }
protected function prepareForInitCollections() { $this->user->expects($this->any())->method('getUID')->will($this->returnValue('alice')); $this->userSession->expects($this->once())->method('getUser')->will($this->returnValue($this->user)); $this->dispatcher->addListener(CommentsEntityEvent::EVENT_ENTITY, function (CommentsEntityEvent $event) { $event->addEntityCollection('files', function () { return true; }); }); }
/** * test updatePrivateKeyPassword() if wrong new password was entered */ public function testUpdatePrivateKeyPasswordWrongNewPassword() { $oldPassword = '******'; $newPassword = '******'; $this->userSessionMock->expects($this->once())->method('getUID')->willReturn('uid'); $this->userManagerMock->expects($this->exactly(2))->method('checkPassword')->willReturn(false); $result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword); $data = $result->getData(); $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus()); $this->assertSame('The current log-in password was not correct, please try again.', $data['message']); }
public function setUp() { parent::setUp(); $this->tree = $this->getMockBuilder('\\Sabre\\DAV\\Tree')->disableOriginalConstructor()->getMock(); $this->server = new \Sabre\DAV\Server($this->tree); $this->tagManager = $this->getMock('\\OCP\\SystemTag\\ISystemTagManager'); $this->groupManager = $this->getMock('\\OCP\\IGroupManager'); $this->user = $this->getMock('\\OCP\\IUser'); $this->userSession = $this->getMock('\\OCP\\IUserSession'); $this->userSession->expects($this->any())->method('getUser')->willReturn($this->user); $this->userSession->expects($this->any())->method('isLoggedIn')->willReturn(true); $this->plugin = new \OCA\DAV\SystemTag\SystemTagPlugin($this->tagManager, $this->groupManager, $this->userSession); $this->plugin->initialize($this->server); }
public function setUp() { parent::setUp(); $this->request = $this->getMock('\\OCP\\IRequest'); $this->urlGenerator = $this->getMock('\\OCP\\IURLGenerator'); $this->navigationManager = $this->getMock('\\OCP\\INavigationManager'); $this->l10n = $this->getMock('\\OCP\\IL10N'); $this->config = $this->getMock('\\OCP\\IConfig'); $this->eventDispatcher = $this->getMock('\\Symfony\\Component\\EventDispatcher\\EventDispatcherInterface'); $this->userSession = $this->getMock('\\OCP\\IUserSession'); $this->user = $this->getMock('\\OCP\\IUser'); $this->userSession->expects($this->any())->method('getUser')->will($this->returnValue($this->user)); $this->viewController = $this->getMockBuilder('\\OCA\\Files\\Controller\\ViewController')->setConstructorArgs(['files', $this->request, $this->urlGenerator, $this->navigationManager, $this->l10n, $this->config, $this->eventDispatcher, $this->userSession])->setMethods(['getStorageInfo', 'renderScript'])->getMock(); }
protected function mockUserSession($user) { $mockUser = $this->getMockBuilder('\\OCP\\IUser')->disableOriginalConstructor()->getMock(); $mockUser->expects($this->any())->method('getUID')->willReturn($user); $this->session->expects($this->any())->method('isLoggedIn')->willReturn(true); $this->session->expects($this->any())->method('getUser')->willReturn($mockUser); }
/** * @dataProvider dataLoadingJSAndCSS * @param string $scriptName * @param string $pathInfo * @param IUser|null $user * @param bool $scriptsAdded */ public function testLoadingJSAndCSS($scriptName, $pathInfo, $user, $scriptsAdded) { $this->request->expects($this->any())->method('getScriptName')->willReturn($scriptName); $this->request->expects($this->any())->method('getPathInfo')->willReturn($pathInfo); $this->session->expects($this->once())->method('getUser')->willReturn($user); \OC_Util::$scripts = []; \OC_Util::$styles = []; include __DIR__ . '/../../appinfo/app.php'; if ($scriptsAdded) { $this->assertNotEmpty(\OC_Util::$scripts); $this->assertNotEmpty(\OC_Util::$styles); } else { $this->assertEmpty(\OC_Util::$scripts); $this->assertEmpty(\OC_Util::$styles); } }
public function setUp() { parent::setUp(); $this->tree = $this->getMockBuilder('\\Sabre\\DAV\\Tree')->disableOriginalConstructor()->getMock(); $this->view = $this->getMockBuilder('\\OC\\Files\\View')->disableOriginalConstructor()->getMock(); $this->server = $this->getMockBuilder('\\Sabre\\DAV\\Server')->setConstructorArgs([$this->tree])->setMethods(['getRequestUri'])->getMock(); $this->groupManager = $this->getMockBuilder('\\OCP\\IGroupManager')->disableOriginalConstructor()->getMock(); $this->userFolder = $this->getMockBuilder('\\OCP\\Files\\Folder')->disableOriginalConstructor()->getMock(); $this->tagManager = $this->getMock('\\OCP\\SystemTag\\ISystemTagManager'); $this->tagMapper = $this->getMock('\\OCP\\SystemTag\\ISystemTagObjectMapper'); $this->userSession = $this->getMock('\\OCP\\IUserSession'); $user = $this->getMock('\\OCP\\IUser'); $user->expects($this->any())->method('getUID')->will($this->returnValue('testuser')); $this->userSession->expects($this->any())->method('getUser')->will($this->returnValue($user)); $this->plugin = new FilesReportPluginImplementation($this->tree, $this->view, $this->tagManager, $this->tagMapper, $this->userSession, $this->groupManager, $this->userFolder); }
/** * @expectedException \Sabre\DAV\Exception\BadRequest * @expectedExceptionMessage Message exceeds allowed character limit of */ public function testCreateCommentMessageTooLong() { $commentData = ['actorType' => 'users', 'verb' => 'comment', 'message' => str_pad('', IComment::MAX_MESSAGE_LENGTH + 1, 'x')]; $comment = new Comment(['objectType' => 'files', 'objectId' => '42', 'actorType' => 'users', 'actorId' => 'alice', 'verb' => 'comment']); $comment->setId('23'); $path = 'comments/files/42'; $requestData = json_encode($commentData); $user = $this->getMock('OCP\\IUser'); $user->expects($this->once())->method('getUID')->will($this->returnValue('alice')); $node = $this->getMockBuilder('\\OCA\\DAV\\Comments\\EntityCollection')->disableOriginalConstructor()->getMock(); $node->expects($this->once())->method('getName')->will($this->returnValue('files')); $node->expects($this->once())->method('getId')->will($this->returnValue('42')); $node->expects($this->never())->method('setReadMarker'); $this->commentsManager->expects($this->once())->method('create')->with('users', 'alice', 'files', '42')->will($this->returnValue($comment)); $this->userSession->expects($this->once())->method('getUser')->will($this->returnValue($user)); // technically, this is a shortcut. Inbetween EntityTypeCollection would // be returned, but doing it exactly right would not be really // unit-testing like, as it would require to haul in a lot of other // things. $this->tree->expects($this->any())->method('getNodeForPath')->with('/' . $path)->will($this->returnValue($node)); $request = $this->getMockBuilder('Sabre\\HTTP\\RequestInterface')->disableOriginalConstructor()->getMock(); $response = $this->getMockBuilder('Sabre\\HTTP\\ResponseInterface')->disableOriginalConstructor()->getMock(); $request->expects($this->once())->method('getPath')->will($this->returnValue('/' . $path)); $request->expects($this->once())->method('getBodyAsString')->will($this->returnValue($requestData)); $request->expects($this->once())->method('getHeader')->with('Content-Type')->will($this->returnValue('application/json')); $response->expects($this->never())->method('setHeader'); $this->server->expects($this->any())->method('getRequestUri')->will($this->returnValue($path)); $this->plugin->initialize($this->server); $this->plugin->httpPost($request, $response); }
/** * @expectedException \OutOfBoundsException */ public function testReadParameterUserInvalid() { $this->data->expects($this->once())->method('validateFilter')->willReturnArgument(0); $this->userSession->expects($this->once())->method('getUser')->willReturn(null); $this->invokePrivate($this->controller, 'readParameters', [[]]); $this->assertSame(null, $this->invokePrivate($this->controller, 'user')); }
public function testIsEnabledForUserLoggedIn() { $user = new User('user1', null); $this->userSession->expects($this->once())->method('getUser')->will($this->returnValue($user)); $this->groupManager->expects($this->once())->method('getUserGroupIds')->with($user)->will($this->returnValue(array('foo', 'bar'))); $this->appConfig->setValue('test', 'enabled', '["foo"]'); $this->assertTrue($this->manager->isEnabledForUser('test')); }
/** * @expectedException \Sabre\DAV\Exception\NotAuthenticated * @expectedExceptionMessage Username or password does not match */ public function testAuthenticateInvalidCredentials() { $server = $this->getMockBuilder('\\Sabre\\DAV\\Server')->disableOriginalConstructor()->getMock(); $server->httpRequest = $this->getMockBuilder('\\Sabre\\HTTP\\RequestInterface')->disableOriginalConstructor()->getMock(); $server->httpRequest->expects($this->once())->method('getHeader')->with('Authorization')->will($this->returnValue('basic dXNlcm5hbWU6cGFzc3dvcmQ=')); $server->httpResponse = $this->getMockBuilder('\\Sabre\\HTTP\\ResponseInterface')->disableOriginalConstructor()->getMock(); $this->userSession->expects($this->once())->method('login')->with('username', 'password')->will($this->returnValue(false)); $this->auth->authenticate($server, 'TestRealm'); }
public function testShowLoginFormForUserNamedNull() { $this->userSession->expects($this->once())->method('isLoggedIn')->willReturn(false); $this->config->expects($this->once())->method('getSystemValue')->with('lost_password_link')->willReturn(false); $user = $this->getMock('\\OCP\\IUser'); $user->expects($this->once())->method('canChangePassword')->willReturn(false); $this->userManager->expects($this->once())->method('get')->with('0')->willReturn($user); $expectedResponse = new TemplateResponse('core', 'login', ['messages' => [], 'loginName' => '0', 'user_autofocus' => false, 'canResetPassword' => false, 'alt_login' => [], 'rememberLoginAllowed' => \OC_Util::rememberLoginAllowed(), 'rememberLoginState' => 0], 'guest'); $this->assertEquals($expectedResponse, $this->loginController->showLoginForm('0', '', '')); }
public function testAuthenticateInvalidCredentials() { $server = $this->getMockBuilder('\\Sabre\\DAV\\Server')->disableOriginalConstructor()->getMock(); $server->httpRequest = $this->getMockBuilder('\\Sabre\\HTTP\\RequestInterface')->disableOriginalConstructor()->getMock(); $server->httpRequest->expects($this->at(0))->method('getHeader')->with('X-Requested-With')->will($this->returnValue(null)); $server->httpRequest->expects($this->at(1))->method('getHeader')->with('Authorization')->will($this->returnValue('basic dXNlcm5hbWU6cGFzc3dvcmQ=')); $server->httpResponse = $this->getMockBuilder('\\Sabre\\HTTP\\ResponseInterface')->disableOriginalConstructor()->getMock(); $this->userSession->expects($this->once())->method('login')->with('username', 'password')->will($this->returnValue(false)); $response = $this->auth->check($server->httpRequest, $server->httpResponse); $this->assertEquals([false, 'Username or password was incorrect'], $response); }
public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStoredUserLanguageAndNotExistingDefaultAndNoAppInScope() { $factory = $this->getFactory(['languageExists']); $this->invokePrivate($factory, 'requestLanguage', ['de']); $factory->expects($this->at(0))->method('languageExists')->with('MyApp', 'de')->willReturn(false); $this->config->expects($this->at(0))->method('getSystemValue')->with('installed', false)->willReturn(true); $user = $this->getMock('\\OCP\\IUser'); $user->expects($this->once())->method('getUID')->willReturn('MyUserUid'); $this->userSession->expects($this->exactly(2))->method('getUser')->willReturn($user); $this->config->expects($this->once())->method('getUserValue')->with('MyUserUid', 'core', 'lang', null)->willReturn('jp'); $factory->expects($this->at(1))->method('languageExists')->with('MyApp', 'jp')->willReturn(false); $this->config->expects($this->at(2))->method('getSystemValue')->with('default_language', false)->willReturn('es'); $factory->expects($this->at(2))->method('languageExists')->with('MyApp', 'es')->willReturn(false); $this->config->expects($this->never())->method('setUserValue')->with('MyUserUid', 'core', 'lang', 'en'); $this->assertSame('en', $factory->findLanguage('MyApp')); }
public function testRemoveFromGroupSuccessful() { $loggedInUser = $this->getMock('\\OCP\\IUser'); $loggedInUser->expects($this->any())->method('getUID')->will($this->returnValue('admin')); $targetUser = $this->getMock('\\OCP\\IUser'); $targetGroup = $this->getMock('\\OCP\\IGroup'); $this->userSession->expects($this->once())->method('getUser')->will($this->returnValue($loggedInUser)); $this->groupManager->expects($this->once())->method('get')->with('admin')->will($this->returnValue($targetGroup)); $this->userManager->expects($this->once())->method('get')->with('AnotherUser')->will($this->returnValue($targetUser)); $subAdminManager = $this->getMockBuilder('\\OC\\Subadmin')->disableOriginalConstructor()->getMock(); $this->groupManager->expects($this->once())->method('getSubAdmin')->will($this->returnValue($subAdminManager)); $this->groupManager->expects($this->any())->method('isAdmin')->with('admin')->will($this->returnValue(true)); $targetGroup->expects($this->once())->method('removeUser')->with($targetUser); $expected = new \OC_OCS_Result(null, 100); $this->assertEquals($expected, $this->api->removeFromGroup(['userid' => 'AnotherUser', '_delete' => ['groupid' => 'admin']])); }
/** * @dataProvider nodeClassProvider * @expectedException \Sabre\DAV\Exception\Conflict */ public function testCreateTagConflict($nodeClass) { $user = $this->getMock('\\OCP\\IUser'); $user->expects($this->once())->method('getUID')->willReturn('admin'); $this->userSession->expects($this->once())->method('isLoggedIn')->willReturn(true); $this->userSession->expects($this->once())->method('getUser')->willReturn($user); $this->groupManager->expects($this->once())->method('isAdmin')->with('admin')->willReturn(true); $requestData = json_encode(['name' => 'Test', 'userVisible' => true, 'userAssignable' => false]); $node = $this->getMockBuilder($nodeClass)->disableOriginalConstructor()->getMock(); $this->tagManager->expects($this->once())->method('createTag')->with('Test', true, false)->will($this->throwException(new TagAlreadyExistsException('Tag already exists'))); $this->tree->expects($this->any())->method('getNodeForPath')->with('/systemtags')->will($this->returnValue($node)); $request = $this->getMockBuilder('Sabre\\HTTP\\RequestInterface')->disableOriginalConstructor()->getMock(); $response = $this->getMockBuilder('Sabre\\HTTP\\ResponseInterface')->disableOriginalConstructor()->getMock(); $request->expects($this->once())->method('getPath')->will($this->returnValue('/systemtags')); $request->expects($this->once())->method('getBodyAsString')->will($this->returnValue($requestData)); $request->expects($this->once())->method('getHeader')->with('Content-Type')->will($this->returnValue('application/json')); $this->plugin->httpPost($request, $response); }
/** * @dataProvider dataRead * * @param IUser $sessionUser * @param int $start * @param int $count * @param string $filter * @param string $user * @param string $expectedUser * @param array $notificationTypes * @param bool $selfSetting * @param string $objectType * @param int $objectId * @param array $conditions * @param array $params * @param string $limitActivities * @param array $parameters */ public function testRead($sessionUser, $start, $count, $filter, $user, $expectedUser, $notificationTypes, $selfSetting, $objectType, $objectId, $conditions, $params, $limitActivities, $parameters) { /** @var \OCA\Activity\GroupHelper|\PHPUnit_Framework_MockObject_MockObject $groupHelper */ $groupHelper = $this->getMockBuilder('OCA\\Activity\\GroupHelper')->disableOriginalConstructor()->getMock(); $groupHelper->expects($expectedUser === null ? $this->never() : $this->once())->method('setUser')->with($expectedUser); /** @var \OCA\Activity\UserSettings|\PHPUnit_Framework_MockObject_MockObject $settings */ $settings = $this->getMockBuilder('OCA\\Activity\\UserSettings')->disableOriginalConstructor()->getMock(); $settings->expects($expectedUser === null ? $this->never() : $this->once())->method('getNotificationTypes')->with($expectedUser, 'stream')->willReturn(['settings']); $settings->expects($selfSetting === null ? $this->never() : $this->any())->method('getUserSetting')->with($expectedUser, 'setting', 'self')->willReturn($selfSetting); /** @var ActivityManager|\PHPUnit_Framework_MockObject_MockObject $activityManager */ $activityManager = $this->getMockBuilder('OCP\\Activity\\IManager')->disableOriginalConstructor()->getMock(); $activityManager->expects($this->any())->method('filterNotificationTypes')->with(['settings'], $filter)->willReturn($notificationTypes); $activityManager->expects($this->any())->method('getQueryForFilter')->with($filter)->willReturn([$conditions, $params]); /** @var \OCA\Activity\Data|\PHPUnit_Framework_MockObject_MockObject $data */ $data = $this->getMockBuilder('OCA\\Activity\\Data')->setConstructorArgs([$activityManager, \OC::$server->getDatabaseConnection(), $this->session])->setMethods(['getActivities'])->getMock(); $data->expects($parameters === null && $limitActivities === null ? $this->never() : $this->once())->method('getActivities')->with($count, $start, $limitActivities, $parameters, $groupHelper)->willReturn([]); $this->session->expects($this->any())->method('getUser')->willReturn($sessionUser); $this->assertEquals([], $data->read($groupHelper, $settings, $start, $count, $filter, $user, $objectType, $objectId)); }
protected function setUp() { parent::setUp(); $this->userSessionMock = $this->getMockBuilder('OCP\\IUserSession')->disableOriginalConstructor()->setMethods(['isLoggedIn', 'getUID', 'login', 'logout', 'setUser', 'getUser'])->getMock(); $this->userSessionMock->expects($this->any())->method('getUID')->will($this->returnValue('admin')); $this->userSessionMock->expects($this->any())->method($this->anything())->will($this->returnSelf()); $this->cryptMock = $this->getMockBuilder('OCA\\Encryption\\Crypto\\Crypt')->disableOriginalConstructor()->getMock(); /** @var \OCP\Security\ISecureRandom $randomMock */ $randomMock = $this->getMock('OCP\\Security\\ISecureRandom'); $this->keyManagerMock = $this->getMockBuilder('OCA\\Encryption\\KeyManager')->disableOriginalConstructor()->getMock(); $this->configMock = $this->getMock('OCP\\IConfig'); /** @var \OCP\Encryption\Keys\IStorage $keyStorageMock */ $keyStorageMock = $this->getMock('OCP\\Encryption\\Keys\\IStorage'); $this->fileMock = $this->getMock('OCP\\Encryption\\IFile'); $this->viewMock = $this->getMock('OC\\Files\\View'); $this->configMock->expects($this->any())->method('setAppValue')->will($this->returnCallback([$this, 'setValueTester'])); $this->configMock->expects($this->any())->method('getAppValue')->will($this->returnCallback([$this, 'getValueTester'])); $this->instance = new Recovery($this->userSessionMock, $this->cryptMock, $randomMock, $this->keyManagerMock, $this->configMock, $keyStorageMock, $this->fileMock, $this->viewMock); }
/** * @dataProvider dataPublish * @param string $author */ public function testPublish($author) { if ($author !== null) { $authorObject = $this->getMockBuilder('OCP\\IUser')->disableOriginalConstructor()->getMock(); $authorObject->expects($this->once())->method('getUID')->willReturn($author); $this->session->expects($this->atLeastOnce())->method('getUser')->willReturn($authorObject); } $event = new \OC\Activity\Event(); $event->setApp('test')->setType('test_type')->setSubject('test_subject', [])->setAffectedUser('test_affected'); $consumer = $this->getMockBuilder('OCP\\Activity\\IConsumer')->disableOriginalConstructor()->getMock(); $consumer->expects($this->once())->method('receive')->with($event)->willReturnCallback(function (\OCP\Activity\IEvent $event) use($author) { $this->assertLessThanOrEqual(time() + 2, $event->getTimestamp(), 'Timestamp not set correctly'); $this->assertGreaterThanOrEqual(time() - 2, $event->getTimestamp(), 'Timestamp not set correctly'); $this->assertSame($author, $event->getAuthor(), 'Author name not set correctly'); }); $this->activityManager->registerConsumer(function () use($consumer) { return $consumer; }); $this->activityManager->publish($event); }
/** * @dataProvider dataGetGroups * * @param string $searchTerm * @param bool $shareWithGroupOnly * @param array $groupResponse * @param array $userGroupsResponse * @param array $exactExpected * @param array $expected * @param bool $reachedEnd * @param mixed $singleGroup */ public function testGetGroups($searchTerm, $shareWithGroupOnly, $groupResponse, $userGroupsResponse, $exactExpected, $expected, $reachedEnd, $singleGroup) { $this->invokePrivate($this->sharees, 'limit', [2]); $this->invokePrivate($this->sharees, 'offset', [0]); $this->invokePrivate($this->sharees, 'shareWithGroupOnly', [$shareWithGroupOnly]); $this->groupManager->expects($this->once())->method('search')->with($searchTerm, $this->invokePrivate($this->sharees, 'limit'), $this->invokePrivate($this->sharees, 'offset'))->willReturn($groupResponse); if ($singleGroup !== false) { $this->groupManager->expects($this->once())->method('get')->with($searchTerm)->willReturn($singleGroup); } if ($shareWithGroupOnly) { $user = $this->getUserMock('admin', 'Administrator'); $this->session->expects($this->any())->method('getUser')->willReturn($user); $numGetUserGroupsCalls = empty($groupResponse) ? 0 : 1; $this->groupManager->expects($this->exactly($numGetUserGroupsCalls))->method('getUserGroups')->with($user)->willReturn($userGroupsResponse); } $this->invokePrivate($this->sharees, 'getGroups', [$searchTerm]); $result = $this->invokePrivate($this->sharees, 'result'); $this->assertEquals($exactExpected, $result['exact']['groups']); $this->assertEquals($expected, $result['groups']); $this->assertCount((int) $reachedEnd, $this->invokePrivate($this->sharees, 'reachedEndFor')); }