Fixed issues with logging
- Fixed log file name template - Fixed buffering changes when running add/edit jobs in batch
This commit is contained in:
parent
404bd979f4
commit
323138bd98
12 changed files with 141 additions and 31 deletions
|
@ -5,7 +5,7 @@ dbUser = "test"
|
||||||
dbPass = "test"
|
dbPass = "test"
|
||||||
filesPath = "./data/files/"
|
filesPath = "./data/files/"
|
||||||
thumbsPath = "./data/thumbs/"
|
thumbsPath = "./data/thumbs/"
|
||||||
logsPath = "./data/logs/{yyyy-mm}.log"
|
logsPath = "./data/logs/{yyyy}-{mm}.log"
|
||||||
mediaPath = "./public_html/media/"
|
mediaPath = "./public_html/media/"
|
||||||
title = "szurubooru"
|
title = "szurubooru"
|
||||||
salt = "1A2/$_4xVa"
|
salt = "1A2/$_4xVa"
|
||||||
|
|
|
@ -7,29 +7,30 @@ class AddPostJob extends AbstractJob
|
||||||
{
|
{
|
||||||
$post = PostModel::spawn();
|
$post = PostModel::spawn();
|
||||||
|
|
||||||
//basic stuff
|
|
||||||
$anonymous = $this->getArgument(self::ANONYMOUS);
|
$anonymous = $this->getArgument(self::ANONYMOUS);
|
||||||
if (Auth::isLoggedIn() and !$anonymous)
|
if (Auth::isLoggedIn() and !$anonymous)
|
||||||
$post->setUploader(Auth::getCurrentUser());
|
$post->setUploader(Auth::getCurrentUser());
|
||||||
|
|
||||||
//store the post to get the ID in the logs
|
|
||||||
PostModel::forgeId($post);
|
PostModel::forgeId($post);
|
||||||
|
|
||||||
//do the edits
|
|
||||||
//warning: it uses internally the same privileges as post editing
|
|
||||||
$arguments = $this->getArguments();
|
$arguments = $this->getArguments();
|
||||||
$arguments[EditPostJob::POST_ENTITY] = $post;
|
$arguments[EditPostJob::POST_ENTITY] = $post;
|
||||||
|
|
||||||
Logger::bufferChanges();
|
Logger::bufferChanges();
|
||||||
$job = new EditPostJob();
|
try
|
||||||
$job->setContext(AbstractJob::CONTEXT_BATCH_ADD);
|
{
|
||||||
Api::run($job, $arguments);
|
$job = new EditPostJob();
|
||||||
Logger::setBuffer([]);
|
$job->setContext(AbstractJob::CONTEXT_BATCH_ADD);
|
||||||
|
Api::run($job, $arguments);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Logger::discardBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
//save to db
|
//save the post to db if everything went okay
|
||||||
PostModel::save($post);
|
PostModel::save($post);
|
||||||
|
|
||||||
//log
|
|
||||||
Logger::log('{user} added {post} (tags: {tags}, safety: {safety}, source: {source})', [
|
Logger::log('{user} added {post} (tags: {tags}, safety: {safety}, source: {source})', [
|
||||||
'user' => ($anonymous and !getConfig()->misc->logAnonymousUploads)
|
'user' => ($anonymous and !getConfig()->misc->logAnonymousUploads)
|
||||||
? TextHelper::reprUser(UserModel::getAnonymousName())
|
? TextHelper::reprUser(UserModel::getAnonymousName())
|
||||||
|
@ -39,7 +40,6 @@ class AddPostJob extends AbstractJob
|
||||||
'safety' => $post->getSafety()->toString(),
|
'safety' => $post->getSafety()->toString(),
|
||||||
'source' => $post->source]);
|
'source' => $post->source]);
|
||||||
|
|
||||||
//finish
|
|
||||||
Logger::flush();
|
Logger::flush();
|
||||||
|
|
||||||
return $post;
|
return $post;
|
||||||
|
|
|
@ -23,10 +23,16 @@ class AddUserJob extends AbstractJob
|
||||||
$arguments[EditUserJob::USER_ENTITY] = $user;
|
$arguments[EditUserJob::USER_ENTITY] = $user;
|
||||||
|
|
||||||
Logger::bufferChanges();
|
Logger::bufferChanges();
|
||||||
$job = new EditUserJob();
|
try
|
||||||
$job->setContext(self::CONTEXT_BATCH_ADD);
|
{
|
||||||
Api::run($job, $arguments);
|
$job = new EditUserJob();
|
||||||
Logger::setBuffer([]);
|
$job->setContext(self::CONTEXT_BATCH_ADD);
|
||||||
|
Api::run($job, $arguments);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Logger::discardBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
//save the user to db if everything went okay
|
//save the user to db if everything went okay
|
||||||
UserModel::save($user);
|
UserModel::save($user);
|
||||||
|
|
|
@ -35,9 +35,11 @@ class EditPostJob extends AbstractPostJob
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->getContext() == AbstractJob::CONTEXT_NORMAL)
|
if ($this->getContext() == AbstractJob::CONTEXT_NORMAL)
|
||||||
|
{
|
||||||
PostModel::save($post);
|
PostModel::save($post);
|
||||||
|
Logger::flush();
|
||||||
|
}
|
||||||
|
|
||||||
Logger::flush();
|
|
||||||
return $post;
|
return $post;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -59,9 +59,9 @@ class EditUserJob extends AbstractUserJob
|
||||||
{
|
{
|
||||||
UserModel::save($user);
|
UserModel::save($user);
|
||||||
EditUserEmailJob::observeSave($user);
|
EditUserEmailJob::observeSave($user);
|
||||||
|
Logger::flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::flush();
|
|
||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,9 @@ class Logger
|
||||||
|
|
||||||
public static function flush()
|
public static function flush()
|
||||||
{
|
{
|
||||||
|
if (empty(self::$buffer))
|
||||||
|
return;
|
||||||
|
|
||||||
$fh = fopen(self::$path, 'ab');
|
$fh = fopen(self::$path, 'ab');
|
||||||
if (!$fh)
|
if (!$fh)
|
||||||
throw new SimpleException('Cannot write to log files');
|
throw new SimpleException('Cannot write to log files');
|
||||||
|
@ -61,8 +64,8 @@ class Logger
|
||||||
return self::$buffer;
|
return self::$buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function setBuffer(array $buffer)
|
public static function discardBuffer()
|
||||||
{
|
{
|
||||||
self::$buffer = $buffer;
|
self::$buffer = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,16 @@ class AddPostJobTest extends AbstractTest
|
||||||
}, 'Insufficient privilege');
|
}, 'Insufficient privilege');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testLogBuffering()
|
||||||
|
{
|
||||||
|
$this->testSaving();
|
||||||
|
|
||||||
|
$logPath = Logger::getLogPath();
|
||||||
|
$x = file_get_contents($logPath);
|
||||||
|
$lines = array_filter(explode("\n", $x));
|
||||||
|
$this->assert->areEqual(1, count($lines));
|
||||||
|
}
|
||||||
|
|
||||||
protected function prepare()
|
protected function prepare()
|
||||||
{
|
{
|
||||||
getConfig()->registration->needEmailForUploading = false;
|
getConfig()->registration->needEmailForUploading = false;
|
||||||
|
|
|
@ -262,4 +262,14 @@ class AddUserJobTest extends AbstractTest
|
||||||
|
|
||||||
$this->assert->areEqual(0, Mailer::getMailCounter());
|
$this->assert->areEqual(0, Mailer::getMailCounter());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testLogBuffering()
|
||||||
|
{
|
||||||
|
$this->testSaving();
|
||||||
|
|
||||||
|
$logPath = Logger::getLogPath();
|
||||||
|
$x = file_get_contents($logPath);
|
||||||
|
$lines = array_filter(explode("\n", $x));
|
||||||
|
$this->assert->areEqual(2, count($lines));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,8 @@ class EditPostJobTest extends AbstractTest
|
||||||
$args =
|
$args =
|
||||||
[
|
[
|
||||||
EditPostJob::POST_ID => $post->getId(),
|
EditPostJob::POST_ID => $post->getId(),
|
||||||
EditPostSafetyJob::SAFETY => PostSafety::Safe,
|
EditPostSafetyJob::SAFETY => PostSafety::Sketchy,
|
||||||
EditPostSourceJob::SOURCE => '',
|
EditPostSourceJob::SOURCE => 'some source huh',
|
||||||
EditPostContentJob::POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'),
|
EditPostContentJob::POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -47,4 +47,14 @@ class EditPostJobTest extends AbstractTest
|
||||||
Api::run(new EditPostJob(), $args);
|
Api::run(new EditPostJob(), $args);
|
||||||
}, 'Insufficient privilege');
|
}, 'Insufficient privilege');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testLogBuffering()
|
||||||
|
{
|
||||||
|
$this->testSaving();
|
||||||
|
|
||||||
|
$logPath = Logger::getLogPath();
|
||||||
|
$x = file_get_contents($logPath);
|
||||||
|
$lines = array_filter(explode("\n", $x));
|
||||||
|
$this->assert->areEqual(3, count($lines));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
39
tests/JobTests/EditUserJobTest.php
Normal file
39
tests/JobTests/EditUserJobTest.php
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
class EditUserJobTest extends AbstractTest
|
||||||
|
{
|
||||||
|
public function testSaving()
|
||||||
|
{
|
||||||
|
$this->grantAccess('changeUserName.own');
|
||||||
|
$this->grantAccess('changeUserPassword.own');
|
||||||
|
$user = $this->mockUser();
|
||||||
|
|
||||||
|
$newName = 'dummy' . uniqid();
|
||||||
|
|
||||||
|
$user = $this->assert->doesNotThrow(function() use ($user, $newName)
|
||||||
|
{
|
||||||
|
return Api::run(
|
||||||
|
new EditUserJob(),
|
||||||
|
[
|
||||||
|
EditUserJob::USER_NAME => $user->getName(),
|
||||||
|
EditUserNameJob::NEW_USER_NAME => $newName,
|
||||||
|
EditUserPasswordJob::NEW_PASSWORD => 'changed',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
//first user = admin
|
||||||
|
$this->assert->areEqual($newName, $user->getName());
|
||||||
|
$this->assert->areEquivalent(new AccessRank(AccessRank::Registered), $user->getAccessRank());
|
||||||
|
$this->assert->isFalse(empty($user->getPasswordSalt()));
|
||||||
|
$this->assert->isFalse(empty($user->getPasswordHash()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLogBuffering()
|
||||||
|
{
|
||||||
|
$this->testSaving();
|
||||||
|
|
||||||
|
$logPath = Logger::getLogPath();
|
||||||
|
$x = file_get_contents($logPath);
|
||||||
|
$lines = array_filter(explode("\n", $x));
|
||||||
|
$this->assert->areEqual(2, count($lines));
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,17 +3,10 @@ class LoggerTest extends AbstractTest
|
||||||
{
|
{
|
||||||
public function testLogging()
|
public function testLogging()
|
||||||
{
|
{
|
||||||
$logPath = __DIR__ . '/logs/{yyyy}-{mm}-{dd}.log';
|
$realLogPath = Logger::getLogPath();
|
||||||
$realLogPath = __DIR__ . '/logs/' . date('Y-m-d') . '.log';
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
getConfig()->main->logsPath = $logPath;
|
|
||||||
$this->assert->doesNotThrow(function()
|
|
||||||
{
|
|
||||||
Logger::init();
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->assert->isFalse(file_exists($realLogPath));
|
$this->assert->isFalse(file_exists($realLogPath));
|
||||||
$this->assert->doesNotThrow(function()
|
$this->assert->doesNotThrow(function()
|
||||||
{
|
{
|
||||||
|
@ -30,4 +23,41 @@ class LoggerTest extends AbstractTest
|
||||||
unlink($realLogPath);
|
unlink($realLogPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testPathChanging()
|
||||||
|
{
|
||||||
|
$logPath = __DIR__ . '/logs/{yyyy}-{mm}-{dd}.log';
|
||||||
|
$realLogPath = __DIR__ . '/logs/' . date('Y-m-d') . '.log';
|
||||||
|
|
||||||
|
getConfig()->main->logsPath = $logPath;
|
||||||
|
$this->assert->doesNotThrow(function()
|
||||||
|
{
|
||||||
|
Logger::init();
|
||||||
|
});
|
||||||
|
$this->assert->areEqual($realLogPath, Logger::getLogPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDiscarding()
|
||||||
|
{
|
||||||
|
$realLogPath = Logger::getLogPath();
|
||||||
|
|
||||||
|
$this->assert->isFalse(file_exists($realLogPath));
|
||||||
|
|
||||||
|
Logger::bufferChanges();
|
||||||
|
Logger::log('line 1');
|
||||||
|
Logger::log('line 2');
|
||||||
|
Logger::log('line 3');
|
||||||
|
Logger::discardBuffer();
|
||||||
|
|
||||||
|
$this->assert->isFalse(file_exists($realLogPath));
|
||||||
|
Logger::log('line 4');
|
||||||
|
Logger::flush();
|
||||||
|
$this->assert->isTrue(file_exists($realLogPath));
|
||||||
|
|
||||||
|
$x = file_get_contents($realLogPath);
|
||||||
|
$this->assert->isTrue(strpos($x, 'line 1') === false);
|
||||||
|
$this->assert->isTrue(strpos($x, 'line 2') === false);
|
||||||
|
$this->assert->isTrue(strpos($x, 'line 3') === false);
|
||||||
|
$this->assert->isTrue(strpos($x, 'line 4') !== false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[main]
|
[main]
|
||||||
filesPath = "./tests/files/"
|
filesPath = "./tests/files/"
|
||||||
thumbsPath = "./tests/thumbs/"
|
thumbsPath = "./tests/thumbs/"
|
||||||
logsPath = "./tests/logs/{yyyy-mm}.log"
|
logsPath = "./tests/logs/{yyyy}-{mm}.log"
|
||||||
mediaPath = "./public_html/media/"
|
mediaPath = "./public_html/media/"
|
||||||
title = "szurubooru/tests"
|
title = "szurubooru/tests"
|
||||||
salt = "salt..."
|
salt = "salt..."
|
||||||
|
|
Loading…
Reference in a new issue