LogFactoryTest.php 4.0 KB

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\IConfig;
  32. use OCP\IServerContainer;
  33. use Test\TestCase;
  34. /**
  35. * Class LogFactoryTest
  36. *
  37. * @package Test\Log
  38. */
  39. class LogFactoryTest extends TestCase {
  40. /** @var IServerContainer|\PHPUnit_Framework_MockObject_MockObject */
  41. protected $c;
  42. /** @var LogFactory */
  43. protected $factory;
  44. /** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
  45. protected $systemConfig;
  46. protected function setUp() {
  47. parent::setUp();
  48. $this->c = $this->createMock(IServerContainer::class);
  49. $this->systemConfig = $this->createMock(SystemConfig::class);
  50. $this->factory = new LogFactory($this->c, $this->systemConfig);
  51. }
  52. public function fileTypeProvider(): array {
  53. return [
  54. [
  55. 'file'
  56. ],
  57. [
  58. 'nextcloud'
  59. ],
  60. [
  61. 'owncloud'
  62. ],
  63. [
  64. 'krzxkyr_default'
  65. ]
  66. ];
  67. }
  68. /**
  69. * @param string $type
  70. * @dataProvider fileTypeProvider
  71. * @throws \OCP\AppFramework\QueryException
  72. */
  73. public function testFile(string $type) {
  74. $datadir = \OC::$SERVERROOT.'/data';
  75. $defaultLog = $datadir . '/nextcloud.log';
  76. $this->systemConfig->expects($this->exactly(3))
  77. ->method('getValue')
  78. ->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog], ['logfilemode', 0640])
  79. ->willReturnOnConsecutiveCalls($datadir, $defaultLog, 0640);
  80. $log = $this->factory->get($type);
  81. $this->assertInstanceOf(File::class, $log);
  82. }
  83. public function logFilePathProvider():array {
  84. return [
  85. [
  86. '/dev/null',
  87. '/dev/null'
  88. ],
  89. [
  90. '/xdev/youshallfallback',
  91. \OC::$SERVERROOT.'/data/nextcloud.log'
  92. ]
  93. ];
  94. }
  95. /**
  96. * @dataProvider logFilePathProvider
  97. * @throws \OCP\AppFramework\QueryException
  98. */
  99. public function testFileCustomPath($path, $expected) {
  100. $datadir = \OC::$SERVERROOT.'/data';
  101. $defaultLog = $datadir . '/nextcloud.log';
  102. $this->systemConfig->expects($this->exactly(3))
  103. ->method('getValue')
  104. ->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog], ['logfilemode', 0640])
  105. ->willReturnOnConsecutiveCalls($datadir, $path, 0640);
  106. $log = $this->factory->get('file');
  107. $this->assertInstanceOf(File::class, $log);
  108. $this->assertSame($expected, $log->getLogFilePath());
  109. }
  110. /**
  111. * @throws \OCP\AppFramework\QueryException
  112. */
  113. public function testErrorLog() {
  114. $log = $this->factory->get('errorlog');
  115. $this->assertInstanceOf(Errorlog::class, $log);
  116. }
  117. /**
  118. * @throws \OCP\AppFramework\QueryException
  119. */
  120. public function testSystemLog() {
  121. $this->c->expects($this->once())
  122. ->method('resolve')
  123. ->with(Syslog::class)
  124. ->willReturn($this->createMock(Syslog::class));
  125. $log = $this->factory->get('syslog');
  126. $this->assertInstanceOf(Syslog::class, $log);
  127. }
  128. /**
  129. * @throws \OCP\AppFramework\QueryException
  130. */
  131. public function testSystemdLog() {
  132. $this->c->expects($this->once())
  133. ->method('resolve')
  134. ->with(Systemdlog::class)
  135. ->willReturn($this->createMock(Systemdlog::class));
  136. $log = $this->factory->get('systemd');
  137. $this->assertInstanceOf(Systemdlog::class, $log);
  138. }
  139. }