Skip to content

Commit 9f48d44

Browse files
Merge pull request #416 from JLG-WOCFR-DEV/codex/refactor-sanitizementions-and-update-addcomment-bv6uaf
Refine comment mention sanitization
2 parents 03f66b9 + 43e2843 commit 9f48d44

File tree

2 files changed

+30
-43
lines changed

2 files changed

+30
-43
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,27 @@ private function hydrateComment(array $raw, array $users): array
205205
*/
206206
private function sanitizeMentions(array $mentions): array
207207
{
208-
$uniqueIds = [];
208+
$ids = [];
209+
209210
foreach ($mentions as $mention) {
210211
$id = is_int($mention) ? $mention : (int) $mention;
211-
if ($id <= 0) {
212+
if ($id <= 0 || in_array($id, $ids, true)) {
212213
continue;
213214
}
214-
if (in_array($id, $uniqueIds, true)) {
215-
continue;
215+
216+
$ids[] = $id;
217+
}
218+
219+
if ($ids === []) {
220+
return [];
221+
}
222+
223+
$users = $this->loadUsers($ids);
224+
225+
$existing = [];
226+
foreach ($ids as $id) {
227+
if (isset($users[$id])) {
228+
$existing[] = $id;
216229
}
217230
$uniqueIds[] = $id;
218231
}
@@ -226,10 +239,7 @@ private function sanitizeMentions(array $mentions): array
226239
return [];
227240
}
228241

229-
return array_values(array_filter(
230-
$uniqueIds,
231-
static fn(int $id): bool => isset($users[$id])
232-
));
242+
return $existing;
233243
}
234244

235245
/**

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

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -79,66 +79,43 @@ public function test_get_comments_uses_single_query_for_shared_users(): void
7979
$this->assertSame($authorId, $second['mentions'][1]['id']);
8080
}
8181

82-
public function test_add_comment_filters_duplicate_and_invalid_mentions(): void
82+
public function test_add_comment_sanitizes_mentions(): void
8383
{
8484
$authorId = self::factory()->user->create([
8585
'display_name' => 'Author',
8686
]);
8787
$firstMentionId = self::factory()->user->create([
88-
'display_name' => 'Bob',
88+
'display_name' => 'First Mention',
8989
]);
9090
$secondMentionId = self::factory()->user->create([
91-
'display_name' => 'Carol',
91+
'display_name' => 'Second Mention',
9292
]);
9393

9494
$result = $this->store->addComment(
9595
'post',
96-
'42',
97-
'Hello mentions',
96+
'1',
97+
'Hello world',
9898
[
9999
$firstMentionId,
100100
(string) $firstMentionId,
101101
0,
102102
-5,
103-
'abc',
104-
$secondMentionId,
105-
99999,
103+
'foo',
106104
$secondMentionId,
105+
999999,
107106
],
108107
$authorId
109108
);
110109

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-
119110
$this->assertCount(2, $result['mentions']);
120111
$this->assertSame($firstMentionId, $result['mentions'][0]['id']);
121112
$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-
);
137113

138114
$stored = get_option('ssc_entity_comments');
139-
$mentions = $stored['post']['99'][0]['mentions'] ?? null;
140-
141-
$this->assertSame([], $mentions);
142-
$this->assertSame([], $result['mentions']);
115+
$this->assertIsArray($stored);
116+
$this->assertSame(
117+
[$firstMentionId, $secondMentionId],
118+
$stored['post']['1'][0]['mentions']
119+
);
143120
}
144121
}

0 commit comments

Comments
 (0)