Skip to content

Commit 45b8d6c

Browse files
committed
Comments: Fixed wrong identification of parent comment
Would cause comment reply notifications to not be sent to expected user. Updated test to cover problem case. For #4548
1 parent 99eb3e5 commit 45b8d6c

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

app/Activity/Models/Comment.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
* @property int $id
1313
* @property string $text
1414
* @property string $html
15-
* @property int|null $parent_id
15+
* @property int|null $parent_id - Relates to local_id, not id
1616
* @property int $local_id
1717
* @property string $entity_type
1818
* @property int $entity_id
19+
* @property int $created_by
20+
* @property int $updated_by
1921
*/
2022
class Comment extends Model implements Loggable
2123
{
@@ -38,7 +40,9 @@ public function entity(): MorphTo
3840
*/
3941
public function parent(): BelongsTo
4042
{
41-
return $this->belongsTo(Comment::class);
43+
return $this->belongsTo(Comment::class, 'parent_id', 'local_id', 'parent')
44+
->where('entity_type', '=', $this->entity_type)
45+
->where('entity_id', '=', $this->entity_id);
4246
}
4347

4448
/**

tests/Activity/WatchTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,22 @@ public function test_notify_comment_replies()
210210
$prefs = new UserNotificationPreferences($editor);
211211
$prefs->updateFromSettingsArray(['comment-replies' => 'true']);
212212

213+
// Create some existing comments to pad IDs to help potentially error
214+
// on mis-identification of parent via ids used.
215+
Comment::factory()->count(5)
216+
->for($entities['page'], 'entity')
217+
->create(['created_by' => $this->users->admin()->id]);
218+
213219
$notifications = Notification::fake();
214220

215221
$this->actingAs($editor)->post("/comment/{$entities['page']->id}", [
216222
'text' => 'My new comment'
217223
]);
218-
$comment = $entities['page']->comments()->first();
224+
$comment = $entities['page']->comments()->orderBy('id', 'desc')->first();
219225

220226
$this->asAdmin()->post("/comment/{$entities['page']->id}", [
221227
'text' => 'My new comment response',
222-
'parent_id' => $comment->id,
228+
'parent_id' => $comment->local_id,
223229
]);
224230
$notifications->assertSentTo($editor, CommentCreationNotification::class);
225231
}

0 commit comments

Comments
 (0)