ManageTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Tests\Core\Command\Log;
  8. use OC\Core\Command\Log\Manage;
  9. use OCP\IConfig;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Test\TestCase;
  13. class ManageTest extends TestCase {
  14. /** @var \PHPUnit\Framework\MockObject\MockObject */
  15. protected $config;
  16. /** @var \PHPUnit\Framework\MockObject\MockObject */
  17. protected $consoleInput;
  18. /** @var \PHPUnit\Framework\MockObject\MockObject */
  19. protected $consoleOutput;
  20. /** @var \Symfony\Component\Console\Command\Command */
  21. protected $command;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $config = $this->config = $this->getMockBuilder(IConfig::class)
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  28. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  29. $this->command = new Manage($config);
  30. }
  31. public function testChangeBackend() {
  32. $this->consoleInput->method('getOption')
  33. ->willReturnMap([
  34. ['backend', 'syslog']
  35. ]);
  36. $this->config->expects($this->once())
  37. ->method('setSystemValue')
  38. ->with('log_type', 'syslog');
  39. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  40. }
  41. public function testChangeLevel() {
  42. $this->consoleInput->method('getOption')
  43. ->willReturnMap([
  44. ['level', 'debug']
  45. ]);
  46. $this->config->expects($this->once())
  47. ->method('setSystemValue')
  48. ->with('loglevel', 0);
  49. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  50. }
  51. public function testChangeTimezone() {
  52. $this->consoleInput->method('getOption')
  53. ->willReturnMap([
  54. ['timezone', 'UTC']
  55. ]);
  56. $this->config->expects($this->once())
  57. ->method('setSystemValue')
  58. ->with('logtimezone', 'UTC');
  59. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  60. }
  61. public function testValidateBackend() {
  62. $this->expectException(\InvalidArgumentException::class);
  63. self::invokePrivate($this->command, 'validateBackend', ['notabackend']);
  64. }
  65. public function testValidateTimezone() {
  66. $this->expectException(\Exception::class);
  67. // this might need to be changed when humanity colonises Mars
  68. self::invokePrivate($this->command, 'validateTimezone', ['Mars/OlympusMons']);
  69. }
  70. public function convertLevelStringProvider() {
  71. return [
  72. ['dEbug', 0],
  73. ['inFO', 1],
  74. ['Warning', 2],
  75. ['wArn', 2],
  76. ['error', 3],
  77. ['eRr', 3],
  78. ['fAtAl', 4],
  79. ];
  80. }
  81. /**
  82. * @dataProvider convertLevelStringProvider
  83. */
  84. public function testConvertLevelString($levelString, $expectedInt) {
  85. $this->assertEquals($expectedInt,
  86. self::invokePrivate($this->command, 'convertLevelString', [$levelString])
  87. );
  88. }
  89. public function testConvertLevelStringInvalid() {
  90. $this->expectException(\InvalidArgumentException::class);
  91. self::invokePrivate($this->command, 'convertLevelString', ['abc']);
  92. }
  93. public function convertLevelNumberProvider() {
  94. return [
  95. [0, 'Debug'],
  96. [1, 'Info'],
  97. [2, 'Warning'],
  98. [3, 'Error'],
  99. [4, 'Fatal'],
  100. ];
  101. }
  102. /**
  103. * @dataProvider convertLevelNumberProvider
  104. */
  105. public function testConvertLevelNumber($levelNum, $expectedString) {
  106. $this->assertEquals($expectedString,
  107. self::invokePrivate($this->command, 'convertLevelNumber', [$levelNum])
  108. );
  109. }
  110. public function testConvertLevelNumberInvalid() {
  111. $this->expectException(\InvalidArgumentException::class);
  112. self::invokePrivate($this->command, 'convertLevelNumber', [11]);
  113. }
  114. public function testGetConfiguration() {
  115. $this->config->expects($this->exactly(3))
  116. ->method('getSystemValue')
  117. ->withConsecutive(
  118. ['log_type', 'file'],
  119. ['loglevel', 2],
  120. ['logtimezone', 'UTC'],
  121. )->willReturnOnConsecutiveCalls(
  122. 'log_type_value',
  123. 0,
  124. 'logtimezone_value'
  125. );
  126. $this->consoleOutput->expects($this->exactly(3))
  127. ->method('writeln')
  128. ->withConsecutive(
  129. ['Enabled logging backend: log_type_value'],
  130. ['Log level: Debug (0)'],
  131. ['Log timezone: logtimezone_value'],
  132. );
  133. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  134. }
  135. }