Skip to content

Commit 9c4a922

Browse files
committed
Comments API: Addressed failing tests and static testing
1 parent 4627dfd commit 9c4a922

File tree

8 files changed

+27
-19
lines changed

8 files changed

+27
-19
lines changed

app/Activity/CommentRepo.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function getVisibleById(int $id): Comment
3131

3232
/**
3333
* Start a query for comments visible to the user.
34+
* @return Builder<Comment>
3435
*/
3536
public function getQueryForVisible(): Builder
3637
{

app/Entities/Models/Entity.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ public function tags(): MorphMany
237237

238238
/**
239239
* Get the comments for an entity.
240+
* @return MorphMany<Comment, $this>
240241
*/
241242
public function comments(bool $orderByCreated = true): MorphMany
242243
{

app/Http/ApiController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract class ApiController extends Controller
1212
* The validation rules for this controller.
1313
* Can alternative be defined in a rules() method is they need to be dynamic.
1414
*
15-
* @var array<string, string[]>
15+
* @var array<string, array<string, string[]>>
1616
*/
1717
protected array $rules = [];
1818

app/Search/SearchRunner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,9 @@ protected function sortByLastCommented(EloquentBuilder $query, Entity $model, bo
461461
{
462462
$commentsTable = DB::getTablePrefix() . 'comments';
463463
$morphClass = str_replace('\\', '\\\\', $model->getMorphClass());
464-
$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');
464+
$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');
465465

466-
$query->join($commentQuery, $model->getTable() . '.id', '=', DB::raw('comments.entity_id'))
466+
$query->join($commentQuery, $model->getTable() . '.id', '=', DB::raw('comments.commentable_id'))
467467
->orderBy('last_commented', $negated ? 'asc' : 'desc');
468468
}
469469
}

database/factories/Activity/Models/CommentFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Database\Factories\Activity\Models;
44

5+
use BookStack\Users\Models\User;
56
use Illuminate\Database\Eloquent\Factories\Factory;
67

78
class CommentFactory extends Factory
@@ -29,12 +30,16 @@ public function definition()
2930
$html = '<p>' . $text . '</p>';
3031
$nextLocalId = static::$nextLocalId++;
3132

33+
$user = User::query()->first();
34+
3235
return [
3336
'html' => $html,
3437
'parent_id' => null,
3538
'local_id' => $nextLocalId,
3639
'content_ref' => '',
3740
'archived' => false,
41+
'created_by' => $user ?? User::factory(),
42+
'updated_by' => $user ?? User::factory(),
3843
];
3944
}
4045
}

tests/Activity/WatchTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ public function test_notifications_sent_in_right_language()
340340
ActivityType::PAGE_CREATE => $entities['page'],
341341
ActivityType::PAGE_UPDATE => $entities['page'],
342342
ActivityType::COMMENT_CREATE => Comment::factory()->make([
343-
'entity_id' => $entities['page']->id,
344-
'entity_type' => $entities['page']->getMorphClass(),
343+
'commentable_id' => $entities['page']->id,
344+
'commentable_type' => $entities['page']->getMorphClass(),
345345
]),
346346
];
347347

tests/Entity/CommentDisplayTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public function test_comment_editor_js_loaded_with_create_or_edit_permissions()
7272

7373
Comment::factory()->create([
7474
'created_by' => $editor->id,
75-
'entity_type' => 'page',
76-
'entity_id' => $page->id,
75+
'commentable_type' => 'page',
76+
'commentable_id' => $page->id,
7777
]);
7878

7979
$resp = $this->actingAs($editor)->get($page->getUrl());
@@ -84,7 +84,7 @@ public function test_comment_editor_js_loaded_with_create_or_edit_permissions()
8484
public function test_comment_displays_relative_times()
8585
{
8686
$page = $this->entities->page();
87-
$comment = Comment::factory()->create(['entity_id' => $page->id, 'entity_type' => $page->getMorphClass()]);
87+
$comment = Comment::factory()->create(['commentable_id' => $page->id, 'commentable_type' => $page->getMorphClass()]);
8888
$comment->created_at = now()->subWeek();
8989
$comment->updated_at = now()->subDay();
9090
$comment->save();

tests/Entity/CommentStoreTest.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function test_add_comment()
1414
$this->asAdmin();
1515
$page = $this->entities->page();
1616

17+
Comment::factory()->create(['commentable_id' => $page->id, 'commentable_type' => 'page', 'local_id' => 2]);
1718
$comment = Comment::factory()->make(['parent_id' => 2]);
1819
$resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
1920

@@ -24,9 +25,9 @@ public function test_add_comment()
2425
$pageResp->assertSee($comment->html, false);
2526

2627
$this->assertDatabaseHas('comments', [
27-
'local_id' => 1,
28-
'entity_id' => $page->id,
29-
'entity_type' => Page::newModelInstance()->getMorphClass(),
28+
'local_id' => 3,
29+
'commentable_id' => $page->id,
30+
'commentable_type' => 'page',
3031
'parent_id' => 2,
3132
]);
3233

@@ -52,9 +53,9 @@ public function test_add_comment_stores_content_reference_only_if_format_valid()
5253
]);
5354

5455
if ($valid) {
55-
$this->assertDatabaseHas('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
56+
$this->assertDatabaseHas('comments', ['commentable_id' => $page->id, 'content_ref' => $ref]);
5657
} else {
57-
$this->assertDatabaseMissing('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
58+
$this->assertDatabaseMissing('comments', ['commentable_id' => $page->id, 'content_ref' => $ref]);
5859
}
5960
}
6061
}
@@ -79,7 +80,7 @@ public function test_comment_edit()
7980

8081
$this->assertDatabaseHas('comments', [
8182
'html' => $newHtml,
82-
'entity_id' => $page->id,
83+
'commentable_id' => $page->id,
8384
]);
8485

8586
$this->assertActivityExists(ActivityType::COMMENT_UPDATE);
@@ -218,7 +219,7 @@ public function test_scripts_are_removed_even_if_already_in_db()
218219
$page = $this->entities->page();
219220
Comment::factory()->create([
220221
'html' => '<script>superbadscript</script><script>superbadscript</script><p onclick="superbadonclick">scriptincommentest</p>',
221-
'entity_type' => 'page', 'entity_id' => $page
222+
'commentable_type' => 'page', 'commentable_id' => $page
222223
]);
223224

224225
$resp = $this->asAdmin()->get($page->getUrl());
@@ -236,8 +237,8 @@ public function test_comment_html_is_limited()
236237
$resp = $this->asAdmin()->post("/comment/{$page->id}", ['html' => $input]);
237238
$resp->assertOk();
238239
$this->assertDatabaseHas('comments', [
239-
'entity_type' => 'page',
240-
'entity_id' => $page->id,
240+
'commentable_type' => 'page',
241+
'commentable_id' => $page->id,
241242
'html' => $expected,
242243
]);
243244

@@ -259,8 +260,8 @@ public function test_comment_html_spans_are_cleaned()
259260
$resp = $this->asAdmin()->post("/comment/{$page->id}", ['html' => $input]);
260261
$resp->assertOk();
261262
$this->assertDatabaseHas('comments', [
262-
'entity_type' => 'page',
263-
'entity_id' => $page->id,
263+
'commentable_type' => 'page',
264+
'commentable_id' => $page->id,
264265
'html' => $expected,
265266
]);
266267

0 commit comments

Comments
 (0)