defaults = $this->createMock(Defaults::class); $this->defaults->method('getLogo') ->willReturn('myLogo'); $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->l10n = $this->createMock(IL10N::class); $this->l10nFactory = $this->createMock(IFactory::class); $this->mailer = $this->createMock(IMailer::class); $template = new EMailTemplate( $this->defaults, $this->urlGenerator, $this->l10nFactory, null, null, 'test.TestTemplate', [] ); $this->mailer->method('createEMailTemplate') ->willReturn($template); $this->secureRandom = $this->createMock(ISecureRandom::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->config = $this->createMock(IConfig::class); $this->config ->expects($this->any()) ->method('getSystemValue') ->willReturnCallback(function ($arg) { switch ($arg) { case 'secret': return 'MyInstanceWideSecret'; case 'customclient_desktop': return 'https://nextcloud.com/install/#install-clients'; } return ''; }); $this->crypto = $this->createMock(ICrypto::class); $this->l10n->method('t') ->willReturnCallback(function ($text, $parameters = []) { return vsprintf($text, $parameters); }); $this->l10nFactory->method('get') ->willReturnCallback(function ($text, $lang) { return $this->l10n; }); $this->newUserMailHelper = new NewUserMailHelper( $this->defaults, $this->urlGenerator, $this->l10nFactory, $this->mailer, $this->secureRandom, $this->timeFactory, $this->config, $this->crypto, 'no-reply@nextcloud.com' ); } public function testGenerateTemplateWithPasswordResetToken(): void { $this->secureRandom ->expects($this->once()) ->method('generate') ->with(21, ISecureRandom::CHAR_ALPHANUMERIC) ->willReturn('MySuperLongSecureRandomToken'); $this->timeFactory ->expects($this->once()) ->method('getTime') ->willReturn(12345); /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(IUser::class); $user ->expects($this->any()) ->method('getEmailAddress') ->willReturn('recipient@example.com'); $this->crypto ->expects($this->once()) ->method('encrypt') ->with('12345:MySuperLongSecureRandomToken', 'recipient@example.comMyInstanceWideSecret') ->willReturn('TokenCiphertext'); $user ->expects($this->any()) ->method('getUID') ->willReturn('john'); $this->config ->expects($this->once()) ->method('setUserValue') ->with('john', 'core', 'lostpassword', 'TokenCiphertext'); $this->urlGenerator ->expects($this->once()) ->method('linkToRouteAbsolute') ->with('core.lost.resetform', ['userId' => 'john', 'token' => 'MySuperLongSecureRandomToken']) ->willReturn('https://example.com/resetPassword/MySuperLongSecureRandomToken'); $user ->expects($this->any()) ->method('getDisplayName') ->willReturn('john'); $this->defaults ->expects($this->any()) ->method('getName') ->willReturn('TestCloud'); $this->defaults ->expects($this->any()) ->method('getTextColorPrimary') ->willReturn('#ffffff'); $expectedHtmlBody = <<
 

Welcome aboard

 

Welcome to your TestCloud account, you can add, protect, and share your data.

Your Login is: john

 
Set your password
Install Client
 
                                                           
EOF; $expectedTextBody = <<newUserMailHelper->generateTemplate($user, true); $this->assertEquals($expectedHtmlBody, $result->renderHtml()); $this->assertEquals($expectedTextBody, $result->renderText()); $this->assertSame('OC\Mail\EMailTemplate', get_class($result)); } public function testGenerateTemplateWithoutPasswordResetToken(): void { $this->urlGenerator ->expects($this->any()) ->method('getAbsoluteURL') ->willReturnMap([ ['/','https://example.com/'], ['myLogo',''], ]); /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(IUser::class); $user ->expects($this->any()) ->method('getDisplayName') ->willReturn('John Doe'); $user ->expects($this->any()) ->method('getUID') ->willReturn('john'); $this->defaults ->expects($this->any()) ->method('getName') ->willReturn('TestCloud'); $this->defaults ->expects($this->any()) ->method('getTextColorPrimary') ->willReturn('#ffffff'); $expectedHtmlBody = <<
 

Welcome aboard John Doe

 

Welcome to your TestCloud account, you can add, protect, and share your data.

Your Login is: john

 
Go to TestCloud
Install Client
 
                                                           
EOF; $expectedTextBody = <<newUserMailHelper->generateTemplate($user, false); $this->assertEquals($expectedHtmlBody, $result->renderHtml()); $this->assertEquals($expectedTextBody, $result->renderText()); $this->assertSame('OC\Mail\EMailTemplate', get_class($result)); } public function testGenerateTemplateWithoutUserId(): void { $this->urlGenerator ->expects($this->any()) ->method('getAbsoluteURL') ->willReturnMap([ ['/', 'https://example.com/'], ['myLogo', ''], ]); /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(IUser::class); $user ->expects($this->any()) ->method('getDisplayName') ->willReturn('John Doe'); $user ->expects($this->any()) ->method('getUID') ->willReturn('john'); $user ->expects($this->atLeastOnce()) ->method('getBackendClassName') ->willReturn('LDAP'); $this->defaults ->expects($this->any()) ->method('getName') ->willReturn('TestCloud'); $this->defaults ->expects($this->any()) ->method('getTextColorPrimary') ->willReturn('#ffffff'); $expectedHtmlBody = <<
 

Welcome aboard John Doe

 

Welcome to your TestCloud account, you can add, protect, and share your data.

 
Go to TestCloud
Install Client
 
                                                           
EOF; $expectedTextBody = <<newUserMailHelper->generateTemplate($user, false); $this->assertEquals($expectedHtmlBody, $result->renderHtml()); $this->assertEquals($expectedTextBody, $result->renderText()); $this->assertSame('OC\Mail\EMailTemplate', get_class($result)); } public function testSendMail(): void { /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(IUser::class); $user ->expects($this->once()) ->method('getEMailAddress') ->willReturn('recipient@example.com'); $user ->expects($this->once()) ->method('getDisplayName') ->willReturn('John Doe'); /** @var IEMailTemplate|\PHPUnit\Framework\MockObject\MockObject $emailTemplate */ $emailTemplate = $this->createMock(IEMailTemplate::class); $message = $this->createMock(Message::class); $message ->expects($this->once()) ->method('setTo') ->with(['recipient@example.com' => 'John Doe']); $message ->expects($this->once()) ->method('setFrom') ->with(['no-reply@nextcloud.com' => 'TestCloud']); $message ->expects($this->once()) ->method('useTemplate') ->with($emailTemplate); $message ->expects($this->once()) ->method('setAutoSubmitted') ->with(AutoSubmitted::VALUE_AUTO_GENERATED); $this->defaults ->expects($this->once()) ->method('getName') ->willReturn('TestCloud'); $this->mailer ->expects($this->once()) ->method('createMessage') ->willReturn($message); $this->newUserMailHelper->sendMail($user, $emailTemplate); } }