DuplicateCommentTest.php 1.89 KB
<?php

namespace FootyRoom\Tests;

use Illuminate\Database\Connection;

class DuplicateCommentTest extends TestCase
{
    use SetsUpFixtures;

    public function setup()
    {
        parent::setup();
        $this->mysql = $this->app->make(Connection::class);
    }

    public function testDuplicateCommentShouldThrowException()
    {
        $comments[] = [
            'user_id' => 1,
            'discussion_id' => 'forumPost:1',
            'content' => 'i am cj',
            'html' => 'i am cj',
            'datetime' => date('Y-m-d H:i:s'),
            'status' => 0,
            'author' => 'chandu',
        ];

        $this->mysql->table('comments')->insert($comments);

        $user = factory('FootyRoom\User\User')->make(['user_id' => 1]);
        $response = $this->actingAs($user)->call('POST', '/forum/comments', ['content' => 'i am cj', 'postId' => 1]);
        $this->assertEquals(500, $response->status());

        $this->assertContains('Seems like you have already posted a comment like that recently. Is this a duplicate? You might want to refresh the page to see your comment.', $response->getContent());
    }

    public function testDuplicateCommentAllowedIfAlreadyBeyondTimeLimit()
    {
        //time limit now is 30 seconds

        $faker = \Faker\Factory::create();

        $comments[] = [
            'user_id' => 1,
            'discussion_id' => 'forumPost:1',
            'content' => 'i am chandu',
            'html' => 'i am chandu',
            'datetime' => $faker->date('Y-m-d H:i:s', '-1 hour'),
            'status' => 0,
            'author' => 'chandu',
        ];

        $this->mysql->table('comments')->insert($comments);

        $user = factory('FootyRoom\User\User')->make(['user_id' => 1]);
        $response = $this->actingAs($user)->call('POST', '/forum/comments', ['content' => 'i am chandu', 'postId' => 1]);
        $this->assertEquals(200, $response->status());
    }
}