managetest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @author Robin McCorkell <rmccorkell@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\Log;
  22. use OC\Core\Command\Log\Manage;
  23. use Test\TestCase;
  24. class ManageTest extends TestCase {
  25. /** @var \PHPUnit_Framework_MockObject_MockObject */
  26. protected $config;
  27. /** @var \PHPUnit_Framework_MockObject_MockObject */
  28. protected $consoleInput;
  29. /** @var \PHPUnit_Framework_MockObject_MockObject */
  30. protected $consoleOutput;
  31. /** @var \Symfony\Component\Console\Command\Command */
  32. protected $command;
  33. protected function setUp() {
  34. parent::setUp();
  35. $config = $this->config = $this->getMockBuilder('OCP\IConfig')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  39. $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
  40. $this->command = new Manage($config);
  41. }
  42. public function testChangeBackend() {
  43. $this->consoleInput->method('getOption')
  44. ->will($this->returnValueMap([
  45. ['backend', 'syslog']
  46. ]));
  47. $this->config->expects($this->once())
  48. ->method('setSystemValue')
  49. ->with('log_type', 'syslog');
  50. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  51. }
  52. public function testChangeLevel() {
  53. $this->consoleInput->method('getOption')
  54. ->will($this->returnValueMap([
  55. ['level', 'debug']
  56. ]));
  57. $this->config->expects($this->once())
  58. ->method('setSystemValue')
  59. ->with('loglevel', 0);
  60. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  61. }
  62. public function testChangeTimezone() {
  63. $this->consoleInput->method('getOption')
  64. ->will($this->returnValueMap([
  65. ['timezone', 'UTC']
  66. ]));
  67. $this->config->expects($this->once())
  68. ->method('setSystemValue')
  69. ->with('logtimezone', 'UTC');
  70. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  71. }
  72. /**
  73. * @expectedException \InvalidArgumentException
  74. */
  75. public function testValidateBackend() {
  76. self::invokePrivate($this->command, 'validateBackend', ['notabackend']);
  77. }
  78. /**
  79. * @expectedException \Exception
  80. */
  81. public function testValidateTimezone() {
  82. // this might need to be changed when humanity colonises Mars
  83. self::invokePrivate($this->command, 'validateTimezone', ['Mars/OlympusMons']);
  84. }
  85. public function convertLevelStringProvider() {
  86. return [
  87. ['dEbug', 0],
  88. ['inFO', 1],
  89. ['Warning', 2],
  90. ['wArn', 2],
  91. ['error', 3],
  92. ['eRr', 3],
  93. ];
  94. }
  95. /**
  96. * @dataProvider convertLevelStringProvider
  97. */
  98. public function testConvertLevelString($levelString, $expectedInt) {
  99. $this->assertEquals($expectedInt,
  100. self::invokePrivate($this->command, 'convertLevelString', [$levelString])
  101. );
  102. }
  103. /**
  104. * @expectedException \InvalidArgumentException
  105. */
  106. public function testConvertLevelStringInvalid() {
  107. self::invokePrivate($this->command, 'convertLevelString', ['abc']);
  108. }
  109. public function convertLevelNumberProvider() {
  110. return [
  111. [0, 'Debug'],
  112. [1, 'Info'],
  113. [2, 'Warning'],
  114. [3, 'Error'],
  115. ];
  116. }
  117. /**
  118. * @dataProvider convertLevelNumberProvider
  119. */
  120. public function testConvertLevelNumber($levelNum, $expectedString) {
  121. $this->assertEquals($expectedString,
  122. self::invokePrivate($this->command, 'convertLevelNumber', [$levelNum])
  123. );
  124. }
  125. /**
  126. * @expectedException \InvalidArgumentException
  127. */
  128. public function testConvertLevelNumberInvalid() {
  129. self::invokePrivate($this->command, 'convertLevelNumber', [11]);
  130. }
  131. public function testGetConfiguration() {
  132. $this->config->expects($this->at(0))
  133. ->method('getSystemValue')
  134. ->with('log_type', 'owncloud')
  135. ->willReturn('log_type_value');
  136. $this->config->expects($this->at(1))
  137. ->method('getSystemValue')
  138. ->with('loglevel', 2)
  139. ->willReturn(0);
  140. $this->config->expects($this->at(2))
  141. ->method('getSystemValue')
  142. ->with('logtimezone', 'UTC')
  143. ->willReturn('logtimezone_value');
  144. $this->consoleOutput->expects($this->at(0))
  145. ->method('writeln')
  146. ->with('Enabled logging backend: log_type_value');
  147. $this->consoleOutput->expects($this->at(1))
  148. ->method('writeln')
  149. ->with('Log level: Debug (0)');
  150. $this->consoleOutput->expects($this->at(2))
  151. ->method('writeln')
  152. ->with('Log timezone: logtimezone_value');
  153. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  154. }
  155. }