EnableTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Tests\Core\Command\Encryption;
  22. use OC\Core\Command\Encryption\Enable;
  23. use OCP\Encryption\IManager;
  24. use OCP\IConfig;
  25. use Symfony\Component\Console\Input\InputInterface;
  26. use Symfony\Component\Console\Output\OutputInterface;
  27. use Test\TestCase;
  28. class EnableTest extends TestCase {
  29. /** @var \PHPUnit\Framework\MockObject\MockObject */
  30. protected $config;
  31. /** @var \PHPUnit\Framework\MockObject\MockObject */
  32. protected $manager;
  33. /** @var \PHPUnit\Framework\MockObject\MockObject */
  34. protected $consoleInput;
  35. /** @var \PHPUnit\Framework\MockObject\MockObject */
  36. protected $consoleOutput;
  37. /** @var \Symfony\Component\Console\Command\Command */
  38. protected $command;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $config = $this->config = $this->getMockBuilder(IConfig::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $manager = $this->manager = $this->getMockBuilder(IManager::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  48. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  49. /** @var \OCP\IConfig $config */
  50. /** @var \OCP\Encryption\IManager $manager */
  51. $this->command = new Enable($config, $manager);
  52. }
  53. public function dataEnable() {
  54. return [
  55. ['no', null, [], true, 'Encryption enabled', 'No encryption module is loaded'],
  56. ['yes', null, [], false, 'Encryption is already enabled', 'No encryption module is loaded'],
  57. ['no', null, ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'No default module is set'],
  58. ['no', 'OC_NO_MODULE', ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'The current default module does not exist: OC_NO_MODULE'],
  59. ['no', 'OC_TEST_MODULE', ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'Default module: OC_TEST_MODULE'],
  60. ];
  61. }
  62. /**
  63. * @dataProvider dataEnable
  64. *
  65. * @param string $oldStatus
  66. * @param string $defaultModule
  67. * @param array $availableModules
  68. * @param bool $isUpdating
  69. * @param string $expectedString
  70. * @param string $expectedDefaultModuleString
  71. */
  72. public function testEnable($oldStatus, $defaultModule, $availableModules, $isUpdating, $expectedString, $expectedDefaultModuleString) {
  73. if ($isUpdating) {
  74. $this->config->expects($this->once())
  75. ->method('setAppValue')
  76. ->with('core', 'encryption_enabled', 'yes');
  77. }
  78. $this->manager->expects($this->atLeastOnce())
  79. ->method('getEncryptionModules')
  80. ->willReturn($availableModules);
  81. if (empty($availableModules)) {
  82. $this->config->expects($this->once())
  83. ->method('getAppValue')
  84. ->with('core', 'encryption_enabled', $this->anything())
  85. ->willReturn($oldStatus);
  86. } else {
  87. $this->config->expects($this->exactly(2))
  88. ->method('getAppValue')
  89. ->withConsecutive(
  90. ['core', 'encryption_enabled', $this->anything()],
  91. ['core', 'default_encryption_module', $this->anything()],
  92. )->willReturnOnConsecutiveCalls(
  93. $oldStatus,
  94. $defaultModule,
  95. );
  96. }
  97. $this->consoleOutput->expects($this->exactly(3))
  98. ->method('writeln')
  99. ->withConsecutive(
  100. [$this->stringContains($expectedString)],
  101. [''],
  102. [$this->stringContains($expectedDefaultModuleString)],
  103. );
  104. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  105. }
  106. }