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 */