mirror of
https://github.com/wearepixel/laravel-comments.git
synced 2026-11-14 14:02:04 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\Comments\Tests;
|
||||
|
||||
use BeyondCode\Comments\Tests\Models\ApprovedUser;
|
||||
use BeyondCode\Comments\Tests\Models\Post;
|
||||
use Illuminate\Foundation\Auth\User;
|
||||
|
||||
class CommentsTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function users_without_commentator_interface_do_not_get_approved()
|
||||
{
|
||||
$post = Post::create([
|
||||
'title' => 'Some post'
|
||||
]);
|
||||
|
||||
$post->comment('this is a comment');
|
||||
|
||||
$comment = $post->comments()->first();
|
||||
|
||||
$this->assertFalse($comment->is_approved);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function models_can_store_comments()
|
||||
{
|
||||
$post = Post::create([
|
||||
'title' => 'Some post'
|
||||
]);
|
||||
|
||||
$post->comment('this is a comment');
|
||||
$post->comment('this is a different comment');
|
||||
|
||||
$this->assertCount(2, $post->comments);
|
||||
|
||||
$this->assertSame('this is a comment', $post->comments[0]->comment);
|
||||
$this->assertSame('this is a different comment', $post->comments[1]->comment);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function comments_without_users_have_no_relation()
|
||||
{
|
||||
$post = Post::create([
|
||||
'title' => 'Some post'
|
||||
]);
|
||||
|
||||
$comment = $post->comment('this is a comment');
|
||||
|
||||
$this->assertNull($comment->commentator);
|
||||
$this->assertNull($comment->user_id);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function comments_can_be_posted_as_authenticated_users()
|
||||
{
|
||||
$user = User::first();
|
||||
|
||||
auth()->login($user);
|
||||
|
||||
$post = Post::create([
|
||||
'title' => 'Some post'
|
||||
]);
|
||||
|
||||
$comment = $post->comment('this is a comment');
|
||||
|
||||
$this->assertSame($user->toArray(), $comment->commentator->toArray());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function comments_can_be_posted_as_different_users()
|
||||
{
|
||||
$user = User::first();
|
||||
|
||||
$post = Post::create([
|
||||
'title' => 'Some post'
|
||||
]);
|
||||
|
||||
$comment = $post->commentAsUser($user, 'this is a comment');
|
||||
|
||||
$this->assertSame($user->toArray(), $comment->commentator->toArray());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function comments_can_be_approved()
|
||||
{
|
||||
$user = User::first();
|
||||
|
||||
$post = Post::create([
|
||||
'title' => 'Some post'
|
||||
]);
|
||||
|
||||
$comment = $post->comment('this is a comment');
|
||||
|
||||
$this->assertFalse($comment->is_approved);
|
||||
|
||||
$comment->approve();
|
||||
|
||||
$this->assertTrue($comment->is_approved);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function comments_resolve_the_commented_model()
|
||||
{
|
||||
$user = User::first();
|
||||
|
||||
$post = Post::create([
|
||||
'title' => 'Some post'
|
||||
]);
|
||||
|
||||
$comment = $post->comment('this is a comment');
|
||||
|
||||
$this->assertSame($comment->commentable->id, $post->id);
|
||||
$this->assertSame($comment->commentable->title, $post->title);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function users_can_be_auto_approved()
|
||||
{
|
||||
$user = ApprovedUser::first();
|
||||
|
||||
$post = Post::create([
|
||||
'title' => 'Some post'
|
||||
]);
|
||||
|
||||
$comment = $post->commentAsUser($user, 'this is a comment');
|
||||
|
||||
$this->assertTrue($comment->is_approved);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function comments_have_an_approved_scope()
|
||||
{
|
||||
$user = ApprovedUser::first();
|
||||
|
||||
$post = Post::create([
|
||||
'title' => 'Some post'
|
||||
]);
|
||||
|
||||
$post->comment('this comment is not approved');
|
||||
$post->commentAsUser($user, 'this comment is approved');
|
||||
|
||||
$this->assertCount(2, $post->comments);
|
||||
$this->assertCount(1, $post->comments()->approved()->get());
|
||||
|
||||
$this->assertSame('this comment is approved', $post->comments()->approved()->first()->comment);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\Comments\Tests\Models;
|
||||
|
||||
use Illuminate\Foundation\Auth\User;
|
||||
use BeyondCode\Comments\Contracts\Commentator;
|
||||
|
||||
class ApprovedUser extends User implements Commentator
|
||||
{
|
||||
|
||||
protected $table = 'users';
|
||||
|
||||
/**
|
||||
* Check if a comment for a specific model needs to be approved.
|
||||
* @param mixed $model
|
||||
* @return bool
|
||||
*/
|
||||
public function needsCommentApproval($model): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\Comments\Tests\Models;
|
||||
|
||||
use BeyondCode\Comments\Traits\HasComments;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Post extends Model
|
||||
{
|
||||
use HasComments;
|
||||
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\Comments\Tests;
|
||||
|
||||
use Illuminate\Foundation\Auth\User;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use BeyondCode\Comments\CommentsServiceProvider;
|
||||
|
||||
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->loadLaravelMigrations(['--database' => 'sqlite']);
|
||||
$this->setUpDatabase();
|
||||
$this->createUser();
|
||||
}
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
{
|
||||
return [
|
||||
CommentsServiceProvider::class,
|
||||
];
|
||||
}
|
||||
|
||||
protected function getEnvironmentSetUp($app)
|
||||
{
|
||||
$app['config']->set('auth.providers.users.model', User::class);
|
||||
$app['config']->set('database.default', 'sqlite');
|
||||
$app['config']->set('database.connections.sqlite', [
|
||||
'driver' => 'sqlite',
|
||||
'database' => ':memory:',
|
||||
'prefix' => '',
|
||||
]);
|
||||
$app['config']->set('app.key', 'base64:6Cu/ozj4gPtIjmXjr8EdVnGFNsdRqZfHfVjQkmTlg4Y=');
|
||||
}
|
||||
|
||||
protected function setUpDatabase()
|
||||
{
|
||||
include_once __DIR__ . '/../database/migrations/create_comments_table.php.stub';
|
||||
|
||||
(new \CreateCommentsTable())->up();
|
||||
|
||||
$this->app['db']->connection()->getSchemaBuilder()->create('posts', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('title');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
protected function createUser()
|
||||
{
|
||||
User::forceCreate([
|
||||
'name' => 'User',
|
||||
'email' => 'user@email.com',
|
||||
'password' => 'test'
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user