Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LogFactoryTest.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Johannes Ernst <jernst@indiecomputing.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Test\Log;
  25. use OC\Log\Errorlog;
  26. use OC\Log\File;
  27. use OC\Log\LogFactory;
  28. use OC\Log\Syslog;
  29. use OC\Log\Systemdlog;
  30. use OC\SystemConfig;
  31. use OCP\IServerContainer;
  32. use Test\TestCase;
  33. /**
  34. * Class LogFactoryTest
  35. *
  36. * @package Test\Log
  37. */
  38. class LogFactoryTest extends TestCase {
  39. /** @var IServerContainer|\PHPUnit\Framework\MockObject\MockObject */
  40. protected $c;
  41. /** @var LogFactory */
  42. protected $factory;
  43. /** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
  44. protected $systemConfig;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->c = $this->createMock(IServerContainer::class);
  48. $this->systemConfig = $this->createMock(SystemConfig::class);
  49. $this->factory = new LogFactory($this->c, $this->systemConfig);
  50. }
  51. public function fileTypeProvider(): array {
  52. return [
  53. [
  54. 'file'
  55. ],
  56. [
  57. 'nextcloud'
  58. ],
  59. [
  60. 'owncloud'
  61. ],
  62. [
  63. 'krzxkyr_default'
  64. ]
  65. ];
  66. }
  67. /**
  68. * @param string $type
  69. * @dataProvider fileTypeProvider
  70. * @throws \OCP\AppFramework\QueryException
  71. */
  72. public function testFile(string $type) {
  73. $datadir = \OC::$SERVERROOT.'/data';
  74. $defaultLog = $datadir . '/nextcloud.log';
  75. $this->systemConfig->expects($this->exactly(3))
  76. ->method('getValue')
  77. ->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog], ['logfilemode', 0640])
  78. ->willReturnOnConsecutiveCalls($datadir, $defaultLog, 0640);
  79. $log = $this->factory->get($type);
  80. $this->assertInstanceOf(File::class, $log);
  81. }
  82. public function logFilePathProvider():array {
  83. return [
  84. [
  85. '/dev/null',
  86. '/dev/null'
  87. ],
  88. [
  89. '/xdev/youshallfallback',
  90. \OC::$SERVERROOT.'/data/nextcloud.log'
  91. ]
  92. ];
  93. }
  94. /**
  95. * @dataProvider logFilePathProvider
  96. * @throws \OCP\AppFramework\QueryException
  97. */
  98. public function testFileCustomPath($path, $expected) {
  99. $datadir = \OC::$SERVERROOT.'/data';
  100. $defaultLog = $datadir . '/nextcloud.log';
  101. $this->systemConfig->expects($this->exactly(3))
  102. ->method('getValue')
  103. ->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog], ['logfilemode', 0640])
  104. ->willReturnOnConsecutiveCalls($datadir, $path, 0640);
  105. $log = $this->factory->get('file');
  106. $this->assertInstanceOf(File::class, $log);
  107. $this->assertSame($expected, $log->getLogFilePath());
  108. }
  109. /**
  110. * @throws \OCP\AppFramework\QueryException
  111. */
  112. public function testErrorLog() {
  113. $log = $this->factory->get('errorlog');
  114. $this->assertInstanceOf(Errorlog::class, $log);
  115. }
  116. /**
  117. * @throws \OCP\AppFramework\QueryException
  118. */
  119. public function testSystemLog() {
  120. $this->c->expects($this->once())
  121. ->method('resolve')
  122. ->with(Syslog::class)
  123. ->willReturn($this->createMock(Syslog::class));
  124. $log = $this->factory->get('syslog');
  125. $this->assertInstanceOf(Syslog::class, $log);
  126. }
  127. /**
  128. * @throws \OCP\AppFramework\QueryException
  129. */
  130. public function testSystemdLog() {
  131. $this->c->expects($this->once())
  132. ->method('resolve')
  133. ->with(Systemdlog::class)
  134. ->willReturn($this->createMock(Systemdlog::class));
  135. $log = $this->factory->get('systemd');
  136. $this->assertInstanceOf(Systemdlog::class, $log);
  137. }
  138. }