SetDefaultModuleTest.php 4.2 KB

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