l = $this->createMock(IL10N::class); $this->config = $this->createMock(IConfig::class); $this->userSession = $this->createMock(IUserSession::class); $this->mailer = $this->createMock(IMailer::class); $this->mailController = new MailSettingsController( 'settings', $this->createMock(IRequest::class), $this->l, $this->config, $this->userSession, $this->mailer, 'no-reply@owncloud.com' ); } public function testSetMailSettings() { $this->config->expects($this->exactly(2)) ->method('setSystemValues') ->withConsecutive( [[ 'mail_domain' => 'owncloud.com', 'mail_from_address' => 'demo@owncloud.com', 'mail_smtpmode' => 'smtp', 'mail_smtpsecure' => 'ssl', 'mail_smtphost' => 'mx.owncloud.org', 'mail_smtpauthtype' => 'NTLM', 'mail_smtpauth' => 1, 'mail_smtpport' => '25', ]], [[ 'mail_domain' => 'owncloud.com', 'mail_from_address' => 'demo@owncloud.com', 'mail_smtpmode' => 'smtp', 'mail_smtpsecure' => 'ssl', 'mail_smtphost' => 'mx.owncloud.org', 'mail_smtpauthtype' => 'NTLM', 'mail_smtpauth' => null, 'mail_smtpport' => '25', 'mail_smtpname' => null, 'mail_smtppassword' => null, ]] ); // With authentication $response = $this->mailController->setMailSettings( 'owncloud.com', 'demo@owncloud.com', 'smtp', 'ssl', 'mx.owncloud.org', 'NTLM', 1, '25' ); $this->assertSame(Http::STATUS_OK, $response->getStatus()); // Without authentication (testing the deletion of the stored password) $response = $this->mailController->setMailSettings( 'owncloud.com', 'demo@owncloud.com', 'smtp', 'ssl', 'mx.owncloud.org', 'NTLM', 0, '25' ); $this->assertSame(Http::STATUS_OK, $response->getStatus()); } public function testStoreCredentials() { $this->config ->expects($this->once()) ->method('setSystemValues') ->with([ 'mail_smtpname' => 'UsernameToStore', 'mail_smtppassword' => 'PasswordToStore', ]); $response = $this->mailController->storeCredentials('UsernameToStore', 'PasswordToStore'); $this->assertSame(Http::STATUS_OK, $response->getStatus()); } public function testSendTestMail() { $user = $this->createMock(User::class); $user->expects($this->any()) ->method('getUID') ->will($this->returnValue('Werner')); $user->expects($this->any()) ->method('getDisplayName') ->will($this->returnValue('Werner Brösel')); $this->l->expects($this->any()) ->method('t') ->willReturnCallback(function($text, $parameters = []) { return vsprintf($text, $parameters); }); $this->userSession ->expects($this->any()) ->method('getUser') ->will($this->returnValue($user)); // Ensure that it fails when no mail address has been specified $response = $this->mailController->sendTestMail(); $this->assertSame(Http::STATUS_BAD_REQUEST, $response->getStatus()); $this->assertSame('You need to set your user email before being able to send test emails.', $response->getData()); // If no exception is thrown it should work $this->config ->expects($this->any()) ->method('getUserValue') ->will($this->returnValue('mail@example.invalid')); $this->mailer->expects($this->once()) ->method('createMessage') ->willReturn($this->createMock(Message::class)); $response = $this->mailController->sendTestMail(); $this->assertSame(Http::STATUS_OK, $response->getStatus(), $response->getData()); } }