SetDefaultModuleTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\SetDefaultModule;
  9. use OCP\Encryption\IManager;
  10. use OCP\IConfig;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Test\TestCase;
  14. class SetDefaultModuleTest extends TestCase {
  15. /** @var \PHPUnit\Framework\MockObject\MockObject|IManager */
  16. protected $manager;
  17. /** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */
  18. protected $config;
  19. /** @var \PHPUnit\Framework\MockObject\MockObject */
  20. protected $consoleInput;
  21. /** @var \PHPUnit\Framework\MockObject\MockObject */
  22. protected $consoleOutput;
  23. /** @var \Symfony\Component\Console\Command\Command */
  24. protected $command;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->manager = $this->getMockBuilder(IManager::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->config = $this->getMockBuilder(IConfig::class)
  31. ->getMock();
  32. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  33. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  34. $this->command = new SetDefaultModule($this->manager, $this->config);
  35. }
  36. public function dataSetDefaultModule() {
  37. return [
  38. ['ID0', 'ID0', null, null, 'already'],
  39. ['ID0', 'ID1', 'ID1', true, 'info'],
  40. ['ID0', 'ID1', 'ID1', false, 'error'],
  41. ];
  42. }
  43. /**
  44. * @dataProvider dataSetDefaultModule
  45. *
  46. * @param string $oldModule
  47. * @param string $newModule
  48. * @param string $updateModule
  49. * @param bool $updateSuccess
  50. * @param string $expectedString
  51. */
  52. public function testSetDefaultModule($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString) {
  53. $this->consoleInput->expects($this->once())
  54. ->method('getArgument')
  55. ->with('module')
  56. ->willReturn($newModule);
  57. $this->manager->expects($this->once())
  58. ->method('getDefaultEncryptionModuleId')
  59. ->willReturn($oldModule);
  60. $this->config->expects($this->once())
  61. ->method('getSystemValue')
  62. ->with('maintenance', false)
  63. ->willReturn(false);
  64. if ($updateModule) {
  65. $this->manager->expects($this->once())
  66. ->method('setDefaultEncryptionModule')
  67. ->with($updateModule)
  68. ->willReturn($updateSuccess);
  69. }
  70. $this->consoleOutput->expects($this->once())
  71. ->method('writeln')
  72. ->with($this->stringContains($expectedString));
  73. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  74. }
  75. /**
  76. * @dataProvider dataSetDefaultModule
  77. *
  78. * @param string $oldModule
  79. * @param string $newModule
  80. * @param string $updateModule
  81. * @param bool $updateSuccess
  82. * @param string $expectedString
  83. */
  84. public function testMaintenanceMode($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString) {
  85. $this->consoleInput->expects($this->never())
  86. ->method('getArgument')
  87. ->with('module')
  88. ->willReturn($newModule);
  89. $this->manager->expects($this->never())
  90. ->method('getDefaultEncryptionModuleId')
  91. ->willReturn($oldModule);
  92. $this->config->expects($this->once())
  93. ->method('getSystemValue')
  94. ->with('maintenance', false)
  95. ->willReturn(true);
  96. $this->consoleOutput->expects($this->exactly(2))
  97. ->method('writeln')
  98. ->withConsecutive(
  99. [$this->stringContains('Maintenance mode must be disabled when setting default module,')],
  100. [$this->stringContains('in order to load the relevant encryption modules correctly.')],
  101. );
  102. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  103. }
  104. }