From 51a1017e3899f9335dbfd8e29b05086eef10f929 Mon Sep 17 00:00:00 2001 From: Paul Mitchum Date: Fri, 2 Aug 2024 14:49:46 -0700 Subject: [PATCH 1/3] moved DkanDocsTest to OpenApiControllerTest, convert to BTB --- .../src/Controller/OpenApiController.php | 2 - .../OpenApiControllerTest.php} | 54 ++++++++++++------- 2 files changed, 35 insertions(+), 21 deletions(-) rename modules/common/tests/src/Functional/{DkanDocsTest.php => Controller/OpenApiControllerTest.php} (68%) diff --git a/modules/common/src/Controller/OpenApiController.php b/modules/common/src/Controller/OpenApiController.php index d0ce5f4241..bfd38cc63d 100644 --- a/modules/common/src/Controller/OpenApiController.php +++ b/modules/common/src/Controller/OpenApiController.php @@ -15,8 +15,6 @@ /** * Serves openapi spec for dataset-related endpoints. - * - * @codeCoverageIgnore */ class OpenApiController implements ContainerInjectionInterface { use JsonResponseTrait; diff --git a/modules/common/tests/src/Functional/DkanDocsTest.php b/modules/common/tests/src/Functional/Controller/OpenApiControllerTest.php similarity index 68% rename from modules/common/tests/src/Functional/DkanDocsTest.php rename to modules/common/tests/src/Functional/Controller/OpenApiControllerTest.php index 5b475e8cbb..c2c05fd2b9 100644 --- a/modules/common/tests/src/Functional/DkanDocsTest.php +++ b/modules/common/tests/src/Functional/Controller/OpenApiControllerTest.php @@ -1,12 +1,29 @@ getController(); @@ -23,15 +40,14 @@ public function testGetCompleteJson() { $data = json_decode($response->getContent(), TRUE); // Basic auth is included. - $this->assertTrue(isset($data["components"]["securitySchemes"]["basic_auth"])); + $this->assertTrue(isset($data['components']['securitySchemes']['basic_auth'])); // Some sample paths from different modules are there - $this->assertArrayHasKey('/api/1/datastore/imports', $data["paths"]); - $this->assertArrayHasKey('/api/1/harvest/plans/{plan_id}', $data["paths"]); - $this->assertArrayHasKey('/api/1/metastore/schemas/{schema_id}', $data["paths"]); + $this->assertArrayHasKey('/api/1/datastore/imports', $data['paths']); + $this->assertArrayHasKey('/api/1/harvest/plans/{plan_id}', $data['paths']); + $this->assertArrayHasKey('/api/1/metastore/schemas/{schema_id}', $data['paths']); } - public function testGetCompleteYaml() { $controller = $this->getController(); $response = $controller->getComplete('yaml'); @@ -39,16 +55,16 @@ public function testGetCompleteYaml() { $data = Yaml::decode($response->getContent(), TRUE); // Basic auth is included. - $this->assertTrue(isset($data["components"]["securitySchemes"]["basic_auth"])); + $this->assertTrue(isset($data['components']['securitySchemes']['basic_auth'])); // Some sample paths from different modules are there - $this->assertArrayHasKey('/api/1/datastore/imports', $data["paths"]); - $this->assertArrayHasKey('/api/1/harvest/plans/{plan_id}', $data["paths"]); - $this->assertArrayHasKey('/api/1/metastore/schemas/{schema_id}', $data["paths"]); + $this->assertArrayHasKey('/api/1/datastore/imports', $data['paths']); + $this->assertArrayHasKey('/api/1/harvest/plans/{plan_id}', $data['paths']); + $this->assertArrayHasKey('/api/1/metastore/schemas/{schema_id}', $data['paths']); } public function testGetNoAuth() { - $controller = $this->getController(["authentication" => "false"]); + $controller = $this->getController(['authentication' => 'false']); $response = $controller->getComplete(); // Simulate an authentication=false parameter in the request stack. @@ -56,18 +72,18 @@ public function testGetNoAuth() { $data = json_decode($response->getContent(), TRUE); // Basic auth is excluded. - $this->assertFalse(isset($data["components"]["securitySchemes"]["basic_auth"])); + $this->assertFalse(isset($data['components']['securitySchemes']['basic_auth'])); // Authorized paths are not there. - $this->assertArrayNotHasKey('/api/1/datastore/imports', $data["paths"]); - $this->assertArrayNotHasKey('/api/1/harvest/plans/{plan_id}', $data["paths"]); + $this->assertArrayNotHasKey('/api/1/datastore/imports', $data['paths']); + $this->assertArrayNotHasKey('/api/1/harvest/plans/{plan_id}', $data['paths']); // Public paths still there. - $this->assertArrayHasKey('/api/1/metastore/schemas/{schema_id}', $data["paths"]); + $this->assertArrayHasKey('/api/1/metastore/schemas/{schema_id}', $data['paths']); } private function getController(array $params = []) { $requestStack = \Drupal::service('request_stack'); - + if (!empty($params)) { $request = $requestStack->pop()->duplicate($params); $requestStack->push($request); From bd3d32ac935299e1ad0b1bc22955fe249ea9995b Mon Sep 17 00:00:00 2001 From: Paul Mitchum Date: Fri, 2 Aug 2024 15:19:59 -0700 Subject: [PATCH 2/3] use OpenApiController::create for full coverage --- .../src/Functional/Controller/OpenApiControllerTest.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/modules/common/tests/src/Functional/Controller/OpenApiControllerTest.php b/modules/common/tests/src/Functional/Controller/OpenApiControllerTest.php index c2c05fd2b9..d44a8aaa08 100644 --- a/modules/common/tests/src/Functional/Controller/OpenApiControllerTest.php +++ b/modules/common/tests/src/Functional/Controller/OpenApiControllerTest.php @@ -82,17 +82,15 @@ public function testGetNoAuth() { } private function getController(array $params = []) { - $requestStack = \Drupal::service('request_stack'); + /** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack */ + $requestStack = $this->container->get('request_stack'); if (!empty($params)) { $request = $requestStack->pop()->duplicate($params); $requestStack->push($request); } - return new OpenApiController( - $requestStack, - \Drupal::service('dkan.common.docs_generator') - ); + return OpenApiController::create($this->container); } } From 1aba3c3b4ddf2dcc28af1ae8ab4312ae04eafb5a Mon Sep 17 00:00:00 2001 From: Paul Mitchum Date: Fri, 2 Aug 2024 15:30:52 -0700 Subject: [PATCH 3/3] minor refactor --- .../src/Functional/Controller/OpenApiControllerTest.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/common/tests/src/Functional/Controller/OpenApiControllerTest.php b/modules/common/tests/src/Functional/Controller/OpenApiControllerTest.php index d44a8aaa08..74a7847d43 100644 --- a/modules/common/tests/src/Functional/Controller/OpenApiControllerTest.php +++ b/modules/common/tests/src/Functional/Controller/OpenApiControllerTest.php @@ -82,10 +82,9 @@ public function testGetNoAuth() { } private function getController(array $params = []) { - /** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack */ - $requestStack = $this->container->get('request_stack'); - - if (!empty($params)) { + if ($params) { + /** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack */ + $requestStack = $this->container->get('request_stack'); $request = $requestStack->pop()->duplicate($params); $requestStack->push($request); }