From bca92f1f7143f64f090a05c6bbba37de3e9bc877 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 13 May 2014 21:08:07 +0200 Subject: [PATCH] Added more unit tests, refactored test support --- src/Models/AbstractCrudModel.php | 48 +- src/Models/CommentModel.php | 4 +- src/Models/Entities/TagEntity.php | 6 +- src/Models/PostModel.php | 4 +- src/Models/TagModel.php | 4 +- src/Models/TokenModel.php | 2 +- src/Models/UserModel.php | 4 +- tests/AbstractTest.php | 51 +- tests/Api/ApiAuthTest.php | 2 +- tests/Api/ApiEmailRequirementsTest.php | 4 +- tests/Api/ApiPrivilegeTest.php | 24 +- tests/Api/Common/CommentRetrieverTest.php | 2 +- tests/Api/Common/PostRetrieverTest.php | 2 +- tests/Api/Common/SafePostRetrieverTest.php | 2 +- tests/Api/Common/UserRetrieverTest.php | 2 +- .../AcceptUserRegistrationJobTest.php | 2 +- tests/JobTests/ActivateUserEmailJobTest.php | 6 +- tests/JobTests/AddCommentJobTest.php | 4 +- tests/JobTests/AddPostJobTest.php | 23 +- tests/JobTests/DeleteCommentJobTest.php | 4 +- tests/JobTests/DeletePostJobTest.php | 10 +- tests/JobTests/DeleteUserJobTest.php | 4 +- tests/JobTests/EditCommentJobTest.php | 6 +- tests/JobTests/EditPostContentJobTest.php | 24 +- tests/JobTests/EditPostJobTest.php | 10 +- tests/JobTests/EditPostRelationsJobTest.php | 15 +- tests/JobTests/EditPostSafetyJobTest.php | 6 +- tests/JobTests/EditPostSourceJobTest.php | 4 +- tests/JobTests/EditPostTagsJobTest.php | 10 +- tests/JobTests/EditPostThumbJobTest.php | 16 +- tests/JobTests/EditUserAccessRankJobTest.php | 6 +- tests/JobTests/EditUserEmailJobTest.php | 10 +- tests/JobTests/EditUserJobTest.php | 8 +- tests/JobTests/EditUserNameJobTest.php | 12 +- tests/JobTests/EditUserPasswordJobTest.php | 4 +- tests/JobTests/FeaturePostJobTest.php | 23 +- tests/JobTests/FlagPostJobTest.php | 4 +- tests/JobTests/FlagUserJobTest.php | 4 +- tests/JobTests/GetLogJobTest.php | 57 ++ tests/JobTests/GetPostContentJobTest.php | 51 ++ tests/JobTests/GetPostJobTest.php | 36 + tests/JobTests/GetPostThumbJobTest.php | 50 ++ tests/JobTests/GetUserJobTest.php | 39 ++ tests/JobTests/ListCommentsJobTest.php | 6 +- tests/JobTests/ListLogsJobTest.php | 27 + tests/JobTests/ListPostsJobTest.php | 644 ++++++++++++++++++ tests/JobTests/ListRelatedTagsJobTest.php | 63 ++ tests/JobTests/ListTagsJobTest.php | 122 ++++ tests/JobTests/ListUsersJobTest.php | 90 +++ tests/JobTests/MergeTagsJobTest.php | 57 +- tests/JobTests/PasswordResetJobTest.php | 8 +- tests/JobTests/PreviewCommentJobTest.php | 10 +- tests/JobTests/RenameTagsJobTest.php | 55 +- tests/JobTests/ScorePostJobTest.php | 39 +- tests/JobTests/TogglePostFavoriteJobTest.php | 29 +- tests/JobTests/TogglePostTagJobTest.php | 2 +- .../JobTests/TogglePostVisibilityJobTest.php | 8 +- tests/JobTests/ToggleUserBanJobTest.php | 4 +- tests/MiscTests/AccessTest.php | 10 +- tests/Mockers/AbstractMocker.php | 16 + tests/Mockers/CommentMocker.php | 19 + tests/Mockers/IMocker.php | 6 + tests/Mockers/PostMocker.php | 24 + tests/Mockers/TagMocker.php | 10 + tests/Mockers/UserMocker.php | 12 + tests/ModelTests/PostModelTest.php | 24 +- tests/ModelTests/UserModelTest.php | 6 +- tests/TestSupport.php | 34 + 68 files changed, 1595 insertions(+), 339 deletions(-) create mode 100644 tests/JobTests/GetLogJobTest.php create mode 100644 tests/JobTests/GetPostContentJobTest.php create mode 100644 tests/JobTests/GetPostJobTest.php create mode 100644 tests/JobTests/GetPostThumbJobTest.php create mode 100644 tests/JobTests/GetUserJobTest.php create mode 100644 tests/JobTests/ListLogsJobTest.php create mode 100644 tests/JobTests/ListPostsJobTest.php create mode 100644 tests/JobTests/ListRelatedTagsJobTest.php create mode 100644 tests/JobTests/ListTagsJobTest.php create mode 100644 tests/JobTests/ListUsersJobTest.php create mode 100644 tests/Mockers/AbstractMocker.php create mode 100644 tests/Mockers/CommentMocker.php create mode 100644 tests/Mockers/IMocker.php create mode 100644 tests/Mockers/PostMocker.php create mode 100644 tests/Mockers/TagMocker.php create mode 100644 tests/Mockers/UserMocker.php create mode 100644 tests/TestSupport.php diff --git a/src/Models/AbstractCrudModel.php b/src/Models/AbstractCrudModel.php index 50e603c3..47aed478 100644 --- a/src/Models/AbstractCrudModel.php +++ b/src/Models/AbstractCrudModel.php @@ -29,12 +29,54 @@ abstract class AbstractCrudModel implements IModel public static function remove($entities) { - throw new NotImplementedException(); + if (is_array($entities)) + return static::removeMultiple($entities); + else + return static::removeSingle($entities); } - public static function save($entity) + protected static function removeMultiple($entities) { - throw new NotImplementedException(); + $cb = [get_called_class(), 'removeSingle']; + Database::transaction(function() use ($entities, $cb) + { + foreach ($entities as $entity) + { + $cb($entity); + } + }); + } + + protected static function removeSingle($entity) + { + throw new BadMethodCallException('Not implemented'); + } + + public static function save($entities) + { + if (is_array($entities)) + return static::saveMultiple($entities); + else + return static::saveSingle($entities); + } + + protected static function saveMultiple($entities) + { + $cb = [get_called_class(), 'saveSingle']; + return Database::transaction(function() use ($entities, $cb) + { + $ret = []; + foreach ($entities as $entity) + { + $ret []= $cb($entity); + } + return $ret; + }); + } + + protected static function saveSingle($entity) + { + throw new BadMethodCallException('Not implemented'); } diff --git a/src/Models/CommentModel.php b/src/Models/CommentModel.php index d3a75e04..38aa9065 100644 --- a/src/Models/CommentModel.php +++ b/src/Models/CommentModel.php @@ -9,7 +9,7 @@ final class CommentModel extends AbstractCrudModel return 'comment'; } - public static function save($comment) + protected static function saveSingle($comment) { $comment->validate(); $comment->getPost()->removeCache('comment_count'); @@ -37,7 +37,7 @@ final class CommentModel extends AbstractCrudModel return $comment; } - public static function remove($comment) + protected static function removeSingle($comment) { Database::transaction(function() use ($comment) { diff --git a/src/Models/Entities/TagEntity.php b/src/Models/Entities/TagEntity.php index 391352db..ea383fc4 100644 --- a/src/Models/Entities/TagEntity.php +++ b/src/Models/Entities/TagEntity.php @@ -55,9 +55,11 @@ final class TagEntity extends AbstractEntity implements IValidatable return $this->getCache('post_count'); $stmt = new Sql\SelectStatement(); - $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); + $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'post_count')); $stmt->setTable('post_tag'); $stmt->setCriterion(new Sql\EqualsFunctor('tag_id', new Sql\Binding($this->getId()))); - return Database::fetchOne($stmt)['count']; + $row = Database::fetchOne($stmt); + $this->setCache('post_count', (int) $row['post_count']); + return $this->getCache('post_count'); } } diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 222239de..d6e0dc3b 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -9,7 +9,7 @@ final class PostModel extends AbstractCrudModel return 'post'; } - public static function save($post) + protected static function saveSingle($post) { $post->validate(); @@ -83,7 +83,7 @@ final class PostModel extends AbstractCrudModel return $post; } - public static function remove($post) + protected static function removeSingle($post) { Database::transaction(function() use ($post) { diff --git a/src/Models/TagModel.php b/src/Models/TagModel.php index 472f4f3e..b968eb0a 100644 --- a/src/Models/TagModel.php +++ b/src/Models/TagModel.php @@ -9,7 +9,7 @@ final class TagModel extends AbstractCrudModel return 'tag'; } - public static function save($tag) + protected static function saveSingle($tag) { $tag->validate(); @@ -28,7 +28,7 @@ final class TagModel extends AbstractCrudModel return $tag; } - public static function remove($tag) + protected static function removeSingle($tag) { $binding = new Sql\Binding($tag->getId()); diff --git a/src/Models/TokenModel.php b/src/Models/TokenModel.php index f1d6a8e4..88f9bc09 100644 --- a/src/Models/TokenModel.php +++ b/src/Models/TokenModel.php @@ -9,7 +9,7 @@ final class TokenModel extends AbstractCrudModel return 'user_token'; } - public static function save($token) + protected static function saveSingle($token) { $token->validate(); diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 9ec6dbeb..55363b55 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -14,7 +14,7 @@ final class UserModel extends AbstractCrudModel return 'user'; } - public static function save($user) + protected static function saveSingle($user) { $user->validate(); @@ -49,7 +49,7 @@ final class UserModel extends AbstractCrudModel return $user; } - public static function remove($user) + protected static function removeSingle($user) { Database::transaction(function() use ($user) { diff --git a/tests/AbstractTest.php b/tests/AbstractTest.php index 5558c5c0..8249d3f4 100644 --- a/tests/AbstractTest.php +++ b/tests/AbstractTest.php @@ -2,10 +2,20 @@ class AbstractTest { public $assert; + protected $postMocker; + protected $tagMocker; + protected $userMocker; + protected $commentMocker; + protected $testSupport; public function __construct() { $this->assert = new Assert(); + $this->testSupport = new TestSupport($this->assert); + $this->tagMocker = new TagMocker(); + $this->postMocker = new PostMocker($this->tagMocker, $this->testSupport); + $this->userMocker = new UserMocker(); + $this->commentMocker = new CommentMocker($this->postMocker); } public function setup() @@ -16,47 +26,11 @@ class AbstractTest { } - protected function mockUser() - { - $user = UserModel::spawn(); - $user->setAccessRank(new AccessRank(AccessRank::Registered)); - $user->setName('dummy'.uniqid()); - $user->setPassword('sekai'); - return UserModel::save($user); - } - - protected function mockTag() - { - $tag = TagModel::spawn(); - $tag->setName(uniqid()); - return TagModel::save($tag); - } - - protected function mockPost($owner) - { - $post = PostModel::spawn(); - $post->setUploader($owner); - $post->setType(new PostType(PostType::Image)); - $post->setTags([$this->mockTag(), $this->mockTag()]); - copy($this->getPath('image.jpg'), $post->getFullPath()); - return PostModel::save($post); - } - protected function login($user) { Auth::setCurrentUser($user); } - protected function mockComment($owner) - { - $post = $this->mockPost($owner); - $comment = CommentModel::spawn(); - $comment->setPost($post); - $comment->setCommenter($owner); - $comment->setText('test test'); - return CommentModel::save($comment); - } - protected function grantAccess($privilege) { getConfig()->privileges->$privilege = 'anonymous'; @@ -68,9 +42,4 @@ class AbstractTest getConfig()->privileges->$privilege = 'nobody'; Access::init(); } - - protected function getPath($name) - { - return getConfig()->rootDir . DS . 'tests' . DS . 'TestFiles' . DS . $name; - } } diff --git a/tests/Api/ApiAuthTest.php b/tests/Api/ApiAuthTest.php index c963c30b..b65d9d61 100644 --- a/tests/Api/ApiAuthTest.php +++ b/tests/Api/ApiAuthTest.php @@ -60,7 +60,7 @@ class ApiAuthTest extends AbstractFullApiTest getConfig()->registration->needEmailForCommenting = false; $this->grantAccess('addComment'); - $comment = $this->mockComment(Auth::getCurrentUser()); + $comment = $this->commentMocker->mockSingle(); $this->assert->throws(function() use ($comment) { diff --git a/tests/Api/ApiEmailRequirementsTest.php b/tests/Api/ApiEmailRequirementsTest.php index 624f480b..569e3144 100644 --- a/tests/Api/ApiEmailRequirementsTest.php +++ b/tests/Api/ApiEmailRequirementsTest.php @@ -88,11 +88,11 @@ class ApiEmailRequirementsTest extends AbstractFullApiTest public function testEnforcing() { $this->grantAccess('addComment'); - $this->login($this->mockUser()); + $this->login($this->userMocker->mockSingle()); getConfig()->registration->needEmailForCommenting = true; $this->assert->throws(function() { - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); return Api::run( new AddCommentJob(), diff --git a/tests/Api/ApiPrivilegeTest.php b/tests/Api/ApiPrivilegeTest.php index c50d5624..58e31ef6 100644 --- a/tests/Api/ApiPrivilegeTest.php +++ b/tests/Api/ApiPrivilegeTest.php @@ -38,7 +38,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest public function testDynamicPostPrivileges() { - $this->login($this->mockUser()); + $this->login($this->userMocker->mockSingle()); $this->testDynamicPostPrivilege(new DeletePostJob(), new Privilege(Privilege::DeletePost)); $this->testDynamicPostPrivilege(new EditPostJob(), new Privilege(Privilege::EditPost)); @@ -74,8 +74,10 @@ class ApiPrivilegeTest extends AbstractFullApiTest { $this->testedJobs []= $job; - $ownPost = $this->mockPost(Auth::getCurrentUser()); - $otherPost = $this->mockPost($this->mockUser()); + list ($ownPost, $otherPost) = $this->postMocker->mockMultiple(2); + $ownPost->setUploader(Auth::getCurrentUser()); + $otherPost->setUploader($this->userMocker->mockSingle()); + PostModel::save([$ownPost, $otherPost]); $expectedPrivilege->secondary = 'all'; $job->setArgument(JobArgs::ARG_POST_ID, $otherPost->getId()); @@ -96,7 +98,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest new GetPostContentJob(), ]; - $post = $this->mockPost($this->mockUser()); + $post = $this->postMocker->mockSingle(); foreach ($jobs as $job) { @@ -123,7 +125,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest public function testDynamicUserPrivileges() { - $ownUser = $this->mockUser(); + $ownUser = $this->userMocker->mockSingle(); $this->login($ownUser); $this->testDynamicUserPrivilege(new DeleteUserJob(), new Privilege(Privilege::DeleteUser)); @@ -140,7 +142,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest { $ownUser = Auth::getCurrentUser(); - $otherUser = $this->mockUser(); + $otherUser = $this->userMocker->mockSingle(); $otherUser->setName('dummy' . uniqid()); UserModel::save($otherUser); @@ -159,7 +161,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest public function testDynamicCommentPrivileges() { - $this->login($this->mockUser()); + $this->login($this->userMocker->mockSingle()); $this->testDynamicCommentPrivilege(new DeleteCommentJob(), new Privilege(Privilege::DeleteComment)); $this->testDynamicCommentPrivilege(new EditCommentJob(), new Privilege(Privilege::EditComment)); @@ -167,8 +169,10 @@ class ApiPrivilegeTest extends AbstractFullApiTest protected function testDynamicCommentPrivilege($job, $expectedPrivilege) { - $ownComment = $this->mockComment(Auth::getCurrentUser()); - $otherComment = $this->mockComment($this->mockUser()); + list ($ownComment, $otherComment) = $this->commentMocker->mockMultiple(2); + $ownComment->setCommenter(Auth::getCurrentUser()); + $otherComment->setCommenter($this->userMocker->mockSingle()); + CommentModel::save([$ownComment, $otherComment]); $this->testedJobs []= $job; @@ -187,7 +191,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest { $this->assert->throws(function() { - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); getConfig()->registration->needEmailForCommenting = false; return Api::run( new AddCommentJob(), diff --git a/tests/Api/Common/CommentRetrieverTest.php b/tests/Api/Common/CommentRetrieverTest.php index 26383b93..01502d19 100644 --- a/tests/Api/Common/CommentRetrieverTest.php +++ b/tests/Api/Common/CommentRetrieverTest.php @@ -75,7 +75,7 @@ class CommentRetrieverTest extends AbstractTest private function prepareComment() { - return $this->mockComment($this->mockUser()); + return $this->commentMocker->mockSingle(); } private function prepareRetriever() diff --git a/tests/Api/Common/PostRetrieverTest.php b/tests/Api/Common/PostRetrieverTest.php index e11eec73..5cff03af 100644 --- a/tests/Api/Common/PostRetrieverTest.php +++ b/tests/Api/Common/PostRetrieverTest.php @@ -84,7 +84,7 @@ class PostRetrieverTest extends AbstractTest private function preparePost() { - return $this->mockPost($this->mockUser()); + return $this->postMocker->mockSingle(); } private function prepareRetriever() diff --git a/tests/Api/Common/SafePostRetrieverTest.php b/tests/Api/Common/SafePostRetrieverTest.php index ad70e140..7f947d00 100644 --- a/tests/Api/Common/SafePostRetrieverTest.php +++ b/tests/Api/Common/SafePostRetrieverTest.php @@ -83,7 +83,7 @@ class SafePostRetrieverTest extends AbstractTest private function preparePost() { - return $this->mockPost($this->mockUser()); + return $this->postMocker->mockSingle(); } private function prepareRetriever() diff --git a/tests/Api/Common/UserRetrieverTest.php b/tests/Api/Common/UserRetrieverTest.php index d961c25c..c0d97f71 100644 --- a/tests/Api/Common/UserRetrieverTest.php +++ b/tests/Api/Common/UserRetrieverTest.php @@ -84,7 +84,7 @@ class UserRetrieverTest extends AbstractTest private function prepareUser() { - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setConfirmedEmail('godzilla@whitestar.gov'); UserModel::save($user); return $user; diff --git a/tests/JobTests/AcceptUserRegistrationJobTest.php b/tests/JobTests/AcceptUserRegistrationJobTest.php index a1e8c839..f6cead59 100644 --- a/tests/JobTests/AcceptUserRegistrationJobTest.php +++ b/tests/JobTests/AcceptUserRegistrationJobTest.php @@ -5,7 +5,7 @@ class AcceptUserRegistrationJobTest extends AbstractTest { $this->grantAccess('acceptUserRegistration'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $this->assert->isFalse($user->isStaffConfirmed()); $user = $this->assert->doesNotThrow(function() use ($user) diff --git a/tests/JobTests/ActivateUserEmailJobTest.php b/tests/JobTests/ActivateUserEmailJobTest.php index 158d6b02..126f512b 100644 --- a/tests/JobTests/ActivateUserEmailJobTest.php +++ b/tests/JobTests/ActivateUserEmailJobTest.php @@ -6,7 +6,7 @@ class ActivateUserEmailJobTest extends AbstractTest getConfig()->registration->needEmailForRegistering = true; Mailer::mockSending(); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setUnconfirmedEmail('godzilla@whitestar.gov'); UserModel::save($user); @@ -38,7 +38,7 @@ class ActivateUserEmailJobTest extends AbstractTest getConfig()->registration->needEmailForRegistering = true; Mailer::mockSending(); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setUnconfirmedEmail('godzilla@whitestar.gov'); UserModel::save($user); @@ -77,7 +77,7 @@ class ActivateUserEmailJobTest extends AbstractTest getConfig()->registration->needEmailForRegistering = true; Mailer::mockSending(); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setUnconfirmedEmail('godzilla@whitestar.gov'); UserModel::save($user); diff --git a/tests/JobTests/AddCommentJobTest.php b/tests/JobTests/AddCommentJobTest.php index 447e8c70..d1f54aea 100644 --- a/tests/JobTests/AddCommentJobTest.php +++ b/tests/JobTests/AddCommentJobTest.php @@ -97,7 +97,7 @@ class AddCommentJobTest extends AbstractTest protected function runApi($text) { - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); return Api::run( new AddCommentJob(), @@ -111,6 +111,6 @@ class AddCommentJobTest extends AbstractTest { getConfig()->registration->needEmailForCommenting = false; $this->grantAccess('addComment'); - $this->login($this->mockUser()); + $this->login($this->userMocker->mockSingle()); } } diff --git a/tests/JobTests/AddPostJobTest.php b/tests/JobTests/AddPostJobTest.php index ebd59152..9da3b71d 100644 --- a/tests/JobTests/AddPostJobTest.php +++ b/tests/JobTests/AddPostJobTest.php @@ -11,7 +11,7 @@ class AddPostJobTest extends AbstractTest $this->grantAccess('addPostSource'); $this->grantAccess('addPostContent'); - $this->login($this->mockUser()); + $this->login($this->userMocker->mockSingle()); $post = $this->assert->doesNotThrow(function() { @@ -20,14 +20,15 @@ class AddPostJobTest extends AbstractTest [ JobArgs::ARG_NEW_SAFETY => PostSafety::Safe, JobArgs::ARG_NEW_SOURCE => '', - JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'), JobArgs::ARG_NEW_TAG_NAMES => ['kamen', 'raider'], + JobArgs::ARG_NEW_POST_CONTENT => + new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'), ]); }); $this->assert->areEqual( file_get_contents($post->getFullPath()), - file_get_contents($this->getPath('image.jpg'))); + file_get_contents($this->testSupport->getPath('image.jpg'))); $this->assert->areEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId()); } @@ -39,7 +40,7 @@ class AddPostJobTest extends AbstractTest $this->grantAccess('addPostTags'); $this->grantAccess('addPostContent'); - $this->login($this->mockUser()); + $this->login($this->userMocker->mockSingle()); $post = $this->assert->doesNotThrow(function() { @@ -47,14 +48,15 @@ class AddPostJobTest extends AbstractTest new AddPostJob(), [ JobArgs::ARG_ANONYMOUS => true, - JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'), JobArgs::ARG_NEW_TAG_NAMES => ['kamen', 'raider'], + JobArgs::ARG_NEW_POST_CONTENT => + new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'), ]); }); $this->assert->areEqual( file_get_contents($post->getFullPath()), - file_get_contents($this->getPath('image.jpg'))); + file_get_contents($this->testSupport->getPath('image.jpg'))); $this->assert->areNotEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId()); $this->assert->areEqual(null, $post->getUploaderId()); } @@ -75,7 +77,8 @@ class AddPostJobTest extends AbstractTest [ JobArgs::ARG_NEW_SAFETY => PostSafety::Safe, JobArgs::ARG_NEW_SOURCE => 'this should make it fail', - JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'), + JobArgs::ARG_NEW_POST_CONTENT => + new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'), ]); }, 'Insufficient privilege'); } @@ -95,8 +98,9 @@ class AddPostJobTest extends AbstractTest new AddPostJob(), [ JobArgs::ARG_NEW_SAFETY => 666, - JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'), JobArgs::ARG_NEW_TAG_NAMES => ['kamen', 'raider'], + JobArgs::ARG_NEW_POST_CONTENT => + new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'), ]); }, 'Invalid safety type'); } @@ -132,7 +136,8 @@ class AddPostJobTest extends AbstractTest Api::run( new AddPostJob(), [ - JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'), + JobArgs::ARG_NEW_POST_CONTENT => + new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'), ]); }, 'No tags set'); } diff --git a/tests/JobTests/DeleteCommentJobTest.php b/tests/JobTests/DeleteCommentJobTest.php index 10f40cdb..70f9e8c0 100644 --- a/tests/JobTests/DeleteCommentJobTest.php +++ b/tests/JobTests/DeleteCommentJobTest.php @@ -6,7 +6,7 @@ class DeleteCommentJobTest extends AbstractTest $this->prepare(); $this->grantAccess('deleteComment'); - $comment = $this->mockComment(Auth::getCurrentUser()); + $comment = $this->commentMocker->mockSingle(); $post = $comment->getPost(); $this->assert->areEqual(1, $post->getCommentCount()); @@ -41,6 +41,6 @@ class DeleteCommentJobTest extends AbstractTest protected function prepare() { - $this->login($this->mockUser()); + $this->login($this->userMocker->mockSingle()); } } diff --git a/tests/JobTests/DeletePostJobTest.php b/tests/JobTests/DeletePostJobTest.php index 55adffcc..3e3f6cdf 100644 --- a/tests/JobTests/DeletePostJobTest.php +++ b/tests/JobTests/DeletePostJobTest.php @@ -3,9 +3,8 @@ class DeletePostJobTest extends AbstractTest { public function testRemoval() { - $user = $this->mockUser(); - $post = $this->mockPost($user); - $this->login($user); + $post = $this->postMocker->mockSingle(); + $this->login($this->userMocker->mockSingle()); $this->grantAccess('deletePost'); $this->assert->doesNotThrow(function() use ($post) @@ -23,9 +22,8 @@ class DeletePostJobTest extends AbstractTest public function testWrongPostId() { - $user = $this->mockUser(); - $post = $this->mockPost($user); - $this->login($user); + $post = $this->postMocker->mockSingle(); + $this->login($this->userMocker->mockSingle()); $this->assert->throws(function() { diff --git a/tests/JobTests/DeleteUserJobTest.php b/tests/JobTests/DeleteUserJobTest.php index 996b8269..0a77011d 100644 --- a/tests/JobTests/DeleteUserJobTest.php +++ b/tests/JobTests/DeleteUserJobTest.php @@ -3,7 +3,7 @@ class DeleteUserJobTest extends AbstractTest { public function testRemoval() { - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $this->login($user); $this->grantAccess('deleteUser'); @@ -22,7 +22,7 @@ class DeleteUserJobTest extends AbstractTest public function testWrongUserId() { - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $this->login($user); $this->assert->throws(function() diff --git a/tests/JobTests/EditCommentJobTest.php b/tests/JobTests/EditCommentJobTest.php index 9616e6cd..cf0b6286 100644 --- a/tests/JobTests/EditCommentJobTest.php +++ b/tests/JobTests/EditCommentJobTest.php @@ -79,7 +79,9 @@ class EditCommentJobTest extends AbstractTest protected function runApi($text) { - $comment = $this->mockComment(Auth::getCurrentUser()); + $comment = $this->commentMocker->mockSingle(); + $comment->setCommenter(Auth::getCurrentUser()); + CommentModel::save($comment); return Api::run( new EditCommentJob(), @@ -91,6 +93,6 @@ class EditCommentJobTest extends AbstractTest protected function prepare() { - $this->login($this->mockUser()); + $this->login($this->userMocker->mockSingle()); } } diff --git a/tests/JobTests/EditPostContentJobTest.php b/tests/JobTests/EditPostContentJobTest.php index 78c04a5d..c9af9ecc 100644 --- a/tests/JobTests/EditPostContentJobTest.php +++ b/tests/JobTests/EditPostContentJobTest.php @@ -91,7 +91,7 @@ class EditPostContentJobTest extends AbstractTest $this->prepare(); $this->grantAccess('editPostContent'); - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $post = Api::run( new EditPostContentJob(), [ @@ -119,7 +119,8 @@ class EditPostContentJobTest extends AbstractTest new EditPostContentJob(), [ JobArgs::ARG_POST_ID => 100, - JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'), + JobArgs::ARG_NEW_POST_CONTENT => + new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'), ]); }, 'Invalid post ID'); } @@ -156,7 +157,7 @@ class EditPostContentJobTest extends AbstractTest $url = 'http://www.youtube.com/watch?v=qWq_jydCUw4'; - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $post = Api::run( new EditPostContentJob(), [ @@ -164,7 +165,7 @@ class EditPostContentJobTest extends AbstractTest JobArgs::ARG_NEW_POST_CONTENT_URL => $url, ]); - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $this->assert->throws(function() use ($post, $url) { Api::run( @@ -178,16 +179,16 @@ class EditPostContentJobTest extends AbstractTest protected function prepare() { - $this->login($this->mockUser()); + $this->login($this->userMocker->mockSingle()); } protected function uploadFromUrl($fileName, $post = null) { if ($post === null) - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $url = 'http://example.com/mock_' . $fileName; - TransferHelper::mockForDownload($url, $this->getPath($fileName)); + TransferHelper::mockForDownload($url, $this->testSupport->getPath($fileName)); $post = Api::run( new EditPostContentJob(), @@ -198,7 +199,7 @@ class EditPostContentJobTest extends AbstractTest $this->assert->isNotNull($post->tryGetWorkingFullPath()); $this->assert->areEqual( - file_get_contents($this->getPath($fileName)), + file_get_contents($this->testSupport->getPath($fileName)), file_get_contents($post->tryGetWorkingFullPath())); return $post; @@ -207,18 +208,19 @@ class EditPostContentJobTest extends AbstractTest protected function uploadFromFile($fileName, $post = null) { if ($post === null) - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $post = Api::run( new EditPostContentJob(), [ JobArgs::ARG_POST_ID => $post->getId(), - JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath($fileName), 'test.jpg'), + JobArgs::ARG_NEW_POST_CONTENT => + new ApiFileInput($this->testSupport->getPath($fileName), 'test.jpg'), ]); $this->assert->isNotNull($post->tryGetWorkingFullPath()); $this->assert->areEqual( - file_get_contents($this->getPath($fileName)), + file_get_contents($this->testSupport->getPath($fileName)), file_get_contents($post->tryGetWorkingFullPath())); return $post; diff --git a/tests/JobTests/EditPostJobTest.php b/tests/JobTests/EditPostJobTest.php index 94a6fbf7..5d206299 100644 --- a/tests/JobTests/EditPostJobTest.php +++ b/tests/JobTests/EditPostJobTest.php @@ -9,14 +9,15 @@ class EditPostJobTest extends AbstractTest $this->grantAccess('editPostSource'); $this->grantAccess('editPostContent'); - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $args = [ JobArgs::ARG_POST_ID => $post->getId(), JobArgs::ARG_NEW_SAFETY => PostSafety::Sketchy, JobArgs::ARG_NEW_SOURCE => 'some source huh', - JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'), + JobArgs::ARG_NEW_POST_CONTENT => + new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'), ]; $this->assert->doesNotThrow(function() use ($args) @@ -32,14 +33,15 @@ class EditPostJobTest extends AbstractTest $this->grantAccess('editPostTags'); $this->grantAccess('editPostContent'); - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $args = [ JobArgs::ARG_POST_ID => $post->getId(), JobArgs::ARG_NEW_SAFETY => PostSafety::Safe, JobArgs::ARG_NEW_SOURCE => 'this should make it fail', - JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'), + JobArgs::ARG_NEW_POST_CONTENT => + new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'), ]; $this->assert->throws(function() use ($args) diff --git a/tests/JobTests/EditPostRelationsJobTest.php b/tests/JobTests/EditPostRelationsJobTest.php index 319c1986..bacf987a 100644 --- a/tests/JobTests/EditPostRelationsJobTest.php +++ b/tests/JobTests/EditPostRelationsJobTest.php @@ -3,11 +3,10 @@ class EditPostRelationsJobTest extends AbstractTest { public function testEditing() { - $basePost = $this->mockPost($this->mockUser()); $this->grantAccess('editPostRelations'); - $post1 = $this->mockPost($this->mockUser()); - $post2 = $this->mockPost($this->mockUser()); + list ($basePost, $post1, $post2) + = $this->postMocker->mockMultiple(3); $basePost = $this->assert->doesNotThrow(function() use ($basePost, $post1, $post2) { @@ -30,11 +29,10 @@ class EditPostRelationsJobTest extends AbstractTest public function testOverwriting() { - $basePost = $this->mockPost($this->mockUser()); $this->grantAccess('editPostRelations'); - $post1 = $this->mockPost($this->mockUser()); - $post2 = $this->mockPost($this->mockUser()); + list ($basePost, $post1, $post2) + = $this->postMocker->mockMultiple(3); $basePost->setRelations([$post1]); PostModel::save($basePost); @@ -61,11 +59,10 @@ class EditPostRelationsJobTest extends AbstractTest public function testOverwritingEmpty() { - $basePost = $this->mockPost($this->mockUser()); $this->grantAccess('editPostRelations'); - $post1 = $this->mockPost($this->mockUser()); - $post2 = $this->mockPost($this->mockUser()); + list ($basePost, $post1, $post2) + = $this->postMocker->mockMultiple(3); $basePost->setRelations([$post1]); PostModel::save($basePost); diff --git a/tests/JobTests/EditPostSafetyJobTest.php b/tests/JobTests/EditPostSafetyJobTest.php index 90865011..40288196 100644 --- a/tests/JobTests/EditPostSafetyJobTest.php +++ b/tests/JobTests/EditPostSafetyJobTest.php @@ -6,7 +6,7 @@ class EditPostSafetyJobTest extends AbstractTest $this->prepare(); $this->grantAccess('editPostSafety.own'); - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $post = $this->assert->doesNotThrow(function() use ($post) { return Api::run( @@ -44,7 +44,7 @@ class EditPostSafetyJobTest extends AbstractTest $this->prepare(); $this->grantAccess('editPostSafety.own'); - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $this->assert->throws(function() use ($post) { Api::run( @@ -58,6 +58,6 @@ class EditPostSafetyJobTest extends AbstractTest protected function prepare() { - $this->login($this->mockUser()); + $this->login($this->userMocker->mockSingle()); } } diff --git a/tests/JobTests/EditPostSourceJobTest.php b/tests/JobTests/EditPostSourceJobTest.php index 633524bb..7fd67112 100644 --- a/tests/JobTests/EditPostSourceJobTest.php +++ b/tests/JobTests/EditPostSourceJobTest.php @@ -65,7 +65,7 @@ class EditPostSourceJobTest extends AbstractTest protected function runApi($text) { - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); return Api::run( new EditPostSourceJob(), [ @@ -76,6 +76,6 @@ class EditPostSourceJobTest extends AbstractTest protected function prepare() { - $this->login($this->mockUser()); + $this->login($this->userMocker->mockSingle()); } } diff --git a/tests/JobTests/EditPostTagsJobTest.php b/tests/JobTests/EditPostTagsJobTest.php index 9ecaea6b..3c5a761f 100644 --- a/tests/JobTests/EditPostTagsJobTest.php +++ b/tests/JobTests/EditPostTagsJobTest.php @@ -3,7 +3,7 @@ class EditPostTagsJobTest extends AbstractTest { public function testEditing() { - $post = $this->mockPost($this->mockUser()); + $post = $this->postMocker->mockSingle(); $this->grantAccess('editPostTags'); $newTagNames = ['big', 'boss']; @@ -30,7 +30,7 @@ class EditPostTagsJobTest extends AbstractTest public function testFailOnEmptyTags() { - $post = $this->mockPost($this->mockUser()); + $post = $this->postMocker->mockSingle(); $this->grantAccess('editPostTags'); $this->assert->throws(function() use ($post) @@ -46,7 +46,7 @@ class EditPostTagsJobTest extends AbstractTest public function testTooShortTag() { - $post = $this->mockPost($this->mockUser()); + $post = $this->postMocker->mockSingle(); $this->grantAccess('editPostTags'); $newTagNames = [str_repeat('u', getConfig()->tags->minLength - 1)]; @@ -63,7 +63,7 @@ class EditPostTagsJobTest extends AbstractTest public function testTooLongTag() { - $post = $this->mockPost($this->mockUser()); + $post = $this->postMocker->mockSingle(); $this->grantAccess('editPostTags'); $newTagNames = [str_repeat('u', getConfig()->tags->maxLength + 1)]; @@ -80,7 +80,7 @@ class EditPostTagsJobTest extends AbstractTest public function testInvalidTag() { - $post = $this->mockPost($this->mockUser()); + $post = $this->postMocker->mockSingle(); $this->grantAccess('editPostTags'); $newTagNames = ['bulma/goku']; diff --git a/tests/JobTests/EditPostThumbJobTest.php b/tests/JobTests/EditPostThumbJobTest.php index ae7d6e2e..a2758e60 100644 --- a/tests/JobTests/EditPostThumbJobTest.php +++ b/tests/JobTests/EditPostThumbJobTest.php @@ -4,18 +4,17 @@ class EditPostThumbJobTest extends AbstractTest public function testFile() { $this->grantAccess('editPostThumb'); - - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $this->assert->isFalse($post->hasCustomThumb()); - $post = $this->assert->doesNotThrow(function() use ($post) { return Api::run( new EditPostThumbJob(), [ JobArgs::ARG_POST_ID => $post->getId(), - JobArgs::ARG_NEW_THUMB_CONTENT => new ApiFileInput($this->getPath('thumb.jpg'), 'test.jpg'), + JobArgs::ARG_NEW_THUMB_CONTENT => + new ApiFileInput($this->testSupport->getPath('thumb.jpg'), 'test.jpg'), ]); }); @@ -23,25 +22,24 @@ class EditPostThumbJobTest extends AbstractTest $this->assert->isNotNull($post->getThumbCustomPath()); $this->assert->areEqual($post->getThumbCustomPath(), $post->tryGetWorkingThumbPath()); $this->assert->areEqual( - file_get_contents($this->getPath('thumb.jpg')), + file_get_contents($this->testSupport->getPath('thumb.jpg')), file_get_contents($post->tryGetWorkingThumbPath())); } public function testFileInvalidDimensions() { $this->grantAccess('editPostThumb'); - - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $this->assert->isFalse($post->hasCustomThumb()); - $this->assert->throws(function() use ($post) { return Api::run( new EditPostThumbJob(), [ JobArgs::ARG_POST_ID => $post->getId(), - JobArgs::ARG_NEW_THUMB_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'), + JobArgs::ARG_NEW_THUMB_CONTENT => + new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'), ]); }, 'invalid thumbnail size'); diff --git a/tests/JobTests/EditUserAccessRankJobTest.php b/tests/JobTests/EditUserAccessRankJobTest.php index d8cc4ae4..63cc0bcb 100644 --- a/tests/JobTests/EditUserAccessRankJobTest.php +++ b/tests/JobTests/EditUserAccessRankJobTest.php @@ -4,7 +4,7 @@ class EditUserAccessRankJobTest extends AbstractTest public function testEditing() { $this->grantAccess('changeUserAccessRank'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $this->assert->areEqual(AccessRank::Registered, $user->getAccessRank()->toInteger()); @@ -24,7 +24,7 @@ class EditUserAccessRankJobTest extends AbstractTest public function testSettingToNobodyDenial() { $this->grantAccess('changeUserAccessRank'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $this->assert->areEqual(AccessRank::Registered, $user->getAccessRank()->toInteger()); @@ -44,7 +44,7 @@ class EditUserAccessRankJobTest extends AbstractTest getConfig()->privileges->changeUserAccessRank = 'power-user'; Access::init(); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setAccessRank(new AccessRank(AccessRank::PowerUser)); UserModel::save($user); diff --git a/tests/JobTests/EditUserEmailJobTest.php b/tests/JobTests/EditUserEmailJobTest.php index cc44029e..d43869fa 100644 --- a/tests/JobTests/EditUserEmailJobTest.php +++ b/tests/JobTests/EditUserEmailJobTest.php @@ -10,7 +10,7 @@ class EditUserEmailJobTest extends AbstractTest getConfig()->privileges->changeUserEmailNoConfirm = 'anonymous'; $this->grantAccess('changeUserEmail'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user = $this->assert->doesNotThrow(function() use ($user) { @@ -37,7 +37,7 @@ class EditUserEmailJobTest extends AbstractTest getConfig()->privileges->changeUserEmailNoConfirm = 'admin'; $this->grantAccess('changeUserEmail'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user = $this->assert->doesNotThrow(function() use ($user) { @@ -63,7 +63,7 @@ class EditUserEmailJobTest extends AbstractTest getConfig()->privileges->changeUserEmailNoConfirm = 'nobody'; $this->grantAccess('changeUserEmail'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $this->assert->throws(function() use ($user) { @@ -85,8 +85,8 @@ class EditUserEmailJobTest extends AbstractTest getConfig()->privileges->changeUserEmailNoConfirm = 'anonymous'; $this->grantAccess('changeUserEmail'); - $user = $this->mockUser(); - $otherUser = $this->mockUser(); + list ($user, $otherUser) + = $this->userMocker->mockMultiple(2); $otherUser->setUnconfirmedEmail('super@mario.plumbing'); UserModel::save($otherUser); diff --git a/tests/JobTests/EditUserJobTest.php b/tests/JobTests/EditUserJobTest.php index 816f1732..1401b9dc 100644 --- a/tests/JobTests/EditUserJobTest.php +++ b/tests/JobTests/EditUserJobTest.php @@ -5,7 +5,7 @@ class EditUserJobTest extends AbstractTest { $this->grantAccess('changeUserName.own'); $this->grantAccess('changeUserPassword.own'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $newName = 'dummy' . uniqid(); @@ -30,7 +30,7 @@ class EditUserJobTest extends AbstractTest public function testPartialPrivilegeFail() { $this->grantAccess('changeUserName.own'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $newName = 'dummy' . uniqid(); @@ -59,13 +59,13 @@ class EditUserJobTest extends AbstractTest public function testCanEditSomething() { $this->grantAccess('changeUserName.own'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user = $this->assert->isTrue((new EditUserJob())->canEditAnything($user)); } public function testCannotEditAnything() { - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user = $this->assert->isFalse((new EditUserJob())->canEditAnything($user)); } } diff --git a/tests/JobTests/EditUserNameJobTest.php b/tests/JobTests/EditUserNameJobTest.php index 18c47892..ff646b62 100644 --- a/tests/JobTests/EditUserNameJobTest.php +++ b/tests/JobTests/EditUserNameJobTest.php @@ -4,7 +4,7 @@ class EditUserNameJobTest extends AbstractTest public function testEditing() { $this->grantAccess('changeUserName'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $newName = uniqid(); @@ -26,7 +26,7 @@ class EditUserNameJobTest extends AbstractTest public function testTooShortName() { $this->grantAccess('changeUserName'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $newName = str_repeat('a', getConfig()->registration->userNameMinLength - 1); @@ -44,7 +44,7 @@ class EditUserNameJobTest extends AbstractTest public function testTooLongName() { $this->grantAccess('changeUserName'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $newName = str_repeat('a', getConfig()->registration->userNameMaxLength + 1); @@ -62,7 +62,7 @@ class EditUserNameJobTest extends AbstractTest public function testInvalidName() { $this->grantAccess('changeUserName'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $newName = 'ble/ble'; @@ -80,8 +80,8 @@ class EditUserNameJobTest extends AbstractTest public function testChangingToExistingDenial() { $this->grantAccess('changeUserName'); - $user = $this->mockUser(); - $otherUser = $this->mockUser(); + list ($user, $otherUser) + = $this->userMocker->mockMultiple(2); $newName = $otherUser->getName(); $this->assert->areNotEqual($newName, $user->getName()); diff --git a/tests/JobTests/EditUserPasswordJobTest.php b/tests/JobTests/EditUserPasswordJobTest.php index 38f8b24a..9650b374 100644 --- a/tests/JobTests/EditUserPasswordJobTest.php +++ b/tests/JobTests/EditUserPasswordJobTest.php @@ -14,7 +14,7 @@ class EditUserPasswordJobTest extends AbstractTest public function testTooShortPassword() { $this->grantAccess('changeUserPassword'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $newPassword = str_repeat('a', getConfig()->registration->passMinLength - 1); $oldPasswordHash = $user->getPasswordHash(); @@ -35,7 +35,7 @@ class EditUserPasswordJobTest extends AbstractTest private function testValidPassword($newPassword) { $this->grantAccess('changeUserPassword'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $newPasswordHash = UserModel::hashPassword($newPassword, $user->getPasswordSalt()); diff --git a/tests/JobTests/FeaturePostJobTest.php b/tests/JobTests/FeaturePostJobTest.php index a9d77c83..894d80af 100644 --- a/tests/JobTests/FeaturePostJobTest.php +++ b/tests/JobTests/FeaturePostJobTest.php @@ -5,21 +5,20 @@ class FeaturePostJobTest extends AbstractTest { $this->grantAccess('featurePost'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $this->login($user); - $post1 = $this->mockPost($user); - $post2 = $this->mockPost($user); + $posts = $this->postMocker->mockMultiple(2); - $this->assert->doesNotThrow(function() use ($post2) + $this->assert->doesNotThrow(function() use ($posts) { Api::run( new FeaturePostJob(), [ - JobArgs::ARG_POST_ID => $post2->getId() + JobArgs::ARG_POST_ID => $posts[1]->getId() ]); }); - $this->assert->areEqual($post2->getId(), PropertyModel::get(PropertyModel::FeaturedPostId)); + $this->assert->areEqual($posts[1]->getId(), PropertyModel::get(PropertyModel::FeaturedPostId)); $this->assert->areEqual($user->getName(), PropertyModel::get(PropertyModel::FeaturedPostUserName)); $this->assert->isNotNull(PropertyModel::get(PropertyModel::FeaturedPostUnixTime)); } @@ -28,22 +27,20 @@ class FeaturePostJobTest extends AbstractTest { $this->grantAccess('featurePost'); - $user = $this->mockUser(); - $this->login($user); - $post1 = $this->mockPost($user); - $post2 = $this->mockPost($user); + $this->login($this->userMocker->mockSingle()); + $posts = $this->postMocker->mockMultiple(2); - $this->assert->doesNotThrow(function() use ($post2) + $this->assert->doesNotThrow(function() use ($posts) { Api::run( new FeaturePostJob(), [ - JobArgs::ARG_POST_ID => $post2->getId(), + JobArgs::ARG_POST_ID => $posts[1]->getId(), JobArgs::ARG_ANONYMOUS => true, ]); }); - $this->assert->areEqual($post2->getId(), PropertyModel::get(PropertyModel::FeaturedPostId)); + $this->assert->areEqual($posts[1]->getId(), PropertyModel::get(PropertyModel::FeaturedPostId)); $this->assert->areEqual(null, PropertyModel::get(PropertyModel::FeaturedPostUserName)); } } diff --git a/tests/JobTests/FlagPostJobTest.php b/tests/JobTests/FlagPostJobTest.php index 33ad9181..0226b216 100644 --- a/tests/JobTests/FlagPostJobTest.php +++ b/tests/JobTests/FlagPostJobTest.php @@ -4,7 +4,7 @@ class FlagPostJobTest extends AbstractTest public function testFlagging() { $this->grantAccess('flagPost'); - $post = $this->mockPost($this->mockUser()); + $post = $this->postMocker->mockSingle(); $post = $this->assert->doesNotThrow(function() use ($post) { @@ -25,7 +25,7 @@ class FlagPostJobTest extends AbstractTest public function testDoubleFlagging() { $this->grantAccess('flagPost'); - $post = $this->mockPost($this->mockUser()); + $post = $this->postMocker->mockSingle(); $this->assert->doesNotThrow(function() use ($post) { diff --git a/tests/JobTests/FlagUserJobTest.php b/tests/JobTests/FlagUserJobTest.php index eb11cd0a..bea9cf77 100644 --- a/tests/JobTests/FlagUserJobTest.php +++ b/tests/JobTests/FlagUserJobTest.php @@ -4,7 +4,7 @@ class FlagUserJobTest extends AbstractTest public function testFlagging() { $this->grantAccess('flagUser'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user = $this->assert->doesNotThrow(function() use ($user) { @@ -25,7 +25,7 @@ class FlagUserJobTest extends AbstractTest public function testDoubleFlagging() { $this->grantAccess('flagUser'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $this->assert->doesNotThrow(function() use ($user) { diff --git a/tests/JobTests/GetLogJobTest.php b/tests/JobTests/GetLogJobTest.php new file mode 100644 index 00000000..0b2a9451 --- /dev/null +++ b/tests/JobTests/GetLogJobTest.php @@ -0,0 +1,57 @@ +run([]); + + $this->assert->areEqual(2, count($ret->entities)); + $this->assert->areEqual(3, $ret->entityCount); + $this->assert->areEqual(2, $ret->pageCount); + $this->assert->areEqual(1, $ret->page); + $this->assert->isTrue(strpos($ret->entities[0], 'line3') !== false); + $this->assert->isTrue(strpos($ret->entities[1], 'line2') !== false); + } + + public function testRetrievingWithPaging() + { + $ret = $this->run([JobArgs::ARG_PAGE_NUMBER => 2]); + + $this->assert->areEqual(1, count($ret->entities)); + $this->assert->areEqual(3, $ret->entityCount); + $this->assert->areEqual(2, $ret->pageCount); + $this->assert->areEqual(2, $ret->page); + $this->assert->isTrue(strpos($ret->entities[0], 'line1') !== false); + } + + public function testRetrievingWithFilter() + { + $ret = $this->run([JobArgs::ARG_QUERY => 'line2']); + + $this->assert->areEqual(1, count($ret->entities)); + $this->assert->areEqual(1, $ret->entityCount); + $this->assert->areEqual(1, $ret->pageCount); + $this->assert->areEqual(1, $ret->page); + $this->assert->isTrue(strpos($ret->entities[0], 'line2') !== false); + } + + private function run($args) + { + getConfig()->browsing->logsPerPage = 2; + $this->grantAccess('viewLog'); + Logger::log('line1'); + Logger::log('line2'); + Logger::log('line3'); + + $logId = Logger::getLogPath(); + $logId = basename($logId); + $args[JobArgs::ARG_LOG_ID] = $logId; + + return $this->assert->doesNotThrow(function() use ($args) + { + return Api::run( + new GetLogJob(), + $args); + }); + } +} diff --git a/tests/JobTests/GetPostContentJobTest.php b/tests/JobTests/GetPostContentJobTest.php new file mode 100644 index 00000000..4a52dc91 --- /dev/null +++ b/tests/JobTests/GetPostContentJobTest.php @@ -0,0 +1,51 @@ +grantAccess('viewPost'); + $post = $this->postMocker->mockSingle(); + + $output = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new GetPostContentJob(), + [ + JobArgs::ARG_POST_NAME => $post->getName(), + ]); + }); + + $this->assert->isNotNull($post->tryGetWorkingFullPath()); + $this->assert->areEqual( + file_get_contents($this->testSupport->getPath('image.jpg')), + $output->fileContent); + } + + public function testIdFail() + { + $this->grantAccess('viewPost'); + + $this->assert->throws(function() + { + Api::run( + new GetPostContentJob(), + [ + JobArgs::ARG_POST_ID => 100, + ]); + }, 'unsatisfied'); + } + + public function testInvalidName() + { + $this->grantAccess('viewPost'); + + $this->assert->throws(function() + { + Api::run( + new GetPostContentJob(), + [ + JobArgs::ARG_POST_NAME => 'nonsense', + ]); + }, 'Invalid post name'); + } +} diff --git a/tests/JobTests/GetPostJobTest.php b/tests/JobTests/GetPostJobTest.php new file mode 100644 index 00000000..ad02c199 --- /dev/null +++ b/tests/JobTests/GetPostJobTest.php @@ -0,0 +1,36 @@ +grantAccess('viewPost'); + $post = $this->postMocker->mockSingle(); + + $samePost = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new GetPostJob(), + [ + JobArgs::ARG_POST_ID => $post->getId(), + ]); + }); + + $post->resetCache(); + $samePost->resetCache(); + $this->assert->areEquivalent($post, $samePost); + } + + public function testInvalidId() + { + $this->grantAccess('viewPost'); + + $this->assert->throws(function() + { + Api::run( + new GetPostJob(), + [ + JobArgs::ARG_POST_ID => 100, + ]); + }, 'Invalid post ID'); + } +} diff --git a/tests/JobTests/GetPostThumbJobTest.php b/tests/JobTests/GetPostThumbJobTest.php new file mode 100644 index 00000000..763a2b84 --- /dev/null +++ b/tests/JobTests/GetPostThumbJobTest.php @@ -0,0 +1,50 @@ +grantAccess('viewPost'); + $post = $this->postMocker->mockSingle(); + + $output = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new GetPostThumbJob(), + [ + JobArgs::ARG_POST_NAME => $post->getName(), + ]); + }); + + $this->assert->isNotNull($post->tryGetWorkingFullPath()); + $this->assert->areEqual('image/jpeg', $output->mimeType); + } + + public function testIdFail() + { + $this->grantAccess('viewPost'); + + $this->assert->throws(function() + { + Api::run( + new GetPostThumbJob(), + [ + JobArgs::ARG_POST_ID => 100, + ]); + }, 'unsatisfied'); + } + + public function testInvalidName() + { + $this->grantAccess('viewPost'); + + $this->assert->throws(function() + { + Api::run( + new GetPostThumbJob(), + [ + JobArgs::ARG_POST_NAME => 'nonsense', + ]); + }, 'Invalid post name'); + } +} + diff --git a/tests/JobTests/GetUserJobTest.php b/tests/JobTests/GetUserJobTest.php new file mode 100644 index 00000000..64fd47c1 --- /dev/null +++ b/tests/JobTests/GetUserJobTest.php @@ -0,0 +1,39 @@ +grantAccess('viewUser'); + $user = $this->userMocker->mockSingle(); + + //reload from model to make sure it's same + $user = UserModel::getById($user->getId()); + + $sameUser = $this->assert->doesNotThrow(function() use ($user) + { + return Api::run( + new GetUserJob(), + [ + JobArgs::ARG_USER_NAME => $user->getName(), + ]); + }); + + $user->resetCache(); + $sameUser->resetCache(); + $this->assert->areEquivalent($user, $sameUser); + } + + public function testInvalidId() + { + $this->grantAccess('viewUser'); + + $this->assert->throws(function() + { + Api::run( + new GetUserJob(), + [ + JobArgs::ARG_USER_NAME => 'nonsense', + ]); + }, 'Invalid user name'); + } +} diff --git a/tests/JobTests/ListCommentsJobTest.php b/tests/JobTests/ListCommentsJobTest.php index 1d3706af..477d0ca1 100644 --- a/tests/JobTests/ListCommentsJobTest.php +++ b/tests/JobTests/ListCommentsJobTest.php @@ -18,7 +18,7 @@ class ListCommentJobTest extends AbstractTest $this->assert->areEqual(0, CommentModel::getCount()); - $comment = $this->mockComment($this->mockUser()); + $comment = $this->commentMocker->mockSingle(); $ret = $this->runApi(1); $this->assert->areEqual(1, count($ret->entities)); @@ -48,9 +48,7 @@ class ListCommentJobTest extends AbstractTest $this->assert->areEqual(0, CommentModel::getCount()); - $this->mockComment($this->mockUser()); - $this->mockComment($this->mockUser()); - $this->mockComment($this->mockUser()); + $this->commentMocker->mockMultiple(3); $ret = $this->runApi(1); $this->assert->areEqual(2, count($ret->entities)); diff --git a/tests/JobTests/ListLogsJobTest.php b/tests/JobTests/ListLogsJobTest.php new file mode 100644 index 00000000..8c7aaafd --- /dev/null +++ b/tests/JobTests/ListLogsJobTest.php @@ -0,0 +1,27 @@ +grantAccess('listLogs'); + + getConfig()->main->logsPath = dirname(__DIR__) . '/logs/test1.log'; + Logger::init(); + + Logger::log('nonsense'); + + getConfig()->main->logsPath = dirname(__DIR__) . '/logs/test2.log'; + Logger::init(); + + Logger::log('nonsense'); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListLogsJob(), []); + }); + + $this->assert->areEqual(2, count($ret)); + $this->assert->areEqual('test2.log', $ret[0]); + $this->assert->areEqual('test1.log', $ret[1]); + } +} diff --git a/tests/JobTests/ListPostsJobTest.php b/tests/JobTests/ListPostsJobTest.php new file mode 100644 index 00000000..feb8b815 --- /dev/null +++ b/tests/JobTests/ListPostsJobTest.php @@ -0,0 +1,644 @@ +grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + getConfig()->browsing->postsPerPage = 2; + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), []); + }); + + $this->assert->areEqual(3, $ret->entityCount); + $this->assert->areEqual(2, count($ret->entities)); + $this->assert->areEqual(2, $ret->pageCount); + $this->assert->areEqual(1, $ret->page); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_PAGE_NUMBER => 2]); + }); + + $this->assert->areEqual(3, $ret->entityCount); + $this->assert->areEqual(1, count($ret->entities)); + $this->assert->areEqual(2, $ret->pageCount); + $this->assert->areEqual(2, $ret->page); + } + + public function testAutomaticSafetyFilterOnlySafeEnabled() + { + $user = $this->userMocker->mockSingle(); + $user->enableSafety(new PostSafety(PostSafety::Safe), true); + $user->enableSafety(new PostSafety(PostSafety::Sketchy), false); + $user->enableSafety(new PostSafety(PostSafety::Unsafe), false); + UserModel::save($user); + $this->login($user); + + $this->grantAccess('listPosts.safe'); + $this->grantAccess('listPosts.sketchy'); + $this->revokeAccess('listPosts.unsafe'); + + $posts = $this->postMocker->mockMultiple(3); + $posts[0]->setSafety(new PostSafety(PostSafety::Safe)); + $posts[1]->setSafety(new PostSafety(PostSafety::Sketchy)); + $posts[2]->setSafety(new PostSafety(PostSafety::Unsafe)); + PostModel::save($posts); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), []); + }); + + $this->assert->areEqual(1, $ret->entityCount); + $this->assert->areEqual(1, count($ret->entities)); + $this->assert->areEqual(1, $ret->pageCount); + $this->assert->areEqual(1, $ret->page); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[0]->getId()); + } + + public function testAutomaticSafetyFilterAllEnabled() + { + $user = $this->userMocker->mockSingle(); + $user->enableSafety(new PostSafety(PostSafety::Safe), true); + $user->enableSafety(new PostSafety(PostSafety::Sketchy), true); + $user->enableSafety(new PostSafety(PostSafety::Unsafe), true); + UserModel::save($user); + $this->login($user); + + $this->grantAccess('listPosts.safe'); + $this->grantAccess('listPosts.sketchy'); + $this->revokeAccess('listPosts.unsafe'); + + $posts = $this->postMocker->mockMultiple(3); + $posts[0]->setSafety(new PostSafety(PostSafety::Safe)); + $posts[1]->setSafety(new PostSafety(PostSafety::Sketchy)); + $posts[2]->setSafety(new PostSafety(PostSafety::Unsafe)); + PostModel::save($posts); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), []); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual(2, count($ret->entities)); + $this->assert->areEqual(1, $ret->pageCount); + $this->assert->areEqual(1, $ret->page); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + + public function testDislikedHiding() + { + $this->grantAccess('listPosts'); + $user = $this->userMocker->mockSingle(); + $user->enableHidingDislikedPosts(true); + $post = $this->postMocker->mockSingle(); + $this->login($user); + + UserModel::updateUserScore($user, $post, -1); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), []); + }); + + $this->assert->areEqual(0, $ret->entityCount); + } + + public function testDislikedShowing() + { + $this->grantAccess('listPosts'); + $user = $this->userMocker->mockSingle(); + $user->enableHidingDislikedPosts(false); + $post = $this->postMocker->mockSingle(); + $this->login($user); + + UserModel::updateUserScore($user, $post, -1); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), []); + }); + + $this->assert->areEqual(1, $ret->entityCount); + } + + public function testDislikedHidingFilterOverride() + { + $this->grantAccess('listPosts'); + $user = $this->userMocker->mockSingle(); + $user->enableHidingDislikedPosts(true); + $post = $this->postMocker->mockSingle(); + $this->login($user); + + UserModel::updateUserScore($user, $post, -1); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'special:disliked']); + }); + + $this->assert->areEqual(1, $ret->entityCount); + } + + public function testHiddenHiding() + { + $this->grantAccess('listPosts'); + + $post = $this->postMocker->mockSingle(); + $post->setHidden(true); + PostModel::save($post); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), []); + }); + + $this->assert->areEqual(0, $ret->entityCount); + } + + public function testHiddenShowingWithoutAccess() + { + $this->grantAccess('listPosts'); + $this->revokeAccess('listPosts.hidden'); + + $post = $this->postMocker->mockSingle(); + $post->setHidden(true); + PostModel::save($post); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'special:hidden']); + }); + + $this->assert->areEqual(0, $ret->entityCount); + } + + public function testHiddenShowingWithAccess() + { + $this->grantAccess('listPosts'); + + $post = $this->postMocker->mockSingle(); + $post->setHidden(true); + PostModel::save($post); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'special:hidden']); + }); + + $this->assert->areEqual(1, $ret->entityCount); + } + + public function testIds() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + + foreach (['id', 'ids'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':1,3,5']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + } + + public function testFavs() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + $user = $this->userMocker->mockSingle(); + UserModel::addToUserFavorites($user, $posts[0]); + UserModel::addToUserFavorites($user, $posts[2]); + + foreach (['fav', 'favs', 'favd'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias, $user) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':' . $user->getName()]); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + } + + public function testOwnFavs() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + $user = $this->userMocker->mockSingle(); + UserModel::addToUserFavorites($user, $posts[0]); + UserModel::addToUserFavorites($user, $posts[2]); + + $this->login($user); + + foreach (['fav', 'favs', 'favd'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias, $user) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'special:' . $alias]); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + } + + public function testOwnLiked() + { + $this->grantAccess('listPosts'); + $user = $this->userMocker->mockSingle(); + $posts = $this->postMocker->mockMultiple(3); + UserModel::updateUserScore($user, $posts[0], 1); + UserModel::updateUserScore($user, $posts[2], 1); + + $this->login($user); + + foreach (['like', 'liked', 'likes'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias, $user) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'special:' . $alias]); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + } + + public function testUploads() + { + $this->grantAccess('listPosts'); + $user1 = $this->userMocker->mockSingle(); + $user2 = $this->userMocker->mockSingle(); + $posts = $this->postMocker->mockMultiple(3); + $posts[0]->setUploader($user1); + $posts[1]->setUploader($user2); + $posts[2]->setUploader($user1); + PostModel::save($posts); + + foreach (['submit', 'upload', 'uploads', 'uploader', 'uploaded'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias, $user1) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':' . $user1->getName()]); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + } + + public function testIdMinMax() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + + foreach (['idmin', 'id_min'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':2']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[1]->getId()); + } + + foreach (['idmax', 'id_max'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':2']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + } + + public function testScoreMinMax() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + + $user1 = $this->userMocker->mockSingle(); + $user2 = $this->userMocker->mockSingle(); + UserModel::updateUserScore($user1, $posts[0], 1); + UserModel::updateUserScore($user2, $posts[0], 1); + UserModel::updateUserScore($user1, $posts[2], 1); + + foreach (['scoremin', 'score_min'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':1']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + + foreach (['scoremax', 'score_max'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':1']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[1]->getId()); + } + } + + public function testFavMinMax() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + + $user1 = $this->userMocker->mockSingle(); + $user2 = $this->userMocker->mockSingle(); + UserModel::addToUserFavorites($user1, $posts[0]); + UserModel::addToUserFavorites($user2, $posts[0]); + UserModel::addToUserFavorites($user1, $posts[2]); + + foreach (['favmin', 'fav_min'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':1']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + + foreach (['favmax', 'fav_max'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':1']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[1]->getId()); + } + } + + public function testCommentMinMax() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + + $comment1 = CommentModel::spawn(); + $comment2 = CommentModel::spawn(); + $comment3 = CommentModel::spawn(); + $comment1->setPost($posts[0]); + $comment2->setPost($posts[0]); + $comment3->setPost($posts[2]); + foreach ([$comment1, $comment2, $comment3] as $comment) + { + $comment->setText('alohaaa'); + CommentModel::save($comment); + } + + foreach (['commentmin', 'comment_min'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':1']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + + foreach (['commentmax', 'comment_max'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':1']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[1]->getId()); + } + } + + public function testTagMinMax() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + $posts[0]->setTags(TagModel::spawnFromNames(['tag1', 'tag2', 'tag3'])); + $posts[1]->setTags(TagModel::spawnFromNames(['tag1'])); + $posts[2]->setTags(TagModel::spawnFromNames(['tag1', 'tag2'])); + PostModel::save($posts); + + foreach (['tagmin', 'tag_min'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':2']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + + foreach (['tagmax', 'tag_max'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':2']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[1]->getId()); + } + } + + public function testDateMinMax() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + $posts[0]->setCreationTime(mktime(0, 0, 0, 10, 23, 1990)); + $posts[1]->setCreationTime(mktime(0, 0, 0, 10, 22, 1990)); + $posts[2]->setCreationTime(mktime(0, 0, 0, 10, 21, 1990)); + PostModel::save($posts); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'date:1990-10-22']); + }); + + $this->assert->areEqual(1, $ret->entityCount); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[0]->getId()); + + foreach (['datemin', 'date_min'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':1990-10-22']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + + foreach (['datemax', 'date_max'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':1990-10-22']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[1]->getId()); + } + } + + public function testInvalidSpecialToken() + { + $this->grantAccess('listPosts'); + + $this->assert->throws(function() + { + Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'special:nonsense']); + }, 'invalid'); + } + + public function testInvalidType() + { + $this->grantAccess('listPosts'); + + $this->assert->throws(function() + { + Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'type:nonsense']); + }, 'invalid'); + } + + public function testTooManyTokens() + { + $this->grantAccess('listPosts'); + + $this->assert->throws(function() + { + Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 't1 t2 t3 t4 t5 t6 t7']); + }, 'too many search tokens'); + } + + public function testTypes() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(4); + $posts[0]->setType(new PostType(PostType::Image)); + $posts[1]->setType(new PostType(PostType::Video)); + $posts[2]->setType(new PostType(PostType::Youtube)); + $posts[3]->setType(new PostType(PostType::Flash)); + PostModel::save($posts); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'type:video']); + }); + + $this->assert->areEqual(1, $ret->entityCount); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[0]->getId()); + + foreach (['flash', 'swf'] as $typeAlias) + { + $ret = $this->assert->doesNotThrow(function() use ($typeAlias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'type:' . $typeAlias]); + }); + + $this->assert->areEqual(1, $ret->entityCount); + $this->assert->areEqual($posts[3]->getId(), $ret->entities[0]->getId()); + } + + foreach (['img', 'image'] as $typeAlias) + { + $ret = $this->assert->doesNotThrow(function() use ($typeAlias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'type:' . $typeAlias]); + }); + + $this->assert->areEqual(1, $ret->entityCount); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[0]->getId()); + } + + foreach (['yt', 'youtube'] as $typeAlias) + { + $ret = $this->assert->doesNotThrow(function() use ($typeAlias) + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'type:' . $typeAlias]); + }); + + $this->assert->areEqual(1, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + } + } + + public function testMultipleTags() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + $posts[0]->setTags(TagModel::spawnFromNames(['tag1', 'tag2', 'tag3'])); + $posts[1]->setTags(TagModel::spawnFromNames(['tag1', 'tag2'])); + $posts[2]->setTags(TagModel::spawnFromNames(['tag1'])); + PostModel::save($posts); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'tag1 tag2']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + + public function testTagNegation() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + $posts[0]->setTags(TagModel::spawnFromNames(['tag1', 'tag2', 'tag3'])); + $posts[1]->setTags(TagModel::spawnFromNames(['tag1', 'tag2'])); + $posts[2]->setTags(TagModel::spawnFromNames(['tag1'])); + PostModel::save($posts); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'tag1 tag2 -tag3']); + }); + + $this->assert->areEqual(1, $ret->entityCount); + $this->assert->areEqual($posts[1]->getId(), $ret->entities[0]->getId()); + } +} diff --git a/tests/JobTests/ListRelatedTagsJobTest.php b/tests/JobTests/ListRelatedTagsJobTest.php new file mode 100644 index 00000000..c0bc6e72 --- /dev/null +++ b/tests/JobTests/ListRelatedTagsJobTest.php @@ -0,0 +1,63 @@ +grantAccess('listTags'); + $this->grantAccess('listPosts'); + + $tags = $this->tagMocker->mockMultiple(3); + getConfig()->browsing->tagsRelated = 1; + + $post = $this->postMocker->mockSingle(); + $post->setTags($tags); + PostModel::save($post); + + $ret = $this->assert->doesNotThrow(function() use ($tags) + { + return Api::run(new ListRelatedTagsJob(), [ + JobArgs::ARG_TAG_NAME => $tags[0]->getName()]); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual(1, count($ret->entities)); + $this->assert->areEqual(2, $ret->pageCount); + $this->assert->areEqual(1, $ret->page); + + $ret = $this->assert->doesNotThrow(function() use ($tags) + { + return Api::run(new ListRelatedTagsJob(), [ + JobArgs::ARG_TAG_NAME => $tags[0]->getName(), + JobArgs::ARG_PAGE_NUMBER => 2]); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual(1, count($ret->entities)); + $this->assert->areEqual(2, $ret->pageCount); + $this->assert->areEqual(2, $ret->page); + } + + public function testSimpleRelations() + { + $this->grantAccess('listTags'); + $this->grantAccess('listPosts'); + + $tags = $this->tagMocker->mockMultiple(3); + $post = $this->postMocker->mockSingle(); + $post->setTags($tags); + PostModel::save($post); + + $ret = $this->assert->doesNotThrow(function() use ($tags) + { + return Api::run(new ListRelatedTagsJob(), [ + JobArgs::ARG_TAG_NAME => $tags[0]->getName()]); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual(2, count($ret->entities)); + $this->assert->areEqual(1, $ret->pageCount); + $this->assert->areEqual(1, $ret->page); + $this->assert->areEqual($tags[1]->getName(), $ret->entities[1]->getName()); + $this->assert->areEqual($tags[2]->getName(), $ret->entities[0]->getName()); + } +} diff --git a/tests/JobTests/ListTagsJobTest.php b/tests/JobTests/ListTagsJobTest.php new file mode 100644 index 00000000..355db9d8 --- /dev/null +++ b/tests/JobTests/ListTagsJobTest.php @@ -0,0 +1,122 @@ +grantAccess('listTags'); + $this->grantAccess('listPosts'); + + $tags = $this->tagMocker->mockMultiple(3); + getConfig()->browsing->tagsPerPage = 2; + + $post = $this->postMocker->mockSingle(); + $post->setTags($tags); + PostModel::save($post); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListTagsJob(), []); + }); + + $this->assert->areEqual(3, $ret->entityCount); + $this->assert->areEqual(2, count($ret->entities)); + $this->assert->areEqual(2, $ret->pageCount); + $this->assert->areEqual(1, $ret->page); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListTagsJob(), [JobArgs::ARG_PAGE_NUMBER => 2]); + }); + + $this->assert->areEqual(3, $ret->entityCount); + $this->assert->areEqual(1, count($ret->entities)); + $this->assert->areEqual(2, $ret->pageCount); + $this->assert->areEqual(2, $ret->page); + } + + public function testOrderPopularity() + { + $this->grantAccess('listTags'); + $this->grantAccess('listPosts'); + $tags = $this->tagMocker->mockMultiple(2); + + $posts = $this->postMocker->mockMultiple(2); + $posts[0]->setTags([$tags[0]]); + $posts[1]->setTags($tags); + PostModel::save($posts); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListTagsJob, [JobArgs::ARG_QUERY => 'order:popularity']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($tags[0]->getName(), $ret->entities[0]->getName()); + $this->assert->areEqual($tags[1]->getName(), $ret->entities[1]->getName()); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListTagsJob, [JobArgs::ARG_QUERY => '-order:popularity']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($tags[1]->getName(), $ret->entities[0]->getName()); + $this->assert->areEqual($tags[0]->getName(), $ret->entities[1]->getName()); + } + + public function testOrderAlphanumeric() + { + $this->grantAccess('listTags'); + $this->grantAccess('listPosts'); + + $tags = $this->tagMocker->mockMultiple(2); + $tags[0]->setName('aaa'); + $tags[1]->setName('bbb'); + TagModel::save($tags); + + $post = $this->postMocker->mockSingle(); + $post->setTags($tags); + PostModel::save($post); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListTagsJob, [JobArgs::ARG_QUERY => 'order:alpha']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($tags[1]->getName(), $ret->entities[0]->getName()); + $this->assert->areEqual($tags[0]->getName(), $ret->entities[1]->getName()); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListTagsJob, [JobArgs::ARG_QUERY => '-order:alpha']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($tags[0]->getName(), $ret->entities[0]->getName()); + $this->assert->areEqual($tags[1]->getName(), $ret->entities[1]->getName()); + } + + public function testFilter() + { + $this->grantAccess('listTags'); + $this->grantAccess('listPosts'); + + $tags = $this->tagMocker->mockMultiple(2); + $tags[0]->setName('alice'); + $tags[1]->setName('bob'); + TagModel::save($tags); + + $post = $this->postMocker->mockSingle(); + $post->setTags($tags); + PostModel::save($post); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListTagsJob, [JobArgs::ARG_QUERY => 'LiC']); + }); + + $this->assert->areEqual(1, $ret->entityCount); + $this->assert->areEqual($tags[0]->getName(), $ret->entities[0]->getName()); + } +} diff --git a/tests/JobTests/ListUsersJobTest.php b/tests/JobTests/ListUsersJobTest.php new file mode 100644 index 00000000..9b5f0bbc --- /dev/null +++ b/tests/JobTests/ListUsersJobTest.php @@ -0,0 +1,90 @@ +grantAccess('listUsers'); + + $users = $this->userMocker->mockMultiple(3); + getConfig()->browsing->usersPerPage = 2; + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListUsersJob(), []); + }); + + $this->assert->areEqual(3, $ret->entityCount); + $this->assert->areEqual(2, count($ret->entities)); + $this->assert->areEqual(2, $ret->pageCount); + $this->assert->areEqual(1, $ret->page); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListUsersJob(), [JobArgs::ARG_PAGE_NUMBER => 2]); + }); + + $this->assert->areEqual(3, $ret->entityCount); + $this->assert->areEqual(1, count($ret->entities)); + $this->assert->areEqual(2, $ret->pageCount); + $this->assert->areEqual(2, $ret->page); + } + + public function testOrderDate() + { + $this->grantAccess('listUsers'); + + $users = $this->userMocker->mockMultiple(3); + $users[0]->setJoinTime(mktime(0, 0, 0, 10, 23, 1990)); + $users[1]->setJoinTime(mktime(0, 0, 0, 10, 22, 1990)); + $users[2]->setJoinTime(mktime(0, 0, 0, 10, 21, 1990)); + UserModel::save($users); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListUsersJob, [JobArgs::ARG_QUERY => 'order:date']); + }); + + $this->assert->areEqual(3, $ret->entityCount); + $this->assert->areEqual($users[0]->getName(), $ret->entities[0]->getName()); + $this->assert->areEqual($users[1]->getName(), $ret->entities[1]->getName()); + $this->assert->areEqual($users[2]->getName(), $ret->entities[2]->getName()); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListUsersJob, [JobArgs::ARG_QUERY => '-order:date']); + }); + + $this->assert->areEqual(3, $ret->entityCount); + $this->assert->areEqual($users[2]->getName(), $ret->entities[0]->getName()); + $this->assert->areEqual($users[1]->getName(), $ret->entities[1]->getName()); + $this->assert->areEqual($users[0]->getName(), $ret->entities[2]->getName()); + } + + public function testOrderAlphanumeric() + { + $this->grantAccess('listUsers'); + + $users = $this->userMocker->mockMultiple(2); + $users[0]->setName('alice'); + $users[1]->setName('bob'); + UserModel::save($users); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListUsersJob, [JobArgs::ARG_QUERY => 'order:alpha']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($users[1]->getName(), $ret->entities[0]->getName()); + $this->assert->areEqual($users[0]->getName(), $ret->entities[1]->getName()); + + $ret = $this->assert->doesNotThrow(function() + { + return Api::run(new ListUsersJob, [JobArgs::ARG_QUERY => '-order:alpha']); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($users[0]->getName(), $ret->entities[0]->getName()); + $this->assert->areEqual($users[1]->getName(), $ret->entities[1]->getName()); + } +} diff --git a/tests/JobTests/MergeTagsJobTest.php b/tests/JobTests/MergeTagsJobTest.php index 6b9db464..1449b1a8 100644 --- a/tests/JobTests/MergeTagsJobTest.php +++ b/tests/JobTests/MergeTagsJobTest.php @@ -5,24 +5,17 @@ class MergeTagsJobTest extends AbstractTest { $this->grantAccess('mergeTags'); - $sourceTag = $this->mockTag(); - $randomTag = $this->mockTag(); - $targetTag = $this->mockTag(); - - $posts = []; - foreach (range(0, 5) as $i) - $posts []= $this->mockPost($this->mockUser()); + list ($sourceTag, $randomTag, $targetTag) + = $this->tagMocker->mockMultiple(3); + $posts = $this->postMocker->mockMultiple(6); $posts[0]->setTags([$sourceTag]); $posts[1]->setTags([$randomTag]); $posts[2]->setTags([$sourceTag, $randomTag]); - $posts[3]->setTags([$sourceTag, $targetTag]); $posts[4]->setTags([$randomTag, $targetTag]); $posts[5]->setTags([$sourceTag, $randomTag, $targetTag]); - - foreach ($posts as $post) - PostModel::save($post); + PostModel::save($posts); $this->assert->doesNotThrow(function() use ($sourceTag, $targetTag) { @@ -37,21 +30,21 @@ class MergeTagsJobTest extends AbstractTest foreach ($posts as $k => $post) $posts[$k] = PostModel::getById($post->getId()); - $this->assertTagNames($posts[0], [$targetTag]); - $this->assertTagNames($posts[1], [$randomTag]); - $this->assertTagNames($posts[2], [$randomTag, $targetTag]); - - $this->assertTagNames($posts[3], [$targetTag]); - $this->assertTagNames($posts[4], [$randomTag, $targetTag]); - $this->assertTagNames($posts[5], [$randomTag, $targetTag]); + $this->testSupport->assertTagNames($posts[0], [$targetTag]); + $this->testSupport->assertTagNames($posts[1], [$randomTag]); + $this->testSupport->assertTagNames($posts[2], [$randomTag, $targetTag]); + $this->testSupport->assertTagNames($posts[3], [$targetTag]); + $this->testSupport->assertTagNames($posts[4], [$randomTag, $targetTag]); + $this->testSupport->assertTagNames($posts[5], [$randomTag, $targetTag]); } public function testMergingToNonExistingTag() { $this->grantAccess('mergeTags'); - $sourceTag = $this->mockTag(); - $post = $this->mockPost($this->mockUser()); + $sourceTag = $this->tagMocker->mockSingle(); + + $post = $this->postMocker->mockSingle(); $post->setTags([$sourceTag]); PostModel::save($post); @@ -70,8 +63,9 @@ class MergeTagsJobTest extends AbstractTest { $this->grantAccess('mergeTags'); - $sourceTag = $this->mockTag(); - $post = $this->mockPost($this->mockUser()); + $sourceTag = $this->tagMocker->mockSingle(); + + $post = $this->postMocker->mockSingle(); $post->setTags([$sourceTag]); PostModel::save($post); @@ -85,23 +79,4 @@ class MergeTagsJobTest extends AbstractTest ]); }, 'Source and target tag are the same'); } - - private function assertTagNames($post, $tags) - { - $tagNames = $this->getTagNames($tags); - $postTagNames = $this->getTagNames($post->getTags()); - $this->assert->areEquivalent($tagNames, $postTagNames); - } - - private function getTagNames($tags) - { - $tagNames = array_map( - function($tag) - { - return $tag->getName(); - }, $tags); - natcasesort($tagNames); - $tagNames = array_values($tagNames); - return $tagNames; - } } diff --git a/tests/JobTests/PasswordResetJobTest.php b/tests/JobTests/PasswordResetJobTest.php index b85d940d..20d01faf 100644 --- a/tests/JobTests/PasswordResetJobTest.php +++ b/tests/JobTests/PasswordResetJobTest.php @@ -6,7 +6,7 @@ class PasswordResetJobTest extends AbstractTest getConfig()->registration->needEmailForRegistering = true; Mailer::mockSending(); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setUnconfirmedEmail('godzilla@whitestar.gov'); UserModel::save($user); @@ -25,7 +25,7 @@ class PasswordResetJobTest extends AbstractTest getConfig()->registration->needEmailForRegistering = true; Mailer::mockSending(); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setConfirmedEmail('godzilla@whitestar.gov'); UserModel::save($user); @@ -57,7 +57,7 @@ class PasswordResetJobTest extends AbstractTest getConfig()->registration->needEmailForRegistering = true; Mailer::mockSending(); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setConfirmedEmail('godzilla@whitestar.gov'); UserModel::save($user); @@ -97,7 +97,7 @@ class PasswordResetJobTest extends AbstractTest getConfig()->registration->needEmailForRegistering = true; Mailer::mockSending(); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setConfirmedEmail('godzilla@whitestar.gov'); UserModel::save($user); diff --git a/tests/JobTests/PreviewCommentJobTest.php b/tests/JobTests/PreviewCommentJobTest.php index b77f786d..30d8e864 100644 --- a/tests/JobTests/PreviewCommentJobTest.php +++ b/tests/JobTests/PreviewCommentJobTest.php @@ -60,7 +60,7 @@ class PreviewCommentJobTest extends AbstractTest public function testViaPost() { $this->prepare(); - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $this->assert->doesNotThrow(function() use ($post) { @@ -76,7 +76,9 @@ class PreviewCommentJobTest extends AbstractTest public function testViaComment() { $this->prepare(); - $comment = $this->mockComment(Auth::getCurrentUser()); + $comment = $this->commentMocker->mockSingle(); + $comment->setCommenter(Auth::getCurrentUser()); + CommentModel::save($comment); $this->assert->doesNotThrow(function() use ($comment) { @@ -92,7 +94,7 @@ class PreviewCommentJobTest extends AbstractTest protected function runApi($text) { - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); return Api::run( new PreviewCommentJob(), @@ -106,6 +108,6 @@ class PreviewCommentJobTest extends AbstractTest { getConfig()->registration->needEmailForCommenting = false; $this->grantAccess('addComment'); - $this->login($this->mockUser()); + $this->login($this->userMocker->mockSingle()); } } diff --git a/tests/JobTests/RenameTagsJobTest.php b/tests/JobTests/RenameTagsJobTest.php index 4c321772..5c1da73c 100644 --- a/tests/JobTests/RenameTagsJobTest.php +++ b/tests/JobTests/RenameTagsJobTest.php @@ -5,20 +5,14 @@ class RenameTagsJobTest extends AbstractTest { $this->grantAccess('renameTags'); - $sourceTag = $this->mockTag(); - $randomTag = $this->mockTag(); - $targetTag = $this->mockTag(); - - $posts = []; - foreach (range(0, 2) as $i) - $posts []= $this->mockPost($this->mockUser()); + list ($sourceTag, $randomTag, $targetTag) + = $this->tagMocker->mockMultiple(3); + $posts = $this->postMocker->mockMultiple(3); $posts[0]->setTags([$sourceTag]); $posts[1]->setTags([$randomTag]); $posts[2]->setTags([$sourceTag, $randomTag]); - - foreach ($posts as $post) - PostModel::save($post); + PostModel::save($posts); $this->assert->doesNotThrow(function() use ($sourceTag, $targetTag) { @@ -33,23 +27,20 @@ class RenameTagsJobTest extends AbstractTest foreach ($posts as $k => $post) $posts[$k] = PostModel::getById($post->getId()); - $this->assertTagNames($posts[0], [$targetTag]); - $this->assertTagNames($posts[1], [$randomTag]); - $this->assertTagNames($posts[2], [$randomTag, $targetTag]); + $this->testSupport->assertTagNames($posts[0], [$targetTag]); + $this->testSupport->assertTagNames($posts[1], [$randomTag]); + $this->testSupport->assertTagNames($posts[2], [$randomTag, $targetTag]); } public function testRenamingToExistingTag() { $this->grantAccess('renameTags'); - $sourceTag = $this->mockTag(); - $post = $this->mockPost($this->mockUser()); - $post->setTags([$sourceTag]); - PostModel::save($post); + list ($sourceTag, $targetTag) + = $this->tagMocker->mockMultiple(2); - $targetTag = $this->mockTag(); - $post = $this->mockPost($this->mockUser()); - $post->setTags([$targetTag]); + $post = $this->postMocker->mockSingle(); + $post->setTags([$sourceTag, $targetTag]); PostModel::save($post); $this->assert->throws(function() use ($sourceTag, $targetTag) @@ -67,8 +58,9 @@ class RenameTagsJobTest extends AbstractTest { $this->grantAccess('renameTags'); - $sourceTag = $this->mockTag(); - $post = $this->mockPost($this->mockUser()); + $sourceTag = $this->tagMocker->mockSingle(); + + $post = $this->postMocker->mockSingle(); $post->setTags([$sourceTag]); PostModel::save($post); @@ -82,23 +74,4 @@ class RenameTagsJobTest extends AbstractTest ]); }, 'Source and target tag are the same'); } - - private function assertTagNames($post, $tags) - { - $tagNames = $this->getTagNames($tags); - $postTagNames = $this->getTagNames($post->getTags()); - $this->assert->areEquivalent($tagNames, $postTagNames); - } - - private function getTagNames($tags) - { - $tagNames = array_map( - function($tag) - { - return $tag->getName(); - }, $tags); - natcasesort($tagNames); - $tagNames = array_values($tagNames); - return $tagNames; - } } diff --git a/tests/JobTests/ScorePostJobTest.php b/tests/JobTests/ScorePostJobTest.php index 2a7415f3..2fb16fbc 100644 --- a/tests/JobTests/ScorePostJobTest.php +++ b/tests/JobTests/ScorePostJobTest.php @@ -3,10 +3,8 @@ class ScorePostJobTest extends AbstractTest { public function testScoring() { - $this->grantAccess('scorePost'); - $this->login($this->mockUser()); - $post = $this->mockPost(Auth::getCurrentUser()); - + $this->loginRandomPerson(); + $post = $this->postMocker->mockSingle(); $this->assert->areEqual(0, $post->getScore()); $post = $this->assert->doesNotThrow(function() use ($post) @@ -24,10 +22,8 @@ class ScorePostJobTest extends AbstractTest public function testNegativeScore() { - $this->grantAccess('scorePost'); - $this->login($this->mockUser()); - $post = $this->mockPost(Auth::getCurrentUser()); - + $this->loginRandomPerson(); + $post = $this->postMocker->mockSingle(); $post = $this->assert->doesNotThrow(function() use ($post) { return Api::run( @@ -43,10 +39,8 @@ class ScorePostJobTest extends AbstractTest public function testInvalidScore() { - $this->grantAccess('scorePost'); - $this->login($this->mockUser()); - $post = $this->mockPost(Auth::getCurrentUser()); - + $this->loginRandomPerson(); + $post = $this->postMocker->mockSingle(); $this->assert->throws(function() use ($post) { Api::run( @@ -62,10 +56,8 @@ class ScorePostJobTest extends AbstractTest public function testScoreOverwriting() { - $this->grantAccess('scorePost'); - $this->login($this->mockUser()); - $post = $this->mockPost(Auth::getCurrentUser()); - + $this->loginRandomPerson(); + $post = $this->postMocker->mockSingle(); $post = $this->assert->doesNotThrow(function() use ($post) { return Api::run( @@ -91,10 +83,8 @@ class ScorePostJobTest extends AbstractTest public function testScoreTwoPeople() { - $this->grantAccess('scorePost'); - $this->login($this->mockUser()); - $post = $this->mockPost(Auth::getCurrentUser()); - + $this->loginRandomPerson(); + $post = $this->postMocker->mockSingle(); $post = $this->assert->doesNotThrow(function() use ($post) { return Api::run( @@ -105,8 +95,7 @@ class ScorePostJobTest extends AbstractTest ]); }); - $this->login($this->mockUser()); - + $this->loginRandomPerson(); $post = $this->assert->doesNotThrow(function() use ($post) { return Api::run( @@ -119,4 +108,10 @@ class ScorePostJobTest extends AbstractTest $this->assert->areEqual(2, $post->getScore()); } + + private function loginRandomPerson() + { + $this->grantAccess('scorePost'); + $this->login($this->userMocker->mockSingle()); + } } diff --git a/tests/JobTests/TogglePostFavoriteJobTest.php b/tests/JobTests/TogglePostFavoriteJobTest.php index fccd90af..9be0f4ac 100644 --- a/tests/JobTests/TogglePostFavoriteJobTest.php +++ b/tests/JobTests/TogglePostFavoriteJobTest.php @@ -4,9 +4,9 @@ class TogglePostFavoriteJobTest extends AbstractTest public function testFaving() { $this->grantAccess('favoritePost'); - $user = $this->mockUser(); + $post = $this->postMocker->mockSingle(); + $user = $this->userMocker->mockSingle(); $this->login($user); - $post = $this->mockPost($user); $this->assert->areEqual(0, $post->getScore()); $this->assert->areEqual(0, $post->getFavoriteCount()); @@ -31,9 +31,9 @@ class TogglePostFavoriteJobTest extends AbstractTest public function testDefaving() { $this->grantAccess('favoritePost'); - $user = $this->mockUser(); + $post = $this->postMocker->mockSingle(); + $user = $this->userMocker->mockSingle(); $this->login($user); - $post = $this->mockPost($user); $this->assert->areEqual(0, $post->getScore()); $this->assert->areEqual(0, $post->getFavoriteCount()); @@ -68,14 +68,13 @@ class TogglePostFavoriteJobTest extends AbstractTest public function testFavingTwoPeople() { $this->grantAccess('favoritePost'); - $user1 = $this->mockUser(); - $user2 = $this->mockUser(); - $post = $this->mockPost($user1); + $users = $this->userMocker->mockMultiple(2); + $post = $this->postMocker->mockSingle(); $this->assert->areEqual(0, $post->getScore()); $this->assert->areEqual(0, $post->getFavoriteCount()); - $this->login($user1); + $this->login($users[0]); $post = $this->assert->doesNotThrow(function() use ($post) { return Api::run( @@ -86,7 +85,7 @@ class TogglePostFavoriteJobTest extends AbstractTest ]); }); - $this->login($user2); + $this->login($users[1]); $post = $this->assert->doesNotThrow(function() use ($post) { return Api::run( @@ -99,11 +98,11 @@ class TogglePostFavoriteJobTest extends AbstractTest $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)); + $this->assert->areEqual(1, $users[0]->getFavoriteCount()); + $this->assert->areEqual(1, $users[0]->getScore($post)); + $this->assert->areEqual(1, $users[1]->getFavoriteCount()); + $this->assert->areEqual(1, $users[1]->getScore($post)); + $this->assert->areEqual(true, $users[0]->hasFavorited($post)); + $this->assert->areEqual(true, $users[1]->hasFavorited($post)); } } diff --git a/tests/JobTests/TogglePostTagJobTest.php b/tests/JobTests/TogglePostTagJobTest.php index f755211d..5f21e01a 100644 --- a/tests/JobTests/TogglePostTagJobTest.php +++ b/tests/JobTests/TogglePostTagJobTest.php @@ -124,7 +124,7 @@ class TogglePostTagJobTest extends AbstractTest private function preparePost(array $tagNames) { $this->grantAccess('editPostTags'); - $post = $this->mockPost($this->mockUser()); + $post = $this->postMocker->mockSingle(); $post->setTags(TagModel::spawnFromNames($tagNames)); PostModel::save($post); return $post; diff --git a/tests/JobTests/TogglePostVisibilityJobTest.php b/tests/JobTests/TogglePostVisibilityJobTest.php index 14f4bb2b..05ecf195 100644 --- a/tests/JobTests/TogglePostVisibilityJobTest.php +++ b/tests/JobTests/TogglePostVisibilityJobTest.php @@ -4,8 +4,8 @@ class TogglePostVisibilityJobTest extends AbstractTest public function testHiding() { $this->grantAccess('hidePost'); - $this->login($this->mockUser()); - $post = $this->mockPost(Auth::getCurrentUser()); + $this->login($this->userMocker->mockSingle()); + $post = $this->postMocker->mockSingle(); $this->assert->isFalse($post->isHidden()); @@ -25,8 +25,8 @@ class TogglePostVisibilityJobTest extends AbstractTest public function testShowing() { $this->grantAccess('hidePost'); - $this->login($this->mockUser()); - $post = $this->mockPost(Auth::getCurrentUser()); + $this->login($this->userMocker->mockSingle()); + $post = $this->postMocker->mockSingle(); $this->assert->isFalse($post->isHidden()); diff --git a/tests/JobTests/ToggleUserBanJobTest.php b/tests/JobTests/ToggleUserBanJobTest.php index 4681c78d..79f48d4c 100644 --- a/tests/JobTests/ToggleUserBanJobTest.php +++ b/tests/JobTests/ToggleUserBanJobTest.php @@ -4,7 +4,7 @@ class ToggleUserBanJobTest extends AbstractTest public function testBanning() { $this->grantAccess('banUser'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $this->login($user); $this->assert->isFalse($user->isBanned()); @@ -25,7 +25,7 @@ class ToggleUserBanJobTest extends AbstractTest public function testUnbanning() { $this->grantAccess('banUser'); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $this->login($user); $this->assert->isFalse($user->isBanned()); diff --git a/tests/MiscTests/AccessTest.php b/tests/MiscTests/AccessTest.php index bc9162a4..379317f1 100644 --- a/tests/MiscTests/AccessTest.php +++ b/tests/MiscTests/AccessTest.php @@ -9,7 +9,7 @@ class AccessTest extends AbstractTest public function testAccessRanks1() { - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setAccessRank(new AccessRank(AccessRank::Admin)); $this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user)); @@ -31,7 +31,7 @@ class AccessTest extends AbstractTest getConfig()->privileges->listPosts = 'power-user'; Access::init(); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setAccessRank(new AccessRank(AccessRank::Admin)); $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user)); @@ -53,7 +53,7 @@ class AccessTest extends AbstractTest getConfig()->privileges->{'listPosts.own'} = 'power-user'; Access::init(); - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setAccessRank(new AccessRank(AccessRank::PowerUser)); $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user)); $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user)); @@ -78,7 +78,7 @@ class AccessTest extends AbstractTest protected function testSubPrivilegesSubAndGeneral() { - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setAccessRank(new AccessRank(AccessRank::PowerUser)); $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user)); $this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts, 'baka'), $user)); @@ -110,7 +110,7 @@ class AccessTest extends AbstractTest protected function testSubPrivilegesMultipleSubAndGeneral() { - $user = $this->mockUser(); + $user = $this->userMocker->mockSingle(); $user->setAccessRank(new AccessRank(AccessRank::PowerUser)); $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'own'), $user)); $this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user)); diff --git a/tests/Mockers/AbstractMocker.php b/tests/Mockers/AbstractMocker.php new file mode 100644 index 00000000..cd17550f --- /dev/null +++ b/tests/Mockers/AbstractMocker.php @@ -0,0 +1,16 @@ +mockSingle(); + } + return $ret; + }); + } +} diff --git a/tests/Mockers/CommentMocker.php b/tests/Mockers/CommentMocker.php new file mode 100644 index 00000000..15d59068 --- /dev/null +++ b/tests/Mockers/CommentMocker.php @@ -0,0 +1,19 @@ +postMocker = $postMocker; + } + + public function mockSingle() + { + $post = $this->postMocker->mockSingle(); + $comment = CommentModel::spawn(); + $comment->setPost($post); + $comment->setText('test test'); + return CommentModel::save($comment); + } +} diff --git a/tests/Mockers/IMocker.php b/tests/Mockers/IMocker.php new file mode 100644 index 00000000..a2380e31 --- /dev/null +++ b/tests/Mockers/IMocker.php @@ -0,0 +1,6 @@ +testSupport = $testSupport; + $this->tagMocker = $tagMocker; + } + + public function mockSingle() + { + $post = PostModel::spawn(); + #$post->setUploader($owner); + $post->setType(new PostType(PostType::Image)); + $post->setTags([$this->tagMocker->mockSingle()]); + copy($this->testSupport->getPath('image.jpg'), $post->getFullPath()); + return PostModel::save($post); + } +} diff --git a/tests/Mockers/TagMocker.php b/tests/Mockers/TagMocker.php new file mode 100644 index 00000000..1f38b25e --- /dev/null +++ b/tests/Mockers/TagMocker.php @@ -0,0 +1,10 @@ +setName(uniqid()); + return TagModel::save($tag); + } +} diff --git a/tests/Mockers/UserMocker.php b/tests/Mockers/UserMocker.php new file mode 100644 index 00000000..0605f5c7 --- /dev/null +++ b/tests/Mockers/UserMocker.php @@ -0,0 +1,12 @@ +setAccessRank(new AccessRank(AccessRank::Registered)); + $user->setName('dummy'.uniqid()); + $user->setPassword('sekai'); + return UserModel::save($user); + } +} diff --git a/tests/ModelTests/PostModelTest.php b/tests/ModelTests/PostModelTest.php index dd5183f0..60d97d91 100644 --- a/tests/ModelTests/PostModelTest.php +++ b/tests/ModelTests/PostModelTest.php @@ -25,7 +25,7 @@ class PostModelTest extends AbstractTest public function testFeaturingRandomPost() { - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); PostModel::featureRandomPost(); @@ -35,16 +35,14 @@ class PostModelTest extends AbstractTest public function testFeaturingIllegalPosts() { $posts = []; - foreach (range(0, 5) as $i) - $posts []= $this->mockPost(Auth::getCurrentUser()); + $posts = $this->postMocker->mockMultiple(6); $posts[0]->setSafety(new PostSafety(PostSafety::Sketchy)); $posts[1]->setSafety(new PostSafety(PostSafety::Sketchy)); $posts[2]->setHidden(true); $posts[3]->setType(new PostType(PostType::Youtube)); $posts[4]->setType(new PostType(PostType::Flash)); $posts[5]->setType(new PostType(PostType::Video)); - foreach ($posts as $post) - PostModel::save($post); + PostModel::save($posts); PostModel::featureRandomPost(); @@ -53,7 +51,7 @@ class PostModelTest extends AbstractTest public function testAutoFeaturingFirstTime() { - $this->mockPost(Auth::getCurrentUser()); + $this->postMocker->mockSingle(); $this->assert->doesNotThrow(function() { @@ -65,7 +63,7 @@ class PostModelTest extends AbstractTest public function testAutoFeaturingTooSoon() { - $this->mockPost(Auth::getCurrentUser()); + $this->postMocker->mockSingle(); $this->assert->isTrue(PostModel::featureRandomPostIfNecessary()); $this->assert->isFalse(PostModel::featureRandomPostIfNecessary()); @@ -75,7 +73,7 @@ class PostModelTest extends AbstractTest public function testAutoFeaturingOutdated() { - $post = $this->mockPost(Auth::getCurrentUser()); + $post = $this->postMocker->mockSingle(); $minTimestamp = getConfig()->misc->featuredPostMaxDays * 24 * 3600; $this->assert->isTrue(PostModel::featureRandomPostIfNecessary()); @@ -89,14 +87,14 @@ class PostModelTest extends AbstractTest public function testAutoFeaturingDeletedPost() { - $post = $this->mockPost(Auth::getCurrentUser()); - + $post = $this->postMocker->mockSingle(); $this->assert->isTrue(PostModel::featureRandomPostIfNecessary()); $this->assert->isNotNull(PostModel::getFeaturedPost()); - PostModel::remove($post); - $anotherPost = $this->mockPost(Auth::getCurrentUser()); - $this->assert->isTrue(PostModel::featureRandomPostIfNecessary()); + PostModel::remove($post); + + $anotherPost = $this->postMocker->mockSingle(); + $this->assert->isTrue(PostModel::featureRandomPostIfNecessary()); $this->assert->isNotNull(PostModel::getFeaturedPost()); } } diff --git a/tests/ModelTests/UserModelTest.php b/tests/ModelTests/UserModelTest.php index b62c714f..4620cb4d 100644 --- a/tests/ModelTests/UserModelTest.php +++ b/tests/ModelTests/UserModelTest.php @@ -90,8 +90,7 @@ class UserModelTest extends AbstractTest private function prepareTwoUsersWithSameName() { - $user1 = $this->mockUser(); - $user2 = $this->mockUser(); + list ($user1, $user2) = $this->userMocker->mockMultiple(2); $user1->setName('pikachu'); $user2->setName('pikachu'); UserModel::save($user1); @@ -100,8 +99,7 @@ class UserModelTest extends AbstractTest private function prepareTwoUsersWithSameEmail($confirmFirst, $confirmSecond) { - $user1 = $this->mockUser(); - $user2 = $this->mockUser(); + list ($user1, $user2) = $this->userMocker->mockMultiple(2); $mail = 'godzilla@whitestar.gov'; if ($confirmFirst) diff --git a/tests/TestSupport.php b/tests/TestSupport.php new file mode 100644 index 00000000..d23662a2 --- /dev/null +++ b/tests/TestSupport.php @@ -0,0 +1,34 @@ +assert = $assert; + } + + public function getPath($assetName) + { + return getConfig()->rootDir . DS . 'tests' . DS . 'TestFiles' . DS . $assetName; + } + + public function assertTagNames($post, $tags) + { + $tagNames = $this->getTagNames($tags); + $postTagNames = $this->getTagNames($post->getTags()); + $this->assert->areEquivalent($tagNames, $postTagNames); + } + + public function getTagNames(array $tags) + { + $tagNames = array_map( + function($tag) + { + return $tag->getName(); + }, $tags); + natcasesort($tagNames); + $tagNames = array_values($tagNames); + return $tagNames; + } +}