DecryptAllTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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\DecryptAll;
  9. use OCP\App\IAppManager;
  10. use OCP\Encryption\IManager;
  11. use OCP\IConfig;
  12. use Symfony\Component\Console\Helper\QuestionHelper;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Test\TestCase;
  16. class DecryptAllTest extends TestCase {
  17. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IConfig */
  18. protected $config;
  19. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IManager */
  20. protected $encryptionManager;
  21. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\App\IAppManager */
  22. protected $appManager;
  23. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */
  24. protected $consoleInput;
  25. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */
  26. protected $consoleOutput;
  27. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
  28. protected $questionHelper;
  29. /** @var \PHPUnit\Framework\MockObject\MockObject | \OC\Encryption\DecryptAll */
  30. protected $decryptAll;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->config = $this->getMockBuilder(IConfig::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->encryptionManager = $this->getMockBuilder(IManager::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->appManager = $this->getMockBuilder(IAppManager::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->decryptAll = $this->getMockBuilder(\OC\Encryption\DecryptAll::class)
  46. ->disableOriginalConstructor()->getMock();
  47. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  48. $this->consoleInput->expects($this->any())
  49. ->method('isInteractive')
  50. ->willReturn(true);
  51. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  52. $this->config->expects($this->any())
  53. ->method('getSystemValue')
  54. ->with('maintenance', false)
  55. ->willReturn(false);
  56. $this->appManager->expects($this->any())
  57. ->method('isEnabledForUser')
  58. ->with('files_trashbin')->willReturn(true);
  59. }
  60. public function testMaintenanceAndTrashbin(): void {
  61. // on construct we enable single-user-mode and disable the trash bin
  62. // on destruct we disable single-user-mode again and enable the trash bin
  63. $this->config->expects($this->exactly(2))
  64. ->method('setSystemValue')
  65. ->withConsecutive(
  66. ['maintenance', true],
  67. ['maintenance', false],
  68. );
  69. $this->appManager->expects($this->once())
  70. ->method('disableApp')
  71. ->with('files_trashbin');
  72. $this->appManager->expects($this->once())
  73. ->method('enableApp')
  74. ->with('files_trashbin');
  75. $instance = new DecryptAll(
  76. $this->encryptionManager,
  77. $this->appManager,
  78. $this->config,
  79. $this->decryptAll,
  80. $this->questionHelper
  81. );
  82. $this->invokePrivate($instance, 'forceMaintenanceAndTrashbin');
  83. $this->assertTrue(
  84. $this->invokePrivate($instance, 'wasTrashbinEnabled')
  85. );
  86. $this->assertFalse(
  87. $this->invokePrivate($instance, 'wasMaintenanceModeEnabled')
  88. );
  89. $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin');
  90. }
  91. /**
  92. * @dataProvider dataTestExecute
  93. */
  94. public function testExecute($encryptionEnabled, $continue): void {
  95. $instance = new DecryptAll(
  96. $this->encryptionManager,
  97. $this->appManager,
  98. $this->config,
  99. $this->decryptAll,
  100. $this->questionHelper
  101. );
  102. $this->encryptionManager->expects($this->once())
  103. ->method('isEnabled')
  104. ->willReturn($encryptionEnabled);
  105. $this->consoleInput->expects($this->any())
  106. ->method('getArgument')
  107. ->with('user')
  108. ->willReturn('user1');
  109. if ($encryptionEnabled) {
  110. $this->config->expects($this->exactly(2))
  111. ->method('setAppValue')
  112. ->withConsecutive(
  113. ['core', 'encryption_enabled', 'no'],
  114. ['core', 'encryption_enabled', 'yes'],
  115. );
  116. $this->questionHelper->expects($this->once())
  117. ->method('ask')
  118. ->willReturn($continue);
  119. if ($continue) {
  120. $this->decryptAll->expects($this->once())
  121. ->method('decryptAll')
  122. ->with($this->consoleInput, $this->consoleOutput, 'user1');
  123. } else {
  124. $this->decryptAll->expects($this->never())->method('decryptAll');
  125. }
  126. } else {
  127. $this->config->expects($this->never())->method('setAppValue');
  128. $this->decryptAll->expects($this->never())->method('decryptAll');
  129. $this->questionHelper->expects($this->never())->method('ask');
  130. }
  131. $this->invokePrivate($instance, 'execute', [$this->consoleInput, $this->consoleOutput]);
  132. }
  133. public function dataTestExecute() {
  134. return [
  135. [true, true],
  136. [true, false],
  137. [false, true],
  138. [false, false]
  139. ];
  140. }
  141. public function testExecuteFailure(): void {
  142. $this->expectException(\Exception::class);
  143. $instance = new DecryptAll(
  144. $this->encryptionManager,
  145. $this->appManager,
  146. $this->config,
  147. $this->decryptAll,
  148. $this->questionHelper
  149. );
  150. // make sure that we enable encryption again after a exception was thrown
  151. $this->config->expects($this->exactly(2))
  152. ->method('setAppValue')
  153. ->withConsecutive(
  154. ['core', 'encryption_enabled', 'no'],
  155. ['core', 'encryption_enabled', 'yes'],
  156. );
  157. $this->encryptionManager->expects($this->once())
  158. ->method('isEnabled')
  159. ->willReturn(true);
  160. $this->consoleInput->expects($this->any())
  161. ->method('getArgument')
  162. ->with('user')
  163. ->willReturn('user1');
  164. $this->questionHelper->expects($this->once())
  165. ->method('ask')
  166. ->willReturn(true);
  167. $this->decryptAll->expects($this->once())
  168. ->method('decryptAll')
  169. ->with($this->consoleInput, $this->consoleOutput, 'user1')
  170. ->willReturnCallback(function () {
  171. throw new \Exception();
  172. });
  173. $this->invokePrivate($instance, 'execute', [$this->consoleInput, $this->consoleOutput]);
  174. }
  175. }