AddTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. declare(strict_types=1);
  7. namespace Core\Command\User;
  8. use OC\Core\Command\User\Add;
  9. use OCA\Settings\Mailer\NewUserMailHelper;
  10. use OCP\EventDispatcher\IEventDispatcher;
  11. use OCP\IAppConfig;
  12. use OCP\IGroupManager;
  13. use OCP\IUser;
  14. use OCP\IUserManager;
  15. use OCP\Mail\IEMailTemplate;
  16. use OCP\mail\IMailer;
  17. use OCP\Security\ISecureRandom;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Test\TestCase;
  21. class AddTest extends TestCase {
  22. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  23. private $userManager;
  24. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  25. private $groupManager;
  26. /** @var IMailer|\PHPUnit\Framework\MockObject\MockObject */
  27. private $mailer;
  28. /** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
  29. private $appConfig;
  30. /** @var NewUserMailHelper|\PHPUnit\Framework\MockObject\MockObject */
  31. private $mailHelper;
  32. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  33. private $eventDispatcher;
  34. /** @var ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */
  35. private $secureRandom;
  36. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  37. private $user;
  38. /** @var InputInterface|\PHPUnit\Framework\MockObject\MockObject */
  39. private $consoleInput;
  40. /** @var OutputInterface|\PHPUnit\Framework\MockObject\MockObject */
  41. private $consoleOutput;
  42. /** @var Add */
  43. private $addCommand;
  44. public function setUp(): void {
  45. parent::setUp();
  46. $this->userManager = static::createMock(IUserManager::class);
  47. $this->groupManager = static::createStub(IGroupManager::class);
  48. $this->mailer = static::createMock(IMailer::class);
  49. $this->appConfig = static::createMock(IAppConfig::class);
  50. $this->mailHelper = static::createMock(NewUserMailHelper::class);
  51. $this->eventDispatcher = static::createStub(IEventDispatcher::class);
  52. $this->secureRandom = static::createStub(ISecureRandom::class);
  53. $this->user = static::createMock(IUser::class);
  54. $this->consoleInput = static::createMock(InputInterface::class);
  55. $this->consoleOutput = static::createMock(OutputInterface::class);
  56. $this->addCommand = new Add(
  57. $this->userManager,
  58. $this->groupManager,
  59. $this->mailer,
  60. $this->appConfig,
  61. $this->mailHelper,
  62. $this->eventDispatcher,
  63. $this->secureRandom
  64. );
  65. }
  66. /**
  67. * @dataProvider addEmailDataProvider
  68. */
  69. public function testAddEmail(
  70. ?string $email,
  71. bool $isEmailValid,
  72. bool $shouldSendEmail,
  73. ): void {
  74. $this->user->expects($isEmailValid ? static::once() : static::never())
  75. ->method('setSystemEMailAddress')
  76. ->with(static::equalTo($email));
  77. $this->userManager->method('createUser')
  78. ->willReturn($this->user);
  79. $this->appConfig->method('getValueString')
  80. ->willReturn($shouldSendEmail ? 'yes' : 'no');
  81. $this->mailer->method('validateMailAddress')
  82. ->willReturn($isEmailValid);
  83. $this->mailHelper->method('generateTemplate')
  84. ->willReturn(static::createMock(IEMailTemplate::class));
  85. $this->mailHelper->expects($isEmailValid && $shouldSendEmail ? static::once() : static::never())
  86. ->method('sendMail');
  87. $this->consoleInput->method('getOption')
  88. ->will(static::returnValueMap([
  89. ['generate-password', 'true'],
  90. ['email', $email],
  91. ['group', []],
  92. ]));
  93. $this->invokePrivate($this->addCommand, 'execute', [
  94. $this->consoleInput,
  95. $this->consoleOutput
  96. ]);
  97. }
  98. /**
  99. * @return array
  100. */
  101. public function addEmailDataProvider(): array {
  102. return [
  103. 'Valid E-Mail' => [
  104. 'info@example.com',
  105. true,
  106. true,
  107. ],
  108. 'Invalid E-Mail' => [
  109. 'info@@example.com',
  110. false,
  111. false,
  112. ],
  113. 'No E-Mail' => [
  114. '',
  115. false,
  116. false,
  117. ],
  118. 'Valid E-Mail, but no mail should be sent' => [
  119. 'info@example.com',
  120. true,
  121. false,
  122. ],
  123. ];
  124. }
  125. }