szurubooru/tests/JobTests/TogglePostFavoriteJobTest.php

110 lines
3 KiB
PHP
Raw Normal View History

2014-05-09 21:08:13 +02:00
<?php
class TogglePostFavoriteJobTest extends AbstractTest
{
public function testFaving()
{
$this->grantAccess('favoritePost');
$user = $this->mockUser();
$this->login($user);
$post = $this->mockPost($user);
$this->assert->areEqual(0, $post->getScore());
$this->assert->areEqual(0, $post->getFavoriteCount());
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostFavoriteJob(),
[
JobArgs::ARG_POST_ID => $post->getId(),
JobArgs::ARG_NEW_STATE => 1,
2014-05-09 21:08:13 +02:00
]);
});
$this->assert->areEqual(1, $post->getScore());
$this->assert->areEqual(1, $post->getFavoriteCount());
$this->assert->areEqual(1, $user->getFavoriteCount());
$this->assert->areEqual(1, $user->getScore($post));
$this->assert->areEqual(true, $user->hasFavorited($post));
}
public function testDefaving()
{
$this->grantAccess('favoritePost');
$user = $this->mockUser();
$this->login($user);
$post = $this->mockPost($user);
$this->assert->areEqual(0, $post->getScore());
$this->assert->areEqual(0, $post->getFavoriteCount());
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostFavoriteJob(),
[
JobArgs::ARG_POST_ID => $post->getId(),
JobArgs::ARG_NEW_STATE => 1,
2014-05-09 21:08:13 +02:00
]);
});
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostFavoriteJob(),
[
JobArgs::ARG_POST_ID => $post->getId(),
JobArgs::ARG_NEW_STATE => 0,
2014-05-09 21:08:13 +02:00
]);
});
$this->assert->areEqual(1, $post->getScore());
$this->assert->areEqual(0, $post->getFavoriteCount());
$this->assert->areEqual(0, $user->getFavoriteCount());
$this->assert->areEqual(1, $user->getScore($post));
$this->assert->areEqual(false, $user->hasFavorited($post));
}
public function testFavingTwoPeople()
{
$this->grantAccess('favoritePost');
$user1 = $this->mockUser();
$user2 = $this->mockUser();
$post = $this->mockPost($user1);
$this->assert->areEqual(0, $post->getScore());
$this->assert->areEqual(0, $post->getFavoriteCount());
$this->login($user1);
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostFavoriteJob(),
[
JobArgs::ARG_POST_ID => $post->getId(),
JobArgs::ARG_NEW_STATE => 1,
2014-05-09 21:08:13 +02:00
]);
});
$this->login($user2);
$post = $this->assert->doesNotThrow(function() use ($post)
{
return Api::run(
new TogglePostFavoriteJob(),
[
JobArgs::ARG_POST_ID => $post->getId(),
JobArgs::ARG_NEW_STATE => 1,
2014-05-09 21:08:13 +02:00
]);
});
$this->assert->areEqual(2, $post->getScore());
$this->assert->areEqual(2, $post->getFavoriteCount());
$this->assert->areEqual(1, $user1->getFavoriteCount());
$this->assert->areEqual(1, $user1->getScore($post));
$this->assert->areEqual(1, $user2->getFavoriteCount());
$this->assert->areEqual(1, $user2->getScore($post));
$this->assert->areEqual(true, $user1->hasFavorited($post));
$this->assert->areEqual(true, $user2->hasFavorited($post));
}
}