diff options
author | Philip Gatzka <philip.gatzka@mailbox.org> | 2021-10-16 13:27:28 +0200 |
---|---|---|
committer | Anupam Kumar <kyteinsky@gmail.com> | 2024-02-24 04:56:52 +0530 |
commit | b587ec39f426a50c1ff28a091ba1c5e910bd02dd (patch) | |
tree | 334d8a67920a9fbb851e1fbe1e96bd5dc1bfb47f /tests/Core | |
parent | eef571e4fb203e255d3bdb2a79737b0d87a79a2b (diff) | |
download | nextcloud-server-b587ec39f426a50c1ff28a091ba1c5e910bd02dd.tar.gz nextcloud-server-b587ec39f426a50c1ff28a091ba1c5e910bd02dd.zip |
Enable adding E-Mail addresses to new user accounts using the CLI
Signed-off-by: Philip Gatzka <philip.gatzka@mailbox.org>
Diffstat (limited to 'tests/Core')
-rw-r--r-- | tests/Core/Command/User/AddTest.php | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/tests/Core/Command/User/AddTest.php b/tests/Core/Command/User/AddTest.php new file mode 100644 index 00000000000..1445e3cfec6 --- /dev/null +++ b/tests/Core/Command/User/AddTest.php @@ -0,0 +1,126 @@ +<?php +/** + * @copyright Copyright (c) 2021, Philip Gatzka (philip.gatzka@mailbox.org) + * + * @author Philip Gatzka <philip.gatzka@mailbox.org> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +declare(strict_types=1); + +namespace Core\Command\User; + +use Egulias\EmailValidator\EmailValidator; +use OC\Core\Command\User\Add; +use OCA\Settings\Mailer\NewUserMailHelper; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\IConfig; +use OCP\IGroupManager; +use OCP\IUser; +use OCP\IUserManager; +use OCP\Mail\IEMailTemplate; +use OCP\Security\ISecureRandom; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +class AddTest extends TestCase { + + /** + * @dataProvider addEmailDataProvider + */ + public function testAddEmail(?string $email, bool $isValid, bool $shouldSendMail): void { + $userManager = static::createMock(IUserManager::class); + $groupManager = static::createStub(IGroupManager::class); + $user = static::createMock(IUser::class); + $config = static::createMock(IConfig::class); + $mailHelper = static::createMock(NewUserMailHelper::class); + $eventDispatcher = static::createStub(IEventDispatcher::class); + $secureRandom = static::createStub(ISecureRandom::class); + + $consoleInput = static::createMock(InputInterface::class); + $consoleOutput = static::createMock(OutputInterface::class); + + $user->expects($isValid ? static::once() : static::never()) + ->method('setSystemEMailAddress') + ->with(static::equalTo($email)); + + $userManager->method('createUser') + ->willReturn($user); + + $config->method('getAppValue') + ->willReturn($shouldSendMail ? 'yes' : 'no'); + + $mailHelper->method('generateTemplate') + ->willReturn(static::createMock(IEMailTemplate::class)); + + $mailHelper->expects($isValid && $shouldSendMail ? static::once() : static::never()) + ->method('sendMail'); + + $consoleInput->method('getOption') + ->will(static::returnValueMap([ + ['password-from-env', ''], + ['email', $email], + ['group', []], + ])); + + $addCommand = new Add( + $userManager, + $groupManager, + new EmailValidator(), + $config, + $mailHelper, + $eventDispatcher, + $secureRandom + ); + + $this->invokePrivate($addCommand, 'execute', [ + $consoleInput, + $consoleOutput + ]); + } + + /** + * @return \Generator<string, array> + */ + public function addEmailDataProvider(): \Generator { + yield 'Valid E-Mail' => [ + 'info@example.com', + true, + true, + ]; + + yield 'Invalid E-Mail' => [ + 'info@@example.com', + false, + true, + ]; + + yield 'No E-Mail' => [ + '', + false, + true, + ]; + + yield 'Valid E-Mail, but no mail should be sent' => [ + 'info@example.com', + true, + false, + ]; + } +} |