Skip to content

Commit

Permalink
Merge pull request #10 from yii-podium/locked-thread-check
Browse files Browse the repository at this point in the history
Checking if thread is locked when creating post
  • Loading branch information
Bizley authored Oct 17, 2020
2 parents 4e275f1 + 78b7be7 commit 5c4d5c7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Interfaces/ThreadRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public function move(ForumRepositoryInterface $forum): bool;

public function lock(): bool;

public function isLocked(): bool;

public function unlock(): bool;

public function archive(): bool;
Expand Down
1 change: 1 addition & 0 deletions src/Messages/en/podium.error.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'thread.already.archived' => 'Thread is already archived.',
'thread.already.hidden' => 'Thread is already hidden.',
'thread.already.subscribed' => 'Thread is already subscribed.',
'thread.locked' => 'Thread is locked.',
'thread.must.be.archived' => 'Thread must be archived first.',
'thread.not.archived' => 'Thread is not archived.',
'thread.not.hidden' => 'Thread is not hidden.',
Expand Down
4 changes: 4 additions & 0 deletions src/Services/Post/PostBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public function create(
throw new ServiceException(['api' => Yii::t('podium.error', 'member.banned')]);
}

if ($thread->isLocked()) {
throw new ServiceException(['api' => Yii::t('podium.error', 'thread.locked')]);
}

/** @var ForumRepositoryInterface $threadParent */
$threadParent = $thread->getParent();
if (!$post->create($author, $thread, $data)) {
Expand Down
25 changes: 23 additions & 2 deletions tests/Unit/Post/PostBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public function testCreateShouldReturnErrorWhenCreatingErrored(): void
$post = $this->createMock(PostRepositoryInterface::class);
$post->method('create')->willReturn(false);
$post->method('getErrors')->willReturn([1]);
$result = $this->service->create($post, $author, $this->createMock(ThreadRepositoryInterface::class));
$thread = $this->createMock(ThreadRepositoryInterface::class);
$thread->method('isLocked')->willReturn(false);
$result = $this->service->create($post, $author, $thread);

self::assertFalse($result->getResult());
self::assertSame([1], $result->getErrors());
Expand All @@ -84,6 +86,20 @@ public function testCreateShouldReturnErrorWhenAuthorIsBanned(): void
self::assertSame(['api' => 'member.banned'], $result->getErrors());
}

public function testCreateShouldReturnErrorWhenThreadIsLocked(): void
{
$this->transaction->expects(self::once())->method('rollBack');

$author = $this->createMock(MemberRepositoryInterface::class);
$author->method('isBanned')->willReturn(false);
$thread = $this->createMock(ThreadRepositoryInterface::class);
$thread->method('isLocked')->willReturn(true);
$result = $this->service->create($this->createMock(PostRepositoryInterface::class), $author, $thread);

self::assertFalse($result->getResult());
self::assertSame(['api' => 'thread.locked'], $result->getErrors());
}

public function testCreateShouldReturnSuccessWhenCreatingIsDone(): void
{
$this->transaction->expects(self::once())->method('commit');
Expand All @@ -94,6 +110,7 @@ public function testCreateShouldReturnSuccessWhenCreatingIsDone(): void
$post->method('create')->willReturn(true);
$thread = $this->createMock(ThreadRepositoryInterface::class);
$thread->method('updateCounters')->with(1)->willReturn(true);
$thread->method('isLocked')->willReturn(false);
$forum = $this->createMock(ForumRepositoryInterface::class);
$forum->method('updateCounters')->with(0, 1)->willReturn(true);
$thread->method('getParent')->willReturn($forum);
Expand All @@ -119,7 +136,9 @@ static function (array $data) {
$author->method('isBanned')->willReturn(false);
$post = $this->createMock(PostRepositoryInterface::class);
$post->method('create')->willThrowException(new Exception('exc'));
$result = $this->service->create($post, $author, $this->createMock(ThreadRepositoryInterface::class));
$thread = $this->createMock(ThreadRepositoryInterface::class);
$thread->method('isLocked')->willReturn(false);
$result = $this->service->create($post, $author, $thread);

self::assertFalse($result->getResult());
self::assertSame('exc', $result->getErrors()['exception']->getMessage());
Expand All @@ -135,6 +154,7 @@ public function testCreateShouldReturnErrorWhenUpdatingThreadCountersErrored():
$post->method('create')->willReturn(true);
$thread = $this->createMock(ThreadRepositoryInterface::class);
$thread->method('updateCounters')->willReturn(false);
$thread->method('isLocked')->willReturn(false);
$result = $this->service->create($post, $author, $thread);

self::assertFalse($result->getResult());
Expand All @@ -151,6 +171,7 @@ public function testCreateShouldReturnErrorWhenUpdatingForumCountersErrored(): v
$post->method('create')->willReturn(true);
$thread = $this->createMock(ThreadRepositoryInterface::class);
$thread->method('updateCounters')->willReturn(true);
$thread->method('isLocked')->willReturn(false);
$forum = $this->createMock(ForumRepositoryInterface::class);
$forum->method('updateCounters')->willReturn(false);
$thread->method('getParent')->willReturn($forum);
Expand Down

0 comments on commit 5c4d5c7

Please sign in to comment.