Moved setRelationsFromText logic to controller

This commit is contained in:
Marcin Kurczewski 2014-05-12 22:39:14 +02:00
parent 4395087a7c
commit b8bb2c865e
5 changed files with 109 additions and 30 deletions

@ -1 +1 @@
Subproject commit a7e3dddbaeb6ba5b711ae3c916307a0896aee758
Subproject commit f912adb6115feaac885c0fc6728cff8c8ce606fc

View file

@ -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)

View file

@ -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;

View file

@ -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'))

View file

@ -0,0 +1,91 @@
<?php
class EditPostRelationsJobTest extends AbstractTest
{
public function testEditing()
{
$basePost = $this->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()));
}
}