diff --git a/tests/JobTests/AddCommentJobTest.php b/tests/JobTests/AddCommentJobTest.php index e06deeae..8ee776a1 100644 --- a/tests/JobTests/AddCommentJobTest.php +++ b/tests/JobTests/AddCommentJobTest.php @@ -12,11 +12,13 @@ class AddCommentJobTest extends AbstractTest { return $this->runApi($text); }); + $post = $comment->getPost(); $this->assert->areEqual(1, CommentModel::getCount()); $this->assert->areEqual($text, $comment->getText()); $this->assert->areEqual(Auth::getCurrentUser()->getId(), $comment->getCommenter()->getId()); - $this->assert->areEqual(1, $comment->getPost()->getId()); + $this->assert->areEqual(1, $post->getId()); + $this->assert->areEqual(1, $post->getCommentCount()); $this->assert->isNotNull($comment->getCreationTime()); $this->assert->doesNotThrow(function() use ($comment) { diff --git a/tests/JobTests/DeleteCommentJobTest.php b/tests/JobTests/DeleteCommentJobTest.php index ba5c9b52..b438475a 100644 --- a/tests/JobTests/DeleteCommentJobTest.php +++ b/tests/JobTests/DeleteCommentJobTest.php @@ -6,11 +6,22 @@ class DeleteCommentJobTest extends AbstractTest $this->prepare(); $this->grantAccess('deleteComment'); - $this->assert->doesNotThrow(function() + $comment = $this->mockComment(Auth::getCurrentUser()); + $post = $comment->getPost(); + $this->assert->areEqual(1, $post->getCommentCount()); + + $this->assert->doesNotThrow(function() use ($comment) { - $this->runApi(); + Api::run( + new DeleteCommentJob(), + [ + DeleteCommentJob::COMMENT_ID => $comment->getId(), + ]); }); + //post needs to be retrieved again from db to refresh cache + $post = PostModel::getById($post->getId()); + $this->assert->areEqual(0, $post->getCommentCount()); $this->assert->areEqual(0, CommentModel::getCount()); } @@ -28,18 +39,6 @@ class DeleteCommentJobTest extends AbstractTest } - protected function runApi($comment = null) - { - if ($comment === null) - $comment = $this->mockComment(Auth::getCurrentUser()); - - return Api::run( - new DeleteCommentJob(), - [ - DeleteCommentJob::COMMENT_ID => $comment->getId(), - ]); - } - protected function prepare() { $this->login($this->mockUser()); diff --git a/tests/JobTests/TogglePostFavoriteJobTest.php b/tests/JobTests/TogglePostFavoriteJobTest.php new file mode 100644 index 00000000..54a865f3 --- /dev/null +++ b/tests/JobTests/TogglePostFavoriteJobTest.php @@ -0,0 +1,109 @@ +grantAccess('favoritePost'); + $user = $this->mockUser(); + $this->login($user); + $post = $this->mockPost($user); + + $this->assert->areEqual(0, $post->getScore()); + $this->assert->areEqual(0, $post->getFavoriteCount()); + + $post = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new TogglePostFavoriteJob(), + [ + ScorePostJob::POST_ID => $post->getId(), + ScorePostJob::STATE => 1, + ]); + }); + + $this->assert->areEqual(1, $post->getScore()); + $this->assert->areEqual(1, $post->getFavoriteCount()); + $this->assert->areEqual(1, $user->getFavoriteCount()); + $this->assert->areEqual(1, $user->getScore($post)); + $this->assert->areEqual(true, $user->hasFavorited($post)); + } + + public function testDefaving() + { + $this->grantAccess('favoritePost'); + $user = $this->mockUser(); + $this->login($user); + $post = $this->mockPost($user); + + $this->assert->areEqual(0, $post->getScore()); + $this->assert->areEqual(0, $post->getFavoriteCount()); + + $post = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new TogglePostFavoriteJob(), + [ + ScorePostJob::POST_ID => $post->getId(), + ScorePostJob::STATE => 1, + ]); + }); + + $post = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new TogglePostFavoriteJob(), + [ + ScorePostJob::POST_ID => $post->getId(), + ScorePostJob::STATE => 0, + ]); + }); + + $this->assert->areEqual(1, $post->getScore()); + $this->assert->areEqual(0, $post->getFavoriteCount()); + $this->assert->areEqual(0, $user->getFavoriteCount()); + $this->assert->areEqual(1, $user->getScore($post)); + $this->assert->areEqual(false, $user->hasFavorited($post)); + } + + public function testFavingTwoPeople() + { + $this->grantAccess('favoritePost'); + $user1 = $this->mockUser(); + $user2 = $this->mockUser(); + $post = $this->mockPost($user1); + + $this->assert->areEqual(0, $post->getScore()); + $this->assert->areEqual(0, $post->getFavoriteCount()); + + $this->login($user1); + $post = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new TogglePostFavoriteJob(), + [ + ScorePostJob::POST_ID => $post->getId(), + ScorePostJob::STATE => 1, + ]); + }); + + $this->login($user2); + $post = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new TogglePostFavoriteJob(), + [ + ScorePostJob::POST_ID => $post->getId(), + ScorePostJob::STATE => 1, + ]); + }); + + $this->assert->areEqual(2, $post->getScore()); + $this->assert->areEqual(2, $post->getFavoriteCount()); + $this->assert->areEqual(1, $user1->getFavoriteCount()); + $this->assert->areEqual(1, $user1->getScore($post)); + $this->assert->areEqual(1, $user2->getFavoriteCount()); + $this->assert->areEqual(1, $user2->getScore($post)); + $this->assert->areEqual(true, $user1->hasFavorited($post)); + $this->assert->areEqual(true, $user2->hasFavorited($post)); + } +} diff --git a/tests/JobTests/TogglePostVisibilityJobTest.php b/tests/JobTests/TogglePostVisibilityJobTest.php new file mode 100644 index 00000000..9ce50e55 --- /dev/null +++ b/tests/JobTests/TogglePostVisibilityJobTest.php @@ -0,0 +1,57 @@ +grantAccess('hidePost'); + $this->login($this->mockUser()); + $post = $this->mockPost(Auth::getCurrentUser()); + + $this->assert->isFalse($post->isHidden()); + + $post = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new TogglePostVisibilityJob(), + [ + TogglePostVisibilityJob::POST_ID => $post->getId(), + TogglePostVisibilityJob::STATE => 0, + ]); + }); + + $this->assert->isTrue($post->isHidden()); + } + + public function testShowing() + { + $this->grantAccess('hidePost'); + $this->login($this->mockUser()); + $post = $this->mockPost(Auth::getCurrentUser()); + + $this->assert->isFalse($post->isHidden()); + + $post = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new TogglePostVisibilityJob(), + [ + TogglePostVisibilityJob::POST_ID => $post->getId(), + TogglePostVisibilityJob::STATE => 0, + ]); + }); + + $this->assert->isTrue($post->isHidden()); + + $post = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new TogglePostVisibilityJob(), + [ + TogglePostVisibilityJob::POST_ID => $post->getId(), + TogglePostVisibilityJob::STATE => 1, + ]); + }); + + $this->assert->isFalse($post->isHidden()); + } +}