DecryptAllTest.php 6.6 KB

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