EnableTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Tests\Core\Command\Encryption;
  8. use OC\Core\Command\Encryption\Enable;
  9. use OCP\Encryption\IManager;
  10. use OCP\IConfig;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Test\TestCase;
  14. class EnableTest extends TestCase {
  15. /** @var \PHPUnit\Framework\MockObject\MockObject */
  16. protected $config;
  17. /** @var \PHPUnit\Framework\MockObject\MockObject */
  18. protected $manager;
  19. /** @var \PHPUnit\Framework\MockObject\MockObject */
  20. protected $consoleInput;
  21. /** @var \PHPUnit\Framework\MockObject\MockObject */
  22. protected $consoleOutput;
  23. /** @var \Symfony\Component\Console\Command\Command */
  24. protected $command;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $config = $this->config = $this->getMockBuilder(IConfig::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $manager = $this->manager = $this->getMockBuilder(IManager::class)
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  34. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  35. /** @var \OCP\IConfig $config */
  36. /** @var \OCP\Encryption\IManager $manager */
  37. $this->command = new Enable($config, $manager);
  38. }
  39. public function dataEnable() {
  40. return [
  41. ['no', null, [], true, 'Encryption enabled', 'No encryption module is loaded'],
  42. ['yes', null, [], false, 'Encryption is already enabled', 'No encryption module is loaded'],
  43. ['no', null, ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'No default module is set'],
  44. ['no', 'OC_NO_MODULE', ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'The current default module does not exist: OC_NO_MODULE'],
  45. ['no', 'OC_TEST_MODULE', ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'Default module: OC_TEST_MODULE'],
  46. ];
  47. }
  48. /**
  49. * @dataProvider dataEnable
  50. *
  51. * @param string $oldStatus
  52. * @param string $defaultModule
  53. * @param array $availableModules
  54. * @param bool $isUpdating
  55. * @param string $expectedString
  56. * @param string $expectedDefaultModuleString
  57. */
  58. public function testEnable($oldStatus, $defaultModule, $availableModules, $isUpdating, $expectedString, $expectedDefaultModuleString) {
  59. if ($isUpdating) {
  60. $this->config->expects($this->once())
  61. ->method('setAppValue')
  62. ->with('core', 'encryption_enabled', 'yes');
  63. }
  64. $this->manager->expects($this->atLeastOnce())
  65. ->method('getEncryptionModules')
  66. ->willReturn($availableModules);
  67. if (empty($availableModules)) {
  68. $this->config->expects($this->once())
  69. ->method('getAppValue')
  70. ->with('core', 'encryption_enabled', $this->anything())
  71. ->willReturn($oldStatus);
  72. } else {
  73. $this->config->expects($this->exactly(2))
  74. ->method('getAppValue')
  75. ->withConsecutive(
  76. ['core', 'encryption_enabled', $this->anything()],
  77. ['core', 'default_encryption_module', $this->anything()],
  78. )->willReturnOnConsecutiveCalls(
  79. $oldStatus,
  80. $defaultModule,
  81. );
  82. }
  83. $this->consoleOutput->expects($this->exactly(3))
  84. ->method('writeln')
  85. ->withConsecutive(
  86. [$this->stringContains($expectedString)],
  87. [''],
  88. [$this->stringContains($expectedDefaultModuleString)],
  89. );
  90. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  91. }
  92. }