EncryptAllTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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() {
  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->at(1))->method('setSystemValue')->with('maintenance', true);
  79. $this->config->expects($this->at(2))->method('setSystemValue')->with('maintenance', false);
  80. $instance = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  81. $this->invokePrivate($instance, 'forceMaintenanceAndTrashbin');
  82. $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin');
  83. }
  84. /**
  85. * @dataProvider dataTestExecute
  86. */
  87. public function testExecute($answer, $askResult) {
  88. $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  89. $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(true);
  90. $this->questionHelper->expects($this->once())->method('ask')->willReturn($askResult);
  91. if ($answer === 'Y' || $answer === 'y') {
  92. $this->encryptionManager->expects($this->once())
  93. ->method('getEncryptionModule')->willReturn($this->encryptionModule);
  94. $this->encryptionModule->expects($this->once())
  95. ->method('encryptAll')->with($this->consoleInput, $this->consoleOutput);
  96. } else {
  97. $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
  98. $this->encryptionModule->expects($this->never())->method('encryptAll');
  99. }
  100. $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  101. }
  102. public function dataTestExecute() {
  103. return [
  104. ['y', true], ['Y', true], ['n', false], ['N', false], ['', false]
  105. ];
  106. }
  107. /**
  108. * @expectedException \Exception
  109. */
  110. public function testExecuteException() {
  111. $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  112. $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(false);
  113. $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
  114. $this->encryptionModule->expects($this->never())->method('encryptAll');
  115. $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  116. }
  117. }