Added tests for post list sort styles

This commit is contained in:
Marcin Kurczewski 2014-05-13 21:44:41 +02:00
parent a2507370cc
commit a99e0d93f0

View file

@ -28,6 +28,16 @@ class ListPostsJobTest extends AbstractTest
$this->assert->areEqual(2, $ret->page);
}
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 testAutomaticSafetyFilterOnlySafeEnabled()
{
$user = $this->userMocker->mockSingle();
@ -41,11 +51,7 @@ class ListPostsJobTest extends AbstractTest
$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);
$posts = $this->preparePostsWithSafety();
$ret = $this->assert->doesNotThrow(function()
{
@ -72,11 +78,7 @@ class ListPostsJobTest extends AbstractTest
$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);
$posts = $this->preparePostsWithSafety();
$ret = $this->assert->doesNotThrow(function()
{
@ -282,19 +284,18 @@ class ListPostsJobTest extends AbstractTest
public function testUploads()
{
$this->grantAccess('listPosts');
$user1 = $this->userMocker->mockSingle();
$user2 = $this->userMocker->mockSingle();
$users = $this->userMocker->mockMultiple(2);
$posts = $this->postMocker->mockMultiple(3);
$posts[0]->setUploader($user1);
$posts[1]->setUploader($user2);
$posts[2]->setUploader($user1);
$posts[0]->setUploader($users[0]);
$posts[1]->setUploader($users[1]);
$posts[2]->setUploader($users[0]);
PostModel::save($posts);
foreach (['submit', 'upload', 'uploads', 'uploader', 'uploaded'] as $alias)
{
$ret = $this->assert->doesNotThrow(function() use ($alias, $user1)
$ret = $this->assert->doesNotThrow(function() use ($alias, $users)
{
return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':' . $user1->getName()]);
return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $alias . ':' . $users[0]->getName()]);
});
$this->assert->areEqual(2, $ret->entityCount);
@ -336,13 +337,7 @@ class ListPostsJobTest extends AbstractTest
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);
$posts = $this->preparePostsWithScores();
foreach (['scoremin', 'score_min'] as $alias)
{
@ -372,13 +367,7 @@ class ListPostsJobTest extends AbstractTest
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]);
$posts = $this->preparePostsWithFavs();
foreach (['favmin', 'fav_min'] as $alias)
{
@ -408,19 +397,7 @@ class ListPostsJobTest extends AbstractTest
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);
}
$posts = $this->preparePostsWithComments();
foreach (['commentmin', 'comment_min'] as $alias)
{
@ -450,11 +427,7 @@ class ListPostsJobTest extends AbstractTest
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);
$posts = $this->preparePostsWithTags();
foreach (['tagmin', 'tag_min'] as $alias)
{
@ -464,7 +437,7 @@ class ListPostsJobTest extends AbstractTest
});
$this->assert->areEqual(2, $ret->entityCount);
$this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId());
$this->assert->areEqual($posts[1]->getId(), $ret->entities[0]->getId());
$this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId());
}
@ -481,14 +454,10 @@ class ListPostsJobTest extends AbstractTest
}
}
public function testDateMinMax()
public function testDate()
{
$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);
$posts = $this->preparePostsWithDates();
$ret = $this->assert->doesNotThrow(function()
{
@ -496,7 +465,13 @@ class ListPostsJobTest extends AbstractTest
});
$this->assert->areEqual(1, $ret->entityCount);
$this->assert->areEqual($posts[1]->getId(), $ret->entities[0]->getId());
$this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId());
}
public function testDateMinMax()
{
$this->grantAccess('listPosts');
$posts = $this->preparePostsWithDates();
foreach (['datemin', 'date_min'] as $alias)
{
@ -506,7 +481,7 @@ class ListPostsJobTest extends AbstractTest
});
$this->assert->areEqual(2, $ret->entityCount);
$this->assert->areEqual($posts[1]->getId(), $ret->entities[0]->getId());
$this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId());
$this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId());
}
@ -533,35 +508,10 @@ class ListPostsJobTest extends AbstractTest
}, '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);
$posts = $this->preparePostsWithTypes();
$ret = $this->assert->doesNotThrow(function()
{
@ -605,14 +555,20 @@ class ListPostsJobTest extends AbstractTest
}
}
public function testInvalidType()
{
$this->grantAccess('listPosts');
$this->assert->throws(function()
{
Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'type:nonsense']);
}, 'invalid');
}
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);
$posts = $this->preparePostsWithTags();
$ret = $this->assert->doesNotThrow(function()
{
@ -627,11 +583,7 @@ class ListPostsJobTest extends AbstractTest
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);
$posts = $this->preparePostsWithTags();
$ret = $this->assert->doesNotThrow(function()
{
@ -641,4 +593,212 @@ class ListPostsJobTest extends AbstractTest
$this->assert->areEqual(1, $ret->entityCount);
$this->assert->areEqual($posts[1]->getId(), $ret->entities[0]->getId());
}
public function testOrderById()
{
$this->grantAccess('listPosts');
$posts = $this->preparePostsWithTags();
$this->testOrder('order:id', [$posts[2], $posts[1], $posts[0]]);
}
public function testOrderByFavCount()
{
$this->grantAccess('listPosts');
$posts = $this->preparePostsWithFavs();
foreach (['fav', 'favs', 'favcount', 'fav_count'] as $alias)
$this->testOrder('order:' . $alias, [$posts[0], $posts[2], $posts[1]]);
}
public function testOrderByCommentCount()
{
$this->grantAccess('listPosts');
$posts = $this->preparePostsWithComments();
foreach (['comment', 'comments', 'commentcount', 'comment_count'] as $alias)
$this->testOrder('order:' . $alias, [$posts[0], $posts[2], $posts[1]]);
}
public function testOrderByScores()
{
$this->grantAccess('listPosts');
$posts = $this->preparePostsWithScores();
$this->testOrder('order:score', [$posts[0], $posts[2], $posts[1]]);
}
public function testOrderByDates()
{
$this->grantAccess('listPosts');
$posts = $this->preparePostsWithDates();
$this->testOrder('order:date', [$posts[0], $posts[2], $posts[1]]);
}
public function testOrderByCommentDates()
{
$this->grantAccess('listPosts');
$posts = $this->preparePostsWithComments();
foreach (['commentdate', 'comment_date'] as $alias)
$this->testOrder('order:' . $alias, [$posts[2], $posts[0], $posts[1]]);
}
public function testOrderByFavDates()
{
$this->grantAccess('listPosts');
$posts = $this->preparePostsWithFavs();
foreach (['favdate', 'fav_date'] as $alias)
$this->testOrder('order:' . $alias, [$posts[2], $posts[0], $posts[1]]);
}
public function testOrderByFilesize()
{
$this->grantAccess('listPosts');
$posts = $this->postMocker->mockMultiple(3);
$posts[0]->setFileSize(100);
$posts[1]->setFileSize(50);
$posts[2]->setFileSize(300);
PostModel::save($posts);
foreach (['filesize', 'file_size'] as $alias)
$this->testOrder('order:' . $alias, [$posts[2], $posts[0], $posts[1]]);
}
public function testOrderByRandom()
{
$this->grantAccess('listPosts');
$num = 15;
$posts = $this->postMocker->mockMultiple($num);
$expectedPostIdsSorted = range(1, $num);
$ret = $this->assert->doesNotThrow(function()
{
return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'order:random']);
});
$postIds1 = array_map(function($x) { return $x->getId(); }, $ret->entities);
$this->assert->areNotEquivalent($expectedPostIdsSorted, $postIds1);
$postIds1Sorted = $postIds1 + [];
sort($postIds1Sorted);
$this->assert->areEquivalent($expectedPostIdsSorted, $postIds1Sorted);
$ret = $this->assert->doesNotThrow(function()
{
return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'order:random']);
});
$postIds2 = array_map(function($x) { return $x->getId(); }, $ret->entities);
$this->assert->areEquivalent($postIds1, $postIds2);
}
public function testInvalidOrderToken()
{
$this->grantAccess('listPosts');
$this->assert->throws(function()
{
Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => 'order:nonsense']);
}, 'invalid');
}
private function testOrder($query, $expectedPosts)
{
$ret = $this->assert->doesNotThrow(function() use ($query)
{
return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $query]);
});
$this->assert->areEqual(count($expectedPosts), $ret->entityCount);
foreach ($expectedPosts as $i => $expectedPost)
$this->assert->areEqual($expectedPost->getId(), $ret->entities[$i]->getId());
$expectedPosts = array_reverse($expectedPosts);
$ret = $this->assert->doesNotThrow(function() use ($query)
{
return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => '-' . $query]);
});
$this->assert->areEqual(count($expectedPosts), $ret->entityCount);
foreach ($expectedPosts as $i => $expectedPost)
$this->assert->areEqual($expectedPost->getId(), $ret->entities[$i]->getId());
}
private function preparePostsWithScores()
{
$posts = $this->postMocker->mockMultiple(3);
$users = $this->userMocker->mockMultiple(2);
UserModel::updateUserScore($users[0], $posts[0], 1);
UserModel::updateUserScore($users[1], $posts[0], 1);
UserModel::updateUserScore($users[0], $posts[2], 1);
return $posts;
}
private function preparePostsWithSafety()
{
$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));
return PostModel::save($posts);
}
private function preparePostsWithTypes()
{
$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));
return PostModel::save($posts);
}
private function preparePostsWithTags()
{
$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']));
return PostModel::save($posts);
}
private function preparePostsWithFavs()
{
$posts = $this->postMocker->mockMultiple(3);
$users = $this->userMocker->mockMultiple(2);
UserModel::addToUserFavorites($users[0], $posts[0]);
UserModel::addToUserFavorites($users[1], $posts[0]);
UserModel::addToUserFavorites($users[0], $posts[2]);
return $posts;
}
private function preparePostsWithComments()
{
$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);
}
return $posts;
}
private function preparePostsWithDates()
{
$posts = $this->postMocker->mockMultiple(3);
$posts[0]->setCreationTime(mktime(0, 0, 0, 10, 23, 1990));
$posts[1]->setCreationTime(mktime(0, 0, 0, 10, 21, 1990));
$posts[2]->setCreationTime(mktime(0, 0, 0, 10, 22, 1990));
return PostModel::save($posts);
}
}