EncryptAllTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\EncryptAll;
  23. use OCP\App\IAppManager;
  24. use OCP\Encryption\IEncryptionModule;
  25. use OCP\Encryption\IManager;
  26. use OCP\IConfig;
  27. use Symfony\Component\Console\Helper\QuestionHelper;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. use Test\TestCase;
  31. class EncryptAllTest extends TestCase {
  32. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IConfig */
  33. protected $config;
  34. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IManager */
  35. protected $encryptionManager;
  36. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\App\IAppManager */
  37. protected $appManager;
  38. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */
  39. protected $consoleInput;
  40. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */
  41. protected $consoleOutput;
  42. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
  43. protected $questionHelper;
  44. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IEncryptionModule */
  45. protected $encryptionModule;
  46. /** @var EncryptAll */
  47. protected $command;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->config = $this->getMockBuilder(IConfig::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->encryptionManager = $this->getMockBuilder(IManager::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->appManager = $this->getMockBuilder(IAppManager::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->encryptionModule = $this->getMockBuilder(IEncryptionModule::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  66. $this->consoleInput->expects($this->any())
  67. ->method('isInteractive')
  68. ->willReturn(true);
  69. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  70. }
  71. public function testEncryptAll() {
  72. // trash bin needs to be disabled in order to avoid adding dummy files to the users
  73. // trash bin which gets deleted during the encryption process
  74. $this->appManager->expects($this->once())->method('disableApp')->with('files_trashbin');
  75. // enable single user mode to avoid that other user login during encryption
  76. // destructor should disable the single user mode again
  77. $this->config->expects($this->once())->method('getSystemValueBool')->with('maintenance', false)->willReturn(false);
  78. $this->config->expects($this->exactly(2))
  79. ->method('setSystemValue')
  80. ->withConsecutive(
  81. ['maintenance', true],
  82. ['maintenance', false],
  83. );
  84. $instance = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  85. $this->invokePrivate($instance, 'forceMaintenanceAndTrashbin');
  86. $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin');
  87. }
  88. /**
  89. * @dataProvider dataTestExecute
  90. */
  91. public function testExecute($answer, $askResult) {
  92. $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  93. $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(true);
  94. $this->questionHelper->expects($this->once())->method('ask')->willReturn($askResult);
  95. if ($answer === 'Y' || $answer === 'y') {
  96. $this->encryptionManager->expects($this->once())
  97. ->method('getEncryptionModule')->willReturn($this->encryptionModule);
  98. $this->encryptionModule->expects($this->once())
  99. ->method('encryptAll')->with($this->consoleInput, $this->consoleOutput);
  100. } else {
  101. $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
  102. $this->encryptionModule->expects($this->never())->method('encryptAll');
  103. }
  104. $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  105. }
  106. public function dataTestExecute() {
  107. return [
  108. ['y', true], ['Y', true], ['n', false], ['N', false], ['', false]
  109. ];
  110. }
  111. public function testExecuteException() {
  112. $this->expectException(\Exception::class);
  113. $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  114. $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(false);
  115. $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
  116. $this->encryptionModule->expects($this->never())->method('encryptAll');
  117. $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  118. }
  119. }