FileTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. protected function setUp() {
  31. parent::setUp();
  32. $config = \OC::$server->getConfig();
  33. $this->restore_logfile = $config->getSystemValue("logfile");
  34. $this->restore_logdateformat = $config->getSystemValue('logdateformat');
  35. $config->setSystemValue("logfile", $config->getSystemValue('datadirectory') . "/logtest");
  36. File::init();
  37. }
  38. protected function tearDown() {
  39. $config = \OC::$server->getConfig();
  40. if (isset($this->restore_logfile)) {
  41. $config->getSystemValue("logfile", $this->restore_logfile);
  42. } else {
  43. $config->deleteSystemValue("logfile");
  44. }
  45. if (isset($this->restore_logdateformat)) {
  46. $config->getSystemValue("logdateformat", $this->restore_logdateformat);
  47. } else {
  48. $config->deleteSystemValue("logdateformat");
  49. }
  50. File::init();
  51. parent::tearDown();
  52. }
  53. public function testMicrosecondsLogTimestamp() {
  54. $config = \OC::$server->getConfig();
  55. # delete old logfile
  56. unlink($config->getSystemValue('logfile'));
  57. # set format & write log line
  58. $config->setSystemValue('logdateformat', 'u');
  59. File::write('test', 'message', ILogger::ERROR);
  60. # read log line
  61. $handle = @fopen($config->getSystemValue('logfile'), 'r');
  62. $line = fread($handle, 1000);
  63. fclose($handle);
  64. # check timestamp has microseconds part
  65. $values = (array) json_decode($line);
  66. $microseconds = $values['time'];
  67. $this->assertNotEquals(0, $microseconds);
  68. }
  69. }