From 6399afd7999f4a924d519cf0bffc3132ce1d1a79 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 13 May 2014 00:02:25 +0200 Subject: [PATCH] Added more unit tests --- tests/JobTests/AddPostJobTest.php | 2 +- tests/JobTests/AddUserJobTest.php | 2 +- tests/JobTests/DeleteCommentJobTest.php | 2 +- tests/JobTests/DeletePostJobTest.php | 40 ++++++ tests/JobTests/DeleteUserJobTest.php | 37 ++++++ tests/JobTests/EditPostContentJobTest.php | 61 ++++++++- tests/JobTests/EditPostJobTest.php | 4 +- tests/JobTests/EditPostSafetyJobTest.php | 63 +++++++++ tests/JobTests/EditPostSourceJobTest.php | 11 ++ tests/JobTests/EditUserEmailJobTest.php | 29 +++++ tests/JobTests/EditUserJobTest.php | 19 +++ tests/JobTests/EditUserNameJobTest.php | 101 +++++++++++++++ tests/JobTests/EditUserPasswordJobTest.php | 61 +++++++++ tests/JobTests/FlagPostJobTest.php | 48 +++++++ tests/JobTests/FlagUserJobTest.php | 48 +++++++ tests/JobTests/TogglePostTagJobTest.php | 141 +++++++++++++++++++++ tests/JobTests/ToggleUserBanJobTest.php | 57 +++++++++ tests/MiscTests/AccessTest.php | 54 ++++---- tests/MiscTests/LoggerTest.php | 4 +- tests/TestFiles/thumb.jpg | Bin 0 -> 437 bytes tests/run-all.php | 1 + 21 files changed, 750 insertions(+), 35 deletions(-) create mode 100644 tests/JobTests/DeletePostJobTest.php create mode 100644 tests/JobTests/DeleteUserJobTest.php create mode 100644 tests/JobTests/EditPostSafetyJobTest.php create mode 100644 tests/JobTests/EditUserNameJobTest.php create mode 100644 tests/JobTests/EditUserPasswordJobTest.php create mode 100644 tests/JobTests/FlagPostJobTest.php create mode 100644 tests/JobTests/FlagUserJobTest.php create mode 100644 tests/JobTests/TogglePostTagJobTest.php create mode 100644 tests/JobTests/ToggleUserBanJobTest.php create mode 100644 tests/TestFiles/thumb.jpg diff --git a/tests/JobTests/AddPostJobTest.php b/tests/JobTests/AddPostJobTest.php index ef2e8fa4..16b4e339 100644 --- a/tests/JobTests/AddPostJobTest.php +++ b/tests/JobTests/AddPostJobTest.php @@ -74,7 +74,7 @@ class AddPostJobTest extends AbstractTest new AddPostJob(), [ JobArgs::ARG_NEW_SAFETY => PostSafety::Safe, - JobArgs::ARG_NEW_SOURCE => '', + JobArgs::ARG_NEW_SOURCE => 'this should make it fail', JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'), ]); }, 'Insufficient privilege'); diff --git a/tests/JobTests/AddUserJobTest.php b/tests/JobTests/AddUserJobTest.php index 9e60b743..055afb73 100644 --- a/tests/JobTests/AddUserJobTest.php +++ b/tests/JobTests/AddUserJobTest.php @@ -122,7 +122,7 @@ class AddUserJobTest extends AbstractTest JobArgs::ARG_NEW_USER_NAME => 'dummy', JobArgs::ARG_NEW_PASSWORD => 'sekai', ]); - }, 'User with'); + }, 'User with this name is already registered'); } public function testAccessRankDenial() diff --git a/tests/JobTests/DeleteCommentJobTest.php b/tests/JobTests/DeleteCommentJobTest.php index 5724a9ef..10f40cdb 100644 --- a/tests/JobTests/DeleteCommentJobTest.php +++ b/tests/JobTests/DeleteCommentJobTest.php @@ -1,7 +1,7 @@ prepare(); $this->grantAccess('deleteComment'); diff --git a/tests/JobTests/DeletePostJobTest.php b/tests/JobTests/DeletePostJobTest.php new file mode 100644 index 00000000..55adffcc --- /dev/null +++ b/tests/JobTests/DeletePostJobTest.php @@ -0,0 +1,40 @@ +mockUser(); + $post = $this->mockPost($user); + $this->login($user); + $this->grantAccess('deletePost'); + + $this->assert->doesNotThrow(function() use ($post) + { + Api::run( + new DeletePostJob(), + [ + JobArgs::ARG_POST_NAME => $post->getName(), + ]); + }); + + $this->assert->areEqual(null, PostModel::tryGetByName($post->getName())); + $this->assert->areEqual(0, PostModel::getCount()); + } + + public function testWrongPostId() + { + $user = $this->mockUser(); + $post = $this->mockPost($user); + $this->login($user); + + $this->assert->throws(function() + { + Api::run( + new DeletePostJob(), + [ + JobArgs::ARG_POST_NAME => 'robocop', + ]); + }, 'Invalid post name'); + } +} + diff --git a/tests/JobTests/DeleteUserJobTest.php b/tests/JobTests/DeleteUserJobTest.php new file mode 100644 index 00000000..996b8269 --- /dev/null +++ b/tests/JobTests/DeleteUserJobTest.php @@ -0,0 +1,37 @@ +mockUser(); + $this->login($user); + $this->grantAccess('deleteUser'); + + $this->assert->doesNotThrow(function() use ($user) + { + Api::run( + new DeleteUserJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + ]); + }); + + $this->assert->areEqual(null, UserModel::tryGetByName($user->getName())); + $this->assert->areEqual(0, UserModel::getCount()); + } + + public function testWrongUserId() + { + $user = $this->mockUser(); + $this->login($user); + + $this->assert->throws(function() + { + Api::run( + new DeleteUserJob(), + [ + JobArgs::ARG_USER_NAME => 'robocop', + ]); + }, 'Invalid user name'); + } +} diff --git a/tests/JobTests/EditPostContentJobTest.php b/tests/JobTests/EditPostContentJobTest.php index 5ac499f6..78c04a5d 100644 --- a/tests/JobTests/EditPostContentJobTest.php +++ b/tests/JobTests/EditPostContentJobTest.php @@ -10,6 +10,10 @@ class EditPostContentJobTest extends AbstractTest { PostModel::getById($post->getId()); }); + $this->assert->isTrue($post->getFileSize() > 0); + $this->assert->isNotNull($post->getFileHash()); + $this->assert->isNotNull($post->getMimeType()); + $this->assert->isNotNull($post->getType()->toInteger()); } public function testFileJpeg() @@ -76,6 +80,10 @@ class EditPostContentJobTest extends AbstractTest { PostModel::getById($post->getId()); }); + $this->assert->isTrue($post->getFileSize() > 0); + $this->assert->isNotNull($post->getFileHash()); + $this->assert->isNotNull($post->getMimeType()); + $this->assert->isNotNull($post->getType()->toInteger()); } public function testUrlYoutube() @@ -88,7 +96,7 @@ class EditPostContentJobTest extends AbstractTest new EditPostContentJob(), [ JobArgs::ARG_POST_ID => $post->getId(), - JobArgs::ARG_NEW_POST_CONTENT_URL => 'http://www.youtube.com/watch?v=qWq_jydCUw4', 'test.jpg', + JobArgs::ARG_NEW_POST_CONTENT_URL => 'http://www.youtube.com/watch?v=qWq_jydCUw4', ]); $this->assert->areEqual(PostType::Youtube, $post->getType()->toInteger()); $this->assert->areEqual('qWq_jydCUw4', $post->getFileHash()); @@ -117,6 +125,57 @@ class EditPostContentJobTest extends AbstractTest } + public function testDuplicateFile() + { + $this->prepare(); + $this->grantAccess('editPostContent'); + $post = $this->uploadFromFile('image.png'); + $this->assert->areEqual('image/png', $post->getMimeType()); + $this->assert->throws(function() + { + $this->uploadFromFile('image.png'); + }, 'Duplicate upload: @1'); + } + + public function testDuplicateUrl() + { + $this->prepare(); + $this->grantAccess('editPostContent'); + $post = $this->uploadFromUrl('image.png'); + $this->assert->areEqual('image/png', $post->getMimeType()); + $this->assert->throws(function() + { + $this->uploadFromUrl('image.png'); + }, 'Duplicate upload: @1'); + } + + public function testDuplicateYoutube() + { + $this->prepare(); + $this->grantAccess('editPostContent'); + + $url = 'http://www.youtube.com/watch?v=qWq_jydCUw4'; + + $post = $this->mockPost(Auth::getCurrentUser()); + $post = Api::run( + new EditPostContentJob(), + [ + JobArgs::ARG_POST_ID => $post->getId(), + JobArgs::ARG_NEW_POST_CONTENT_URL => $url, + ]); + + $post = $this->mockPost(Auth::getCurrentUser()); + $this->assert->throws(function() use ($post, $url) + { + Api::run( + new EditPostContentJob(), + [ + JobArgs::ARG_POST_ID => $post->getId(), + JobArgs::ARG_NEW_POST_CONTENT_URL => $url, + ]); + }, 'Duplicate upload: @1'); + } + protected function prepare() { $this->login($this->mockUser()); diff --git a/tests/JobTests/EditPostJobTest.php b/tests/JobTests/EditPostJobTest.php index 4308a906..1e85b351 100644 --- a/tests/JobTests/EditPostJobTest.php +++ b/tests/JobTests/EditPostJobTest.php @@ -25,7 +25,7 @@ class EditPostJobTest extends AbstractTest }); } - public function testPrivilegeFail() + public function testPartialPrivilegeFail() { $this->grantAccess('editPost'); $this->grantAccess('editPostSafety'); @@ -38,7 +38,7 @@ class EditPostJobTest extends AbstractTest [ JobArgs::ARG_POST_ID => $post->getId(), JobArgs::ARG_NEW_SAFETY => PostSafety::Safe, - JobArgs::ARG_NEW_SOURCE => '', + JobArgs::ARG_NEW_SOURCE => 'this should make it fail', JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'), ]; diff --git a/tests/JobTests/EditPostSafetyJobTest.php b/tests/JobTests/EditPostSafetyJobTest.php new file mode 100644 index 00000000..90865011 --- /dev/null +++ b/tests/JobTests/EditPostSafetyJobTest.php @@ -0,0 +1,63 @@ +prepare(); + $this->grantAccess('editPostSafety.own'); + + $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new EditPostSafetyJob(), + [ + JobArgs::ARG_POST_ID => $post->getId(), + JobArgs::ARG_NEW_SAFETY => PostSafety::Sketchy, + ]); + }); + + $this->assert->areEqual(PostSafety::Sketchy, $post->getSafety()->toInteger()); + $this->assert->doesNotThrow(function() use ($post) + { + PostModel::getById($post->getId()); + }); + } + + public function testWrongPostId() + { + $this->prepare(); + + $this->assert->throws(function() + { + Api::run( + new EditPostSafetyJob(), + [ + JobArgs::ARG_POST_ID => 100, + JobArgs::ARG_NEW_SAFETY => PostSafety::Sketchy, + ]); + }, 'Invalid post ID'); + } + + public function testWrongSafety() + { + $this->prepare(); + $this->grantAccess('editPostSafety.own'); + + $post = $this->mockPost(Auth::getCurrentUser()); + $this->assert->throws(function() use ($post) + { + Api::run( + new EditPostSafetyJob(), + [ + JobArgs::ARG_POST_ID => $post->getId(), + JobArgs::ARG_NEW_SAFETY => '', + ]); + }, 'Invalid safety type'); + } + + protected function prepare() + { + $this->login($this->mockUser()); + } +} diff --git a/tests/JobTests/EditPostSourceJobTest.php b/tests/JobTests/EditPostSourceJobTest.php index 5e2c6526..633524bb 100644 --- a/tests/JobTests/EditPostSourceJobTest.php +++ b/tests/JobTests/EditPostSourceJobTest.php @@ -37,6 +37,17 @@ class EditPostSourceJobTest extends AbstractTest }, 'Source must have at most'); } + public function testEmptyText() + { + $this->prepare(); + $this->grantAccess('editPostSource.own'); + $post = $this->assert->doesNotThrow(function() + { + return $this->runApi(''); + }); + $this->assert->areEqual('', $post->getSource()); + } + public function testWrongPostId() { $this->prepare(); diff --git a/tests/JobTests/EditUserEmailJobTest.php b/tests/JobTests/EditUserEmailJobTest.php index 92ab7ba0..cc44029e 100644 --- a/tests/JobTests/EditUserEmailJobTest.php +++ b/tests/JobTests/EditUserEmailJobTest.php @@ -74,6 +74,35 @@ class EditUserEmailJobTest extends AbstractTest JobArgs::ARG_NEW_EMAIL => 'hrmfbpdvpds@brtedf', ]); }, 'E-mail address appears to be invalid'); + } + public function testChangingToExistingDenial() + { + getConfig()->registration->needEmailForRegistering = false; + Mailer::mockSending(); + $this->assert->areEqual(0, Mailer::getMailCounter()); + + getConfig()->privileges->changeUserEmailNoConfirm = 'anonymous'; + $this->grantAccess('changeUserEmail'); + + $user = $this->mockUser(); + $otherUser = $this->mockUser(); + $otherUser->setUnconfirmedEmail('super@mario.plumbing'); + UserModel::save($otherUser); + + $this->assert->throws(function() use ($user, $otherUser) + { + Api::run( + new EditUserEmailJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + JobArgs::ARG_NEW_EMAIL => $otherUser->getUnconfirmedEmail(), + ]); + }, 'User with this e-mail is already registered'); + + $this->assert->areEqual(null, $user->getUnconfirmedEmail()); + $this->assert->areEqual(null, $user->getConfirmedEmail()); + + $this->assert->areEqual(0, Mailer::getMailCounter()); } } diff --git a/tests/JobTests/EditUserJobTest.php b/tests/JobTests/EditUserJobTest.php index 1caba0c9..1f243c22 100644 --- a/tests/JobTests/EditUserJobTest.php +++ b/tests/JobTests/EditUserJobTest.php @@ -27,6 +27,25 @@ class EditUserJobTest extends AbstractTest $this->assert->isFalse(empty($user->getPasswordHash())); } + public function testPartialPrivilegeFail() + { + $this->grantAccess('changeUserName.own'); + $user = $this->mockUser(); + + $newName = 'dummy' . uniqid(); + + $this->assert->throws(function() use ($user, $newName) + { + return Api::run( + new EditUserJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + JobArgs::ARG_NEW_USER_NAME => $newName, + JobArgs::ARG_NEW_PASSWORD => 'this should make it fail', + ]); + }, 'Insufficient privilege'); + } + public function testLogBuffering() { $this->testSaving(); diff --git a/tests/JobTests/EditUserNameJobTest.php b/tests/JobTests/EditUserNameJobTest.php new file mode 100644 index 00000000..18c47892 --- /dev/null +++ b/tests/JobTests/EditUserNameJobTest.php @@ -0,0 +1,101 @@ +grantAccess('changeUserName'); + $user = $this->mockUser(); + + $newName = uniqid(); + + $this->assert->areNotEqual($newName, $user->getName()); + + $user = $this->assert->doesNotThrow(function() use ($user, $newName) + { + return Api::run( + new EditUserNameJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + JobArgs::ARG_NEW_USER_NAME => $newName, + ]); + }); + + $this->assert->areEqual($newName, $user->getName()); + } + + public function testTooShortName() + { + $this->grantAccess('changeUserName'); + $user = $this->mockUser(); + + $newName = str_repeat('a', getConfig()->registration->userNameMinLength - 1); + + $this->assert->throws(function() use ($user, $newName) + { + Api::run( + new EditUserNameJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + JobArgs::ARG_NEW_USER_NAME => $newName, + ]); + }, 'user name must have at least'); + } + + public function testTooLongName() + { + $this->grantAccess('changeUserName'); + $user = $this->mockUser(); + + $newName = str_repeat('a', getConfig()->registration->userNameMaxLength + 1); + + $this->assert->throws(function() use ($user, $newName) + { + Api::run( + new EditUserNameJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + JobArgs::ARG_NEW_USER_NAME => $newName, + ]); + }, 'user name must have at most'); + } + + public function testInvalidName() + { + $this->grantAccess('changeUserName'); + $user = $this->mockUser(); + + $newName = 'ble/ble'; + + $this->assert->throws(function() use ($user, $newName) + { + Api::run( + new EditUserNameJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + JobArgs::ARG_NEW_USER_NAME => $newName, + ]); + }, 'user name contains invalid characters'); + } + + public function testChangingToExistingDenial() + { + $this->grantAccess('changeUserName'); + $user = $this->mockUser(); + $otherUser = $this->mockUser(); + + $newName = $otherUser->getName(); + $this->assert->areNotEqual($newName, $user->getName()); + + $this->assert->throws(function() use ($user, $newName) + { + Api::run( + new EditUserNameJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + JobArgs::ARG_NEW_USER_NAME => $newName, + ]); + }, 'User with this name is already registered'); + + $this->assert->areNotEqual($newName, $user->getName()); + } +} diff --git a/tests/JobTests/EditUserPasswordJobTest.php b/tests/JobTests/EditUserPasswordJobTest.php new file mode 100644 index 00000000..38f8b24a --- /dev/null +++ b/tests/JobTests/EditUserPasswordJobTest.php @@ -0,0 +1,61 @@ +testValidPassword('flintstone'); + } + + public function testVeryLongPassword() + { + $this->testValidPassword(str_repeat('flintstone', 100)); + } + + public function testTooShortPassword() + { + $this->grantAccess('changeUserPassword'); + $user = $this->mockUser(); + + $newPassword = str_repeat('a', getConfig()->registration->passMinLength - 1); + $oldPasswordHash = $user->getPasswordHash(); + + $this->assert->throws(function() use ($user, $newPassword) + { + return Api::run( + new EditUserPasswordJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + JobArgs::ARG_NEW_PASSWORD => $newPassword, + ]); + }, 'Password must have at least'); + + $this->assert->areEqual($oldPasswordHash, $user->getPasswordHash()); + } + + private function testValidPassword($newPassword) + { + $this->grantAccess('changeUserPassword'); + $user = $this->mockUser(); + + $newPasswordHash = UserModel::hashPassword($newPassword, $user->getPasswordSalt()); + + $user = $this->assert->doesNotThrow(function() use ($user, $newPassword) + { + return Api::run( + new EditUserPasswordJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + JobArgs::ARG_NEW_PASSWORD => $newPassword, + ]); + }); + + $this->assert->areEqual($newPasswordHash, $user->getPasswordHash()); + + getConfig()->registration->needEmailForRegistering = false; + $this->assert->doesNotThrow(function() use ($user, $newPassword) + { + Auth::login($user->getName(), $newPassword, false); + }); + $this->assert->isTrue(Auth::isLoggedIn()); + } +} diff --git a/tests/JobTests/FlagPostJobTest.php b/tests/JobTests/FlagPostJobTest.php new file mode 100644 index 00000000..0587db75 --- /dev/null +++ b/tests/JobTests/FlagPostJobTest.php @@ -0,0 +1,48 @@ +grantAccess('flagPost'); + $post = $this->mockPost($this->mockUser()); + + $post = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new FlagPostJob(), + [ + JobArgs::ARG_POST_NAME => $post->getName(), + ]); + }); + + $logPath = Logger::getLogPath(); + $logs = file_get_contents($logPath); + $logs = array_filter(explode("\n", $logs)); + $this->assert->areEqual(1, count($logs)); + $this->assert->isTrue(strpos($logs[0], 'flagged @' . $post->getId() . ' for moderator attention') !== false); + } + + public function testDoubleFlagging() + { + $this->grantAccess('flagPost'); + $post = $this->mockPost($this->mockUser()); + + $this->assert->doesNotThrow(function() use ($post) + { + Api::run( + new FlagPostJob(), + [ + JobArgs::ARG_POST_NAME => $post->getName(), + ]); + }); + + $this->assert->throws(function() use ($post) + { + return Api::run( + new FlagPostJob(), + [ + JobArgs::ARG_POST_NAME => $post->getName(), + ]); + }, 'You already flagged this post'); + } +} diff --git a/tests/JobTests/FlagUserJobTest.php b/tests/JobTests/FlagUserJobTest.php new file mode 100644 index 00000000..e1385f34 --- /dev/null +++ b/tests/JobTests/FlagUserJobTest.php @@ -0,0 +1,48 @@ +grantAccess('flagUser'); + $user = $this->mockUser(); + + $user = $this->assert->doesNotThrow(function() use ($user) + { + return Api::run( + new FlagUserJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + ]); + }); + + $logPath = Logger::getLogPath(); + $logs = file_get_contents($logPath); + $logs = array_filter(explode("\n", $logs)); + $this->assert->areEqual(1, count($logs)); + $this->assert->isTrue(strpos($logs[0], 'flagged +' . $user->getName() . ' for moderator attention') !== false); + } + + public function testDoubleFlagging() + { + $this->grantAccess('flagUser'); + $user = $this->mockUser(); + + $this->assert->doesNotThrow(function() use ($user) + { + Api::run( + new FlagUserJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + ]); + }); + + $this->assert->throws(function() use ($user) + { + return Api::run( + new FlagUserJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + ]); + }, 'You already flagged this user'); + } +} diff --git a/tests/JobTests/TogglePostTagJobTest.php b/tests/JobTests/TogglePostTagJobTest.php new file mode 100644 index 00000000..f755211d --- /dev/null +++ b/tests/JobTests/TogglePostTagJobTest.php @@ -0,0 +1,141 @@ +preparePost(['test']); + + $this->assertHasTag($post, 'test'); + $this->assertDoesNotHaveTag($post, 'test2'); + + $post = $this->assert->doesNotThrow(function() use ($post) + { + return $this->addTag($post, 'test2'); + }); + + $this->assertHasTag($post, 'test'); + $this->assertHasTag($post, 'test2'); + } + + public function testEnablingAlreadyEnabled() + { + $post = $this->preparePost(['test']); + + $this->assertHasTag($post, 'test'); + $this->assertDoesNotHaveTag($post, 'test2'); + + $post = $this->assert->doesNotThrow(function() use ($post) + { + return $this->addTag($post, 'test2'); + }); + $post = $this->assert->doesNotThrow(function() use ($post) + { + return $this->addTag($post, 'test2'); + }); + + $this->assertHasTag($post, 'test'); + $this->assertHasTag($post, 'test2'); + } + + public function testDisabling() + { + $post = $this->preparePost(['test', 'test2']); + + $this->assertHasTag($post, 'test'); + $this->assertHasTag($post, 'test2'); + + $post = $this->assert->doesNotThrow(function() use ($post) + { + return $this->removeTag($post, 'test2'); + }); + + $this->assertHasTag($post, 'test'); + $this->assertDoesNotHaveTag($post, 'test2'); + } + + public function testDisablingAlreadyDisabled() + { + $post = $this->preparePost(['test', 'test2']); + + $this->assertHasTag($post, 'test'); + $this->assertHasTag($post, 'test2'); + + $post = $this->assert->doesNotThrow(function() use ($post) + { + return $this->removeTag($post, 'test2'); + }); + $post = $this->assert->doesNotThrow(function() use ($post) + { + return $this->removeTag($post, 'test2'); + }); + + $this->assertHasTag($post, 'test'); + $this->assertDoesNotHaveTag($post, 'test2'); + } + + public function testDisablingLastTag() + { + $post = $this->preparePost(['test']); + + $this->assertHasTag($post, 'test'); + + $this->assert->throws(function() use ($post) + { + $this->removeTag($post, 'test'); + }, 'No tags set'); + + $this->assertHasTag($post, 'test'); + } + + private function addTag($post, $tagName) + { + return Api::run( + new TogglePostTagJob(), + [ + JobArgs::ARG_POST_ID => $post->getId(), + JobArgs::ARG_TAG_NAME => $tagName, + JobArgs::ARG_NEW_STATE => true, + ]); + } + + private function removeTag($post, $tagName) + { + return Api::run( + new TogglePostTagJob(), + [ + JobArgs::ARG_POST_ID => $post->getId(), + JobArgs::ARG_TAG_NAME => $tagName, + JobArgs::ARG_NEW_STATE => false, + ]); + } + + private function assertDoesNotHaveTag($post, $tagName) + { + $tagNames = $this->getTagNames($post); + $this->assert->isFalse(in_array($tagName, $tagNames)); + } + + private function assertHasTag($post, $tagName) + { + $tagNames = $this->getTagNames($post); + $this->assert->isTrue(in_array($tagName, $tagNames)); + } + + private function preparePost(array $tagNames) + { + $this->grantAccess('editPostTags'); + $post = $this->mockPost($this->mockUser()); + $post->setTags(TagModel::spawnFromNames($tagNames)); + PostModel::save($post); + return $post; + } + + private function getTagNames($post) + { + return array_map( + function($tag) + { + return $tag->getName(); + }, $post->getTags()); + } +} diff --git a/tests/JobTests/ToggleUserBanJobTest.php b/tests/JobTests/ToggleUserBanJobTest.php new file mode 100644 index 00000000..4681c78d --- /dev/null +++ b/tests/JobTests/ToggleUserBanJobTest.php @@ -0,0 +1,57 @@ +grantAccess('banUser'); + $user = $this->mockUser(); + $this->login($user); + + $this->assert->isFalse($user->isBanned()); + + $user = $this->assert->doesNotThrow(function() use ($user) + { + return Api::run( + new ToggleUserBanJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + JobArgs::ARG_NEW_STATE => 1, + ]); + }); + + $this->assert->isTrue($user->isBanned()); + } + + public function testUnbanning() + { + $this->grantAccess('banUser'); + $user = $this->mockUser(); + $this->login($user); + + $this->assert->isFalse($user->isBanned()); + + $user = $this->assert->doesNotThrow(function() use ($user) + { + return Api::run( + new ToggleUserBanJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + JobArgs::ARG_NEW_STATE => 1, + ]); + }); + + $this->assert->isTrue($user->isBanned()); + + $user = $this->assert->doesNotThrow(function() use ($user) + { + return Api::run( + new ToggleUserBanJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + JobArgs::ARG_NEW_STATE => 0, + ]); + }); + + $this->assert->isFalse($user->isBanned()); + } +} diff --git a/tests/MiscTests/AccessTest.php b/tests/MiscTests/AccessTest.php index 787c8b22..bc9162a4 100644 --- a/tests/MiscTests/AccessTest.php +++ b/tests/MiscTests/AccessTest.php @@ -48,77 +48,77 @@ class AccessTest extends AbstractTest $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user)); } - public function testSubPrivileges1() + public function testSubPrivilegesOnlySub() { - getConfig()->privileges->{'listPosts.dummy'} = 'power-user'; + getConfig()->privileges->{'listPosts.own'} = 'power-user'; Access::init(); $user = $this->mockUser(); $user->setAccessRank(new AccessRank(AccessRank::PowerUser)); - $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'dummy'), $user)); - $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'baka'), $user)); + $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user)); + $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user)); $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user)); } - public function testSubPrivileges2a() + public function testSubPrivilegesSubAndGeneralNormalOrder() { - getConfig()->privileges->{'listPosts.dummy'} = 'power-user'; + getConfig()->privileges->{'listPosts.own'} = 'power-user'; getConfig()->privileges->{'listPosts'} = 'admin'; Access::init(); - $this->testSubPrivileges2(); + $this->testSubPrivilegesSubAndGeneral(); } - public function testSubPrivileges2b() + public function testSubPrivilegesSubAndGeneralReverseOrder() { getConfig()->privileges->{'listPosts'} = 'admin'; - getConfig()->privileges->{'listPosts.dummy'} = 'power-user'; + getConfig()->privileges->{'listPosts.own'} = 'power-user'; Access::init(); - $this->testSubPrivileges2(); + $this->testSubPrivilegesSubAndGeneral(); } - protected function testSubPrivileges2() + protected function testSubPrivilegesSubAndGeneral() { $user = $this->mockUser(); $user->setAccessRank(new AccessRank(AccessRank::PowerUser)); - $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'dummy'), $user)); + $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user)); $this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts, 'baka'), $user)); $this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user)); $user->setAccessRank(new AccessRank(AccessRank::Admin)); - $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'dummy'), $user)); - $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'baka'), $user)); + $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user)); + $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user)); $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user)); } - public function testSubPrivileges3a() + public function testSubPrivilegesMultipleSubAndGeneralNormalOrder() { - getConfig()->privileges->{'listPosts.dummy'} = 'power-user'; - getConfig()->privileges->{'listPosts.baka'} = 'admin'; + getConfig()->privileges->{'listPosts.own'} = 'power-user'; + getConfig()->privileges->{'listPosts.all'} = 'admin'; getConfig()->privileges->{'listPosts'} = 'nobody'; Access::init(); - $this->testSubPrivileges3(); + $this->testSubPrivilegesMultipleSubAndGeneral(); } - public function testSubPrivileges3b() + public function testSubPrivilegesMultipleSubAndGeneralReverseOrder() { getConfig()->privileges->{'listPosts'} = 'nobody'; - getConfig()->privileges->{'listPosts.dummy'} = 'power-user'; - getConfig()->privileges->{'listPosts.baka'} = 'admin'; + getConfig()->privileges->{'listPosts.own'} = 'power-user'; + getConfig()->privileges->{'listPosts.all'} = 'admin'; Access::init(); - $this->testSubPrivileges3(); + $this->testSubPrivilegesMultipleSubAndGeneral(); } - protected function testSubPrivileges3() + protected function testSubPrivilegesMultipleSubAndGeneral() { $user = $this->mockUser(); $user->setAccessRank(new AccessRank(AccessRank::PowerUser)); - $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'dummy'), $user)); - $this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts, 'baka'), $user)); + $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user)); + $this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user)); $this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user)); $user->setAccessRank(new AccessRank(AccessRank::Admin)); - $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'dummy'), $user)); - $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'baka'), $user)); + $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user)); + $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user)); $this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user)); } } diff --git a/tests/MiscTests/LoggerTest.php b/tests/MiscTests/LoggerTest.php index baa556bb..b61a34d9 100644 --- a/tests/MiscTests/LoggerTest.php +++ b/tests/MiscTests/LoggerTest.php @@ -26,8 +26,8 @@ class LoggerTest extends AbstractTest public function testPathChanging() { - $logPath = __DIR__ . '/logs/{yyyy}-{mm}-{dd}.log'; - $realLogPath = __DIR__ . '/logs/' . date('Y-m-d') . '.log'; + $logPath = dirname(__DIR__) . '/logs/{yyyy}-{mm}-{dd}.log'; + $realLogPath = dirname(__DIR__) . '/logs/' . date('Y-m-d') . '.log'; getConfig()->main->logsPath = $logPath; $this->assert->doesNotThrow(function() diff --git a/tests/TestFiles/thumb.jpg b/tests/TestFiles/thumb.jpg new file mode 100644 index 0000000000000000000000000000000000000000..20090f5a89b14fafc1e25c574941c20adeed8028 GIT binary patch literal 437 zcmd6iITFG!3`M`NG2W8M32*=|LIINj4w$o0rr-z^EDbj?a+!2=JkMzCH(IRx$RF^f zKldOaoa7+Cae!JBN>qY2x?qi28fU$C);S-W=tCWxt5Q`r%!z$$yOuk-b7?}QQfVuN z99$6lBY8x!7%(W>VUb8B$~SD}tC(U%uX%1Eb}MVmyk3>;G_$hz3sI5GQ3-ooalDVy KDSz>z{QLr9b0H-F literal 0 HcmV?d00001 diff --git a/tests/run-all.php b/tests/run-all.php index c11ae9fe..fc8112b5 100644 --- a/tests/run-all.php +++ b/tests/run-all.php @@ -48,6 +48,7 @@ class TestRunner protected function resetEnvironment() { + $_SESSION = []; prepareConfig(true); getConfig()->main->dbDriver = 'sqlite'; getConfig()->main->dbLocation = $this->dbPath;