TestEnableMasterKey.php 3.5 KB

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