TestEnableMasterKey.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Encryption\Tests\Command;
  26. use OCA\Encryption\Command\EnableMasterKey;
  27. use OCA\Encryption\Util;
  28. use OCP\IConfig;
  29. use Symfony\Component\Console\Helper\QuestionHelper;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. use Test\TestCase;
  33. class TestEnableMasterKey extends TestCase {
  34. /** @var EnableMasterKey */
  35. protected $enableMasterKey;
  36. /** @var Util | \PHPUnit\Framework\MockObject\MockObject */
  37. protected $util;
  38. /** @var \OCP\IConfig | \PHPUnit\Framework\MockObject\MockObject */
  39. protected $config;
  40. /** @var \Symfony\Component\Console\Helper\QuestionHelper | \PHPUnit\Framework\MockObject\MockObject */
  41. protected $questionHelper;
  42. /** @var \Symfony\Component\Console\Output\OutputInterface | \PHPUnit\Framework\MockObject\MockObject */
  43. protected $output;
  44. /** @var \Symfony\Component\Console\Input\InputInterface | \PHPUnit\Framework\MockObject\MockObject */
  45. protected $input;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->util = $this->getMockBuilder(Util::class)
  49. ->disableOriginalConstructor()->getMock();
  50. $this->config = $this->getMockBuilder(IConfig::class)
  51. ->disableOriginalConstructor()->getMock();
  52. $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
  53. ->disableOriginalConstructor()->getMock();
  54. $this->output = $this->getMockBuilder(OutputInterface::class)
  55. ->disableOriginalConstructor()->getMock();
  56. $this->input = $this->getMockBuilder(InputInterface::class)
  57. ->disableOriginalConstructor()->getMock();
  58. $this->enableMasterKey = new EnableMasterKey($this->util, $this->config, $this->questionHelper);
  59. }
  60. /**
  61. * @dataProvider dataTestExecute
  62. *
  63. * @param bool $isAlreadyEnabled
  64. * @param string $answer
  65. */
  66. public function testExecute($isAlreadyEnabled, $answer) {
  67. $this->util->expects($this->once())->method('isMasterKeyEnabled')
  68. ->willReturn($isAlreadyEnabled);
  69. if ($isAlreadyEnabled) {
  70. $this->output->expects($this->once())->method('writeln')
  71. ->with('Master key already enabled');
  72. } else {
  73. if ($answer === 'y') {
  74. $this->questionHelper->expects($this->once())->method('ask')->willReturn(true);
  75. $this->config->expects($this->once())->method('setAppValue')
  76. ->with('encryption', 'useMasterKey', '1');
  77. } else {
  78. $this->questionHelper->expects($this->once())->method('ask')->willReturn(false);
  79. $this->config->expects($this->never())->method('setAppValue');
  80. }
  81. }
  82. $this->invokePrivate($this->enableMasterKey, 'execute', [$this->input, $this->output]);
  83. }
  84. public function dataTestExecute() {
  85. return [
  86. [true, ''],
  87. [false, 'y'],
  88. [false, 'n'],
  89. [false, '']
  90. ];
  91. }
  92. }