szurubooru/tests/Tests/JobTests/PreviewCommentJobTest.php
2014-05-26 12:30:23 +02:00

113 lines
2.6 KiB
PHP

<?php
class PreviewCommentJobTest extends AbstractTest
{
public function testPreview()
{
$this->prepare();
$text = 'alohaaaaaaa';
$comment = $this->assert->doesNotThrow(function() use ($text)
{
return $this->runApi($text);
});
$this->assert->areEqual(0, CommentModel::getCount());
$this->assert->areEqual($text, $comment->getText());
$this->assert->areEqual(Auth::getCurrentUser()->getId(), $comment->getCommenter()->getId());
$this->assert->isNotNull($comment->getCreationTime());
$this->assert->throws(function() use ($comment)
{
CommentModel::getById($comment->getId());
}, 'Invalid comment ID');
}
public function testAlmostTooShortText()
{
$this->prepare();
$this->assert->doesNotThrow(function()
{
return $this->runApi(str_repeat('b', Core::getConfig()->comments->minLength));
});
}
public function testAlmostTooLongText()
{
$this->prepare();
$this->assert->doesNotThrow(function()
{
return $this->runApi(str_repeat('b', Core::getConfig()->comments->maxLength));
});
}
public function testTooShortText()
{
$this->prepare();
$this->assert->throws(function()
{
return $this->runApi(str_repeat('b', Core::getConfig()->comments->minLength - 1));
}, 'Comment must have at least');
}
public function testTooLongText()
{
$this->prepare();
$this->assert->throws(function()
{
return $this->runApi(str_repeat('b', Core::getConfig()->comments->maxLength + 1));
}, 'Comment must have at most');
}
public function testViaPost()
{
$this->prepare();
$post = $this->postMocker->mockSingle();
$this->assert->doesNotThrow(function() use ($post)
{
Api::run(
new PreviewCommentJob(),
[
JobArgs::ARG_POST_ID => $post->getId(),
JobArgs::ARG_NEW_TEXT => 'alohaaa',
]);
});
}
public function testViaComment()
{
$this->prepare();
$comment = $this->commentMocker->mockSingle();
$comment->setCommenter(Auth::getCurrentUser());
CommentModel::save($comment);
$this->assert->doesNotThrow(function() use ($comment)
{
Api::run(
new PreviewCommentJob(),
[
JobArgs::ARG_COMMENT_ID => $comment->getId(),
JobArgs::ARG_NEW_TEXT => 'alohaaa',
]);
});
}
protected function runApi($text)
{
$post = $this->postMocker->mockSingle();
return Api::run(
new PreviewCommentJob(),
[
JobArgs::ARG_POST_ID => $post->getId(),
JobArgs::ARG_NEW_TEXT => $text,
]);
}
protected function prepare()
{
Core::getConfig()->comments->needEmailForCommenting = false;
$this->grantAccess('addComment');
$this->login($this->userMocker->mockSingle());
}
}