From 552f8a631c3cd90a84b14a0d179041dd64d35185 Mon Sep 17 00:00:00 2001 From: Arpit Jain Date: Sun, 15 Nov 2026 13:16:04 -0500 Subject: [PATCH] fix: use the last tag when the search value starts with a comma Co-Authored-By: Claude Signed-off-by: Arpit Jain --- .../Administration/Api/TagController.php | 2 +- .../Administration/Api/TagControllerTest.php | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/phpmyfaq/src/phpMyFAQ/Controller/Administration/Api/TagController.php b/phpmyfaq/src/phpMyFAQ/Controller/Administration/Api/TagController.php index 79477e3f1b..eb1840be96 100644 --- a/phpmyfaq/src/phpMyFAQ/Controller/Administration/Api/TagController.php +++ b/phpmyfaq/src/phpMyFAQ/Controller/Administration/Api/TagController.php @@ -89,7 +89,7 @@ public function search(Request $request): JsonResponse $tags = []; if (!is_null($autoCompleteValue)) { - if (strpos($autoCompleteValue, needle: ',')) { + if (str_contains($autoCompleteValue, needle: ',')) { $arrayOfValues = explode(separator: ',', string: $autoCompleteValue); $autoCompleteValue = end($arrayOfValues); } diff --git a/tests/phpMyFAQ/Controller/Administration/Api/TagControllerTest.php b/tests/phpMyFAQ/Controller/Administration/Api/TagControllerTest.php index 47c25f9ab3..ce8de8a1be 100644 --- a/tests/phpMyFAQ/Controller/Administration/Api/TagControllerTest.php +++ b/tests/phpMyFAQ/Controller/Administration/Api/TagControllerTest.php @@ -436,6 +436,28 @@ public function testSearchUsesTheLastValueOfACommaSeparatedList(): void self::assertSame([['id' => '0', 'name' => 'bar'], ['id' => '1', 'name' => 'barbaz']], $payload); } + /** + * @throws \Exception + */ + public function testSearchUsesTheLastValueWhenTheListStartsWithAComma(): void + { + $tags = $this->createMock(Tags::class); + $tags + ->expects($this->once()) + ->method('getAllTags') + ->with('bar', PMF_TAGS_CLOUD_RESULT_SET_SIZE) + ->willReturn(['bar', 'barbaz']); + + $controller = $this->createController($tags); + $controller->setContainer($this->createAuthenticatedContainer()); + + $response = $controller->search(new Request(['search' => ', Bar'])); + $payload = json_decode((string) $response->getContent(), true, 512, JSON_THROW_ON_ERROR); + + self::assertSame(Response::HTTP_OK, $response->getStatusCode()); + self::assertSame([['id' => '0', 'name' => 'bar'], ['id' => '1', 'name' => 'barbaz']], $payload); + } + /** * @throws \Exception */