diff --git a/src/Controller/LdapDropdownController.php b/src/Controller/LdapDropdownController.php index 6499512..2ae6ef5 100644 --- a/src/Controller/LdapDropdownController.php +++ b/src/Controller/LdapDropdownController.php @@ -93,7 +93,7 @@ public function __invoke(Request $request): Response $this->checkFormAccessPolicies($question->getForm(), $request); // Read others parameters - $search_text = $request->request->getString(''); + $search_text = $request->request->getString('searchText', ''); $page = $request->request->getInt('page', 0); $page_limit = $request->request->getInt('page_limit', 0); diff --git a/tests/Controller/LdapDropdownControllerTest.php b/tests/Controller/LdapDropdownControllerTest.php index 55d2699..ea27b9e 100644 --- a/tests/Controller/LdapDropdownControllerTest.php +++ b/tests/Controller/LdapDropdownControllerTest.php @@ -130,6 +130,64 @@ public function testInvalidConditonParameter(): void ]); } + public function testSearchTextParameter(): void + { + // Arrange: create a valid form + $this->enableConfigurableItem(LdapQuestion::class); + $ldap = $this->setupAuthLdap(); + $form = $this->createFormWithLdapQuestion($ldap); + + // Act: execute route with searchText + $this->login('post-only'); + $response = $this->renderRoute([ + 'condition' => $this->buildAndGetConditionUuid($form), + 'page' => 1, + 'page_limit' => 10, + 'searchText' => 'pierre', + ]); + + // Assert: response should be successful + $this->assertEquals(200, $response->getStatusCode()); + + // Assert: response should be valid JSON + $content = $response->getContent(); + $this->assertNotFalse($content); + $data = json_decode($content, true); + $this->assertIsArray($data); + $this->assertArrayHasKey('results', $data); + $this->assertArrayHasKey('count', $data); + $this->assertEquals([['id' => 'pierre', 'text' => 'pierre']], $data['results']); + } + + public function testEmptySearchTextParameter(): void + { + // Arrange: create a valid form + $this->enableConfigurableItem(LdapQuestion::class); + $ldap = $this->setupAuthLdap(); + $form = $this->createFormWithLdapQuestion($ldap); + + // Act: execute route with empty searchText + $this->login('post-only'); + $response = $this->renderRoute([ + 'condition' => $this->buildAndGetConditionUuid($form), + 'page' => 1, + 'page_limit' => 10, + 'searchText' => '', + ]); + + // Assert: response should be successful + $this->assertEquals(200, $response->getStatusCode()); + + // Assert: response should be valid JSON + $content = $response->getContent(); + $this->assertNotFalse($content); + $data = json_decode($content, true); + $this->assertIsArray($data); + $this->assertArrayHasKey('results', $data); + $this->assertArrayHasKey('count', $data); + $this->assertCount(10, $data['results']); + } + private function renderRoute(array $post): Response { $controller = new LdapDropdownController();