diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 669c69c1..96451c39 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -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); } diff --git a/tests/Tests/JobTests/EditPostRelationsJobTest.php b/tests/Tests/JobTests/EditPostRelationsJobTest.php index ae733acd..92afdb23 100644 --- a/tests/Tests/JobTests/EditPostRelationsJobTest.php +++ b/tests/Tests/JobTests/EditPostRelationsJobTest.php @@ -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');