Fixed being able to add post relation to itself

This commit is contained in:
Marcin Kurczewski 2014-08-03 22:06:13 +02:00
parent 9e29441c68
commit 0d9f39d645
2 changed files with 66 additions and 1 deletions

View file

@ -203,7 +203,8 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
throw new Exception('All related posts must be saved');
$uniqueRelations = [];
foreach ($relations as $relatedPost)
$uniqueRelations[$relatedPost->getId()] = $relatedPost;
if ($relatedPost->getId() != $this->getId())
$uniqueRelations[$relatedPost->getId()] = $relatedPost;
$relations = array_values($uniqueRelations);
$this->setCache('relations', $relations);
}

View file

@ -28,6 +28,70 @@ class EditPostRelationsJobTest extends AbstractTest
$this->assert->areEqual($post2->getId(), $basePost->getRelations()[1]->getId());
}
public function testBothDirections()
{
$this->grantAccess('editPostRelations');
list ($basePost, $post1, $post2)
= $this->postMocker->mockMultiple(3);
$basePost = $this->assert->doesNotThrow(function() use ($basePost, $post1, $post2)
{
return Api::run(
new EditPostRelationsJob(),
[
JobArgs::ARG_POST_ID => $basePost->getId(),
JobArgs::ARG_POST_REVISION => $basePost->getRevision(),
JobArgs::ARG_NEW_RELATED_POST_IDS =>
[
$post1->getId(),
$post2->getId(),
]
]);
});
$post1 = PostModel::getById($post1->getId());
$post2 = PostModel::getById($post2->getId());
$this->assert->areEqual(2, count($basePost->getRelations()));
$this->assert->areEqual($post1->getId(), $basePost->getRelations()[0]->getId());
$this->assert->areEqual($post2->getId(), $basePost->getRelations()[1]->getId());
$this->assert->areEqual(1, count($post1->getRelations()));
$this->assert->areEqual($basePost->getId(), $post1->getRelations()[0]->getId());
$this->assert->areEqual(1, count($post2->getRelations()));
$this->assert->areEqual($basePost->getId(), $post2->getRelations()[0]->getId());
}
public function testRelationsToItself()
{
$this->grantAccess('editPostRelations');
list ($basePost, $post1, $post2)
= $this->postMocker->mockMultiple(3);
$basePost = $this->assert->doesNotThrow(function() use ($basePost, $post1, $post2)
{
return Api::run(
new EditPostRelationsJob(),
[
JobArgs::ARG_POST_ID => $basePost->getId(),
JobArgs::ARG_POST_REVISION => $basePost->getRevision(),
JobArgs::ARG_NEW_RELATED_POST_IDS =>
[
$post1->getId(),
$basePost->getId(),
$post2->getId(),
]
]);
});
$this->assert->areEqual(2, count($basePost->getRelations()));
$this->assert->areEqual($post1->getId(), $basePost->getRelations()[0]->getId());
$this->assert->areEqual($post2->getId(), $basePost->getRelations()[1]->getId());
}
public function testOverwriting()
{
$this->grantAccess('editPostRelations');