Comments API: Addressed failing tests and static testing

This commit is contained in:
Dan Brown 2025-10-24 14:22:53 +01:00
parent 4627dfd4f7
commit 9c4a9225af
No known key found for this signature in database
GPG Key ID: 116094FE15AE65C0
8 changed files with 27 additions and 19 deletions

View File

@ -31,6 +31,7 @@ class CommentRepo
/**
* Start a query for comments visible to the user.
* @return Builder<Comment>
*/
public function getQueryForVisible(): Builder
{

View File

@ -237,6 +237,7 @@ abstract class Entity extends Model implements
/**
* Get the comments for an entity.
* @return MorphMany<Comment, $this>
*/
public function comments(bool $orderByCreated = true): MorphMany
{

View File

@ -12,7 +12,7 @@ abstract class ApiController extends Controller
* The validation rules for this controller.
* Can alternative be defined in a rules() method is they need to be dynamic.
*
* @var array<string, string[]>
* @var array<string, array<string, string[]>>
*/
protected array $rules = [];

View File

@ -461,9 +461,9 @@ class SearchRunner
{
$commentsTable = DB::getTablePrefix() . 'comments';
$morphClass = str_replace('\\', '\\\\', $model->getMorphClass());
$commentQuery = DB::raw('(SELECT c1.entity_id, c1.entity_type, c1.created_at as last_commented FROM ' . $commentsTable . ' c1 LEFT JOIN ' . $commentsTable . ' c2 ON (c1.entity_id = c2.entity_id AND c1.entity_type = c2.entity_type AND c1.created_at < c2.created_at) WHERE c1.entity_type = \'' . $morphClass . '\' AND c2.created_at IS NULL) as comments');
$commentQuery = DB::raw('(SELECT c1.commentable_id, c1.commentable_type, c1.created_at as last_commented FROM ' . $commentsTable . ' c1 LEFT JOIN ' . $commentsTable . ' c2 ON (c1.commentable_id = c2.commentable_id AND c1.commentable_type = c2.commentable_type AND c1.created_at < c2.created_at) WHERE c1.commentable_type = \'' . $morphClass . '\' AND c2.created_at IS NULL) as comments');
$query->join($commentQuery, $model->getTable() . '.id', '=', DB::raw('comments.entity_id'))
$query->join($commentQuery, $model->getTable() . '.id', '=', DB::raw('comments.commentable_id'))
->orderBy('last_commented', $negated ? 'asc' : 'desc');
}
}

View File

@ -2,6 +2,7 @@
namespace Database\Factories\Activity\Models;
use BookStack\Users\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class CommentFactory extends Factory
@ -29,12 +30,16 @@ class CommentFactory extends Factory
$html = '<p>' . $text . '</p>';
$nextLocalId = static::$nextLocalId++;
$user = User::query()->first();
return [
'html' => $html,
'parent_id' => null,
'local_id' => $nextLocalId,
'content_ref' => '',
'archived' => false,
'created_by' => $user ?? User::factory(),
'updated_by' => $user ?? User::factory(),
];
}
}

View File

@ -340,8 +340,8 @@ class WatchTest extends TestCase
ActivityType::PAGE_CREATE => $entities['page'],
ActivityType::PAGE_UPDATE => $entities['page'],
ActivityType::COMMENT_CREATE => Comment::factory()->make([
'entity_id' => $entities['page']->id,
'entity_type' => $entities['page']->getMorphClass(),
'commentable_id' => $entities['page']->id,
'commentable_type' => $entities['page']->getMorphClass(),
]),
];

View File

@ -72,8 +72,8 @@ class CommentDisplayTest extends TestCase
Comment::factory()->create([
'created_by' => $editor->id,
'entity_type' => 'page',
'entity_id' => $page->id,
'commentable_type' => 'page',
'commentable_id' => $page->id,
]);
$resp = $this->actingAs($editor)->get($page->getUrl());
@ -84,7 +84,7 @@ class CommentDisplayTest extends TestCase
public function test_comment_displays_relative_times()
{
$page = $this->entities->page();
$comment = Comment::factory()->create(['entity_id' => $page->id, 'entity_type' => $page->getMorphClass()]);
$comment = Comment::factory()->create(['commentable_id' => $page->id, 'commentable_type' => $page->getMorphClass()]);
$comment->created_at = now()->subWeek();
$comment->updated_at = now()->subDay();
$comment->save();

View File

@ -14,6 +14,7 @@ class CommentStoreTest extends TestCase
$this->asAdmin();
$page = $this->entities->page();
Comment::factory()->create(['commentable_id' => $page->id, 'commentable_type' => 'page', 'local_id' => 2]);
$comment = Comment::factory()->make(['parent_id' => 2]);
$resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
@ -24,9 +25,9 @@ class CommentStoreTest extends TestCase
$pageResp->assertSee($comment->html, false);
$this->assertDatabaseHas('comments', [
'local_id' => 1,
'entity_id' => $page->id,
'entity_type' => Page::newModelInstance()->getMorphClass(),
'local_id' => 3,
'commentable_id' => $page->id,
'commentable_type' => 'page',
'parent_id' => 2,
]);
@ -52,9 +53,9 @@ class CommentStoreTest extends TestCase
]);
if ($valid) {
$this->assertDatabaseHas('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
$this->assertDatabaseHas('comments', ['commentable_id' => $page->id, 'content_ref' => $ref]);
} else {
$this->assertDatabaseMissing('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
$this->assertDatabaseMissing('comments', ['commentable_id' => $page->id, 'content_ref' => $ref]);
}
}
}
@ -79,7 +80,7 @@ class CommentStoreTest extends TestCase
$this->assertDatabaseHas('comments', [
'html' => $newHtml,
'entity_id' => $page->id,
'commentable_id' => $page->id,
]);
$this->assertActivityExists(ActivityType::COMMENT_UPDATE);
@ -218,7 +219,7 @@ class CommentStoreTest extends TestCase
$page = $this->entities->page();
Comment::factory()->create([
'html' => '<script>superbadscript</script><script>superbadscript</script><p onclick="superbadonclick">scriptincommentest</p>',
'entity_type' => 'page', 'entity_id' => $page
'commentable_type' => 'page', 'commentable_id' => $page
]);
$resp = $this->asAdmin()->get($page->getUrl());
@ -236,8 +237,8 @@ class CommentStoreTest extends TestCase
$resp = $this->asAdmin()->post("/comment/{$page->id}", ['html' => $input]);
$resp->assertOk();
$this->assertDatabaseHas('comments', [
'entity_type' => 'page',
'entity_id' => $page->id,
'commentable_type' => 'page',
'commentable_id' => $page->id,
'html' => $expected,
]);
@ -259,8 +260,8 @@ class CommentStoreTest extends TestCase
$resp = $this->asAdmin()->post("/comment/{$page->id}", ['html' => $input]);
$resp->assertOk();
$this->assertDatabaseHas('comments', [
'entity_type' => 'page',
'entity_id' => $page->id,
'commentable_type' => 'page',
'commentable_id' => $page->id,
'html' => $expected,
]);