FileTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  6. * License as published by the Free Software Foundation; either
  7. * version 3 of the License, or any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public
  15. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. namespace Test\Log;
  18. use OC\Log\File;
  19. use OCP\ILogger;
  20. use Test\TestCase;
  21. /**
  22. * Class FileTest
  23. *
  24. * @group DB
  25. */
  26. class FileTest extends TestCase
  27. {
  28. private $restore_logfile;
  29. private $restore_logdateformat;
  30. /** @var File */
  31. protected $logFile;
  32. protected function setUp() {
  33. parent::setUp();
  34. $config = \OC::$server->getSystemConfig();
  35. $this->restore_logfile = $config->getValue("logfile");
  36. $this->restore_logdateformat = $config->getValue('logdateformat');
  37. $config->setValue("logfile", $config->getValue('datadirectory') . "/logtest.log");
  38. $this->logFile = new File($config->getValue('datadirectory') . '/logtest.log', '', $config);
  39. }
  40. protected function tearDown() {
  41. $config = \OC::$server->getSystemConfig();
  42. if (isset($this->restore_logfile)) {
  43. $config->getValue("logfile", $this->restore_logfile);
  44. } else {
  45. $config->deleteValue("logfile");
  46. }
  47. if (isset($this->restore_logdateformat)) {
  48. $config->getValue("logdateformat", $this->restore_logdateformat);
  49. } else {
  50. $config->deleteValue("logdateformat");
  51. }
  52. $this->logFile = new File($this->restore_logfile, '', $config);
  53. parent::tearDown();
  54. }
  55. public function testMicrosecondsLogTimestamp() {
  56. $config = \OC::$server->getConfig();
  57. # delete old logfile
  58. unlink($config->getSystemValue('logfile'));
  59. # set format & write log line
  60. $config->setSystemValue('logdateformat', 'u');
  61. $this->logFile->write('test', 'message', ILogger::ERROR);
  62. # read log line
  63. $handle = @fopen($config->getSystemValue('logfile'), 'r');
  64. $line = fread($handle, 1000);
  65. fclose($handle);
  66. # check timestamp has microseconds part
  67. $values = (array) json_decode($line);
  68. $microseconds = $values['time'];
  69. $this->assertNotEquals(0, $microseconds);
  70. }
  71. }