Added more unit tests, refactored test support

This commit is contained in:
Marcin Kurczewski 2014-05-13 21:08:07 +02:00
parent 6ce47ec2a7
commit bca92f1f71
68 changed files with 1595 additions and 339 deletions

View file

@ -29,12 +29,54 @@ abstract class AbstractCrudModel implements IModel
public static function remove($entities) 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');
} }

View file

@ -9,7 +9,7 @@ final class CommentModel extends AbstractCrudModel
return 'comment'; return 'comment';
} }
public static function save($comment) protected static function saveSingle($comment)
{ {
$comment->validate(); $comment->validate();
$comment->getPost()->removeCache('comment_count'); $comment->getPost()->removeCache('comment_count');
@ -37,7 +37,7 @@ final class CommentModel extends AbstractCrudModel
return $comment; return $comment;
} }
public static function remove($comment) protected static function removeSingle($comment)
{ {
Database::transaction(function() use ($comment) Database::transaction(function() use ($comment)
{ {

View file

@ -55,9 +55,11 @@ final class TagEntity extends AbstractEntity implements IValidatable
return $this->getCache('post_count'); return $this->getCache('post_count');
$stmt = new Sql\SelectStatement(); $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->setTable('post_tag');
$stmt->setCriterion(new Sql\EqualsFunctor('tag_id', new Sql\Binding($this->getId()))); $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');
} }
} }

View file

@ -9,7 +9,7 @@ final class PostModel extends AbstractCrudModel
return 'post'; return 'post';
} }
public static function save($post) protected static function saveSingle($post)
{ {
$post->validate(); $post->validate();
@ -83,7 +83,7 @@ final class PostModel extends AbstractCrudModel
return $post; return $post;
} }
public static function remove($post) protected static function removeSingle($post)
{ {
Database::transaction(function() use ($post) Database::transaction(function() use ($post)
{ {

View file

@ -9,7 +9,7 @@ final class TagModel extends AbstractCrudModel
return 'tag'; return 'tag';
} }
public static function save($tag) protected static function saveSingle($tag)
{ {
$tag->validate(); $tag->validate();
@ -28,7 +28,7 @@ final class TagModel extends AbstractCrudModel
return $tag; return $tag;
} }
public static function remove($tag) protected static function removeSingle($tag)
{ {
$binding = new Sql\Binding($tag->getId()); $binding = new Sql\Binding($tag->getId());

View file

@ -9,7 +9,7 @@ final class TokenModel extends AbstractCrudModel
return 'user_token'; return 'user_token';
} }
public static function save($token) protected static function saveSingle($token)
{ {
$token->validate(); $token->validate();

View file

@ -14,7 +14,7 @@ final class UserModel extends AbstractCrudModel
return 'user'; return 'user';
} }
public static function save($user) protected static function saveSingle($user)
{ {
$user->validate(); $user->validate();
@ -49,7 +49,7 @@ final class UserModel extends AbstractCrudModel
return $user; return $user;
} }
public static function remove($user) protected static function removeSingle($user)
{ {
Database::transaction(function() use ($user) Database::transaction(function() use ($user)
{ {

View file

@ -2,10 +2,20 @@
class AbstractTest class AbstractTest
{ {
public $assert; public $assert;
protected $postMocker;
protected $tagMocker;
protected $userMocker;
protected $commentMocker;
protected $testSupport;
public function __construct() public function __construct()
{ {
$this->assert = new Assert(); $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() 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) protected function login($user)
{ {
Auth::setCurrentUser($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) protected function grantAccess($privilege)
{ {
getConfig()->privileges->$privilege = 'anonymous'; getConfig()->privileges->$privilege = 'anonymous';
@ -68,9 +42,4 @@ class AbstractTest
getConfig()->privileges->$privilege = 'nobody'; getConfig()->privileges->$privilege = 'nobody';
Access::init(); Access::init();
} }
protected function getPath($name)
{
return getConfig()->rootDir . DS . 'tests' . DS . 'TestFiles' . DS . $name;
}
} }

View file

@ -60,7 +60,7 @@ class ApiAuthTest extends AbstractFullApiTest
getConfig()->registration->needEmailForCommenting = false; getConfig()->registration->needEmailForCommenting = false;
$this->grantAccess('addComment'); $this->grantAccess('addComment');
$comment = $this->mockComment(Auth::getCurrentUser()); $comment = $this->commentMocker->mockSingle();
$this->assert->throws(function() use ($comment) $this->assert->throws(function() use ($comment)
{ {

View file

@ -88,11 +88,11 @@ class ApiEmailRequirementsTest extends AbstractFullApiTest
public function testEnforcing() public function testEnforcing()
{ {
$this->grantAccess('addComment'); $this->grantAccess('addComment');
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
getConfig()->registration->needEmailForCommenting = true; getConfig()->registration->needEmailForCommenting = true;
$this->assert->throws(function() $this->assert->throws(function()
{ {
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
return Api::run( return Api::run(
new AddCommentJob(), new AddCommentJob(),

View file

@ -38,7 +38,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest
public function testDynamicPostPrivileges() public function testDynamicPostPrivileges()
{ {
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
$this->testDynamicPostPrivilege(new DeletePostJob(), new Privilege(Privilege::DeletePost)); $this->testDynamicPostPrivilege(new DeletePostJob(), new Privilege(Privilege::DeletePost));
$this->testDynamicPostPrivilege(new EditPostJob(), new Privilege(Privilege::EditPost)); $this->testDynamicPostPrivilege(new EditPostJob(), new Privilege(Privilege::EditPost));
@ -74,8 +74,10 @@ class ApiPrivilegeTest extends AbstractFullApiTest
{ {
$this->testedJobs []= $job; $this->testedJobs []= $job;
$ownPost = $this->mockPost(Auth::getCurrentUser()); list ($ownPost, $otherPost) = $this->postMocker->mockMultiple(2);
$otherPost = $this->mockPost($this->mockUser()); $ownPost->setUploader(Auth::getCurrentUser());
$otherPost->setUploader($this->userMocker->mockSingle());
PostModel::save([$ownPost, $otherPost]);
$expectedPrivilege->secondary = 'all'; $expectedPrivilege->secondary = 'all';
$job->setArgument(JobArgs::ARG_POST_ID, $otherPost->getId()); $job->setArgument(JobArgs::ARG_POST_ID, $otherPost->getId());
@ -96,7 +98,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest
new GetPostContentJob(), new GetPostContentJob(),
]; ];
$post = $this->mockPost($this->mockUser()); $post = $this->postMocker->mockSingle();
foreach ($jobs as $job) foreach ($jobs as $job)
{ {
@ -123,7 +125,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest
public function testDynamicUserPrivileges() public function testDynamicUserPrivileges()
{ {
$ownUser = $this->mockUser(); $ownUser = $this->userMocker->mockSingle();
$this->login($ownUser); $this->login($ownUser);
$this->testDynamicUserPrivilege(new DeleteUserJob(), new Privilege(Privilege::DeleteUser)); $this->testDynamicUserPrivilege(new DeleteUserJob(), new Privilege(Privilege::DeleteUser));
@ -140,7 +142,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest
{ {
$ownUser = Auth::getCurrentUser(); $ownUser = Auth::getCurrentUser();
$otherUser = $this->mockUser(); $otherUser = $this->userMocker->mockSingle();
$otherUser->setName('dummy' . uniqid()); $otherUser->setName('dummy' . uniqid());
UserModel::save($otherUser); UserModel::save($otherUser);
@ -159,7 +161,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest
public function testDynamicCommentPrivileges() public function testDynamicCommentPrivileges()
{ {
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
$this->testDynamicCommentPrivilege(new DeleteCommentJob(), new Privilege(Privilege::DeleteComment)); $this->testDynamicCommentPrivilege(new DeleteCommentJob(), new Privilege(Privilege::DeleteComment));
$this->testDynamicCommentPrivilege(new EditCommentJob(), new Privilege(Privilege::EditComment)); $this->testDynamicCommentPrivilege(new EditCommentJob(), new Privilege(Privilege::EditComment));
@ -167,8 +169,10 @@ class ApiPrivilegeTest extends AbstractFullApiTest
protected function testDynamicCommentPrivilege($job, $expectedPrivilege) protected function testDynamicCommentPrivilege($job, $expectedPrivilege)
{ {
$ownComment = $this->mockComment(Auth::getCurrentUser()); list ($ownComment, $otherComment) = $this->commentMocker->mockMultiple(2);
$otherComment = $this->mockComment($this->mockUser()); $ownComment->setCommenter(Auth::getCurrentUser());
$otherComment->setCommenter($this->userMocker->mockSingle());
CommentModel::save([$ownComment, $otherComment]);
$this->testedJobs []= $job; $this->testedJobs []= $job;
@ -187,7 +191,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest
{ {
$this->assert->throws(function() $this->assert->throws(function()
{ {
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
getConfig()->registration->needEmailForCommenting = false; getConfig()->registration->needEmailForCommenting = false;
return Api::run( return Api::run(
new AddCommentJob(), new AddCommentJob(),

View file

@ -75,7 +75,7 @@ class CommentRetrieverTest extends AbstractTest
private function prepareComment() private function prepareComment()
{ {
return $this->mockComment($this->mockUser()); return $this->commentMocker->mockSingle();
} }
private function prepareRetriever() private function prepareRetriever()

View file

@ -84,7 +84,7 @@ class PostRetrieverTest extends AbstractTest
private function preparePost() private function preparePost()
{ {
return $this->mockPost($this->mockUser()); return $this->postMocker->mockSingle();
} }
private function prepareRetriever() private function prepareRetriever()

View file

@ -83,7 +83,7 @@ class SafePostRetrieverTest extends AbstractTest
private function preparePost() private function preparePost()
{ {
return $this->mockPost($this->mockUser()); return $this->postMocker->mockSingle();
} }
private function prepareRetriever() private function prepareRetriever()

View file

@ -84,7 +84,7 @@ class UserRetrieverTest extends AbstractTest
private function prepareUser() private function prepareUser()
{ {
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setConfirmedEmail('godzilla@whitestar.gov'); $user->setConfirmedEmail('godzilla@whitestar.gov');
UserModel::save($user); UserModel::save($user);
return $user; return $user;

View file

@ -5,7 +5,7 @@ class AcceptUserRegistrationJobTest extends AbstractTest
{ {
$this->grantAccess('acceptUserRegistration'); $this->grantAccess('acceptUserRegistration');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$this->assert->isFalse($user->isStaffConfirmed()); $this->assert->isFalse($user->isStaffConfirmed());
$user = $this->assert->doesNotThrow(function() use ($user) $user = $this->assert->doesNotThrow(function() use ($user)

View file

@ -6,7 +6,7 @@ class ActivateUserEmailJobTest extends AbstractTest
getConfig()->registration->needEmailForRegistering = true; getConfig()->registration->needEmailForRegistering = true;
Mailer::mockSending(); Mailer::mockSending();
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setUnconfirmedEmail('godzilla@whitestar.gov'); $user->setUnconfirmedEmail('godzilla@whitestar.gov');
UserModel::save($user); UserModel::save($user);
@ -38,7 +38,7 @@ class ActivateUserEmailJobTest extends AbstractTest
getConfig()->registration->needEmailForRegistering = true; getConfig()->registration->needEmailForRegistering = true;
Mailer::mockSending(); Mailer::mockSending();
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setUnconfirmedEmail('godzilla@whitestar.gov'); $user->setUnconfirmedEmail('godzilla@whitestar.gov');
UserModel::save($user); UserModel::save($user);
@ -77,7 +77,7 @@ class ActivateUserEmailJobTest extends AbstractTest
getConfig()->registration->needEmailForRegistering = true; getConfig()->registration->needEmailForRegistering = true;
Mailer::mockSending(); Mailer::mockSending();
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setUnconfirmedEmail('godzilla@whitestar.gov'); $user->setUnconfirmedEmail('godzilla@whitestar.gov');
UserModel::save($user); UserModel::save($user);

View file

@ -97,7 +97,7 @@ class AddCommentJobTest extends AbstractTest
protected function runApi($text) protected function runApi($text)
{ {
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
return Api::run( return Api::run(
new AddCommentJob(), new AddCommentJob(),
@ -111,6 +111,6 @@ class AddCommentJobTest extends AbstractTest
{ {
getConfig()->registration->needEmailForCommenting = false; getConfig()->registration->needEmailForCommenting = false;
$this->grantAccess('addComment'); $this->grantAccess('addComment');
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
} }
} }

View file

@ -11,7 +11,7 @@ class AddPostJobTest extends AbstractTest
$this->grantAccess('addPostSource'); $this->grantAccess('addPostSource');
$this->grantAccess('addPostContent'); $this->grantAccess('addPostContent');
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
$post = $this->assert->doesNotThrow(function() $post = $this->assert->doesNotThrow(function()
{ {
@ -20,14 +20,15 @@ class AddPostJobTest extends AbstractTest
[ [
JobArgs::ARG_NEW_SAFETY => PostSafety::Safe, JobArgs::ARG_NEW_SAFETY => PostSafety::Safe,
JobArgs::ARG_NEW_SOURCE => '', 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_TAG_NAMES => ['kamen', 'raider'],
JobArgs::ARG_NEW_POST_CONTENT =>
new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'),
]); ]);
}); });
$this->assert->areEqual( $this->assert->areEqual(
file_get_contents($post->getFullPath()), 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()); $this->assert->areEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId());
} }
@ -39,7 +40,7 @@ class AddPostJobTest extends AbstractTest
$this->grantAccess('addPostTags'); $this->grantAccess('addPostTags');
$this->grantAccess('addPostContent'); $this->grantAccess('addPostContent');
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
$post = $this->assert->doesNotThrow(function() $post = $this->assert->doesNotThrow(function()
{ {
@ -47,14 +48,15 @@ class AddPostJobTest extends AbstractTest
new AddPostJob(), new AddPostJob(),
[ [
JobArgs::ARG_ANONYMOUS => true, 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_TAG_NAMES => ['kamen', 'raider'],
JobArgs::ARG_NEW_POST_CONTENT =>
new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'),
]); ]);
}); });
$this->assert->areEqual( $this->assert->areEqual(
file_get_contents($post->getFullPath()), 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->areNotEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId());
$this->assert->areEqual(null, $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_SAFETY => PostSafety::Safe,
JobArgs::ARG_NEW_SOURCE => 'this should make it fail', 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'); }, 'Insufficient privilege');
} }
@ -95,8 +98,9 @@ class AddPostJobTest extends AbstractTest
new AddPostJob(), new AddPostJob(),
[ [
JobArgs::ARG_NEW_SAFETY => 666, 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_TAG_NAMES => ['kamen', 'raider'],
JobArgs::ARG_NEW_POST_CONTENT =>
new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'),
]); ]);
}, 'Invalid safety type'); }, 'Invalid safety type');
} }
@ -132,7 +136,8 @@ class AddPostJobTest extends AbstractTest
Api::run( Api::run(
new AddPostJob(), 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'); }, 'No tags set');
} }

View file

@ -6,7 +6,7 @@ class DeleteCommentJobTest extends AbstractTest
$this->prepare(); $this->prepare();
$this->grantAccess('deleteComment'); $this->grantAccess('deleteComment');
$comment = $this->mockComment(Auth::getCurrentUser()); $comment = $this->commentMocker->mockSingle();
$post = $comment->getPost(); $post = $comment->getPost();
$this->assert->areEqual(1, $post->getCommentCount()); $this->assert->areEqual(1, $post->getCommentCount());
@ -41,6 +41,6 @@ class DeleteCommentJobTest extends AbstractTest
protected function prepare() protected function prepare()
{ {
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
} }
} }

View file

@ -3,9 +3,8 @@ class DeletePostJobTest extends AbstractTest
{ {
public function testRemoval() public function testRemoval()
{ {
$user = $this->mockUser(); $post = $this->postMocker->mockSingle();
$post = $this->mockPost($user); $this->login($this->userMocker->mockSingle());
$this->login($user);
$this->grantAccess('deletePost'); $this->grantAccess('deletePost');
$this->assert->doesNotThrow(function() use ($post) $this->assert->doesNotThrow(function() use ($post)
@ -23,9 +22,8 @@ class DeletePostJobTest extends AbstractTest
public function testWrongPostId() public function testWrongPostId()
{ {
$user = $this->mockUser(); $post = $this->postMocker->mockSingle();
$post = $this->mockPost($user); $this->login($this->userMocker->mockSingle());
$this->login($user);
$this->assert->throws(function() $this->assert->throws(function()
{ {

View file

@ -3,7 +3,7 @@ class DeleteUserJobTest extends AbstractTest
{ {
public function testRemoval() public function testRemoval()
{ {
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$this->login($user); $this->login($user);
$this->grantAccess('deleteUser'); $this->grantAccess('deleteUser');
@ -22,7 +22,7 @@ class DeleteUserJobTest extends AbstractTest
public function testWrongUserId() public function testWrongUserId()
{ {
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$this->login($user); $this->login($user);
$this->assert->throws(function() $this->assert->throws(function()

View file

@ -79,7 +79,9 @@ class EditCommentJobTest extends AbstractTest
protected function runApi($text) protected function runApi($text)
{ {
$comment = $this->mockComment(Auth::getCurrentUser()); $comment = $this->commentMocker->mockSingle();
$comment->setCommenter(Auth::getCurrentUser());
CommentModel::save($comment);
return Api::run( return Api::run(
new EditCommentJob(), new EditCommentJob(),
@ -91,6 +93,6 @@ class EditCommentJobTest extends AbstractTest
protected function prepare() protected function prepare()
{ {
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
} }
} }

View file

@ -91,7 +91,7 @@ class EditPostContentJobTest extends AbstractTest
$this->prepare(); $this->prepare();
$this->grantAccess('editPostContent'); $this->grantAccess('editPostContent');
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$post = Api::run( $post = Api::run(
new EditPostContentJob(), new EditPostContentJob(),
[ [
@ -119,7 +119,8 @@ class EditPostContentJobTest extends AbstractTest
new EditPostContentJob(), new EditPostContentJob(),
[ [
JobArgs::ARG_POST_ID => 100, 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'); }, 'Invalid post ID');
} }
@ -156,7 +157,7 @@ class EditPostContentJobTest extends AbstractTest
$url = 'http://www.youtube.com/watch?v=qWq_jydCUw4'; $url = 'http://www.youtube.com/watch?v=qWq_jydCUw4';
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$post = Api::run( $post = Api::run(
new EditPostContentJob(), new EditPostContentJob(),
[ [
@ -164,7 +165,7 @@ class EditPostContentJobTest extends AbstractTest
JobArgs::ARG_NEW_POST_CONTENT_URL => $url, JobArgs::ARG_NEW_POST_CONTENT_URL => $url,
]); ]);
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$this->assert->throws(function() use ($post, $url) $this->assert->throws(function() use ($post, $url)
{ {
Api::run( Api::run(
@ -178,16 +179,16 @@ class EditPostContentJobTest extends AbstractTest
protected function prepare() protected function prepare()
{ {
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
} }
protected function uploadFromUrl($fileName, $post = null) protected function uploadFromUrl($fileName, $post = null)
{ {
if ($post === null) if ($post === null)
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$url = 'http://example.com/mock_' . $fileName; $url = 'http://example.com/mock_' . $fileName;
TransferHelper::mockForDownload($url, $this->getPath($fileName)); TransferHelper::mockForDownload($url, $this->testSupport->getPath($fileName));
$post = Api::run( $post = Api::run(
new EditPostContentJob(), new EditPostContentJob(),
@ -198,7 +199,7 @@ class EditPostContentJobTest extends AbstractTest
$this->assert->isNotNull($post->tryGetWorkingFullPath()); $this->assert->isNotNull($post->tryGetWorkingFullPath());
$this->assert->areEqual( $this->assert->areEqual(
file_get_contents($this->getPath($fileName)), file_get_contents($this->testSupport->getPath($fileName)),
file_get_contents($post->tryGetWorkingFullPath())); file_get_contents($post->tryGetWorkingFullPath()));
return $post; return $post;
@ -207,18 +208,19 @@ class EditPostContentJobTest extends AbstractTest
protected function uploadFromFile($fileName, $post = null) protected function uploadFromFile($fileName, $post = null)
{ {
if ($post === null) if ($post === null)
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$post = Api::run( $post = Api::run(
new EditPostContentJob(), new EditPostContentJob(),
[ [
JobArgs::ARG_POST_ID => $post->getId(), 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->isNotNull($post->tryGetWorkingFullPath());
$this->assert->areEqual( $this->assert->areEqual(
file_get_contents($this->getPath($fileName)), file_get_contents($this->testSupport->getPath($fileName)),
file_get_contents($post->tryGetWorkingFullPath())); file_get_contents($post->tryGetWorkingFullPath()));
return $post; return $post;

View file

@ -9,14 +9,15 @@ class EditPostJobTest extends AbstractTest
$this->grantAccess('editPostSource'); $this->grantAccess('editPostSource');
$this->grantAccess('editPostContent'); $this->grantAccess('editPostContent');
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$args = $args =
[ [
JobArgs::ARG_POST_ID => $post->getId(), JobArgs::ARG_POST_ID => $post->getId(),
JobArgs::ARG_NEW_SAFETY => PostSafety::Sketchy, JobArgs::ARG_NEW_SAFETY => PostSafety::Sketchy,
JobArgs::ARG_NEW_SOURCE => 'some source huh', 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) $this->assert->doesNotThrow(function() use ($args)
@ -32,14 +33,15 @@ class EditPostJobTest extends AbstractTest
$this->grantAccess('editPostTags'); $this->grantAccess('editPostTags');
$this->grantAccess('editPostContent'); $this->grantAccess('editPostContent');
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$args = $args =
[ [
JobArgs::ARG_POST_ID => $post->getId(), JobArgs::ARG_POST_ID => $post->getId(),
JobArgs::ARG_NEW_SAFETY => PostSafety::Safe, JobArgs::ARG_NEW_SAFETY => PostSafety::Safe,
JobArgs::ARG_NEW_SOURCE => 'this should make it fail', 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) $this->assert->throws(function() use ($args)

View file

@ -3,11 +3,10 @@ class EditPostRelationsJobTest extends AbstractTest
{ {
public function testEditing() public function testEditing()
{ {
$basePost = $this->mockPost($this->mockUser());
$this->grantAccess('editPostRelations'); $this->grantAccess('editPostRelations');
$post1 = $this->mockPost($this->mockUser()); list ($basePost, $post1, $post2)
$post2 = $this->mockPost($this->mockUser()); = $this->postMocker->mockMultiple(3);
$basePost = $this->assert->doesNotThrow(function() use ($basePost, $post1, $post2) $basePost = $this->assert->doesNotThrow(function() use ($basePost, $post1, $post2)
{ {
@ -30,11 +29,10 @@ class EditPostRelationsJobTest extends AbstractTest
public function testOverwriting() public function testOverwriting()
{ {
$basePost = $this->mockPost($this->mockUser());
$this->grantAccess('editPostRelations'); $this->grantAccess('editPostRelations');
$post1 = $this->mockPost($this->mockUser()); list ($basePost, $post1, $post2)
$post2 = $this->mockPost($this->mockUser()); = $this->postMocker->mockMultiple(3);
$basePost->setRelations([$post1]); $basePost->setRelations([$post1]);
PostModel::save($basePost); PostModel::save($basePost);
@ -61,11 +59,10 @@ class EditPostRelationsJobTest extends AbstractTest
public function testOverwritingEmpty() public function testOverwritingEmpty()
{ {
$basePost = $this->mockPost($this->mockUser());
$this->grantAccess('editPostRelations'); $this->grantAccess('editPostRelations');
$post1 = $this->mockPost($this->mockUser()); list ($basePost, $post1, $post2)
$post2 = $this->mockPost($this->mockUser()); = $this->postMocker->mockMultiple(3);
$basePost->setRelations([$post1]); $basePost->setRelations([$post1]);
PostModel::save($basePost); PostModel::save($basePost);

View file

@ -6,7 +6,7 @@ class EditPostSafetyJobTest extends AbstractTest
$this->prepare(); $this->prepare();
$this->grantAccess('editPostSafety.own'); $this->grantAccess('editPostSafety.own');
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$post = $this->assert->doesNotThrow(function() use ($post) $post = $this->assert->doesNotThrow(function() use ($post)
{ {
return Api::run( return Api::run(
@ -44,7 +44,7 @@ class EditPostSafetyJobTest extends AbstractTest
$this->prepare(); $this->prepare();
$this->grantAccess('editPostSafety.own'); $this->grantAccess('editPostSafety.own');
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$this->assert->throws(function() use ($post) $this->assert->throws(function() use ($post)
{ {
Api::run( Api::run(
@ -58,6 +58,6 @@ class EditPostSafetyJobTest extends AbstractTest
protected function prepare() protected function prepare()
{ {
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
} }
} }

View file

@ -65,7 +65,7 @@ class EditPostSourceJobTest extends AbstractTest
protected function runApi($text) protected function runApi($text)
{ {
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
return Api::run( return Api::run(
new EditPostSourceJob(), new EditPostSourceJob(),
[ [
@ -76,6 +76,6 @@ class EditPostSourceJobTest extends AbstractTest
protected function prepare() protected function prepare()
{ {
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
} }
} }

View file

@ -3,7 +3,7 @@ class EditPostTagsJobTest extends AbstractTest
{ {
public function testEditing() public function testEditing()
{ {
$post = $this->mockPost($this->mockUser()); $post = $this->postMocker->mockSingle();
$this->grantAccess('editPostTags'); $this->grantAccess('editPostTags');
$newTagNames = ['big', 'boss']; $newTagNames = ['big', 'boss'];
@ -30,7 +30,7 @@ class EditPostTagsJobTest extends AbstractTest
public function testFailOnEmptyTags() public function testFailOnEmptyTags()
{ {
$post = $this->mockPost($this->mockUser()); $post = $this->postMocker->mockSingle();
$this->grantAccess('editPostTags'); $this->grantAccess('editPostTags');
$this->assert->throws(function() use ($post) $this->assert->throws(function() use ($post)
@ -46,7 +46,7 @@ class EditPostTagsJobTest extends AbstractTest
public function testTooShortTag() public function testTooShortTag()
{ {
$post = $this->mockPost($this->mockUser()); $post = $this->postMocker->mockSingle();
$this->grantAccess('editPostTags'); $this->grantAccess('editPostTags');
$newTagNames = [str_repeat('u', getConfig()->tags->minLength - 1)]; $newTagNames = [str_repeat('u', getConfig()->tags->minLength - 1)];
@ -63,7 +63,7 @@ class EditPostTagsJobTest extends AbstractTest
public function testTooLongTag() public function testTooLongTag()
{ {
$post = $this->mockPost($this->mockUser()); $post = $this->postMocker->mockSingle();
$this->grantAccess('editPostTags'); $this->grantAccess('editPostTags');
$newTagNames = [str_repeat('u', getConfig()->tags->maxLength + 1)]; $newTagNames = [str_repeat('u', getConfig()->tags->maxLength + 1)];
@ -80,7 +80,7 @@ class EditPostTagsJobTest extends AbstractTest
public function testInvalidTag() public function testInvalidTag()
{ {
$post = $this->mockPost($this->mockUser()); $post = $this->postMocker->mockSingle();
$this->grantAccess('editPostTags'); $this->grantAccess('editPostTags');
$newTagNames = ['bulma/goku']; $newTagNames = ['bulma/goku'];

View file

@ -4,18 +4,17 @@ class EditPostThumbJobTest extends AbstractTest
public function testFile() public function testFile()
{ {
$this->grantAccess('editPostThumb'); $this->grantAccess('editPostThumb');
$post = $this->postMocker->mockSingle();
$post = $this->mockPost(Auth::getCurrentUser());
$this->assert->isFalse($post->hasCustomThumb()); $this->assert->isFalse($post->hasCustomThumb());
$post = $this->assert->doesNotThrow(function() use ($post) $post = $this->assert->doesNotThrow(function() use ($post)
{ {
return Api::run( return Api::run(
new EditPostThumbJob(), new EditPostThumbJob(),
[ [
JobArgs::ARG_POST_ID => $post->getId(), 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->isNotNull($post->getThumbCustomPath());
$this->assert->areEqual($post->getThumbCustomPath(), $post->tryGetWorkingThumbPath()); $this->assert->areEqual($post->getThumbCustomPath(), $post->tryGetWorkingThumbPath());
$this->assert->areEqual( $this->assert->areEqual(
file_get_contents($this->getPath('thumb.jpg')), file_get_contents($this->testSupport->getPath('thumb.jpg')),
file_get_contents($post->tryGetWorkingThumbPath())); file_get_contents($post->tryGetWorkingThumbPath()));
} }
public function testFileInvalidDimensions() public function testFileInvalidDimensions()
{ {
$this->grantAccess('editPostThumb'); $this->grantAccess('editPostThumb');
$post = $this->postMocker->mockSingle();
$post = $this->mockPost(Auth::getCurrentUser());
$this->assert->isFalse($post->hasCustomThumb()); $this->assert->isFalse($post->hasCustomThumb());
$this->assert->throws(function() use ($post) $this->assert->throws(function() use ($post)
{ {
return Api::run( return Api::run(
new EditPostThumbJob(), new EditPostThumbJob(),
[ [
JobArgs::ARG_POST_ID => $post->getId(), 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'); }, 'invalid thumbnail size');

View file

@ -4,7 +4,7 @@ class EditUserAccessRankJobTest extends AbstractTest
public function testEditing() public function testEditing()
{ {
$this->grantAccess('changeUserAccessRank'); $this->grantAccess('changeUserAccessRank');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$this->assert->areEqual(AccessRank::Registered, $user->getAccessRank()->toInteger()); $this->assert->areEqual(AccessRank::Registered, $user->getAccessRank()->toInteger());
@ -24,7 +24,7 @@ class EditUserAccessRankJobTest extends AbstractTest
public function testSettingToNobodyDenial() public function testSettingToNobodyDenial()
{ {
$this->grantAccess('changeUserAccessRank'); $this->grantAccess('changeUserAccessRank');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$this->assert->areEqual(AccessRank::Registered, $user->getAccessRank()->toInteger()); $this->assert->areEqual(AccessRank::Registered, $user->getAccessRank()->toInteger());
@ -44,7 +44,7 @@ class EditUserAccessRankJobTest extends AbstractTest
getConfig()->privileges->changeUserAccessRank = 'power-user'; getConfig()->privileges->changeUserAccessRank = 'power-user';
Access::init(); Access::init();
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setAccessRank(new AccessRank(AccessRank::PowerUser)); $user->setAccessRank(new AccessRank(AccessRank::PowerUser));
UserModel::save($user); UserModel::save($user);

View file

@ -10,7 +10,7 @@ class EditUserEmailJobTest extends AbstractTest
getConfig()->privileges->changeUserEmailNoConfirm = 'anonymous'; getConfig()->privileges->changeUserEmailNoConfirm = 'anonymous';
$this->grantAccess('changeUserEmail'); $this->grantAccess('changeUserEmail');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user = $this->assert->doesNotThrow(function() use ($user) $user = $this->assert->doesNotThrow(function() use ($user)
{ {
@ -37,7 +37,7 @@ class EditUserEmailJobTest extends AbstractTest
getConfig()->privileges->changeUserEmailNoConfirm = 'admin'; getConfig()->privileges->changeUserEmailNoConfirm = 'admin';
$this->grantAccess('changeUserEmail'); $this->grantAccess('changeUserEmail');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user = $this->assert->doesNotThrow(function() use ($user) $user = $this->assert->doesNotThrow(function() use ($user)
{ {
@ -63,7 +63,7 @@ class EditUserEmailJobTest extends AbstractTest
getConfig()->privileges->changeUserEmailNoConfirm = 'nobody'; getConfig()->privileges->changeUserEmailNoConfirm = 'nobody';
$this->grantAccess('changeUserEmail'); $this->grantAccess('changeUserEmail');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$this->assert->throws(function() use ($user) $this->assert->throws(function() use ($user)
{ {
@ -85,8 +85,8 @@ class EditUserEmailJobTest extends AbstractTest
getConfig()->privileges->changeUserEmailNoConfirm = 'anonymous'; getConfig()->privileges->changeUserEmailNoConfirm = 'anonymous';
$this->grantAccess('changeUserEmail'); $this->grantAccess('changeUserEmail');
$user = $this->mockUser(); list ($user, $otherUser)
$otherUser = $this->mockUser(); = $this->userMocker->mockMultiple(2);
$otherUser->setUnconfirmedEmail('super@mario.plumbing'); $otherUser->setUnconfirmedEmail('super@mario.plumbing');
UserModel::save($otherUser); UserModel::save($otherUser);

View file

@ -5,7 +5,7 @@ class EditUserJobTest extends AbstractTest
{ {
$this->grantAccess('changeUserName.own'); $this->grantAccess('changeUserName.own');
$this->grantAccess('changeUserPassword.own'); $this->grantAccess('changeUserPassword.own');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$newName = 'dummy' . uniqid(); $newName = 'dummy' . uniqid();
@ -30,7 +30,7 @@ class EditUserJobTest extends AbstractTest
public function testPartialPrivilegeFail() public function testPartialPrivilegeFail()
{ {
$this->grantAccess('changeUserName.own'); $this->grantAccess('changeUserName.own');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$newName = 'dummy' . uniqid(); $newName = 'dummy' . uniqid();
@ -59,13 +59,13 @@ class EditUserJobTest extends AbstractTest
public function testCanEditSomething() public function testCanEditSomething()
{ {
$this->grantAccess('changeUserName.own'); $this->grantAccess('changeUserName.own');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user = $this->assert->isTrue((new EditUserJob())->canEditAnything($user)); $user = $this->assert->isTrue((new EditUserJob())->canEditAnything($user));
} }
public function testCannotEditAnything() public function testCannotEditAnything()
{ {
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user = $this->assert->isFalse((new EditUserJob())->canEditAnything($user)); $user = $this->assert->isFalse((new EditUserJob())->canEditAnything($user));
} }
} }

View file

@ -4,7 +4,7 @@ class EditUserNameJobTest extends AbstractTest
public function testEditing() public function testEditing()
{ {
$this->grantAccess('changeUserName'); $this->grantAccess('changeUserName');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$newName = uniqid(); $newName = uniqid();
@ -26,7 +26,7 @@ class EditUserNameJobTest extends AbstractTest
public function testTooShortName() public function testTooShortName()
{ {
$this->grantAccess('changeUserName'); $this->grantAccess('changeUserName');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$newName = str_repeat('a', getConfig()->registration->userNameMinLength - 1); $newName = str_repeat('a', getConfig()->registration->userNameMinLength - 1);
@ -44,7 +44,7 @@ class EditUserNameJobTest extends AbstractTest
public function testTooLongName() public function testTooLongName()
{ {
$this->grantAccess('changeUserName'); $this->grantAccess('changeUserName');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$newName = str_repeat('a', getConfig()->registration->userNameMaxLength + 1); $newName = str_repeat('a', getConfig()->registration->userNameMaxLength + 1);
@ -62,7 +62,7 @@ class EditUserNameJobTest extends AbstractTest
public function testInvalidName() public function testInvalidName()
{ {
$this->grantAccess('changeUserName'); $this->grantAccess('changeUserName');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$newName = 'ble/ble'; $newName = 'ble/ble';
@ -80,8 +80,8 @@ class EditUserNameJobTest extends AbstractTest
public function testChangingToExistingDenial() public function testChangingToExistingDenial()
{ {
$this->grantAccess('changeUserName'); $this->grantAccess('changeUserName');
$user = $this->mockUser(); list ($user, $otherUser)
$otherUser = $this->mockUser(); = $this->userMocker->mockMultiple(2);
$newName = $otherUser->getName(); $newName = $otherUser->getName();
$this->assert->areNotEqual($newName, $user->getName()); $this->assert->areNotEqual($newName, $user->getName());

View file

@ -14,7 +14,7 @@ class EditUserPasswordJobTest extends AbstractTest
public function testTooShortPassword() public function testTooShortPassword()
{ {
$this->grantAccess('changeUserPassword'); $this->grantAccess('changeUserPassword');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$newPassword = str_repeat('a', getConfig()->registration->passMinLength - 1); $newPassword = str_repeat('a', getConfig()->registration->passMinLength - 1);
$oldPasswordHash = $user->getPasswordHash(); $oldPasswordHash = $user->getPasswordHash();
@ -35,7 +35,7 @@ class EditUserPasswordJobTest extends AbstractTest
private function testValidPassword($newPassword) private function testValidPassword($newPassword)
{ {
$this->grantAccess('changeUserPassword'); $this->grantAccess('changeUserPassword');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$newPasswordHash = UserModel::hashPassword($newPassword, $user->getPasswordSalt()); $newPasswordHash = UserModel::hashPassword($newPassword, $user->getPasswordSalt());

View file

@ -5,21 +5,20 @@ class FeaturePostJobTest extends AbstractTest
{ {
$this->grantAccess('featurePost'); $this->grantAccess('featurePost');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$this->login($user); $this->login($user);
$post1 = $this->mockPost($user); $posts = $this->postMocker->mockMultiple(2);
$post2 = $this->mockPost($user);
$this->assert->doesNotThrow(function() use ($post2) $this->assert->doesNotThrow(function() use ($posts)
{ {
Api::run( Api::run(
new FeaturePostJob(), 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->areEqual($user->getName(), PropertyModel::get(PropertyModel::FeaturedPostUserName));
$this->assert->isNotNull(PropertyModel::get(PropertyModel::FeaturedPostUnixTime)); $this->assert->isNotNull(PropertyModel::get(PropertyModel::FeaturedPostUnixTime));
} }
@ -28,22 +27,20 @@ class FeaturePostJobTest extends AbstractTest
{ {
$this->grantAccess('featurePost'); $this->grantAccess('featurePost');
$user = $this->mockUser(); $this->login($this->userMocker->mockSingle());
$this->login($user); $posts = $this->postMocker->mockMultiple(2);
$post1 = $this->mockPost($user);
$post2 = $this->mockPost($user);
$this->assert->doesNotThrow(function() use ($post2) $this->assert->doesNotThrow(function() use ($posts)
{ {
Api::run( Api::run(
new FeaturePostJob(), new FeaturePostJob(),
[ [
JobArgs::ARG_POST_ID => $post2->getId(), JobArgs::ARG_POST_ID => $posts[1]->getId(),
JobArgs::ARG_ANONYMOUS => true, 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)); $this->assert->areEqual(null, PropertyModel::get(PropertyModel::FeaturedPostUserName));
} }
} }

View file

@ -4,7 +4,7 @@ class FlagPostJobTest extends AbstractTest
public function testFlagging() public function testFlagging()
{ {
$this->grantAccess('flagPost'); $this->grantAccess('flagPost');
$post = $this->mockPost($this->mockUser()); $post = $this->postMocker->mockSingle();
$post = $this->assert->doesNotThrow(function() use ($post) $post = $this->assert->doesNotThrow(function() use ($post)
{ {
@ -25,7 +25,7 @@ class FlagPostJobTest extends AbstractTest
public function testDoubleFlagging() public function testDoubleFlagging()
{ {
$this->grantAccess('flagPost'); $this->grantAccess('flagPost');
$post = $this->mockPost($this->mockUser()); $post = $this->postMocker->mockSingle();
$this->assert->doesNotThrow(function() use ($post) $this->assert->doesNotThrow(function() use ($post)
{ {

View file

@ -4,7 +4,7 @@ class FlagUserJobTest extends AbstractTest
public function testFlagging() public function testFlagging()
{ {
$this->grantAccess('flagUser'); $this->grantAccess('flagUser');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user = $this->assert->doesNotThrow(function() use ($user) $user = $this->assert->doesNotThrow(function() use ($user)
{ {
@ -25,7 +25,7 @@ class FlagUserJobTest extends AbstractTest
public function testDoubleFlagging() public function testDoubleFlagging()
{ {
$this->grantAccess('flagUser'); $this->grantAccess('flagUser');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$this->assert->doesNotThrow(function() use ($user) $this->assert->doesNotThrow(function() use ($user)
{ {

View file

@ -0,0 +1,57 @@
<?php
class GetLogJobTest extends AbstractTest
{
public function testRetrieving()
{
$ret = $this->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);
});
}
}

View file

@ -0,0 +1,51 @@
<?php
class GetPostContentJobTest extends AbstractTest
{
public function testPostRetrieval()
{
$this->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');
}
}

View file

@ -0,0 +1,36 @@
<?php
class GetPostJobTest extends AbstractTest
{
public function testPostRetrieval()
{
$this->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');
}
}

View file

@ -0,0 +1,50 @@
<?php
class GetPostThumbJobTest extends AbstractTest
{
public function testThumbRetrieval()
{
$this->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');
}
}

View file

@ -0,0 +1,39 @@
<?php
class GetUserJobTest extends AbstractTest
{
public function testUserRetrieval()
{
$this->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');
}
}

View file

@ -18,7 +18,7 @@ class ListCommentJobTest extends AbstractTest
$this->assert->areEqual(0, CommentModel::getCount()); $this->assert->areEqual(0, CommentModel::getCount());
$comment = $this->mockComment($this->mockUser()); $comment = $this->commentMocker->mockSingle();
$ret = $this->runApi(1); $ret = $this->runApi(1);
$this->assert->areEqual(1, count($ret->entities)); $this->assert->areEqual(1, count($ret->entities));
@ -48,9 +48,7 @@ class ListCommentJobTest extends AbstractTest
$this->assert->areEqual(0, CommentModel::getCount()); $this->assert->areEqual(0, CommentModel::getCount());
$this->mockComment($this->mockUser()); $this->commentMocker->mockMultiple(3);
$this->mockComment($this->mockUser());
$this->mockComment($this->mockUser());
$ret = $this->runApi(1); $ret = $this->runApi(1);
$this->assert->areEqual(2, count($ret->entities)); $this->assert->areEqual(2, count($ret->entities));

View file

@ -0,0 +1,27 @@
<?php
class ListLogsJobTest extends AbstractTest
{
public function testListing()
{
$this->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]);
}
}

View file

@ -0,0 +1,644 @@
<?php
class ListPostsJobTest extends AbstractTest
{
public function testPaging()
{
$this->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());
}
}

View file

@ -0,0 +1,63 @@
<?php
class ListRelatedTagsJobTest extends AbstractTest
{
public function testPaging()
{
$this->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());
}
}

View file

@ -0,0 +1,122 @@
<?php
class ListTagsJobTest extends AbstractTest
{
public function testPaging()
{
$this->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());
}
}

View file

@ -0,0 +1,90 @@
<?php
class ListUsersJobTest extends AbstractTest
{
public function testPaging()
{
$this->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());
}
}

View file

@ -5,24 +5,17 @@ class MergeTagsJobTest extends AbstractTest
{ {
$this->grantAccess('mergeTags'); $this->grantAccess('mergeTags');
$sourceTag = $this->mockTag(); list ($sourceTag, $randomTag, $targetTag)
$randomTag = $this->mockTag(); = $this->tagMocker->mockMultiple(3);
$targetTag = $this->mockTag();
$posts = [];
foreach (range(0, 5) as $i)
$posts []= $this->mockPost($this->mockUser());
$posts = $this->postMocker->mockMultiple(6);
$posts[0]->setTags([$sourceTag]); $posts[0]->setTags([$sourceTag]);
$posts[1]->setTags([$randomTag]); $posts[1]->setTags([$randomTag]);
$posts[2]->setTags([$sourceTag, $randomTag]); $posts[2]->setTags([$sourceTag, $randomTag]);
$posts[3]->setTags([$sourceTag, $targetTag]); $posts[3]->setTags([$sourceTag, $targetTag]);
$posts[4]->setTags([$randomTag, $targetTag]); $posts[4]->setTags([$randomTag, $targetTag]);
$posts[5]->setTags([$sourceTag, $randomTag, $targetTag]); $posts[5]->setTags([$sourceTag, $randomTag, $targetTag]);
PostModel::save($posts);
foreach ($posts as $post)
PostModel::save($post);
$this->assert->doesNotThrow(function() use ($sourceTag, $targetTag) $this->assert->doesNotThrow(function() use ($sourceTag, $targetTag)
{ {
@ -37,21 +30,21 @@ class MergeTagsJobTest extends AbstractTest
foreach ($posts as $k => $post) foreach ($posts as $k => $post)
$posts[$k] = PostModel::getById($post->getId()); $posts[$k] = PostModel::getById($post->getId());
$this->assertTagNames($posts[0], [$targetTag]); $this->testSupport->assertTagNames($posts[0], [$targetTag]);
$this->assertTagNames($posts[1], [$randomTag]); $this->testSupport->assertTagNames($posts[1], [$randomTag]);
$this->assertTagNames($posts[2], [$randomTag, $targetTag]); $this->testSupport->assertTagNames($posts[2], [$randomTag, $targetTag]);
$this->testSupport->assertTagNames($posts[3], [$targetTag]);
$this->assertTagNames($posts[3], [$targetTag]); $this->testSupport->assertTagNames($posts[4], [$randomTag, $targetTag]);
$this->assertTagNames($posts[4], [$randomTag, $targetTag]); $this->testSupport->assertTagNames($posts[5], [$randomTag, $targetTag]);
$this->assertTagNames($posts[5], [$randomTag, $targetTag]);
} }
public function testMergingToNonExistingTag() public function testMergingToNonExistingTag()
{ {
$this->grantAccess('mergeTags'); $this->grantAccess('mergeTags');
$sourceTag = $this->mockTag(); $sourceTag = $this->tagMocker->mockSingle();
$post = $this->mockPost($this->mockUser());
$post = $this->postMocker->mockSingle();
$post->setTags([$sourceTag]); $post->setTags([$sourceTag]);
PostModel::save($post); PostModel::save($post);
@ -70,8 +63,9 @@ class MergeTagsJobTest extends AbstractTest
{ {
$this->grantAccess('mergeTags'); $this->grantAccess('mergeTags');
$sourceTag = $this->mockTag(); $sourceTag = $this->tagMocker->mockSingle();
$post = $this->mockPost($this->mockUser());
$post = $this->postMocker->mockSingle();
$post->setTags([$sourceTag]); $post->setTags([$sourceTag]);
PostModel::save($post); PostModel::save($post);
@ -85,23 +79,4 @@ class MergeTagsJobTest extends AbstractTest
]); ]);
}, 'Source and target tag are the same'); }, '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;
}
} }

View file

@ -6,7 +6,7 @@ class PasswordResetJobTest extends AbstractTest
getConfig()->registration->needEmailForRegistering = true; getConfig()->registration->needEmailForRegistering = true;
Mailer::mockSending(); Mailer::mockSending();
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setUnconfirmedEmail('godzilla@whitestar.gov'); $user->setUnconfirmedEmail('godzilla@whitestar.gov');
UserModel::save($user); UserModel::save($user);
@ -25,7 +25,7 @@ class PasswordResetJobTest extends AbstractTest
getConfig()->registration->needEmailForRegistering = true; getConfig()->registration->needEmailForRegistering = true;
Mailer::mockSending(); Mailer::mockSending();
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setConfirmedEmail('godzilla@whitestar.gov'); $user->setConfirmedEmail('godzilla@whitestar.gov');
UserModel::save($user); UserModel::save($user);
@ -57,7 +57,7 @@ class PasswordResetJobTest extends AbstractTest
getConfig()->registration->needEmailForRegistering = true; getConfig()->registration->needEmailForRegistering = true;
Mailer::mockSending(); Mailer::mockSending();
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setConfirmedEmail('godzilla@whitestar.gov'); $user->setConfirmedEmail('godzilla@whitestar.gov');
UserModel::save($user); UserModel::save($user);
@ -97,7 +97,7 @@ class PasswordResetJobTest extends AbstractTest
getConfig()->registration->needEmailForRegistering = true; getConfig()->registration->needEmailForRegistering = true;
Mailer::mockSending(); Mailer::mockSending();
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setConfirmedEmail('godzilla@whitestar.gov'); $user->setConfirmedEmail('godzilla@whitestar.gov');
UserModel::save($user); UserModel::save($user);

View file

@ -60,7 +60,7 @@ class PreviewCommentJobTest extends AbstractTest
public function testViaPost() public function testViaPost()
{ {
$this->prepare(); $this->prepare();
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$this->assert->doesNotThrow(function() use ($post) $this->assert->doesNotThrow(function() use ($post)
{ {
@ -76,7 +76,9 @@ class PreviewCommentJobTest extends AbstractTest
public function testViaComment() public function testViaComment()
{ {
$this->prepare(); $this->prepare();
$comment = $this->mockComment(Auth::getCurrentUser()); $comment = $this->commentMocker->mockSingle();
$comment->setCommenter(Auth::getCurrentUser());
CommentModel::save($comment);
$this->assert->doesNotThrow(function() use ($comment) $this->assert->doesNotThrow(function() use ($comment)
{ {
@ -92,7 +94,7 @@ class PreviewCommentJobTest extends AbstractTest
protected function runApi($text) protected function runApi($text)
{ {
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
return Api::run( return Api::run(
new PreviewCommentJob(), new PreviewCommentJob(),
@ -106,6 +108,6 @@ class PreviewCommentJobTest extends AbstractTest
{ {
getConfig()->registration->needEmailForCommenting = false; getConfig()->registration->needEmailForCommenting = false;
$this->grantAccess('addComment'); $this->grantAccess('addComment');
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
} }
} }

View file

@ -5,20 +5,14 @@ class RenameTagsJobTest extends AbstractTest
{ {
$this->grantAccess('renameTags'); $this->grantAccess('renameTags');
$sourceTag = $this->mockTag(); list ($sourceTag, $randomTag, $targetTag)
$randomTag = $this->mockTag(); = $this->tagMocker->mockMultiple(3);
$targetTag = $this->mockTag();
$posts = [];
foreach (range(0, 2) as $i)
$posts []= $this->mockPost($this->mockUser());
$posts = $this->postMocker->mockMultiple(3);
$posts[0]->setTags([$sourceTag]); $posts[0]->setTags([$sourceTag]);
$posts[1]->setTags([$randomTag]); $posts[1]->setTags([$randomTag]);
$posts[2]->setTags([$sourceTag, $randomTag]); $posts[2]->setTags([$sourceTag, $randomTag]);
PostModel::save($posts);
foreach ($posts as $post)
PostModel::save($post);
$this->assert->doesNotThrow(function() use ($sourceTag, $targetTag) $this->assert->doesNotThrow(function() use ($sourceTag, $targetTag)
{ {
@ -33,23 +27,20 @@ class RenameTagsJobTest extends AbstractTest
foreach ($posts as $k => $post) foreach ($posts as $k => $post)
$posts[$k] = PostModel::getById($post->getId()); $posts[$k] = PostModel::getById($post->getId());
$this->assertTagNames($posts[0], [$targetTag]); $this->testSupport->assertTagNames($posts[0], [$targetTag]);
$this->assertTagNames($posts[1], [$randomTag]); $this->testSupport->assertTagNames($posts[1], [$randomTag]);
$this->assertTagNames($posts[2], [$randomTag, $targetTag]); $this->testSupport->assertTagNames($posts[2], [$randomTag, $targetTag]);
} }
public function testRenamingToExistingTag() public function testRenamingToExistingTag()
{ {
$this->grantAccess('renameTags'); $this->grantAccess('renameTags');
$sourceTag = $this->mockTag(); list ($sourceTag, $targetTag)
$post = $this->mockPost($this->mockUser()); = $this->tagMocker->mockMultiple(2);
$post->setTags([$sourceTag]);
PostModel::save($post);
$targetTag = $this->mockTag(); $post = $this->postMocker->mockSingle();
$post = $this->mockPost($this->mockUser()); $post->setTags([$sourceTag, $targetTag]);
$post->setTags([$targetTag]);
PostModel::save($post); PostModel::save($post);
$this->assert->throws(function() use ($sourceTag, $targetTag) $this->assert->throws(function() use ($sourceTag, $targetTag)
@ -67,8 +58,9 @@ class RenameTagsJobTest extends AbstractTest
{ {
$this->grantAccess('renameTags'); $this->grantAccess('renameTags');
$sourceTag = $this->mockTag(); $sourceTag = $this->tagMocker->mockSingle();
$post = $this->mockPost($this->mockUser());
$post = $this->postMocker->mockSingle();
$post->setTags([$sourceTag]); $post->setTags([$sourceTag]);
PostModel::save($post); PostModel::save($post);
@ -82,23 +74,4 @@ class RenameTagsJobTest extends AbstractTest
]); ]);
}, 'Source and target tag are the same'); }, '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;
}
} }

View file

@ -3,10 +3,8 @@ class ScorePostJobTest extends AbstractTest
{ {
public function testScoring() public function testScoring()
{ {
$this->grantAccess('scorePost'); $this->loginRandomPerson();
$this->login($this->mockUser()); $post = $this->postMocker->mockSingle();
$post = $this->mockPost(Auth::getCurrentUser());
$this->assert->areEqual(0, $post->getScore()); $this->assert->areEqual(0, $post->getScore());
$post = $this->assert->doesNotThrow(function() use ($post) $post = $this->assert->doesNotThrow(function() use ($post)
@ -24,10 +22,8 @@ class ScorePostJobTest extends AbstractTest
public function testNegativeScore() public function testNegativeScore()
{ {
$this->grantAccess('scorePost'); $this->loginRandomPerson();
$this->login($this->mockUser()); $post = $this->postMocker->mockSingle();
$post = $this->mockPost(Auth::getCurrentUser());
$post = $this->assert->doesNotThrow(function() use ($post) $post = $this->assert->doesNotThrow(function() use ($post)
{ {
return Api::run( return Api::run(
@ -43,10 +39,8 @@ class ScorePostJobTest extends AbstractTest
public function testInvalidScore() public function testInvalidScore()
{ {
$this->grantAccess('scorePost'); $this->loginRandomPerson();
$this->login($this->mockUser()); $post = $this->postMocker->mockSingle();
$post = $this->mockPost(Auth::getCurrentUser());
$this->assert->throws(function() use ($post) $this->assert->throws(function() use ($post)
{ {
Api::run( Api::run(
@ -62,10 +56,8 @@ class ScorePostJobTest extends AbstractTest
public function testScoreOverwriting() public function testScoreOverwriting()
{ {
$this->grantAccess('scorePost'); $this->loginRandomPerson();
$this->login($this->mockUser()); $post = $this->postMocker->mockSingle();
$post = $this->mockPost(Auth::getCurrentUser());
$post = $this->assert->doesNotThrow(function() use ($post) $post = $this->assert->doesNotThrow(function() use ($post)
{ {
return Api::run( return Api::run(
@ -91,10 +83,8 @@ class ScorePostJobTest extends AbstractTest
public function testScoreTwoPeople() public function testScoreTwoPeople()
{ {
$this->grantAccess('scorePost'); $this->loginRandomPerson();
$this->login($this->mockUser()); $post = $this->postMocker->mockSingle();
$post = $this->mockPost(Auth::getCurrentUser());
$post = $this->assert->doesNotThrow(function() use ($post) $post = $this->assert->doesNotThrow(function() use ($post)
{ {
return Api::run( return Api::run(
@ -105,8 +95,7 @@ class ScorePostJobTest extends AbstractTest
]); ]);
}); });
$this->login($this->mockUser()); $this->loginRandomPerson();
$post = $this->assert->doesNotThrow(function() use ($post) $post = $this->assert->doesNotThrow(function() use ($post)
{ {
return Api::run( return Api::run(
@ -119,4 +108,10 @@ class ScorePostJobTest extends AbstractTest
$this->assert->areEqual(2, $post->getScore()); $this->assert->areEqual(2, $post->getScore());
} }
private function loginRandomPerson()
{
$this->grantAccess('scorePost');
$this->login($this->userMocker->mockSingle());
}
} }

View file

@ -4,9 +4,9 @@ class TogglePostFavoriteJobTest extends AbstractTest
public function testFaving() public function testFaving()
{ {
$this->grantAccess('favoritePost'); $this->grantAccess('favoritePost');
$user = $this->mockUser(); $post = $this->postMocker->mockSingle();
$user = $this->userMocker->mockSingle();
$this->login($user); $this->login($user);
$post = $this->mockPost($user);
$this->assert->areEqual(0, $post->getScore()); $this->assert->areEqual(0, $post->getScore());
$this->assert->areEqual(0, $post->getFavoriteCount()); $this->assert->areEqual(0, $post->getFavoriteCount());
@ -31,9 +31,9 @@ class TogglePostFavoriteJobTest extends AbstractTest
public function testDefaving() public function testDefaving()
{ {
$this->grantAccess('favoritePost'); $this->grantAccess('favoritePost');
$user = $this->mockUser(); $post = $this->postMocker->mockSingle();
$user = $this->userMocker->mockSingle();
$this->login($user); $this->login($user);
$post = $this->mockPost($user);
$this->assert->areEqual(0, $post->getScore()); $this->assert->areEqual(0, $post->getScore());
$this->assert->areEqual(0, $post->getFavoriteCount()); $this->assert->areEqual(0, $post->getFavoriteCount());
@ -68,14 +68,13 @@ class TogglePostFavoriteJobTest extends AbstractTest
public function testFavingTwoPeople() public function testFavingTwoPeople()
{ {
$this->grantAccess('favoritePost'); $this->grantAccess('favoritePost');
$user1 = $this->mockUser(); $users = $this->userMocker->mockMultiple(2);
$user2 = $this->mockUser(); $post = $this->postMocker->mockSingle();
$post = $this->mockPost($user1);
$this->assert->areEqual(0, $post->getScore()); $this->assert->areEqual(0, $post->getScore());
$this->assert->areEqual(0, $post->getFavoriteCount()); $this->assert->areEqual(0, $post->getFavoriteCount());
$this->login($user1); $this->login($users[0]);
$post = $this->assert->doesNotThrow(function() use ($post) $post = $this->assert->doesNotThrow(function() use ($post)
{ {
return Api::run( 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) $post = $this->assert->doesNotThrow(function() use ($post)
{ {
return Api::run( return Api::run(
@ -99,11 +98,11 @@ class TogglePostFavoriteJobTest extends AbstractTest
$this->assert->areEqual(2, $post->getScore()); $this->assert->areEqual(2, $post->getScore());
$this->assert->areEqual(2, $post->getFavoriteCount()); $this->assert->areEqual(2, $post->getFavoriteCount());
$this->assert->areEqual(1, $user1->getFavoriteCount()); $this->assert->areEqual(1, $users[0]->getFavoriteCount());
$this->assert->areEqual(1, $user1->getScore($post)); $this->assert->areEqual(1, $users[0]->getScore($post));
$this->assert->areEqual(1, $user2->getFavoriteCount()); $this->assert->areEqual(1, $users[1]->getFavoriteCount());
$this->assert->areEqual(1, $user2->getScore($post)); $this->assert->areEqual(1, $users[1]->getScore($post));
$this->assert->areEqual(true, $user1->hasFavorited($post)); $this->assert->areEqual(true, $users[0]->hasFavorited($post));
$this->assert->areEqual(true, $user2->hasFavorited($post)); $this->assert->areEqual(true, $users[1]->hasFavorited($post));
} }
} }

View file

@ -124,7 +124,7 @@ class TogglePostTagJobTest extends AbstractTest
private function preparePost(array $tagNames) private function preparePost(array $tagNames)
{ {
$this->grantAccess('editPostTags'); $this->grantAccess('editPostTags');
$post = $this->mockPost($this->mockUser()); $post = $this->postMocker->mockSingle();
$post->setTags(TagModel::spawnFromNames($tagNames)); $post->setTags(TagModel::spawnFromNames($tagNames));
PostModel::save($post); PostModel::save($post);
return $post; return $post;

View file

@ -4,8 +4,8 @@ class TogglePostVisibilityJobTest extends AbstractTest
public function testHiding() public function testHiding()
{ {
$this->grantAccess('hidePost'); $this->grantAccess('hidePost');
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$this->assert->isFalse($post->isHidden()); $this->assert->isFalse($post->isHidden());
@ -25,8 +25,8 @@ class TogglePostVisibilityJobTest extends AbstractTest
public function testShowing() public function testShowing()
{ {
$this->grantAccess('hidePost'); $this->grantAccess('hidePost');
$this->login($this->mockUser()); $this->login($this->userMocker->mockSingle());
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$this->assert->isFalse($post->isHidden()); $this->assert->isFalse($post->isHidden());

View file

@ -4,7 +4,7 @@ class ToggleUserBanJobTest extends AbstractTest
public function testBanning() public function testBanning()
{ {
$this->grantAccess('banUser'); $this->grantAccess('banUser');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$this->login($user); $this->login($user);
$this->assert->isFalse($user->isBanned()); $this->assert->isFalse($user->isBanned());
@ -25,7 +25,7 @@ class ToggleUserBanJobTest extends AbstractTest
public function testUnbanning() public function testUnbanning()
{ {
$this->grantAccess('banUser'); $this->grantAccess('banUser');
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$this->login($user); $this->login($user);
$this->assert->isFalse($user->isBanned()); $this->assert->isFalse($user->isBanned());

View file

@ -9,7 +9,7 @@ class AccessTest extends AbstractTest
public function testAccessRanks1() public function testAccessRanks1()
{ {
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setAccessRank(new AccessRank(AccessRank::Admin)); $user->setAccessRank(new AccessRank(AccessRank::Admin));
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user)); $this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts), $user));
@ -31,7 +31,7 @@ class AccessTest extends AbstractTest
getConfig()->privileges->listPosts = 'power-user'; getConfig()->privileges->listPosts = 'power-user';
Access::init(); Access::init();
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setAccessRank(new AccessRank(AccessRank::Admin)); $user->setAccessRank(new AccessRank(AccessRank::Admin));
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user)); $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts), $user));
@ -53,7 +53,7 @@ class AccessTest extends AbstractTest
getConfig()->privileges->{'listPosts.own'} = 'power-user'; getConfig()->privileges->{'listPosts.own'} = 'power-user';
Access::init(); Access::init();
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setAccessRank(new AccessRank(AccessRank::PowerUser)); $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, 'own'), $user));
$this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user)); $this->assert->isTrue(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user));
@ -78,7 +78,7 @@ class AccessTest extends AbstractTest
protected function testSubPrivilegesSubAndGeneral() protected function testSubPrivilegesSubAndGeneral()
{ {
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setAccessRank(new AccessRank(AccessRank::PowerUser)); $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, 'own'), $user));
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts, 'baka'), $user)); $this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts, 'baka'), $user));
@ -110,7 +110,7 @@ class AccessTest extends AbstractTest
protected function testSubPrivilegesMultipleSubAndGeneral() protected function testSubPrivilegesMultipleSubAndGeneral()
{ {
$user = $this->mockUser(); $user = $this->userMocker->mockSingle();
$user->setAccessRank(new AccessRank(AccessRank::PowerUser)); $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, 'own'), $user));
$this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user)); $this->assert->isFalse(Access::check(new Privilege(Privilege::ListPosts, 'all'), $user));

View file

@ -0,0 +1,16 @@
<?php
abstract class AbstractMocker implements IMocker
{
public function mockMultiple($number = null)
{
return \Chibi\Database::transaction(function() use ($number)
{
$ret = [];
foreach (range(1, $number) as $_)
{
$ret []= $this->mockSingle();
}
return $ret;
});
}
}

View file

@ -0,0 +1,19 @@
<?php
class CommentMocker extends AbstractMocker implements IMocker
{
protected $postMocker;
public function __construct(PostMocker $postMocker)
{
$this->postMocker = $postMocker;
}
public function mockSingle()
{
$post = $this->postMocker->mockSingle();
$comment = CommentModel::spawn();
$comment->setPost($post);
$comment->setText('test test');
return CommentModel::save($comment);
}
}

View file

@ -0,0 +1,6 @@
<?php
interface IMocker
{
public function mockSingle();
public function mockMultiple($number);
}

View file

@ -0,0 +1,24 @@
<?php
class PostMocker extends AbstractMocker implements IMocker
{
private $tagMocker;
private $testSupport;
public function __construct(
TagMocker $tagMocker,
TestSupport $testSupport)
{
$this->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);
}
}

View file

@ -0,0 +1,10 @@
<?php
class TagMocker extends AbstractMocker implements IMocker
{
public function mockSingle()
{
$tag = TagModel::spawn();
$tag->setName(uniqid());
return TagModel::save($tag);
}
}

View file

@ -0,0 +1,12 @@
<?php
class UserMocker extends AbstractMocker implements IMocker
{
public function mockSingle()
{
$user = UserModel::spawn();
$user->setAccessRank(new AccessRank(AccessRank::Registered));
$user->setName('dummy'.uniqid());
$user->setPassword('sekai');
return UserModel::save($user);
}
}

View file

@ -25,7 +25,7 @@ class PostModelTest extends AbstractTest
public function testFeaturingRandomPost() public function testFeaturingRandomPost()
{ {
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
PostModel::featureRandomPost(); PostModel::featureRandomPost();
@ -35,16 +35,14 @@ class PostModelTest extends AbstractTest
public function testFeaturingIllegalPosts() public function testFeaturingIllegalPosts()
{ {
$posts = []; $posts = [];
foreach (range(0, 5) as $i) $posts = $this->postMocker->mockMultiple(6);
$posts []= $this->mockPost(Auth::getCurrentUser());
$posts[0]->setSafety(new PostSafety(PostSafety::Sketchy)); $posts[0]->setSafety(new PostSafety(PostSafety::Sketchy));
$posts[1]->setSafety(new PostSafety(PostSafety::Sketchy)); $posts[1]->setSafety(new PostSafety(PostSafety::Sketchy));
$posts[2]->setHidden(true); $posts[2]->setHidden(true);
$posts[3]->setType(new PostType(PostType::Youtube)); $posts[3]->setType(new PostType(PostType::Youtube));
$posts[4]->setType(new PostType(PostType::Flash)); $posts[4]->setType(new PostType(PostType::Flash));
$posts[5]->setType(new PostType(PostType::Video)); $posts[5]->setType(new PostType(PostType::Video));
foreach ($posts as $post) PostModel::save($posts);
PostModel::save($post);
PostModel::featureRandomPost(); PostModel::featureRandomPost();
@ -53,7 +51,7 @@ class PostModelTest extends AbstractTest
public function testAutoFeaturingFirstTime() public function testAutoFeaturingFirstTime()
{ {
$this->mockPost(Auth::getCurrentUser()); $this->postMocker->mockSingle();
$this->assert->doesNotThrow(function() $this->assert->doesNotThrow(function()
{ {
@ -65,7 +63,7 @@ class PostModelTest extends AbstractTest
public function testAutoFeaturingTooSoon() public function testAutoFeaturingTooSoon()
{ {
$this->mockPost(Auth::getCurrentUser()); $this->postMocker->mockSingle();
$this->assert->isTrue(PostModel::featureRandomPostIfNecessary()); $this->assert->isTrue(PostModel::featureRandomPostIfNecessary());
$this->assert->isFalse(PostModel::featureRandomPostIfNecessary()); $this->assert->isFalse(PostModel::featureRandomPostIfNecessary());
@ -75,7 +73,7 @@ class PostModelTest extends AbstractTest
public function testAutoFeaturingOutdated() public function testAutoFeaturingOutdated()
{ {
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$minTimestamp = getConfig()->misc->featuredPostMaxDays * 24 * 3600; $minTimestamp = getConfig()->misc->featuredPostMaxDays * 24 * 3600;
$this->assert->isTrue(PostModel::featureRandomPostIfNecessary()); $this->assert->isTrue(PostModel::featureRandomPostIfNecessary());
@ -89,14 +87,14 @@ class PostModelTest extends AbstractTest
public function testAutoFeaturingDeletedPost() public function testAutoFeaturingDeletedPost()
{ {
$post = $this->mockPost(Auth::getCurrentUser()); $post = $this->postMocker->mockSingle();
$this->assert->isTrue(PostModel::featureRandomPostIfNecessary()); $this->assert->isTrue(PostModel::featureRandomPostIfNecessary());
$this->assert->isNotNull(PostModel::getFeaturedPost()); $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()); $this->assert->isNotNull(PostModel::getFeaturedPost());
} }
} }

View file

@ -90,8 +90,7 @@ class UserModelTest extends AbstractTest
private function prepareTwoUsersWithSameName() private function prepareTwoUsersWithSameName()
{ {
$user1 = $this->mockUser(); list ($user1, $user2) = $this->userMocker->mockMultiple(2);
$user2 = $this->mockUser();
$user1->setName('pikachu'); $user1->setName('pikachu');
$user2->setName('pikachu'); $user2->setName('pikachu');
UserModel::save($user1); UserModel::save($user1);
@ -100,8 +99,7 @@ class UserModelTest extends AbstractTest
private function prepareTwoUsersWithSameEmail($confirmFirst, $confirmSecond) private function prepareTwoUsersWithSameEmail($confirmFirst, $confirmSecond)
{ {
$user1 = $this->mockUser(); list ($user1, $user2) = $this->userMocker->mockMultiple(2);
$user2 = $this->mockUser();
$mail = 'godzilla@whitestar.gov'; $mail = 'godzilla@whitestar.gov';
if ($confirmFirst) if ($confirmFirst)

34
tests/TestSupport.php Normal file
View file

@ -0,0 +1,34 @@
<?php
class TestSupport
{
private $assert;
public function __construct(Assert $assert)
{
$this->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;
}
}