Fixed being able to add post relation to itself
This commit is contained in:
parent
9e29441c68
commit
0d9f39d645
2 changed files with 66 additions and 1 deletions
|
@ -203,6 +203,7 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
|
||||||
throw new Exception('All related posts must be saved');
|
throw new Exception('All related posts must be saved');
|
||||||
$uniqueRelations = [];
|
$uniqueRelations = [];
|
||||||
foreach ($relations as $relatedPost)
|
foreach ($relations as $relatedPost)
|
||||||
|
if ($relatedPost->getId() != $this->getId())
|
||||||
$uniqueRelations[$relatedPost->getId()] = $relatedPost;
|
$uniqueRelations[$relatedPost->getId()] = $relatedPost;
|
||||||
$relations = array_values($uniqueRelations);
|
$relations = array_values($uniqueRelations);
|
||||||
$this->setCache('relations', $relations);
|
$this->setCache('relations', $relations);
|
||||||
|
|
|
@ -28,6 +28,70 @@ class EditPostRelationsJobTest extends AbstractTest
|
||||||
$this->assert->areEqual($post2->getId(), $basePost->getRelations()[1]->getId());
|
$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()
|
public function testOverwriting()
|
||||||
{
|
{
|
||||||
$this->grantAccess('editPostRelations');
|
$this->grantAccess('editPostRelations');
|
||||||
|
|
Loading…
Reference in a new issue