EnableTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Enable;
  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 EnableTest extends TestCase {
  29. /** @var \PHPUnit_Framework_MockObject_MockObject */
  30. protected $config;
  31. /** @var \PHPUnit_Framework_MockObject_MockObject */
  32. protected $manager;
  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() {
  40. parent::setUp();
  41. $config = $this->config = $this->getMockBuilder(IConfig::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $manager = $this->manager = $this->getMockBuilder(IManager::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  48. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  49. /** @var \OCP\IConfig $config */
  50. /** @var \OCP\Encryption\IManager $manager */
  51. $this->command = new Enable($config, $manager);
  52. }
  53. public function dataEnable() {
  54. return [
  55. ['no', null, [], true, 'Encryption enabled', 'No encryption module is loaded'],
  56. ['yes', null, [], false, 'Encryption is already enabled', 'No encryption module is loaded'],
  57. ['no', null, ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'No default module is set'],
  58. ['no', 'OC_NO_MODULE', ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'The current default module does not exist: OC_NO_MODULE'],
  59. ['no', 'OC_TEST_MODULE', ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'Default module: OC_TEST_MODULE'],
  60. ];
  61. }
  62. /**
  63. * @dataProvider dataEnable
  64. *
  65. * @param string $oldStatus
  66. * @param string $defaultModule
  67. * @param array $availableModules
  68. * @param bool $isUpdating
  69. * @param string $expectedString
  70. * @param string $expectedDefaultModuleString
  71. */
  72. public function testEnable($oldStatus, $defaultModule, $availableModules, $isUpdating, $expectedString, $expectedDefaultModuleString) {
  73. $invokeCount = 0;
  74. $this->config->expects($this->at($invokeCount))
  75. ->method('getAppValue')
  76. ->with('core', 'encryption_enabled', $this->anything())
  77. ->willReturn($oldStatus);
  78. $invokeCount++;
  79. if ($isUpdating) {
  80. $this->config->expects($this->once())
  81. ->method('setAppValue')
  82. ->with('core', 'encryption_enabled', 'yes');
  83. $invokeCount++;
  84. }
  85. $this->manager->expects($this->atLeastOnce())
  86. ->method('getEncryptionModules')
  87. ->willReturn($availableModules);
  88. if (!empty($availableModules)) {
  89. $this->config->expects($this->at($invokeCount))
  90. ->method('getAppValue')
  91. ->with('core', 'default_encryption_module', $this->anything())
  92. ->willReturn($defaultModule);
  93. }
  94. $this->consoleOutput->expects($this->at(0))
  95. ->method('writeln')
  96. ->with($this->stringContains($expectedString));
  97. $this->consoleOutput->expects($this->at(1))
  98. ->method('writeln')
  99. ->with('');
  100. $this->consoleOutput->expects($this->at(2))
  101. ->method('writeln')
  102. ->with($this->stringContains($expectedDefaultModuleString));
  103. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  104. }
  105. }