mailHelper = $this->createMock(MailHelper::class); $this->userTokenService = $this->createMock(UserTokenServiceInterface::class); $this->entityManager = $this->createMock(EntityManager::class); $this->user = $this->createMock(User::class); $this->router = $this->createMock(Router::class); $this->translator = $this->createMock(Translator::class); $this->userToken = $this->createMock(UserToken::class); $this->logger = $this->createMock(LoggerInterface::class); $this->userModel = new UserModel( $this->mailHelper, $this->userTokenService, $this->entityManager, $this->createMock(CorePermissions::class), $this->createMock(EventDispatcherInterface::class), $this->router, $this->translator, $this->createMock(UserHelper::class), $this->logger, $this->createMock(CoreParametersHelper::class) ); } public function testThatItSendsResetPasswordEmailAndRouterGetsCalledWithCorrectParamters(): void { $this->userTokenService->expects($this->once()) ->method('generateSecret') ->willReturn($this->userToken); $this->mailHelper ->method('getMailer') ->willReturn($this->mailHelper); $this->mailHelper->expects($this->once()) ->method('send'); $this->userTokenService->expects($this->once()) ->method('generateSecret') ->willReturn($this->userToken); $this->router->expects($this->once()) ->method('generate') ->with('mautic_user_passwordresetconfirm', ['token' => null], UrlGeneratorInterface::ABSOLUTE_URL); $this->translator ->expects($this->any()) ->method('trans') ->willReturn('test'); $this->userModel->sendResetEmail($this->user); } public function testThatDatabaseErrorThrowsRuntimeExceptionAndItIsLoggedWhenWeTryToSaveTokenToTheDatabaseWhenWeSendResetPasswordEmail(): void { $errorMessage = 'Some error message'; $this->expectException(\RuntimeException::class); $this->entityManager->expects($this->once()) ->method('flush') ->willThrowException(new \Exception($errorMessage)); $this->logger->expects($this->once()) ->method('error') ->with($errorMessage); $this->userModel->sendResetEmail($this->user); } public function testEmailUser(): void { $email = 'a@test.com'; $name = 'name'; $toMail = [$email => $name]; $subject = 'subject'; $content = 'content'; $this->user->expects($this->once()) ->method('getEmail') ->willReturn($email); $this->user->expects($this->once()) ->method('getName') ->willReturn($name); $this->mailHelper->expects($this->once()) ->method('getMailer') ->willReturn($this->mailHelper); $this->mailHelper->expects($this->once()) ->method('setTo') ->with($toMail) ->willReturn(true); $this->mailHelper->expects($this->once()) ->method('send'); // Means no erros. $this->userModel->emailUser($this->user, $subject, $content); } public function testSendMailToEmailAddresses(): void { $toMails = ['a@test.com', 'b@test.com']; $subject = 'subject'; $content = 'content'; $this->mailHelper->expects($this->once()) ->method('getMailer') ->willReturn($this->mailHelper); $this->mailHelper->expects($this->once()) ->method('setTo') ->with($toMails) ->willReturn(true); $this->mailHelper->expects($this->once()) ->method('send'); // Means no erros. $this->userModel->sendMailToEmailAddresses($toMails, $subject, $content); } }