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