EnableTest.php 3.9 KB

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