TestEnableMasterKey.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption\Tests\Command;
  8. use OCA\Encryption\Command\EnableMasterKey;
  9. use OCA\Encryption\Util;
  10. use OCP\IConfig;
  11. use Symfony\Component\Console\Helper\QuestionHelper;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Test\TestCase;
  15. class TestEnableMasterKey extends TestCase {
  16. /** @var EnableMasterKey */
  17. protected $enableMasterKey;
  18. /** @var Util | \PHPUnit\Framework\MockObject\MockObject */
  19. protected $util;
  20. /** @var \OCP\IConfig | \PHPUnit\Framework\MockObject\MockObject */
  21. protected $config;
  22. /** @var \Symfony\Component\Console\Helper\QuestionHelper | \PHPUnit\Framework\MockObject\MockObject */
  23. protected $questionHelper;
  24. /** @var \Symfony\Component\Console\Output\OutputInterface | \PHPUnit\Framework\MockObject\MockObject */
  25. protected $output;
  26. /** @var \Symfony\Component\Console\Input\InputInterface | \PHPUnit\Framework\MockObject\MockObject */
  27. protected $input;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->util = $this->getMockBuilder(Util::class)
  31. ->disableOriginalConstructor()->getMock();
  32. $this->config = $this->getMockBuilder(IConfig::class)
  33. ->disableOriginalConstructor()->getMock();
  34. $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
  35. ->disableOriginalConstructor()->getMock();
  36. $this->output = $this->getMockBuilder(OutputInterface::class)
  37. ->disableOriginalConstructor()->getMock();
  38. $this->input = $this->getMockBuilder(InputInterface::class)
  39. ->disableOriginalConstructor()->getMock();
  40. $this->enableMasterKey = new EnableMasterKey($this->util, $this->config, $this->questionHelper);
  41. }
  42. /**
  43. * @dataProvider dataTestExecute
  44. *
  45. * @param bool $isAlreadyEnabled
  46. * @param string $answer
  47. */
  48. public function testExecute($isAlreadyEnabled, $answer) {
  49. $this->util->expects($this->once())->method('isMasterKeyEnabled')
  50. ->willReturn($isAlreadyEnabled);
  51. if ($isAlreadyEnabled) {
  52. $this->output->expects($this->once())->method('writeln')
  53. ->with('Master key already enabled');
  54. } else {
  55. if ($answer === 'y') {
  56. $this->questionHelper->expects($this->once())->method('ask')->willReturn(true);
  57. $this->config->expects($this->once())->method('setAppValue')
  58. ->with('encryption', 'useMasterKey', '1');
  59. } else {
  60. $this->questionHelper->expects($this->once())->method('ask')->willReturn(false);
  61. $this->config->expects($this->never())->method('setAppValue');
  62. }
  63. }
  64. $this->invokePrivate($this->enableMasterKey, 'execute', [$this->input, $this->output]);
  65. }
  66. public function dataTestExecute() {
  67. return [
  68. [true, ''],
  69. [false, 'y'],
  70. [false, 'n'],
  71. [false, '']
  72. ];
  73. }
  74. }