EncryptAllTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. $instance = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  62. $this->invokePrivate($instance, 'forceMaintenanceAndTrashbin');
  63. $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin');
  64. }
  65. /**
  66. * @dataProvider dataTestExecute
  67. */
  68. public function testExecute($answer, $askResult): void {
  69. $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  70. $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(true);
  71. $this->questionHelper->expects($this->once())->method('ask')->willReturn($askResult);
  72. if ($answer === 'Y' || $answer === 'y') {
  73. $this->encryptionManager->expects($this->once())
  74. ->method('getEncryptionModule')->willReturn($this->encryptionModule);
  75. $this->encryptionModule->expects($this->once())
  76. ->method('encryptAll')->with($this->consoleInput, $this->consoleOutput);
  77. } else {
  78. $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
  79. $this->encryptionModule->expects($this->never())->method('encryptAll');
  80. }
  81. $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  82. }
  83. public function dataTestExecute() {
  84. return [
  85. ['y', true], ['Y', true], ['n', false], ['N', false], ['', false]
  86. ];
  87. }
  88. public function testExecuteException(): void {
  89. $this->expectException(\Exception::class);
  90. $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
  91. $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(false);
  92. $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
  93. $this->encryptionModule->expects($this->never())->method('encryptAll');
  94. $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  95. }
  96. }