refactor: rename for Laravel conventions and better DX

- Rename IsCommenter trait to CanComment, Commenter interface to Commentator
- Move models to Models/ namespace (Comment, Reaction, Attachment, Subscription)
- Rename user_type/user_id polymorphic columns to commenter_type/commenter_id
- Rename Config class to CommentsConfig, update config key tables->table_names
- Rename getCommentName() to getCommentDisplayName() on commentator models
- Add column_names config section for commenter morph customization
- Add table_names config with all 5 tables individually configurable
- Expand translation file with structured i18n groups
- Update all Blade views, Livewire components, events, listeners, and tests
This commit is contained in:
manukminasyan
2026-03-27 14:53:12 +04:00
parent 43b66f60f3
commit fd5bc5271b
62 changed files with 733 additions and 653 deletions

View File

@@ -3,11 +3,11 @@
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Relaticle\Comments\Comment;
use Relaticle\Comments\CommentAttachment;
use Relaticle\Comments\Config;
use Relaticle\Comments\CommentsConfig;
use Relaticle\Comments\Livewire\CommentItem;
use Relaticle\Comments\Livewire\Comments;
use Relaticle\Comments\Models\Attachment;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -29,7 +29,7 @@ it('creates comment with file attachment via Livewire component', function () {
->assertSet('attachments', []);
expect(Comment::count())->toBe(1);
expect(CommentAttachment::count())->toBe(1);
expect(Attachment::count())->toBe(1);
});
it('stores attachment with correct metadata', function () {
@@ -47,7 +47,7 @@ it('stores attachment with correct metadata', function () {
->set('attachments', [$file])
->call('addComment');
$attachment = CommentAttachment::first();
$attachment = Attachment::first();
$comment = Comment::first();
expect($attachment->original_name)->toBe('vacation.jpg')
@@ -73,7 +73,7 @@ it('stores file on configured disk at comments/attachments/{comment_id}/ path',
->set('attachments', [$file])
->call('addComment');
$attachment = CommentAttachment::first();
$attachment = Attachment::first();
Storage::disk('public')->assertExists($attachment->file_path);
expect($attachment->file_path)->toContain("comments/attachments/{$attachment->comment_id}/");
@@ -88,15 +88,15 @@ it('displays image attachment thumbnail in comment item view', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Image comment</p>',
]);
$file = UploadedFile::fake()->image('photo.jpg', 100, 100);
$path = $file->store("comments/attachments/{$comment->id}", 'public');
CommentAttachment::create([
Attachment::create([
'comment_id' => $comment->id,
'file_path' => $path,
'original_name' => 'photo.jpg',
@@ -123,15 +123,15 @@ it('displays non-image attachment as download link', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>PDF comment</p>',
]);
$file = UploadedFile::fake()->create('document.pdf', 2048, 'application/pdf');
$path = $file->store("comments/attachments/{$comment->id}", 'public');
CommentAttachment::create([
Attachment::create([
'comment_id' => $comment->id,
'file_path' => $path,
'original_name' => 'document.pdf',
@@ -157,7 +157,7 @@ it('rejects file exceeding max size', function () {
$this->actingAs($user);
$oversizedFile = UploadedFile::fake()->create('big.pdf', Config::getAttachmentMaxSize() + 1, 'application/pdf');
$oversizedFile = UploadedFile::fake()->create('big.pdf', CommentsConfig::getAttachmentMaxSize() + 1, 'application/pdf');
Livewire::test(Comments::class, ['model' => $post])
->set('newComment', '<p>Oversized file</p>')
@@ -166,7 +166,7 @@ it('rejects file exceeding max size', function () {
->assertHasErrors('attachments.0');
expect(Comment::count())->toBe(0);
expect(CommentAttachment::count())->toBe(0);
expect(Attachment::count())->toBe(0);
});
it('rejects disallowed file type', function () {
@@ -186,7 +186,7 @@ it('rejects disallowed file type', function () {
->assertHasErrors('attachments.0');
expect(Comment::count())->toBe(0);
expect(CommentAttachment::count())->toBe(0);
expect(Attachment::count())->toBe(0);
});
it('accepts allowed file types', function () {
@@ -206,7 +206,7 @@ it('accepts allowed file types', function () {
->assertHasNoErrors('attachments.0');
expect(Comment::count())->toBe(1);
expect(CommentAttachment::count())->toBe(1);
expect(Attachment::count())->toBe(1);
});
it('hides upload UI when attachments disabled', function () {
@@ -248,9 +248,9 @@ it('creates comment with multiple file attachments', function () {
->call('addComment');
expect(Comment::count())->toBe(1);
expect(CommentAttachment::count())->toBe(2);
expect(Attachment::count())->toBe(2);
$attachments = CommentAttachment::all();
$attachments = Attachment::all();
expect($attachments->pluck('original_name')->toArray())
->toContain('photo1.jpg')
->toContain('notes.pdf');
@@ -265,8 +265,8 @@ it('creates reply with file attachment via CommentItem component', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Parent comment</p>',
]);

View File

@@ -2,11 +2,11 @@
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Events\CommentCreated;
use Relaticle\Comments\Events\CommentDeleted;
use Relaticle\Comments\Events\CommentReacted;
use Relaticle\Comments\Events\CommentUpdated;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -17,8 +17,8 @@ it('CommentCreated event implements ShouldBroadcast', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$event = new CommentCreated($comment);
@@ -33,8 +33,8 @@ it('CommentUpdated event implements ShouldBroadcast', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$event = new CommentUpdated($comment);
@@ -49,8 +49,8 @@ it('CommentDeleted event implements ShouldBroadcast', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$event = new CommentDeleted($comment);
@@ -65,8 +65,8 @@ it('CommentReacted event implements ShouldBroadcast', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$event = new CommentReacted($comment, $user, 'thumbs_up', 'added');
@@ -81,8 +81,8 @@ it('broadcastOn returns PrivateChannel with correct channel name', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$event = new CommentCreated($comment);
@@ -100,8 +100,8 @@ it('broadcastWhen returns false when broadcasting is disabled', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$event = new CommentCreated($comment);
@@ -118,8 +118,8 @@ it('broadcastWhen returns true when broadcasting is enabled', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$event = new CommentCreated($comment);
@@ -134,8 +134,8 @@ it('broadcastWith returns array with comment_id for CommentCreated', function ()
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$event = new CommentCreated($comment);
@@ -154,8 +154,8 @@ it('broadcastWith returns array with comment_id, reaction, and action for Commen
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$event = new CommentReacted($comment, $user, 'thumbs_up', 'added');
@@ -176,8 +176,8 @@ it('uses custom channel prefix from config in broadcastOn', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$event = new CommentCreated($comment);

View File

@@ -1,8 +1,8 @@
<?php
use Relaticle\Comments\Comment;
use Relaticle\Comments\CommentAttachment;
use Relaticle\Comments\Config;
use Relaticle\Comments\CommentsConfig;
use Relaticle\Comments\Models\Attachment;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -13,12 +13,12 @@ it('creates a comment attachment with all metadata fields', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Test comment</p>',
]);
$attachment = CommentAttachment::create([
$attachment = Attachment::create([
'comment_id' => $comment->id,
'file_path' => 'comments/attachments/1/photo.jpg',
'original_name' => 'photo.jpg',
@@ -27,7 +27,7 @@ it('creates a comment attachment with all metadata fields', function () {
'disk' => 'public',
]);
expect($attachment)->toBeInstanceOf(CommentAttachment::class)
expect($attachment)->toBeInstanceOf(Attachment::class)
->and($attachment->file_path)->toBe('comments/attachments/1/photo.jpg')
->and($attachment->original_name)->toBe('photo.jpg')
->and($attachment->mime_type)->toBe('image/jpeg')
@@ -42,12 +42,12 @@ it('belongs to a comment via comment() relationship', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Test</p>',
]);
$attachment = CommentAttachment::create([
$attachment = Attachment::create([
'comment_id' => $comment->id,
'file_path' => 'comments/attachments/1/test.png',
'original_name' => 'test.png',
@@ -67,12 +67,12 @@ it('has attachments() hasMany relationship on Comment', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Test</p>',
]);
CommentAttachment::create([
Attachment::create([
'comment_id' => $comment->id,
'file_path' => 'comments/attachments/1/file1.png',
'original_name' => 'file1.png',
@@ -81,7 +81,7 @@ it('has attachments() hasMany relationship on Comment', function () {
'disk' => 'public',
]);
CommentAttachment::create([
Attachment::create([
'comment_id' => $comment->id,
'file_path' => 'comments/attachments/1/file2.pdf',
'original_name' => 'file2.pdf',
@@ -91,7 +91,7 @@ it('has attachments() hasMany relationship on Comment', function () {
]);
expect($comment->attachments)->toHaveCount(2)
->and($comment->attachments->first())->toBeInstanceOf(CommentAttachment::class);
->and($comment->attachments->first())->toBeInstanceOf(Attachment::class);
});
it('cascade deletes attachments when comment is force deleted', function () {
@@ -101,12 +101,12 @@ it('cascade deletes attachments when comment is force deleted', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Test</p>',
]);
CommentAttachment::create([
Attachment::create([
'comment_id' => $comment->id,
'file_path' => 'comments/attachments/1/photo.jpg',
'original_name' => 'photo.jpg',
@@ -115,15 +115,15 @@ it('cascade deletes attachments when comment is force deleted', function () {
'disk' => 'public',
]);
expect(CommentAttachment::where('comment_id', $comment->id)->count())->toBe(1);
expect(Attachment::where('comment_id', $comment->id)->count())->toBe(1);
$comment->forceDelete();
expect(CommentAttachment::where('comment_id', $comment->id)->count())->toBe(0);
expect(Attachment::where('comment_id', $comment->id)->count())->toBe(0);
});
it('correctly identifies image and non-image mime types via isImage()', function (string $mimeType, bool $expected) {
$attachment = new CommentAttachment(['mime_type' => $mimeType]);
$attachment = new Attachment(['mime_type' => $mimeType]);
expect($attachment->isImage())->toBe($expected);
})->with([
@@ -142,12 +142,12 @@ it('formats bytes into human-readable size via formattedSize()', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Test</p>',
]);
$attachment = CommentAttachment::create([
$attachment = Attachment::create([
'comment_id' => $comment->id,
'file_path' => 'comments/attachments/1/file.pdf',
'original_name' => 'file.pdf',
@@ -160,15 +160,15 @@ it('formats bytes into human-readable size via formattedSize()', function () {
});
it('returns default attachment disk as public', function () {
expect(Config::getAttachmentDisk())->toBe('public');
expect(CommentsConfig::getAttachmentDisk())->toBe('public');
});
it('returns default attachment max size as 10240', function () {
expect(Config::getAttachmentMaxSize())->toBe(10240);
expect(CommentsConfig::getAttachmentMaxSize())->toBe(10240);
});
it('returns default allowed attachment types', function () {
$allowedTypes = Config::getAttachmentAllowedTypes();
$allowedTypes = CommentsConfig::getAttachmentAllowedTypes();
expect($allowedTypes)->toBeArray()
->toContain('image/jpeg')
@@ -181,17 +181,17 @@ it('respects custom config overrides for attachment settings', function () {
config(['comments.attachments.max_size' => 5120]);
config(['comments.attachments.allowed_types' => ['image/png']]);
expect(Config::getAttachmentDisk())->toBe('s3')
->and(Config::getAttachmentMaxSize())->toBe(5120)
->and(Config::getAttachmentAllowedTypes())->toBe(['image/png']);
expect(CommentsConfig::getAttachmentDisk())->toBe('s3')
->and(CommentsConfig::getAttachmentMaxSize())->toBe(5120)
->and(CommentsConfig::getAttachmentAllowedTypes())->toBe(['image/png']);
});
it('reports attachments as enabled by default', function () {
expect(Config::areAttachmentsEnabled())->toBeTrue();
expect(CommentsConfig::areAttachmentsEnabled())->toBeTrue();
});
it('respects disabled attachments config', function () {
config(['comments.attachments.enabled' => false]);
expect(Config::areAttachmentsEnabled())->toBeFalse();
expect(CommentsConfig::areAttachmentsEnabled())->toBeFalse();
});

View File

@@ -2,12 +2,12 @@
use Illuminate\Support\Facades\Event;
use Livewire\Livewire;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Events\CommentCreated;
use Relaticle\Comments\Events\CommentDeleted;
use Relaticle\Comments\Events\CommentUpdated;
use Relaticle\Comments\Livewire\CommentItem;
use Relaticle\Comments\Livewire\Comments;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -38,8 +38,8 @@ it('fires CommentUpdated event when editing a comment', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Original</p>',
]);
@@ -64,8 +64,8 @@ it('fires CommentDeleted event when deleting a comment', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
@@ -87,8 +87,8 @@ it('fires CommentCreated event when adding a reply', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
@@ -119,6 +119,6 @@ it('carries correct comment and commentable in event payload', function () {
Event::assertDispatched(CommentCreated::class, function (CommentCreated $event) use ($post, $user) {
return $event->comment instanceof Comment
&& $event->commentable->id === $post->id
&& $event->comment->user_id === $user->id;
&& $event->comment->commenter_id === $user->id;
});
});

View File

@@ -1,8 +1,8 @@
<?php
use Livewire\Livewire;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Livewire\CommentItem;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -13,8 +13,8 @@ it('allows author to start and save edit on their comment', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Original body</p>',
]);
@@ -42,8 +42,8 @@ it('marks edited comment with edited indicator', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Original</p>',
]);
@@ -70,8 +70,8 @@ it('prevents non-author from editing a comment', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'body' => '<p>Author comment</p>',
]);
@@ -89,8 +89,8 @@ it('allows author to delete their own comment', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
@@ -108,8 +108,8 @@ it('preserves replies when parent comment is deleted', function () {
$attrs = [
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
];
$parent = Comment::factory()->create($attrs);
@@ -133,8 +133,8 @@ it('prevents non-author from deleting a comment', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
]);
$this->actingAs($otherUser);
@@ -151,8 +151,8 @@ it('allows user to reply to a comment', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
@@ -169,7 +169,7 @@ it('allows user to reply to a comment', function () {
expect($reply)->not->toBeNull();
expect($reply->body)->toBe('<p>My reply</p>');
expect($reply->user_id)->toBe($user->id);
expect($reply->commenter_id)->toBe($user->id);
expect($reply->commentable_id)->toBe($post->id);
});
@@ -179,8 +179,8 @@ it('respects max depth for replies', function () {
$attrs = [
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
];
config(['comments.threading.max_depth' => 1]);
@@ -202,8 +202,8 @@ it('resets state when cancelling edit', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Some body</p>',
]);
@@ -224,8 +224,8 @@ it('resets state when cancelling reply', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
@@ -245,14 +245,14 @@ it('loads all replies within a thread eagerly', function () {
$attrs = [
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
];
$parent = Comment::factory()->create($attrs);
Comment::factory()->count(3)->withParent($parent)->create($attrs);
$parentWithReplies = Comment::with('replies.user')->find($parent->id);
$parentWithReplies = Comment::with('replies.commenter')->find($parent->id);
$this->actingAs($user);

View File

@@ -1,9 +1,9 @@
<?php
use Illuminate\Database\QueryException;
use Relaticle\Comments\Comment;
use Relaticle\Comments\CommentReaction;
use Relaticle\Comments\Events\CommentReacted;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Models\Reaction;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -14,15 +14,15 @@ it('belongs to a comment via comment() relationship', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Test</p>',
]);
$reaction = CommentReaction::create([
$reaction = Reaction::create([
'comment_id' => $comment->id,
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'reaction' => 'thumbs_up',
]);
@@ -30,27 +30,27 @@ it('belongs to a comment via comment() relationship', function () {
->and($reaction->comment->id)->toBe($comment->id);
});
it('belongs to a user via polymorphic user() relationship', function () {
it('belongs to a commenter via polymorphic commenter() relationship', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Test</p>',
]);
$reaction = CommentReaction::create([
$reaction = Reaction::create([
'comment_id' => $comment->id,
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'reaction' => 'heart',
]);
expect($reaction->user)->toBeInstanceOf(User::class)
->and($reaction->user->id)->toBe($user->id);
expect($reaction->commenter)->toBeInstanceOf(User::class)
->and($reaction->commenter->id)->toBe($user->id);
});
it('prevents duplicate reactions with unique constraint', function () {
@@ -60,22 +60,22 @@ it('prevents duplicate reactions with unique constraint', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Test</p>',
]);
CommentReaction::create([
Reaction::create([
'comment_id' => $comment->id,
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'reaction' => 'thumbs_up',
]);
expect(fn () => CommentReaction::create([
expect(fn () => Reaction::create([
'comment_id' => $comment->id,
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'reaction' => 'thumbs_up',
]))->toThrow(QueryException::class);
});
@@ -87,8 +87,8 @@ it('carries comment, user, reaction key, and action in CommentReacted event', fu
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Test</p>',
]);

View File

@@ -1,8 +1,7 @@
<?php
use Relaticle\Comments\Comment;
use Relaticle\Comments\CommentSubscription;
use Relaticle\Comments\Config;
use Relaticle\Comments\CommentsConfig;
use Relaticle\Comments\Models\Subscription;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -10,98 +9,98 @@ it('has commentable morphTo relationship', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
$subscription = CommentSubscription::create([
$subscription = Subscription::create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
expect($subscription->commentable)->toBeInstanceOf(Post::class)
->and($subscription->commentable->id)->toBe($post->id);
});
it('has user morphTo relationship', function () {
it('has commenter morphTo relationship', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
$subscription = CommentSubscription::create([
$subscription = Subscription::create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
expect($subscription->user)->toBeInstanceOf(User::class)
->and($subscription->user->id)->toBe($user->id);
expect($subscription->commenter)->toBeInstanceOf(User::class)
->and($subscription->commenter->id)->toBe($user->id);
});
it('returns database as default notification channel', function () {
expect(Config::getNotificationChannels())->toBe(['database']);
expect(CommentsConfig::getNotificationChannels())->toBe(['database']);
});
it('returns custom channels when configured', function () {
config()->set('comments.notifications.channels', ['database', 'mail']);
expect(Config::getNotificationChannels())->toBe(['database', 'mail']);
expect(CommentsConfig::getNotificationChannels())->toBe(['database', 'mail']);
});
it('returns true for shouldAutoSubscribe by default', function () {
expect(Config::shouldAutoSubscribe())->toBeTrue();
expect(CommentsConfig::shouldAutoSubscribe())->toBeTrue();
});
it('returns false for shouldAutoSubscribe when configured', function () {
config()->set('comments.subscriptions.auto_subscribe', false);
expect(Config::shouldAutoSubscribe())->toBeFalse();
expect(CommentsConfig::shouldAutoSubscribe())->toBeFalse();
});
it('checks if user is subscribed to a commentable via isSubscribed()', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
expect(CommentSubscription::isSubscribed($post, $user))->toBeFalse();
expect(Subscription::isSubscribed($post, $user))->toBeFalse();
CommentSubscription::create([
Subscription::create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
expect(CommentSubscription::isSubscribed($post, $user))->toBeTrue();
expect(Subscription::isSubscribed($post, $user))->toBeTrue();
});
it('creates subscription via subscribe() static method', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $user);
Subscription::subscribe($post, $user);
expect(CommentSubscription::isSubscribed($post, $user))->toBeTrue();
expect(Subscription::isSubscribed($post, $user))->toBeTrue();
});
it('removes subscription via unsubscribe() static method', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $user);
CommentSubscription::unsubscribe($post, $user);
Subscription::subscribe($post, $user);
Subscription::unsubscribe($post, $user);
expect(CommentSubscription::isSubscribed($post, $user))->toBeFalse();
expect(Subscription::isSubscribed($post, $user))->toBeFalse();
});
it('is idempotent when subscribing twice', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $user);
CommentSubscription::subscribe($post, $user);
Subscription::subscribe($post, $user);
Subscription::subscribe($post, $user);
expect(CommentSubscription::where([
expect(Subscription::where([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
])->count())->toBe(1);
});

View File

@@ -1,7 +1,7 @@
<?php
use Carbon\Carbon;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -12,14 +12,14 @@ it('can be created with factory', function () {
$comment = Comment::factory()->create([
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
expect($comment)->toBeInstanceOf(Comment::class);
expect($comment->body)->toBeString();
expect($comment->commentable_id)->toBe($post->id);
expect($comment->user_id)->toBe($user->id);
expect($comment->commenter_id)->toBe($user->id);
});
it('belongs to a commentable model via morphTo', function () {
@@ -29,27 +29,27 @@ it('belongs to a commentable model via morphTo', function () {
$comment = Comment::factory()->create([
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
expect($comment->commentable)->toBeInstanceOf(Post::class);
expect($comment->commentable->id)->toBe($post->id);
});
it('belongs to a user via morphTo', function () {
it('belongs to a commenter via morphTo', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
$comment = Comment::factory()->create([
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
expect($comment->user)->toBeInstanceOf(User::class);
expect($comment->user->id)->toBe($user->id);
expect($comment->commenter)->toBeInstanceOf(User::class);
expect($comment->commenter->id)->toBe($user->id);
});
it('supports threading with parent and replies', function () {
@@ -59,15 +59,15 @@ it('supports threading with parent and replies', function () {
$parent = Comment::factory()->create([
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
$reply = Comment::factory()->withParent($parent)->create([
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
expect($reply->parent->id)->toBe($parent->id);
@@ -82,15 +82,15 @@ it('identifies top-level vs reply comments', function () {
$topLevel = Comment::factory()->create([
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
$reply = Comment::factory()->withParent($topLevel)->create([
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
expect($topLevel->isTopLevel())->toBeTrue();
@@ -105,8 +105,8 @@ it('calculates depth correctly', function () {
$attrs = [
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
];
$level0 = Comment::factory()->create($attrs);
@@ -124,8 +124,8 @@ it('checks canReply based on max depth', function () {
$attrs = [
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
];
$level0 = Comment::factory()->create($attrs);
@@ -144,8 +144,8 @@ it('supports soft deletes', function () {
$comment = Comment::factory()->create([
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
$comment->delete();
@@ -162,8 +162,8 @@ it('tracks edited state', function () {
$comment = Comment::factory()->create([
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
expect($comment->isEdited())->toBeFalse();
@@ -171,8 +171,8 @@ it('tracks edited state', function () {
$edited = Comment::factory()->edited()->create([
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
expect($edited->isEdited())->toBeTrue();
@@ -185,8 +185,8 @@ it('detects when it has replies', function () {
$attrs = [
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
];
$parent = Comment::factory()->create($attrs);

View File

@@ -1,7 +1,7 @@
<?php
use Relaticle\Comments\Comment;
use Relaticle\Comments\Filament\Actions\CommentsAction;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -42,8 +42,8 @@ it('shows badge with comment count when comments exist', function () {
Comment::factory()->count(3)->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$action = CommentsAction::make('comments');

View File

@@ -1,8 +1,8 @@
<?php
use Livewire\Livewire;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Livewire\Comments;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -33,8 +33,8 @@ it('associates new comment with the authenticated user', function () {
$comment = Comment::first();
expect($comment->user_id)->toBe($user->id);
expect($comment->user_type)->toBe($user->getMorphClass());
expect($comment->commenter_id)->toBe($user->id);
expect($comment->commenter_type)->toBe($user->getMorphClass());
expect($comment->commentable_id)->toBe($post->id);
expect($comment->commentable_type)->toBe($post->getMorphClass());
});
@@ -71,8 +71,8 @@ it('paginates top-level comments with load more', function () {
Comment::factory()->count(12)->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
@@ -99,8 +99,8 @@ it('hides load more button when all comments are loaded', function () {
Comment::factory()->count(5)->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
@@ -130,8 +130,8 @@ it('returns comments in correct sort order via computed property', function () {
$older = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Older comment</p>',
'created_at' => now()->subHour(),
]);
@@ -139,8 +139,8 @@ it('returns comments in correct sort order via computed property', function () {
$newer = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Newer comment</p>',
'created_at' => now(),
]);
@@ -167,8 +167,8 @@ it('displays total comment count', function () {
Comment::factory()->count(3)->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);

View File

@@ -1,7 +1,7 @@
<?php
use Relaticle\Comments\Comment;
use Relaticle\Comments\Filament\Actions\CommentsTableAction;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -30,8 +30,8 @@ it('shows badge with comment count for the record', function () {
Comment::factory()->count(5)->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$action = CommentsTableAction::make('comments');

View File

@@ -1,8 +1,8 @@
<?php
use Livewire\Livewire;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Livewire\Comments;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -13,8 +13,8 @@ it('strips script tags from comment body on create', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Hello</p><script>alert(1)</script>',
]);
@@ -29,8 +29,8 @@ it('strips event handler attributes from comment body', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<img onerror="alert(1)" src="x">',
]);
@@ -45,8 +45,8 @@ it('strips style tags from comment body', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Hi</p><style>body{display:none}</style>',
]);
@@ -61,8 +61,8 @@ it('strips iframe tags from comment body', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Hi</p><iframe src="evil.com"></iframe>',
]);
@@ -85,8 +85,8 @@ it('preserves safe HTML formatting through sanitization', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => $safeHtml,
]);
@@ -108,8 +108,8 @@ it('sanitizes comment body on update', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Clean content</p>',
]);
@@ -130,8 +130,8 @@ it('strips javascript protocol from link href', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<a href="javascript:alert(1)">click me</a>',
]);
@@ -146,8 +146,8 @@ it('strips onclick handler from elements', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<div onclick="alert(1)">click me</div>',
]);

View File

@@ -1,6 +1,6 @@
<?php
use Relaticle\Comments\Comment;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -11,8 +11,8 @@ it('provides comments relationship on commentable model', function () {
Comment::factory()->count(3)->create([
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
expect($post->comments)->toHaveCount(3);
@@ -25,8 +25,8 @@ it('provides topLevelComments excluding replies', function () {
$attrs = [
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
];
$topLevel = Comment::factory()->create($attrs);
@@ -46,8 +46,8 @@ it('provides comment count', function () {
Comment::factory()->count(5)->create([
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
expect($post->commentCount())->toBe(5);
@@ -61,15 +61,15 @@ it('scopes comments to the specific commentable', function () {
Comment::factory()->count(3)->create([
'commentable_type' => $post1->getMorphClass(),
'commentable_id' => $post1->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
Comment::factory()->count(2)->create([
'commentable_type' => $post2->getMorphClass(),
'commentable_id' => $post2->id,
'user_type' => $user->getMorphClass(),
'user_id' => $user->id,
'commenter_type' => $user->getMorphClass(),
'commenter_id' => $user->id,
]);
expect($post1->commentCount())->toBe(3);

View File

@@ -1,8 +1,8 @@
<?php
use Livewire\Livewire;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Livewire\CommentItem;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -14,12 +14,12 @@ it('renders mention with styled span', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>@Alice said hi</p>',
]);
$comment->mentions()->attach($alice->id, ['user_type' => $alice->getMorphClass()]);
$comment->mentions()->attach($alice->id, ['commenter_type' => $alice->getMorphClass()]);
$rendered = $comment->renderBodyWithMentions();
@@ -36,13 +36,13 @@ it('renders multiple mentions with styled spans', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>@Alice and @Bob</p>',
]);
$comment->mentions()->attach($alice->id, ['user_type' => $alice->getMorphClass()]);
$comment->mentions()->attach($bob->id, ['user_type' => $bob->getMorphClass()]);
$comment->mentions()->attach($alice->id, ['commenter_type' => $alice->getMorphClass()]);
$comment->mentions()->attach($bob->id, ['commenter_type' => $bob->getMorphClass()]);
$rendered = $comment->renderBodyWithMentions();
@@ -58,8 +58,8 @@ it('does not style non-mentioned @text', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>@ghost is not here</p>',
]);
@@ -76,12 +76,12 @@ it('renders comment-mention class in Livewire component', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Hello @Alice</p>',
]);
$comment->mentions()->attach($alice->id, ['user_type' => $alice->getMorphClass()]);
$comment->mentions()->attach($alice->id, ['commenter_type' => $alice->getMorphClass()]);
$this->actingAs($user);

View File

@@ -2,11 +2,11 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Event;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Contracts\MentionResolver;
use Relaticle\Comments\Events\UserMentioned;
use Relaticle\Comments\Mentions\DefaultMentionResolver;
use Relaticle\Comments\Mentions\MentionParser;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -62,8 +62,8 @@ it('stores mentions in comment_mentions table on create', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Hello @john and @jane</p>',
]);
@@ -85,8 +85,8 @@ it('dispatches UserMentioned event for each mentioned user', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Hello @john and @jane</p>',
]);
@@ -116,8 +116,8 @@ it('only dispatches UserMentioned for newly added mentions on update', function
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Hello @john</p>',
]);
@@ -146,8 +146,8 @@ it('removes mentions from pivot when user removed from body', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Hello @john and @jane</p>',
]);

View File

@@ -1,9 +1,9 @@
<?php
use Livewire\Livewire;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Livewire\CommentItem;
use Relaticle\Comments\Livewire\Comments;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -92,8 +92,8 @@ it('stores mentions when editing comment with @mention', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Original comment</p>',
]);

View File

@@ -1,12 +1,12 @@
<?php
use Illuminate\Support\Facades\Notification;
use Relaticle\Comments\Comment;
use Relaticle\Comments\CommentSubscription;
use Relaticle\Comments\Events\CommentCreated;
use Relaticle\Comments\Events\UserMentioned;
use Relaticle\Comments\Listeners\SendCommentRepliedNotification;
use Relaticle\Comments\Listeners\SendUserMentionedNotification;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Models\Subscription;
use Relaticle\Comments\Notifications\CommentRepliedNotification;
use Relaticle\Comments\Notifications\UserMentionedNotification;
use Relaticle\Comments\Tests\Models\Post;
@@ -19,21 +19,21 @@ it('sends CommentRepliedNotification to parent comment author when reply is crea
$replyAuthor = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $parentAuthor);
Subscription::subscribe($post, $parentAuthor);
$parentComment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $parentAuthor->getKey(),
'user_type' => $parentAuthor->getMorphClass(),
'commenter_id' => $parentAuthor->getKey(),
'commenter_type' => $parentAuthor->getMorphClass(),
'body' => '<p>Parent comment</p>',
]);
$reply = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $replyAuthor->getKey(),
'user_type' => $replyAuthor->getMorphClass(),
'commenter_id' => $replyAuthor->getKey(),
'commenter_type' => $replyAuthor->getMorphClass(),
'parent_id' => $parentComment->id,
'body' => '<p>A reply</p>',
]);
@@ -51,13 +51,13 @@ it('does NOT send reply notification for top-level comments', function () {
$subscriber = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $subscriber);
Subscription::subscribe($post, $subscriber);
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'body' => '<p>Top-level comment</p>',
]);
@@ -73,21 +73,21 @@ it('does NOT send reply notification to the reply author', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $user);
Subscription::subscribe($post, $user);
$parentComment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>My comment</p>',
]);
$reply = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'parent_id' => $parentComment->id,
'body' => '<p>My own reply</p>',
]);
@@ -108,8 +108,8 @@ it('sends UserMentionedNotification when a user is mentioned', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'body' => '<p>Hey @someone</p>',
]);
@@ -128,8 +128,8 @@ it('does NOT send mention notification to the comment author', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'body' => '<p>Hey @myself</p>',
]);
@@ -146,22 +146,22 @@ it('does NOT send reply notification to unsubscribed user', function () {
$unsubscribedUser = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $unsubscribedUser);
CommentSubscription::unsubscribe($post, $unsubscribedUser);
Subscription::subscribe($post, $unsubscribedUser);
Subscription::unsubscribe($post, $unsubscribedUser);
$parentComment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $unsubscribedUser->getKey(),
'user_type' => $unsubscribedUser->getMorphClass(),
'commenter_id' => $unsubscribedUser->getKey(),
'commenter_type' => $unsubscribedUser->getMorphClass(),
'body' => '<p>Original</p>',
]);
$reply = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'parent_id' => $parentComment->id,
'body' => '<p>Reply</p>',
]);
@@ -178,20 +178,20 @@ it('auto-subscribes the comment author when creating a comment', function () {
$author = User::factory()->create();
$post = Post::factory()->create();
expect(CommentSubscription::isSubscribed($post, $author))->toBeFalse();
expect(Subscription::isSubscribed($post, $author))->toBeFalse();
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'body' => '<p>My comment</p>',
]);
$listener = new SendCommentRepliedNotification;
$listener->handle(new CommentCreated($comment));
expect(CommentSubscription::isSubscribed($post, $author))->toBeTrue();
expect(Subscription::isSubscribed($post, $author))->toBeTrue();
});
it('suppresses all notifications when notifications are disabled via config', function () {
@@ -203,21 +203,21 @@ it('suppresses all notifications when notifications are disabled via config', fu
$mentioned = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $subscriber);
Subscription::subscribe($post, $subscriber);
$parentComment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $subscriber->getKey(),
'user_type' => $subscriber->getMorphClass(),
'commenter_id' => $subscriber->getKey(),
'commenter_type' => $subscriber->getMorphClass(),
'body' => '<p>Original</p>',
]);
$reply = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'parent_id' => $parentComment->id,
'body' => '<p>Reply</p>',
]);

View File

@@ -1,13 +1,12 @@
<?php
use Illuminate\Support\Facades\Notification;
use Relaticle\Comments\Comment;
use Relaticle\Comments\CommentSubscription;
use Relaticle\Comments\Config;
use Relaticle\Comments\Events\CommentCreated;
use Relaticle\Comments\Events\UserMentioned;
use Relaticle\Comments\Listeners\SendCommentRepliedNotification;
use Relaticle\Comments\Listeners\SendUserMentionedNotification;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Models\Subscription;
use Relaticle\Comments\Notifications\CommentRepliedNotification;
use Relaticle\Comments\Notifications\UserMentionedNotification;
use Relaticle\Comments\Tests\Models\Post;
@@ -22,8 +21,8 @@ it('returns correct via channels from config for CommentRepliedNotification', fu
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Hello</p>',
]);
@@ -39,8 +38,8 @@ it('returns toDatabase array with comment data for CommentRepliedNotification',
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>This is a reply body</p>',
]);
@@ -51,7 +50,7 @@ it('returns toDatabase array with comment data for CommentRepliedNotification',
->and($data['comment_id'])->toBe($comment->id)
->and($data['commentable_type'])->toBe($post->getMorphClass())
->and($data['commentable_id'])->toBe($post->id)
->and($data['commenter_name'])->toBe($user->getCommentName());
->and($data['commenter_name'])->toBe($user->getCommentDisplayName());
});
it('returns correct via channels from config for UserMentionedNotification', function () {
@@ -64,8 +63,8 @@ it('returns correct via channels from config for UserMentionedNotification', fun
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $mentionedBy->getKey(),
'user_type' => $mentionedBy->getMorphClass(),
'commenter_id' => $mentionedBy->getKey(),
'commenter_type' => $mentionedBy->getMorphClass(),
'body' => '<p>Hey @someone</p>',
]);
@@ -82,8 +81,8 @@ it('returns toDatabase array with mention data for UserMentionedNotification', f
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $mentioner->getKey(),
'user_type' => $mentioner->getMorphClass(),
'commenter_id' => $mentioner->getKey(),
'commenter_type' => $mentioner->getMorphClass(),
'body' => '<p>Hey @mentioned</p>',
]);
@@ -92,7 +91,7 @@ it('returns toDatabase array with mention data for UserMentionedNotification', f
expect($data)->toHaveKeys(['comment_id', 'commentable_type', 'commentable_id', 'mentioner_name', 'body'])
->and($data['comment_id'])->toBe($comment->id)
->and($data['mentioner_name'])->toBe($mentioner->getCommentName());
->and($data['mentioner_name'])->toBe($mentioner->getCommentDisplayName());
});
it('sends notification to subscribers when reply comment is created', function () {
@@ -102,21 +101,21 @@ it('sends notification to subscribers when reply comment is created', function (
$subscriber = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $subscriber);
Subscription::subscribe($post, $subscriber);
$parentComment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $subscriber->getKey(),
'user_type' => $subscriber->getMorphClass(),
'commenter_id' => $subscriber->getKey(),
'commenter_type' => $subscriber->getMorphClass(),
'body' => '<p>Original comment</p>',
]);
$reply = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'parent_id' => $parentComment->id,
'body' => '<p>Reply to original</p>',
]);
@@ -134,13 +133,13 @@ it('does NOT send notification for top-level comments', function () {
$subscriber = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $subscriber);
Subscription::subscribe($post, $subscriber);
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'body' => '<p>Top-level comment</p>',
]);
@@ -156,21 +155,21 @@ it('does NOT notify the reply author themselves', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $user);
Subscription::subscribe($post, $user);
$parentComment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>My comment</p>',
]);
$reply = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'parent_id' => $parentComment->id,
'body' => '<p>My own reply</p>',
]);
@@ -187,20 +186,20 @@ it('auto-subscribes comment author to the thread', function () {
$author = User::factory()->create();
$post = Post::factory()->create();
expect(CommentSubscription::isSubscribed($post, $author))->toBeFalse();
expect(Subscription::isSubscribed($post, $author))->toBeFalse();
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'body' => '<p>Comment</p>',
]);
$listener = new SendCommentRepliedNotification;
$listener->handle(new CommentCreated($comment));
expect(CommentSubscription::isSubscribed($post, $author))->toBeTrue();
expect(Subscription::isSubscribed($post, $author))->toBeTrue();
});
it('only notifies subscribed users for reply notifications', function () {
@@ -211,21 +210,21 @@ it('only notifies subscribed users for reply notifications', function () {
$nonSubscriber = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $subscriber);
Subscription::subscribe($post, $subscriber);
$parentComment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $subscriber->getKey(),
'user_type' => $subscriber->getMorphClass(),
'commenter_id' => $subscriber->getKey(),
'commenter_type' => $subscriber->getMorphClass(),
'body' => '<p>Original</p>',
]);
$reply = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'parent_id' => $parentComment->id,
'body' => '<p>Reply</p>',
]);
@@ -247,8 +246,8 @@ it('sends mention notification to mentioned user', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'body' => '<p>Hey @mentioned</p>',
]);
@@ -268,8 +267,8 @@ it('does NOT send mention notification to the comment author', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'body' => '<p>Hey @myself</p>',
]);
@@ -287,13 +286,13 @@ it('auto-subscribes mentioned user to the thread', function () {
$mentioned = User::factory()->create();
$post = Post::factory()->create();
expect(CommentSubscription::isSubscribed($post, $mentioned))->toBeFalse();
expect(Subscription::isSubscribed($post, $mentioned))->toBeFalse();
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'body' => '<p>Hey @mentioned</p>',
]);
@@ -301,7 +300,7 @@ it('auto-subscribes mentioned user to the thread', function () {
$listener = new SendUserMentionedNotification;
$listener->handle($event);
expect(CommentSubscription::isSubscribed($post, $mentioned))->toBeTrue();
expect(Subscription::isSubscribed($post, $mentioned))->toBeTrue();
});
it('does not send notifications when notifications are disabled', function () {
@@ -313,21 +312,21 @@ it('does not send notifications when notifications are disabled', function () {
$mentioned = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $subscriber);
Subscription::subscribe($post, $subscriber);
$parentComment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $subscriber->getKey(),
'user_type' => $subscriber->getMorphClass(),
'commenter_id' => $subscriber->getKey(),
'commenter_type' => $subscriber->getMorphClass(),
'body' => '<p>Original</p>',
]);
$reply = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $author->getKey(),
'user_type' => $author->getMorphClass(),
'commenter_id' => $author->getKey(),
'commenter_type' => $author->getMorphClass(),
'parent_id' => $parentComment->id,
'body' => '<p>Reply</p>',
]);

View File

@@ -1,33 +1,33 @@
<?php
use Relaticle\Comments\Config;
use Relaticle\Comments\CommentsConfig;
it('returns false for isBroadcastingEnabled by default', function () {
expect(Config::isBroadcastingEnabled())->toBeFalse();
expect(CommentsConfig::isBroadcastingEnabled())->toBeFalse();
});
it('returns true for isBroadcastingEnabled when config overridden', function () {
config()->set('comments.broadcasting.enabled', true);
expect(Config::isBroadcastingEnabled())->toBeTrue();
expect(CommentsConfig::isBroadcastingEnabled())->toBeTrue();
});
it('returns comments as default broadcast channel prefix', function () {
expect(Config::getBroadcastChannelPrefix())->toBe('comments');
expect(CommentsConfig::getBroadcastChannelPrefix())->toBe('comments');
});
it('returns custom broadcast channel prefix when overridden', function () {
config()->set('comments.broadcasting.channel_prefix', 'my-app-comments');
expect(Config::getBroadcastChannelPrefix())->toBe('my-app-comments');
expect(CommentsConfig::getBroadcastChannelPrefix())->toBe('my-app-comments');
});
it('returns 10s as default polling interval', function () {
expect(Config::getPollingInterval())->toBe('10s');
expect(CommentsConfig::getPollingInterval())->toBe('10s');
});
it('returns custom polling interval when overridden', function () {
config()->set('comments.polling.interval', '30s');
expect(Config::getPollingInterval())->toBe('30s');
expect(CommentsConfig::getPollingInterval())->toBe('30s');
});

View File

@@ -2,11 +2,11 @@
use Illuminate\Support\Facades\Event;
use Livewire\Livewire;
use Relaticle\Comments\Comment;
use Relaticle\Comments\CommentReaction;
use Relaticle\Comments\Config;
use Relaticle\Comments\CommentsConfig;
use Relaticle\Comments\Events\CommentReacted;
use Relaticle\Comments\Livewire\Reactions;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Models\Reaction;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -17,8 +17,8 @@ it('adds a reaction when user clicks an emoji', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
@@ -26,10 +26,10 @@ it('adds a reaction when user clicks an emoji', function () {
Livewire::test(Reactions::class, ['comment' => $comment])
->call('toggleReaction', 'thumbs_up');
expect(CommentReaction::where([
expect(Reaction::where([
'comment_id' => $comment->id,
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'reaction' => 'thumbs_up',
])->exists())->toBeTrue();
});
@@ -41,14 +41,14 @@ it('removes a reaction when toggling same emoji', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
CommentReaction::create([
Reaction::create([
'comment_id' => $comment->id,
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'reaction' => 'thumbs_up',
]);
@@ -57,10 +57,10 @@ it('removes a reaction when toggling same emoji', function () {
Livewire::test(Reactions::class, ['comment' => $comment])
->call('toggleReaction', 'thumbs_up');
expect(CommentReaction::where([
expect(Reaction::where([
'comment_id' => $comment->id,
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'reaction' => 'thumbs_up',
])->exists())->toBeFalse();
});
@@ -74,8 +74,8 @@ it('fires CommentReacted event with added action', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
@@ -98,14 +98,14 @@ it('fires CommentReacted event with removed action', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
CommentReaction::create([
Reaction::create([
'comment_id' => $comment->id,
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'reaction' => 'heart',
]);
@@ -133,35 +133,35 @@ it('returns correct reaction summary with counts', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user1->getKey(),
'user_type' => $user1->getMorphClass(),
'commenter_id' => $user1->getKey(),
'commenter_type' => $user1->getMorphClass(),
]);
CommentReaction::create([
Reaction::create([
'comment_id' => $comment->id,
'user_id' => $user1->getKey(),
'user_type' => $user1->getMorphClass(),
'commenter_id' => $user1->getKey(),
'commenter_type' => $user1->getMorphClass(),
'reaction' => 'thumbs_up',
]);
CommentReaction::create([
Reaction::create([
'comment_id' => $comment->id,
'user_id' => $user2->getKey(),
'user_type' => $user2->getMorphClass(),
'commenter_id' => $user2->getKey(),
'commenter_type' => $user2->getMorphClass(),
'reaction' => 'thumbs_up',
]);
CommentReaction::create([
Reaction::create([
'comment_id' => $comment->id,
'user_id' => $user3->getKey(),
'user_type' => $user3->getMorphClass(),
'commenter_id' => $user3->getKey(),
'commenter_type' => $user3->getMorphClass(),
'reaction' => 'thumbs_up',
]);
CommentReaction::create([
Reaction::create([
'comment_id' => $comment->id,
'user_id' => $user1->getKey(),
'user_type' => $user1->getMorphClass(),
'commenter_id' => $user1->getKey(),
'commenter_type' => $user1->getMorphClass(),
'reaction' => 'heart',
]);
@@ -187,14 +187,14 @@ it('requires authentication to react', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
Livewire::test(Reactions::class, ['comment' => $comment])
->call('toggleReaction', 'thumbs_up');
expect(CommentReaction::count())->toBe(0);
expect(Reaction::count())->toBe(0);
});
it('allows multiple reaction types from same user', function () {
@@ -204,8 +204,8 @@ it('allows multiple reaction types from same user', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
@@ -215,17 +215,17 @@ it('allows multiple reaction types from same user', function () {
$component->call('toggleReaction', 'thumbs_up');
$component->call('toggleReaction', 'heart');
expect(CommentReaction::where([
expect(Reaction::where([
'comment_id' => $comment->id,
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'reaction' => 'thumbs_up',
])->exists())->toBeTrue();
expect(CommentReaction::where([
expect(Reaction::where([
'comment_id' => $comment->id,
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'reaction' => 'heart',
])->exists())->toBeTrue();
});
@@ -238,8 +238,8 @@ it('allows same reaction from multiple users', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user1->getKey(),
'user_type' => $user1->getMorphClass(),
'commenter_id' => $user1->getKey(),
'commenter_type' => $user1->getMorphClass(),
]);
$this->actingAs($user1);
@@ -250,7 +250,7 @@ it('allows same reaction from multiple users', function () {
Livewire::test(Reactions::class, ['comment' => $comment])
->call('toggleReaction', 'thumbs_up');
expect(CommentReaction::where([
expect(Reaction::where([
'comment_id' => $comment->id,
'reaction' => 'thumbs_up',
])->count())->toBe(2);
@@ -263,8 +263,8 @@ it('rejects invalid reaction keys', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
@@ -272,7 +272,7 @@ it('rejects invalid reaction keys', function () {
Livewire::test(Reactions::class, ['comment' => $comment])
->call('toggleReaction', 'invalid_emoji');
expect(CommentReaction::count())->toBe(0);
expect(Reaction::count())->toBe(0);
});
it('marks reacted_by_user correctly in summary', function () {
@@ -283,14 +283,14 @@ it('marks reacted_by_user correctly in summary', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $userA->getKey(),
'user_type' => $userA->getMorphClass(),
'commenter_id' => $userA->getKey(),
'commenter_type' => $userA->getMorphClass(),
]);
CommentReaction::create([
Reaction::create([
'comment_id' => $comment->id,
'user_id' => $userA->getKey(),
'user_type' => $userA->getMorphClass(),
'commenter_id' => $userA->getKey(),
'commenter_type' => $userA->getMorphClass(),
'reaction' => 'thumbs_up',
]);
@@ -310,7 +310,7 @@ it('marks reacted_by_user correctly in summary', function () {
});
it('returns configured emoji set from config', function () {
$emojiSet = Config::getReactionEmojiSet();
$emojiSet = CommentsConfig::getReactionEmojiSet();
expect($emojiSet)->toBeArray();
expect($emojiSet)->toHaveKey('thumbs_up');
@@ -322,7 +322,7 @@ it('returns configured emoji set from config', function () {
});
it('returns allowed reaction keys from config', function () {
$allowed = Config::getAllowedReactions();
$allowed = CommentsConfig::getAllowedReactions();
expect($allowed)->toBeArray();
expect($allowed)->toContain('thumbs_up');

View File

@@ -1,10 +1,10 @@
<?php
use Livewire\Livewire;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Config;
use Relaticle\Comments\CommentsConfig;
use Relaticle\Comments\Livewire\CommentItem;
use Relaticle\Comments\Livewire\Comments;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -35,8 +35,8 @@ it('pre-fills editBody with existing comment HTML when starting edit', function
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => $originalHtml,
]);
@@ -54,8 +54,8 @@ it('saves edited HTML content through edit form', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Original</p>',
]);
@@ -81,8 +81,8 @@ it('creates reply with rich HTML content', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
@@ -108,8 +108,8 @@ it('renders comment body with fi-prose class', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>Styled comment</p>',
]);
@@ -120,7 +120,7 @@ it('renders comment body with fi-prose class', function () {
});
it('returns editor toolbar configuration as nested array', function () {
$toolbar = Config::getEditorToolbar();
$toolbar = CommentsConfig::getEditorToolbar();
expect($toolbar)->toBeArray();
expect($toolbar)->not->toBeEmpty();
@@ -134,7 +134,7 @@ it('uses custom toolbar config when overridden', function () {
['bold', 'italic'],
]]);
$toolbar = Config::getEditorToolbar();
$toolbar = CommentsConfig::getEditorToolbar();
expect($toolbar)->toHaveCount(1);
expect($toolbar[0])->toBe(['bold', 'italic']);

View File

@@ -1,8 +1,8 @@
<?php
use Illuminate\Database\Eloquent\Relations\Relation;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Config;
use Relaticle\Comments\CommentsConfig;
use Relaticle\Comments\Models\Comment;
it('registers the config file', function () {
expect(config('comments'))->toBeArray();
@@ -11,15 +11,15 @@ it('registers the config file', function () {
});
it('resolves the comment model from config', function () {
expect(Config::getCommentModel())->toBe(Comment::class);
expect(CommentsConfig::getCommentModel())->toBe(Comment::class);
});
it('resolves the comment table from config', function () {
expect(Config::getCommentTable())->toBe('comments');
expect(CommentsConfig::getCommentTable())->toBe('comments');
});
it('resolves max depth from config', function () {
expect(Config::getMaxDepth())->toBe(2);
expect(CommentsConfig::getMaxDepth())->toBe(2);
});
it('registers the morph map for comment', function () {
@@ -32,7 +32,7 @@ it('creates the comments table via migration', function () {
expect(Schema::hasTable('comments'))->toBeTrue();
expect(Schema::hasColumns('comments', [
'id', 'commentable_type', 'commentable_id',
'user_type', 'user_id', 'parent_id', 'body',
'commenter_type', 'commenter_id', 'parent_id', 'body',
'edited_at', 'deleted_at', 'created_at', 'updated_at',
]))->toBeTrue();
});

View File

@@ -1,8 +1,8 @@
<?php
use Livewire\Livewire;
use Relaticle\Comments\CommentSubscription;
use Relaticle\Comments\Livewire\Comments;
use Relaticle\Comments\Models\Subscription;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -12,35 +12,35 @@ it('subscribes user when toggling from unsubscribed state', function () {
$this->actingAs($user);
expect(CommentSubscription::isSubscribed($post, $user))->toBeFalse();
expect(Subscription::isSubscribed($post, $user))->toBeFalse();
Livewire::test(Comments::class, ['model' => $post])
->call('toggleSubscription');
expect(CommentSubscription::isSubscribed($post, $user))->toBeTrue();
expect(Subscription::isSubscribed($post, $user))->toBeTrue();
});
it('unsubscribes user when toggling from subscribed state', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $user);
Subscription::subscribe($post, $user);
$this->actingAs($user);
expect(CommentSubscription::isSubscribed($post, $user))->toBeTrue();
expect(Subscription::isSubscribed($post, $user))->toBeTrue();
Livewire::test(Comments::class, ['model' => $post])
->call('toggleSubscription');
expect(CommentSubscription::isSubscribed($post, $user))->toBeFalse();
expect(Subscription::isSubscribed($post, $user))->toBeFalse();
});
it('returns true for isSubscribed computed when user is subscribed', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $user);
Subscription::subscribe($post, $user);
$this->actingAs($user);
@@ -64,7 +64,7 @@ it('renders Subscribed text for subscribed user', function () {
$user = User::factory()->create();
$post = Post::factory()->create();
CommentSubscription::subscribe($post, $user);
Subscription::subscribe($post, $user);
$this->actingAs($user);

View File

@@ -1,7 +1,7 @@
<?php
use Relaticle\Comments\Comment;
use Relaticle\Comments\Events\UserMentioned;
use Relaticle\Comments\Models\Comment;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
@@ -13,8 +13,8 @@ it('carries correct comment and mentioned user in payload', function () {
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'commenter_id' => $user->getKey(),
'commenter_type' => $user->getMorphClass(),
'body' => '<p>@john</p>',
]);

View File

@@ -5,14 +5,14 @@ namespace Relaticle\Comments\Tests\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Relaticle\Comments\Concerns\IsCommenter;
use Relaticle\Comments\Contracts\Commenter;
use Relaticle\Comments\Concerns\CanComment;
use Relaticle\Comments\Contracts\Commentator;
use Relaticle\Comments\Tests\Database\Factories\UserFactory;
class User extends Authenticatable implements Commenter
class User extends Authenticatable implements Commentator
{
use CanComment;
use HasFactory;
use IsCommenter;
use Notifiable;
protected $table = 'users';

View File

@@ -48,13 +48,13 @@ abstract class TestCase extends Orchestra
$table->timestamps();
});
Schema::create(config('comments.tables.comments', 'comments'), function (Blueprint $table) {
Schema::create(config('comments.table_names.comments', 'comments'), function (Blueprint $table) {
$table->id();
$table->morphs('commentable');
$table->morphs('user');
$table->morphs('commenter');
$table->foreignId('parent_id')
->nullable()
->constrained(config('comments.tables.comments', 'comments'))
->constrained(config('comments.table_names.comments', 'comments'))
->cascadeOnDelete();
$table->text('body');
$table->timestamp('edited_at')->nullable();
@@ -67,30 +67,30 @@ abstract class TestCase extends Orchestra
Schema::create('comment_mentions', function (Blueprint $table) {
$table->id();
$table->foreignId('comment_id')
->constrained(config('comments.tables.comments', 'comments'))
->constrained(config('comments.table_names.comments', 'comments'))
->cascadeOnDelete();
$table->morphs('user');
$table->morphs('commenter');
$table->timestamps();
$table->unique(['comment_id', 'user_id', 'user_type']);
$table->unique(['comment_id', 'commenter_id', 'commenter_type']);
});
Schema::create('comment_reactions', function (Blueprint $table) {
$table->id();
$table->foreignId('comment_id')
->constrained(config('comments.tables.comments', 'comments'))
->constrained(config('comments.table_names.comments', 'comments'))
->cascadeOnDelete();
$table->morphs('user');
$table->morphs('commenter');
$table->string('reaction');
$table->timestamps();
$table->unique(['comment_id', 'user_id', 'user_type', 'reaction']);
$table->unique(['comment_id', 'commenter_id', 'commenter_type', 'reaction']);
});
Schema::create('comment_attachments', function (Blueprint $table) {
$table->id();
$table->foreignId('comment_id')
->constrained(config('comments.tables.comments', 'comments'))
->constrained(config('comments.table_names.comments', 'comments'))
->cascadeOnDelete();
$table->string('file_path');
$table->string('original_name');
@@ -112,10 +112,10 @@ abstract class TestCase extends Orchestra
Schema::create('comment_subscriptions', function (Blueprint $table) {
$table->id();
$table->morphs('commentable');
$table->morphs('user');
$table->morphs('commenter');
$table->timestamp('created_at')->nullable();
$table->unique(['commentable_type', 'commentable_id', 'user_type', 'user_id'], 'comment_subscriptions_unique');
$table->unique(['commentable_type', 'commentable_id', 'commenter_type', 'commenter_id'], 'comment_subscriptions_unique');
});
}