LogFactoryTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Log;
  7. use OC\Log\Errorlog;
  8. use OC\Log\File;
  9. use OC\Log\LogFactory;
  10. use OC\Log\Syslog;
  11. use OC\Log\Systemdlog;
  12. use OC\SystemConfig;
  13. use OCP\IServerContainer;
  14. use Test\TestCase;
  15. /**
  16. * Class LogFactoryTest
  17. *
  18. * @package Test\Log
  19. */
  20. class LogFactoryTest extends TestCase {
  21. /** @var IServerContainer|\PHPUnit\Framework\MockObject\MockObject */
  22. protected $c;
  23. /** @var LogFactory */
  24. protected $factory;
  25. /** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
  26. protected $systemConfig;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->c = $this->createMock(IServerContainer::class);
  30. $this->systemConfig = $this->createMock(SystemConfig::class);
  31. $this->factory = new LogFactory($this->c, $this->systemConfig);
  32. }
  33. public function fileTypeProvider(): array {
  34. return [
  35. [
  36. 'file'
  37. ],
  38. [
  39. 'nextcloud'
  40. ],
  41. [
  42. 'owncloud'
  43. ],
  44. [
  45. 'krzxkyr_default'
  46. ]
  47. ];
  48. }
  49. /**
  50. * @param string $type
  51. * @dataProvider fileTypeProvider
  52. * @throws \OCP\AppFramework\QueryException
  53. */
  54. public function testFile(string $type): void {
  55. $datadir = \OC::$SERVERROOT.'/data';
  56. $defaultLog = $datadir . '/nextcloud.log';
  57. $this->systemConfig->expects($this->exactly(3))
  58. ->method('getValue')
  59. ->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog], ['logfilemode', 0640])
  60. ->willReturnOnConsecutiveCalls($datadir, $defaultLog, 0640);
  61. $log = $this->factory->get($type);
  62. $this->assertInstanceOf(File::class, $log);
  63. }
  64. public function logFilePathProvider():array {
  65. return [
  66. [
  67. '/dev/null',
  68. '/dev/null'
  69. ],
  70. [
  71. '/xdev/youshallfallback',
  72. \OC::$SERVERROOT.'/data/nextcloud.log'
  73. ]
  74. ];
  75. }
  76. /**
  77. * @dataProvider logFilePathProvider
  78. * @throws \OCP\AppFramework\QueryException
  79. */
  80. public function testFileCustomPath($path, $expected): void {
  81. $datadir = \OC::$SERVERROOT.'/data';
  82. $defaultLog = $datadir . '/nextcloud.log';
  83. $this->systemConfig->expects($this->exactly(3))
  84. ->method('getValue')
  85. ->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog], ['logfilemode', 0640])
  86. ->willReturnOnConsecutiveCalls($datadir, $path, 0640);
  87. $log = $this->factory->get('file');
  88. $this->assertInstanceOf(File::class, $log);
  89. $this->assertSame($expected, $log->getLogFilePath());
  90. }
  91. /**
  92. * @throws \OCP\AppFramework\QueryException
  93. */
  94. public function testErrorLog(): void {
  95. $log = $this->factory->get('errorlog');
  96. $this->assertInstanceOf(Errorlog::class, $log);
  97. }
  98. /**
  99. * @throws \OCP\AppFramework\QueryException
  100. */
  101. public function testSystemLog(): void {
  102. $this->c->expects($this->once())
  103. ->method('resolve')
  104. ->with(Syslog::class)
  105. ->willReturn($this->createMock(Syslog::class));
  106. $log = $this->factory->get('syslog');
  107. $this->assertInstanceOf(Syslog::class, $log);
  108. }
  109. /**
  110. * @throws \OCP\AppFramework\QueryException
  111. */
  112. public function testSystemdLog(): void {
  113. $this->c->expects($this->once())
  114. ->method('resolve')
  115. ->with(Systemdlog::class)
  116. ->willReturn($this->createMock(Systemdlog::class));
  117. $log = $this->factory->get('systemd');
  118. $this->assertInstanceOf(Systemdlog::class, $log);
  119. }
  120. }