From b8bb2c865e3c358719058f8100e798cd40b07a01 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 12 May 2014 22:39:14 +0200 Subject: [PATCH] Moved setRelationsFromText logic to controller --- lib/chibi-sql | 2 +- .../Jobs/PostJobs/EditPostRelationsJob.php | 9 +- src/Controllers/PostController.php | 13 ++- src/Models/Entities/PostEntity.php | 24 ----- tests/JobTests/EditPostRelationsJobTest.php | 91 +++++++++++++++++++ 5 files changed, 109 insertions(+), 30 deletions(-) create mode 100644 tests/JobTests/EditPostRelationsJobTest.php diff --git a/lib/chibi-sql b/lib/chibi-sql index a7e3dddb..f912adb6 160000 --- a/lib/chibi-sql +++ b/lib/chibi-sql @@ -1 +1 @@ -Subproject commit a7e3dddbaeb6ba5b711ae3c916307a0896aee758 +Subproject commit f912adb6115feaac885c0fc6728cff8c8ce606fc diff --git a/src/Api/Jobs/PostJobs/EditPostRelationsJob.php b/src/Api/Jobs/PostJobs/EditPostRelationsJob.php index 41b0ab08..7ccdbeed 100644 --- a/src/Api/Jobs/PostJobs/EditPostRelationsJob.php +++ b/src/Api/Jobs/PostJobs/EditPostRelationsJob.php @@ -11,10 +11,15 @@ class EditPostRelationsJob extends AbstractJob public function execute() { $post = $this->postRetriever->retrieve(); - $relations = $this->getArgument(JobArgs::ARG_NEW_RELATED_POST_IDS); + $relatedPostIds = $this->getArgument(JobArgs::ARG_NEW_RELATED_POST_IDS); + + if (!is_array($relatedPostIds)) + throw new SimpleException('Expected array'); + + $relatedPosts = PostModel::getAllByIds($relatedPostIds); $oldRelatedIds = array_map(function($post) { return $post->getId(); }, $post->getRelations()); - $post->setRelationsFromText($relations); + $post->setRelations($relatedPosts); $newRelatedIds = array_map(function($post) { return $post->getId(); }, $post->getRelations()); if ($this->getContext() == self::CONTEXT_NORMAL) diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 934ac5eb..134949d5 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -133,7 +133,7 @@ class PostController JobArgs::ARG_NEW_SAFETY => InputHelper::get('safety'), JobArgs::ARG_NEW_TAG_NAMES => $this->splitTags(InputHelper::get('tags')), JobArgs::ARG_NEW_SOURCE => InputHelper::get('source'), - JobArgs::ARG_NEW_RELATED_POST_IDS => InputHelper::get('relations'), + JobArgs::ARG_NEW_RELATED_POST_IDS => $this->splitPostIds(InputHelper::get('relations')), ]; if (!empty(InputHelper::get('url'))) @@ -284,11 +284,18 @@ class PostController $context->layoutName = 'layout-file'; } + protected function splitPostIds($string) + { + $ids = preg_split('/\D/', trim($string)); + $ids = array_filter($ids, function($x) { return $x != ''; }); + $ids = array_map('intval', $ids); + $ids = array_unique($ids); + return $ids; + } protected function splitTags($string) { - $tags = trim($string); - $tags = preg_split('/[,;\s]+/', $tags); + $tags = preg_split('/[,;\s]+/', trim($string)); $tags = array_filter($tags, function($x) { return $x != ''; }); $tags = array_unique($tags); return $tags; diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 2ba08ba7..80473743 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -165,30 +165,6 @@ final class PostEntity extends AbstractEntity implements IValidatable $this->setCache('relations', $relations); } - public function setRelationsFromText($relationsText) - { - $config = getConfig(); - $relatedIds = array_filter(preg_split('/\D/', $relationsText)); - - $relatedPosts = []; - foreach ($relatedIds as $relatedId) - { - if ($relatedId == $this->getId()) - continue; - - if (count($relatedPosts) > $config->browsing->maxRelatedPosts) - { - throw new SimpleException( - 'Too many related posts (maximum: %d)', - $config->browsing->maxRelatedPosts); - } - - $relatedPosts []= PostModel::getById($relatedId); - } - - $this->setRelations($relatedPosts); - } - public function getTags() { if ($this->hasCache('tags')) diff --git a/tests/JobTests/EditPostRelationsJobTest.php b/tests/JobTests/EditPostRelationsJobTest.php new file mode 100644 index 00000000..319c1986 --- /dev/null +++ b/tests/JobTests/EditPostRelationsJobTest.php @@ -0,0 +1,91 @@ +mockPost($this->mockUser()); + $this->grantAccess('editPostRelations'); + + $post1 = $this->mockPost($this->mockUser()); + $post2 = $this->mockPost($this->mockUser()); + + $basePost = $this->assert->doesNotThrow(function() use ($basePost, $post1, $post2) + { + return Api::run( + new EditPostRelationsJob(), + [ + JobArgs::ARG_POST_ID => $basePost->getId(), + JobArgs::ARG_NEW_RELATED_POST_IDS => + [ + $post1->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() + { + $basePost = $this->mockPost($this->mockUser()); + $this->grantAccess('editPostRelations'); + + $post1 = $this->mockPost($this->mockUser()); + $post2 = $this->mockPost($this->mockUser()); + + $basePost->setRelations([$post1]); + PostModel::save($basePost); + + $this->assert->areEqual(1, count($basePost->getRelations())); + $this->assert->areEqual($post1->getId(), $basePost->getRelations()[0]->getId()); + + $basePost = $this->assert->doesNotThrow(function() use ($basePost, $post2) + { + return Api::run( + new EditPostRelationsJob(), + [ + JobArgs::ARG_POST_ID => $basePost->getId(), + JobArgs::ARG_NEW_RELATED_POST_IDS => + [ + $post2->getId(), + ] + ]); + }); + + $this->assert->areEqual(1, count($basePost->getRelations())); + $this->assert->areEqual($post2->getId(), $basePost->getRelations()[0]->getId()); + } + + public function testOverwritingEmpty() + { + $basePost = $this->mockPost($this->mockUser()); + $this->grantAccess('editPostRelations'); + + $post1 = $this->mockPost($this->mockUser()); + $post2 = $this->mockPost($this->mockUser()); + + $basePost->setRelations([$post1]); + PostModel::save($basePost); + + $this->assert->areEqual(1, count($basePost->getRelations())); + $this->assert->areEqual($post1->getId(), $basePost->getRelations()[0]->getId()); + + $basePost = $this->assert->doesNotThrow(function() use ($basePost) + { + return Api::run( + new EditPostRelationsJob(), + [ + JobArgs::ARG_POST_ID => $basePost->getId(), + JobArgs::ARG_NEW_RELATED_POST_IDS => + [ + ] + ]); + }); + + $basePost = PostModel::getById($basePost->getId()); + $this->assert->areEqual(0, count($basePost->getRelations())); + } +}