AddTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021, Philip Gatzka (philip.gatzka@mailbox.org)
  4. *
  5. * @author Philip Gatzka <philip.gatzka@mailbox.org>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. declare(strict_types=1);
  24. namespace Core\Command\User;
  25. use OC\Core\Command\User\Add;
  26. use OCA\Settings\Mailer\NewUserMailHelper;
  27. use OCP\EventDispatcher\IEventDispatcher;
  28. use OCP\IAppConfig;
  29. use OCP\IGroupManager;
  30. use OCP\IUser;
  31. use OCP\IUserManager;
  32. use OCP\Mail\IEMailTemplate;
  33. use OCP\mail\IMailer;
  34. use OCP\Security\ISecureRandom;
  35. use Symfony\Component\Console\Input\InputInterface;
  36. use Symfony\Component\Console\Output\OutputInterface;
  37. use Test\TestCase;
  38. class AddTest extends TestCase {
  39. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  40. private $userManager;
  41. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  42. private $groupManager;
  43. /** @var IMailer|\PHPUnit\Framework\MockObject\MockObject */
  44. private $mailer;
  45. /** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
  46. private $appConfig;
  47. /** @var NewUserMailHelper|\PHPUnit\Framework\MockObject\MockObject */
  48. private $mailHelper;
  49. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  50. private $eventDispatcher;
  51. /** @var ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */
  52. private $secureRandom;
  53. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  54. private $user;
  55. /** @var InputInterface|\PHPUnit\Framework\MockObject\MockObject */
  56. private $consoleInput;
  57. /** @var OutputInterface|\PHPUnit\Framework\MockObject\MockObject */
  58. private $consoleOutput;
  59. /** @var Add */
  60. private $addCommand;
  61. public function setUp(): void {
  62. parent::setUp();
  63. $this->userManager = static::createMock(IUserManager::class);
  64. $this->groupManager = static::createStub(IGroupManager::class);
  65. $this->mailer = static::createMock(IMailer::class);
  66. $this->appConfig = static::createMock(IAppConfig::class);
  67. $this->mailHelper = static::createMock(NewUserMailHelper::class);
  68. $this->eventDispatcher = static::createStub(IEventDispatcher::class);
  69. $this->secureRandom = static::createStub(ISecureRandom::class);
  70. $this->user = static::createMock(IUser::class);
  71. $this->consoleInput = static::createMock(InputInterface::class);
  72. $this->consoleOutput = static::createMock(OutputInterface::class);
  73. $this->addCommand = new Add(
  74. $this->userManager,
  75. $this->groupManager,
  76. $this->mailer,
  77. $this->appConfig,
  78. $this->mailHelper,
  79. $this->eventDispatcher,
  80. $this->secureRandom
  81. );
  82. }
  83. /**
  84. * @dataProvider addEmailDataProvider
  85. */
  86. public function testAddEmail(
  87. ?string $email,
  88. bool $isEmailValid,
  89. bool $shouldSendEmail,
  90. ): void {
  91. $this->user->expects($isEmailValid ? static::once() : static::never())
  92. ->method('setSystemEMailAddress')
  93. ->with(static::equalTo($email));
  94. $this->userManager->method('createUser')
  95. ->willReturn($this->user);
  96. $this->appConfig->method('getValueString')
  97. ->willReturn($shouldSendEmail ? 'yes' : 'no');
  98. $this->mailer->method('validateMailAddress')
  99. ->willReturn($isEmailValid);
  100. $this->mailHelper->method('generateTemplate')
  101. ->willReturn(static::createMock(IEMailTemplate::class));
  102. $this->mailHelper->expects($isEmailValid && $shouldSendEmail ? static::once() : static::never())
  103. ->method('sendMail');
  104. $this->consoleInput->method('getOption')
  105. ->will(static::returnValueMap([
  106. ['generate-password', 'true'],
  107. ['email', $email],
  108. ['group', []],
  109. ]));
  110. $this->invokePrivate($this->addCommand, 'execute', [
  111. $this->consoleInput,
  112. $this->consoleOutput
  113. ]);
  114. }
  115. /**
  116. * @return array
  117. */
  118. public function addEmailDataProvider(): array {
  119. return [
  120. 'Valid E-Mail' => [
  121. 'info@example.com',
  122. true,
  123. true,
  124. ],
  125. 'Invalid E-Mail' => [
  126. 'info@@example.com',
  127. false,
  128. false,
  129. ],
  130. 'No E-Mail' => [
  131. '',
  132. false,
  133. false,
  134. ],
  135. 'Valid E-Mail, but no mail should be sent' => [
  136. 'info@example.com',
  137. true,
  138. false,
  139. ],
  140. ];
  141. }
  142. }