l = $this->createMock(IL10N::class); $this->config = $this->createMock(IConfig::class); $this->userSession = $this->createMock(IUserSession::class); $this->mailer = $this->createMock(IMailer::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); /** @var IRequest&MockObject $request */ $request = $this->createMock(IRequest::class); $this->mailController = new MailSettingsController( 'settings', $request, $this->l, $this->config, $this->userSession, $this->urlGenerator, $this->mailer, ); } public function testSetMailSettings(): void { $calls = [ [[ 'mail_domain' => 'nextcloud.com', 'mail_from_address' => 'demo@nextcloud.com', 'mail_smtpmode' => 'smtp', 'mail_smtpsecure' => 'ssl', 'mail_smtphost' => 'mx.nextcloud.org', 'mail_smtpauth' => 1, 'mail_smtpport' => '25', 'mail_sendmailmode' => 'smtp', ]], [[ 'mail_domain' => 'nextcloud.com', 'mail_from_address' => 'demo@nextcloud.com', 'mail_smtpmode' => 'smtp', 'mail_smtpsecure' => 'ssl', 'mail_smtphost' => 'mx.nextcloud.org', 'mail_smtpauth' => null, 'mail_smtpport' => '25', 'mail_smtpname' => null, 'mail_smtppassword' => null, 'mail_sendmailmode' => 'smtp', ]], ]; $this->config->expects($this->exactly(2)) ->method('setSystemValues') ->willReturnCallback(function () use (&$calls): void { $expected = array_shift($calls); $this->assertEquals($expected, func_get_args()); }); // With authentication $response = $this->mailController->setMailSettings( 'nextcloud.com', 'demo@nextcloud.com', 'smtp', 'ssl', 'mx.nextcloud.org', '1', '25', 'smtp' ); $this->assertSame(Http::STATUS_OK, $response->getStatus()); // Without authentication (testing the deletion of the stored password) $response = $this->mailController->setMailSettings( 'nextcloud.com', 'demo@nextcloud.com', 'smtp', 'ssl', 'mx.nextcloud.org', '0', '25', 'smtp' ); $this->assertSame(Http::STATUS_OK, $response->getStatus()); } public function testStoreCredentials(): void { $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(): void { $user = $this->createMock(User::class); $user->expects($this->any()) ->method('getUID') ->willReturn('Werner'); $user->expects($this->any()) ->method('getDisplayName') ->willReturn('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') ->willReturn($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 account email before being able to send test emails. Go to for that.', $response->getData()); // If no exception is thrown it should work $this->config ->expects($this->any()) ->method('getUserValue') ->willReturn('mail@example.invalid'); $this->mailer->expects($this->once()) ->method('createMessage') ->willReturn($this->createMock(Message::class)); $emailTemplate = $this->createMock(IEMailTemplate::class); $this->mailer ->expects($this->once()) ->method('createEMailTemplate') ->willReturn($emailTemplate); $response = $this->mailController->sendTestMail(); $this->assertSame(Http::STATUS_OK, $response->getStatus()); } } a id='n35' href='#n35'>35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173