CommentPolicyTest.php 7.13 KB
<?php

namespace FootyRoom\Tests\Integration\Comment;

use DateTime;
use FootyRoom\Tests\SetsUpFixtures;
use FootyRoom\Core\Comment\CommentPolicy;
use FootyRoom\User\User;
use FootyRoom\Queries\Ban\Ban;
use FootyRoom\Queries\Ban\BanQueryHandler;
use FootyRoom\Queries\Comment\CommentQueryHandler;
use FootyRoom\Support\AutoMapper;
use FootyRoom\Tests\TestCase;

class CommentPolicyTest extends TestCase
{
    use SetsUpFixtures;

    public function setUp()
    {
        parent::setup();

        $this->mockBanQueryHandler = $this->getMockBuilder(BanQueryHandler::class)
                            ->disableOriginalConstructor()
                            ->setMethods(['findActiveBan'])
                            ->getMock();

        $this->mockCommentQueryHandler = $this->getMockBuilder(CommentQueryHandler::class)
                            ->disableOriginalConstructor()
                            ->setMethods(['commentsInPeriod'])
                            ->getMock();

        $this->commentPolicy = new CommentPolicy($this->mockBanQueryHandler, $this->mockCommentQueryHandler);
    }

    /**
     * @doesNotPerformAssertions
     */
    public function testCanComment()
    {
        $user = new User(['user_id' => 41301]);
        $throwException = true;

        $this->mockBanQueryHandler->method('findActiveBan')
                            ->will($this->returnValue(false));

        $this->commentPolicy->canComment($user, $throwException);
    }

    public function testCommentsInPeriodWasIfUserIsNewbornAndNotBanned()
    {
        $user = new User(['user_id' => 41301, 'status' => 'newborn']);
        $throwException = false;

        $this->mockCommentQueryHandler->expects($this->once())
                                ->method('commentsInPeriod')
                                ->will($this->returnValue(2));

        $result = $this->commentPolicy->canComment($user, $throwException);
    }

    /**
     * @expectedException        \FootyRoom\Core\CoreException
     */
    public function testNewbornUserCanNotCommentIfCommentCountMoreThanFiveWithThrowException()
    {
        $user = new User(['user_id' => 41301, 'status' => 'newborn']);
        $throwException = true;

        $this->mockBanQueryHandler->method('findActiveBan')
                            ->will($this->returnValue(null));

        $this->mockCommentQueryHandler->method('commentsInPeriod')
                            ->will($this->returnValue(5));

        $this->commentPolicy->canComment($user, $throwException);
    }

    public function testNewbornUserCanNotCommentIfReachedDailyLimit()
    {
        $user = new User(['user_id' => 41301, 'status' => 'newborn']);
        $throwException = false;

        $this->mockCommentQueryHandler->method('commentsInPeriod')
                                ->will($this->returnValue(5));

        $result = $this->commentPolicy->canComment($user, $throwException);

        $this->assertFalse($result);
    }

    public function testNewbornUserCanCommentIfCommentCountLessThanFive()
    {
        $user = new User(['user_id' => 41301, 'status' => 'newborn']);
        $throwException = false;

        $this->mockCommentQueryHandler->method('commentsInPeriod')
                                ->will($this->returnValue(2));

        $result = $this->commentPolicy->canComment($user, $throwException);

        $this->assertTrue($result);
    }

    /**
     * @expectedException        \FootyRoom\Core\CoreException
     */
    public function testUserHasBeenBannedCanNotComment()
    {
        $banDto = (object) [
            'createdAt' => new DateTime(),
            'duration' => 10,
        ];
        $ban = AutoMapper::map($banDto, Ban::class);
        $user = new User(['user_id' => 41301]);

        $throwException = true;

        $this->mockBanQueryHandler->method('findActiveBan')
                            ->will($this->returnValue($ban));

        $this->commentPolicy->canComment($user, $throwException);
    }

    public function testCanModerate()
    {
        $user = new User(['user_id' => 41301, 'user_role' => 20]);
        $throwException = true;

        $result = $this->commentPolicy->canModerate($user, $throwException);

        $this->assertTrue($result);
    }

    /**
     * @expectedException        \FootyRoom\Core\AuthException
     */
    public function testCanNotModerateWithThrowException()
    {
        $user = new User(['user_id' => 41301, 'user_role' => 10]);
        $throwException = true;

        $result = $this->commentPolicy->canModerate($user, $throwException);
    }

    public function testCanNotModerateWithOutThrowException()
    {
        $user = new User(['user_id' => 41301, 'user_role' => 10]);
        $throwException = false;

        $result = $this->commentPolicy->canModerate($user, $throwException);
        $this->assertFalse($result);
    }

    public function testGetVisibleMeta()
    {
        $result = CommentPolicy::getVisibleMeta(25);
        $this->assertEquals(['edited'], $result);
    }

    public function testSeeUnpublished()
    {
        $result = CommentPolicy::seeUnpublished(25);
        $this->assertTrue($result);
    }

    public function testCanNotSeeUnpublished()
    {
        $result = CommentPolicy::seeUnpublished(10);
        $this->assertFalse($result);
    }

    public function testUserCanEditHisComment()
    {
        $user = new User(['user_id' => 41301]);
        $commentUserId = 41301;
        $discussionId = 'test:1111';
        $throwException = true;

        $result = $this->commentPolicy->canEdit($user, $commentUserId, $discussionId, $throwException);

        $this->assertTrue($result);
    }

    public function testCheckModeratorCanEditComment()
    {
        $user = new User(['user_id' => 41301, 'user_role' => 20]);
        $commentUserId = 4321;
        $discussionId = 'forum:1111';
        $throwException = true;

        $result = $this->commentPolicy->canEdit($user, $commentUserId, $discussionId, $throwException);

        $this->assertTrue($result);
    }

    public function testCheckCanNotEditCommentWithOutThrowException()
    {
        $user = new User(['user_id' => 41301, 'user_role' => 10]);
        $commentUserId = 4321;
        $discussionId = 'wall:1111';
        $throwException = false;

        $result = $this->commentPolicy->canEdit($user, $commentUserId, $discussionId, $throwException);

        $this->assertFalse($result);
    }

    /**
     * @expectedException \FootyRoom\Core\AuthException
     */
    public function testCheckCanNotEditCommentWithThrowException()
    {
        $user = new User(['user_id' => 41301, 'user_role' => 10]);
        $commentUserId = 4321;
        $discussionId = 'wall:1111';
        $throwException = true;

        $result = $this->commentPolicy->canEdit($user, $commentUserId, $discussionId, $throwException);
    }

    /**
     * @expectedException \FootyRoom\Core\AuthException
     */
    public function testCheckModeratorCanNotEditCommentInWallDiscussion()
    {
        $user = new User(['user_id' => 41301, 'user_role' => 20]);
        $commentUserId = 4321;
        $discussionId = 'wall:1111';
        $throwException = true;

        $result = $this->commentPolicy->canEdit($user, $commentUserId, $discussionId, $throwException);
    }
}