EncryptAllTest.php 5.3 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() {
  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->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  67. }
  68. public function testEncryptAll() {
  69. // trash bin needs to be disabled in order to avoid adding dummy files to the users
  70. // trash bin which gets deleted during the encryption process
  71. $this->appManager->expects($this->once())->method('disableApp')->with('files_trashbin');
  72. // enable single user mode to avoid that other user login during encryption
  73. // destructor should disable the single user mode again
  74. $this->config->expects($this->once())->method('getSystemValue')->with('maintenance', false)->willReturn(false);
  75. $this->config->expects($this->at(1))->method('setSystemValue')->with('maintenance', true);
  76. $this->config->expects($this->at(2))->method('setSystemValue')->with('maintenance', false);
  77. $instance = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  78. $this->invokePrivate($instance, 'forceMaintenanceAndTrashbin');
  79. $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin');
  80. }
  81. /**
  82. * @dataProvider dataTestExecute
  83. */
  84. public function testExecute($answer, $askResult) {
  85. $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  86. $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(true);
  87. $this->questionHelper->expects($this->once())->method('ask')->willReturn($askResult);
  88. if ($answer === 'Y' || $answer === 'y') {
  89. $this->encryptionManager->expects($this->once())
  90. ->method('getEncryptionModule')->willReturn($this->encryptionModule);
  91. $this->encryptionModule->expects($this->once())
  92. ->method('encryptAll')->with($this->consoleInput, $this->consoleOutput);
  93. } else {
  94. $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
  95. $this->encryptionModule->expects($this->never())->method('encryptAll');
  96. }
  97. $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  98. }
  99. public function dataTestExecute() {
  100. return [
  101. ['y', true], ['Y', true], ['n', false], ['N', false], ['', false]
  102. ];
  103. }
  104. /**
  105. * @expectedException \Exception
  106. */
  107. public function testExecuteException() {
  108. $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  109. $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(false);
  110. $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
  111. $this->encryptionModule->expects($this->never())->method('encryptAll');
  112. $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  113. }
  114. }