Moved setRelationsFromText logic to controller
This commit is contained in:
parent
4395087a7c
commit
b8bb2c865e
5 changed files with 109 additions and 30 deletions
|
@ -1 +1 @@
|
||||||
Subproject commit a7e3dddbaeb6ba5b711ae3c916307a0896aee758
|
Subproject commit f912adb6115feaac885c0fc6728cff8c8ce606fc
|
|
@ -11,10 +11,15 @@ class EditPostRelationsJob extends AbstractJob
|
||||||
public function execute()
|
public function execute()
|
||||||
{
|
{
|
||||||
$post = $this->postRetriever->retrieve();
|
$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());
|
$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());
|
$newRelatedIds = array_map(function($post) { return $post->getId(); }, $post->getRelations());
|
||||||
|
|
||||||
if ($this->getContext() == self::CONTEXT_NORMAL)
|
if ($this->getContext() == self::CONTEXT_NORMAL)
|
||||||
|
|
|
@ -133,7 +133,7 @@ class PostController
|
||||||
JobArgs::ARG_NEW_SAFETY => InputHelper::get('safety'),
|
JobArgs::ARG_NEW_SAFETY => InputHelper::get('safety'),
|
||||||
JobArgs::ARG_NEW_TAG_NAMES => $this->splitTags(InputHelper::get('tags')),
|
JobArgs::ARG_NEW_TAG_NAMES => $this->splitTags(InputHelper::get('tags')),
|
||||||
JobArgs::ARG_NEW_SOURCE => InputHelper::get('source'),
|
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')))
|
if (!empty(InputHelper::get('url')))
|
||||||
|
@ -284,11 +284,18 @@ class PostController
|
||||||
$context->layoutName = 'layout-file';
|
$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)
|
protected function splitTags($string)
|
||||||
{
|
{
|
||||||
$tags = trim($string);
|
$tags = preg_split('/[,;\s]+/', trim($string));
|
||||||
$tags = preg_split('/[,;\s]+/', $tags);
|
|
||||||
$tags = array_filter($tags, function($x) { return $x != ''; });
|
$tags = array_filter($tags, function($x) { return $x != ''; });
|
||||||
$tags = array_unique($tags);
|
$tags = array_unique($tags);
|
||||||
return $tags;
|
return $tags;
|
||||||
|
|
|
@ -165,30 +165,6 @@ final class PostEntity extends AbstractEntity implements IValidatable
|
||||||
$this->setCache('relations', $relations);
|
$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()
|
public function getTags()
|
||||||
{
|
{
|
||||||
if ($this->hasCache('tags'))
|
if ($this->hasCache('tags'))
|
||||||
|
|
91
tests/JobTests/EditPostRelationsJobTest.php
Normal file
91
tests/JobTests/EditPostRelationsJobTest.php
Normal 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()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue