DecryptAllTest.php 6.4 KB

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