Комментарии к записям — на примере заметок

This commit is contained in:
rsgrinko committed 2026-09-16 11:12:00 +03:00
1 parent 5d01831ebd
commit f5da740bc8
8 files changed
+344

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
/**
* Комментарии: обсуждение к любой записи.
*/
use App\Models\Note;
use Rsgrinko\Proton\Comments\Comment;
use Rsgrinko\Proton\Database\Model\Factory;
use Rsgrinko\Proton\Models\User;
test('комментарии: добавляются к записи и читаются по порядку', function (): void {
withOwnDatabase(static function (): void {
/** @var User $author */
$author = Factory::of(User::class)->create();
/** @var Note $note */
$note = Factory::of(Note::class)->create(['user_id' => $author->id()]);
$first = Comment::add('note', $note->id(), $author->id(), 'Первый');
$second = Comment::add('note', $note->id(), $author->id(), ' Второй ');
assertSame('Второй', $second->body, 'пробелы по краям обрезаются');
$found = Comment::of('note', $note->id());
assertCount(2, $found);
assertSame($first->id(), $found[0]->id(), 'старые сверху');
assertSame($second->id(), $found[1]->id());
assertSame(2, Comment::countOf('note', $note->id()));
assertSame(0, Comment::countOf('note', $note->id() + 1), 'у чужой записи комментариев нет');
});
});
test('комментарии: знают своего автора', function (): void {
withOwnDatabase(static function (): void {
/** @var User $author */
$author = Factory::of(User::class)->create();
/** @var Note $note */
$note = Factory::of(Note::class)->create(['user_id' => $author->id()]);
$comment = Comment::add('note', $note->id(), $author->id(), 'Текст');
assertSame($author->id(), $comment->author?->id());
});
});
test('комментарии: удаление записи разом убирает все её комментарии', function (): void {
withOwnDatabase(static function (): void {
/** @var User $author */
$author = Factory::of(User::class)->create();
/** @var Note $note */
$note = Factory::of(Note::class)->create(['user_id' => $author->id()]);
Comment::add('note', $note->id(), $author->id(), 'Раз');
Comment::add('note', $note->id(), $author->id(), 'Два');
assertSame(2, Comment::removeAll('note', $note->id()));
assertSame(0, Comment::countOf('note', $note->id()));
});
});
+41
View File
@@ -13,6 +13,7 @@ use App\Models\Note;
use Rsgrinko\Proton\Access\Permission;
use Rsgrinko\Proton\Auth\Auth;
use Rsgrinko\Proton\Auth\Csrf;
use Rsgrinko\Proton\Comments\Comment;
use Rsgrinko\Proton\Http\Kernel;
use Rsgrinko\Proton\Http\Request;
use Rsgrinko\Proton\Http\Response;
@@ -217,6 +218,46 @@ test('http: чужая запись для обычного пользовате
assertNotContains('Чужая заметка', $list->body());
});
test('http: комментарий к заметке пишет и стирает свой, чужой — только с notes.manage', function (): void {
$note = Note::create(['title' => 'Заметка для обсуждения']);
$note->forceFill(['user_id' => httpAdmin()->id()])->save();
$response = httpRequest('POST', '/notes/' . $note->id() . '/comments', httpAdmin(), ['body' => 'Первый комментарий']);
assertStatus(302, $response);
$shown = httpRequest('GET', '/notes/' . $note->id(), httpAdmin());
assertSee('Первый комментарий', $shown);
$comment = assertNotNull(Comment::query()->where('entity', 'note')->where('entity_id', (string) $note->id())->first());
// Читатель без notes.manage чужой комментарий не уберёт
$role = Role::create(['name' => 'Только смотрит ' . bin2hex(random_bytes(3)), 'permissions' => ['notes.view']]);
$viewer = User::register('http_comment_viewer_' . bin2hex(random_bytes(3)), 'секрет123', [
'email' => 'commentviewer' . bin2hex(random_bytes(3)) . '@example.com',
'role_id' => $role->id(),
]);
afterTests(static function () use ($viewer, $role): void {
$viewer->forceDelete();
$role->forceDelete();
});
$denied = httpRequest('POST', '/notes/' . $note->id() . '/comments/delete', $viewer, ['comment' => $comment->id()]);
assertStatus(302, $denied);
assertNotNull(Comment::find($comment->id()), 'чужой комментарий остался цел');
// Сам автор свой комментарий убирает
$removed = httpRequest('POST', '/notes/' . $note->id() . '/comments/delete', httpAdmin(), ['comment' => $comment->id()]);
assertStatus(302, $removed);
assertNull(Comment::find($comment->id()));
});
test('http: форма без токена ничего не меняет', function (): void {
$before = Note::query()->where('user_id', httpAdmin()->id())->count();