userManager = $this->createMock(\OC\User\Manager::class); $this->userSession = $this->createMock(Session::class); $this->groupManager = $this->createMock(\OC\Group\Manager::class); $this->appManager = $this->createMock(IAppManager::class); $this->l = $this->createMock(IL10N::class); $this->l->method('t')->willReturnArgument(0); /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject $request */ $request = $this->createMock(IRequest::class); $this->controller = new ChangePasswordController( 'core', $request, $this->userId, $this->userManager, $this->userSession, $this->groupManager, $this->appManager, $this->l ); } public function testChangePersonalPasswordWrongPassword() { $this->userSession->expects($this->once()) ->method('getLoginName') ->willReturn($this->loginName); $this->userManager->expects($this->once()) ->method('checkPassword') ->with($this->loginName, 'old') ->willReturn(false); $expects = new JSONResponse([ 'status' => 'error', 'data' => [ 'message' => 'Wrong password', ], ]); $expects->throttle(); $actual = $this->controller->changePersonalPassword('old', 'new'); $this->assertEquals($expects, $actual); } public function testChangePersonalPasswordCommonPassword() { $this->userSession->expects($this->once()) ->method('getLoginName') ->willReturn($this->loginName); $user = $this->getMockBuilder(IUser::class)->getMock(); $this->userManager->expects($this->once()) ->method('checkPassword') ->with($this->loginName, 'old') ->willReturn($user); $user->expects($this->once()) ->method('setPassword') ->with('new') ->will($this->throwException(new HintException('Common password'))); $expects = new JSONResponse([ 'status' => 'error', 'data' => [ 'message' => 'Common password', ], ]); $actual = $this->controller->changePersonalPassword('old', 'new'); $this->assertEquals($expects, $actual); } public function testChangePersonalPasswordNoNewPassword() { $this->userSession->expects($this->once()) ->method('getLoginName') ->willReturn($this->loginName); $user = $this->getMockBuilder(IUser::class)->getMock(); $this->userManager->expects($this->once()) ->method('checkPassword') ->with($this->loginName, 'old') ->willReturn($user); $expects = [ 'status' => 'error', 'data' => [ 'message' => 'Unable to change personal password', ], ]; $res = $this->controller->changePersonalPassword('old'); $this->assertEquals($expects, $res->getData()); } public function testChangePersonalPasswordCantSetPassword() { $this->userSession->expects($this->once()) ->method('getLoginName') ->willReturn($this->loginName); $user = $this->getMockBuilder(IUser::class)->getMock(); $this->userManager->expects($this->once()) ->method('checkPassword') ->with($this->loginName, 'old') ->willReturn($user); $user->expects($this->once()) ->method('setPassword') ->with('new') ->willReturn(false); $expects = new JSONResponse([ 'status' => 'error', 'data' => [ 'message' => 'Unable to change personal password', ], ]); $actual = $this->controller->changePersonalPassword('old', 'new'); $this->assertEquals($expects, $actual); } public function testChangePersonalPassword() { $this->userSession->expects($this->once()) ->method('getLoginName') ->willReturn($this->loginName); $user = $this->getMockBuilder(IUser::class)->getMock(); $this->userManager->expects($this->once()) ->method('checkPassword') ->with($this->loginName, 'old') ->willReturn($user); $user->expects($this->once()) ->method('setPassword') ->with('new') ->willReturn(true); $this->userSession->expects($this->once()) ->method('updateSessionTokenPassword') ->with('new'); $expects = new JSONResponse([ 'status' => 'success', 'data' => [ 'message' => 'Saved', ], ]); $actual = $this->controller->changePersonalPassword('old', 'new'); $this->assertEquals($expects, $actual); } }