EncryptAllTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Tests\Core\Command\Encryption;
  8. use OC\Core\Command\Encryption\EncryptAll;
  9. use OCP\App\IAppManager;
  10. use OCP\Encryption\IEncryptionModule;
  11. use OCP\Encryption\IManager;
  12. use OCP\IConfig;
  13. use Symfony\Component\Console\Helper\QuestionHelper;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Test\TestCase;
  17. class EncryptAllTest extends TestCase {
  18. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IConfig */
  19. protected $config;
  20. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IManager */
  21. protected $encryptionManager;
  22. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\App\IAppManager */
  23. protected $appManager;
  24. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */
  25. protected $consoleInput;
  26. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */
  27. protected $consoleOutput;
  28. /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
  29. protected $questionHelper;
  30. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IEncryptionModule */
  31. protected $encryptionModule;
  32. /** @var EncryptAll */
  33. protected $command;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->config = $this->getMockBuilder(IConfig::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->encryptionManager = $this->getMockBuilder(IManager::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->appManager = $this->getMockBuilder(IAppManager::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->encryptionModule = $this->getMockBuilder(IEncryptionModule::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  52. $this->consoleInput->expects($this->any())
  53. ->method('isInteractive')
  54. ->willReturn(true);
  55. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  56. }
  57. public function testEncryptAll(): void {
  58. // trash bin needs to be disabled in order to avoid adding dummy files to the users
  59. // trash bin which gets deleted during the encryption process
  60. $this->appManager->expects($this->once())->method('disableApp')->with('files_trashbin');
  61. // enable single user mode to avoid that other user login during encryption
  62. // destructor should disable the single user mode again
  63. $this->config->expects($this->once())->method('getSystemValueBool')->with('maintenance', false)->willReturn(false);
  64. $this->config->expects($this->exactly(2))
  65. ->method('setSystemValue')
  66. ->withConsecutive(
  67. ['maintenance', true],
  68. ['maintenance', false],
  69. );
  70. $instance = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  71. $this->invokePrivate($instance, 'forceMaintenanceAndTrashbin');
  72. $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin');
  73. }
  74. /**
  75. * @dataProvider dataTestExecute
  76. */
  77. public function testExecute($answer, $askResult): void {
  78. $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  79. $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(true);
  80. $this->questionHelper->expects($this->once())->method('ask')->willReturn($askResult);
  81. if ($answer === 'Y' || $answer === 'y') {
  82. $this->encryptionManager->expects($this->once())
  83. ->method('getEncryptionModule')->willReturn($this->encryptionModule);
  84. $this->encryptionModule->expects($this->once())
  85. ->method('encryptAll')->with($this->consoleInput, $this->consoleOutput);
  86. } else {
  87. $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
  88. $this->encryptionModule->expects($this->never())->method('encryptAll');
  89. }
  90. $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  91. }
  92. public function dataTestExecute() {
  93. return [
  94. ['y', true], ['Y', true], ['n', false], ['N', false], ['', false]
  95. ];
  96. }
  97. public function testExecuteException(): void {
  98. $this->expectException(\Exception::class);
  99. $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  100. $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(false);
  101. $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
  102. $this->encryptionModule->expects($this->never())->method('encryptAll');
  103. $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  104. }
  105. }