Skip to content

Commit c6ce8c8

Browse files
Merge pull request #413 from JLG-WOCFR-DEV/codex/refactor-sanitizementions-and-update-addcomment
Refine mention sanitization in CommentStore
2 parents 4cfb477 + 92c7ee0 commit c6ce8c8

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

supersede-css-jlg-enhanced/src/Infra/Comments/CommentStore.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function addComment(string $entityType, string $entityId, string $message
6969
}
7070

7171
$userId = (int) $userId;
72-
$mentions = $this->sanitizeMentions($mentions);
72+
$cleanMentions = $this->sanitizeMentions($mentions);
7373

7474
$comments = $this->read();
7575
$entityComments = $comments[$entityType][$entityId] ?? [];
@@ -83,7 +83,7 @@ public function addComment(string $entityType, string $entityId, string $message
8383
'entity_type' => $entityType,
8484
'entity_id' => $entityId,
8585
'message' => $message,
86-
'mentions' => $mentions,
86+
'mentions' => $cleanMentions,
8787
'created_by' => $userId,
8888
'created_at' => $timestamp,
8989
];
@@ -105,7 +105,7 @@ public function addComment(string $entityType, string $entityId, string $message
105105
'entity_id' => $entityId,
106106
'details' => [
107107
'comment_id' => $commentId,
108-
'mentions' => $mentions,
108+
'mentions' => $cleanMentions,
109109
],
110110
]);
111111

@@ -205,19 +205,31 @@ private function hydrateComment(array $raw, array $users): array
205205
*/
206206
private function sanitizeMentions(array $mentions): array
207207
{
208-
$sanitized = [];
208+
$uniqueIds = [];
209209
foreach ($mentions as $mention) {
210210
$id = is_int($mention) ? $mention : (int) $mention;
211211
if ($id <= 0) {
212212
continue;
213213
}
214-
$user = get_userdata($id);
215-
if ($user instanceof WP_User) {
216-
$sanitized[] = $user->ID;
214+
if (in_array($id, $uniqueIds, true)) {
215+
continue;
217216
}
217+
$uniqueIds[] = $id;
218+
}
219+
220+
if ($uniqueIds === []) {
221+
return [];
222+
}
223+
224+
$users = $this->loadUsers($uniqueIds);
225+
if ($users === []) {
226+
return [];
218227
}
219228

220-
return array_values(array_unique($sanitized));
229+
return array_values(array_filter(
230+
$uniqueIds,
231+
static fn(int $id): bool => isset($users[$id])
232+
));
221233
}
222234

223235
/**

supersede-css-jlg-enhanced/tests/Infra/Comments/CommentStoreTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,67 @@ public function test_get_comments_uses_single_query_for_shared_users(): void
7878
$this->assertSame($mentionId, $second['mentions'][0]['id']);
7979
$this->assertSame($authorId, $second['mentions'][1]['id']);
8080
}
81+
82+
public function test_add_comment_filters_duplicate_and_invalid_mentions(): void
83+
{
84+
$authorId = self::factory()->user->create([
85+
'display_name' => 'Author',
86+
]);
87+
$firstMentionId = self::factory()->user->create([
88+
'display_name' => 'Bob',
89+
]);
90+
$secondMentionId = self::factory()->user->create([
91+
'display_name' => 'Carol',
92+
]);
93+
94+
$result = $this->store->addComment(
95+
'post',
96+
'42',
97+
'Hello mentions',
98+
[
99+
$firstMentionId,
100+
(string) $firstMentionId,
101+
0,
102+
-5,
103+
'abc',
104+
$secondMentionId,
105+
99999,
106+
$secondMentionId,
107+
],
108+
$authorId
109+
);
110+
111+
$stored = get_option('ssc_entity_comments');
112+
$mentions = $stored['post']['42'][0]['mentions'] ?? null;
113+
114+
$this->assertSame([
115+
$firstMentionId,
116+
$secondMentionId,
117+
], $mentions);
118+
119+
$this->assertCount(2, $result['mentions']);
120+
$this->assertSame($firstMentionId, $result['mentions'][0]['id']);
121+
$this->assertSame($secondMentionId, $result['mentions'][1]['id']);
122+
}
123+
124+
public function test_add_comment_discards_nonexistent_mentions(): void
125+
{
126+
$authorId = self::factory()->user->create([
127+
'display_name' => 'Author',
128+
]);
129+
130+
$result = $this->store->addComment(
131+
'post',
132+
'99',
133+
'No valid mentions',
134+
[123456, 0, 'foo'],
135+
$authorId
136+
);
137+
138+
$stored = get_option('ssc_entity_comments');
139+
$mentions = $stored['post']['99'][0]['mentions'] ?? null;
140+
141+
$this->assertSame([], $mentions);
142+
$this->assertSame([], $result['mentions']);
143+
}
81144
}

0 commit comments

Comments
 (0)