diff --git a/config/optional/rest.resource.helfi_debug_package_version.yml b/config/optional/rest.resource.helfi_debug_package_version.yml deleted file mode 100644 index 7762b686..00000000 --- a/config/optional/rest.resource.helfi_debug_package_version.yml +++ /dev/null @@ -1,16 +0,0 @@ -langcode: en -status: true -dependencies: - module: - - user -id: helfi_debug_package_version -plugin_id: 'helfi_debug_package_version' -granularity: resource -configuration: - methods: - - GET - formats: - - json - authentication: - - cookie - - basic_auth diff --git a/documentation/debug.md b/documentation/debug.md index 943a1a76..78503c83 100644 --- a/documentation/debug.md +++ b/documentation/debug.md @@ -13,14 +13,3 @@ See [src/Plugin/DebugDataItem/Composer.php](/src/Plugin/DebugDataItem/Composer.p At minimum, you need: - A plugin class that implements `\Drupal\helfi_debug\DebugDataItemInterface` - A plugin specific template (`debug-item--{plugin_id}.html.twig`). See [templates/debug-item.html.twig](/templates/debug-item.html.twig) for more information. - -## Package version checker - -The `/api/v1/version` endpoint can be used to fetch the latest version of a composer package. - -For example: - -Request `GET /api/v1/version?package=drupal/helfi_api_base&version=1.2.0` will respond with: - -- The latest version number -- Whether the given version is the latest version diff --git a/helfi_api_base.install b/helfi_api_base.install index 039871db..c4105a06 100644 --- a/helfi_api_base.install +++ b/helfi_api_base.install @@ -338,3 +338,13 @@ function _helfi_api_base_process_links(ContentEntityInterface $entity, string $f $entity->save(); } } + +/** + * UHF-10713 Remove VersionChecker. + */ +function helfi_api_base_update_9023(): void { + \Drupal::entityTypeManager() + ->getStorage('rest_resource_config') + ->load('helfi_debug_package_version') + ->delete(); +} diff --git a/helfi_api_base.permissions.yml b/helfi_api_base.permissions.yml index aea19c88..4076c7ff 100644 --- a/helfi_api_base.permissions.yml +++ b/helfi_api_base.permissions.yml @@ -11,3 +11,5 @@ view remote entities: title: View remote entities edit remote entities: title: Edit remote entities +access debug page: + title: Access debug page diff --git a/helfi_api_base.routing.yml b/helfi_api_base.routing.yml index 3dc54f8f..aef22451 100644 --- a/helfi_api_base.routing.yml +++ b/helfi_api_base.routing.yml @@ -20,4 +20,4 @@ helfi_api_base.debug_list: _title: 'Debug' _controller: '\Drupal\helfi_api_base\Controller\DebugController::build' requirements: - _permission: 'restful get helfi_debug_package_version' + _permission: 'access debug page' diff --git a/helfi_api_base.services.yml b/helfi_api_base.services.yml index 3842e577..c62e535e 100644 --- a/helfi_api_base.services.yml +++ b/helfi_api_base.services.yml @@ -70,20 +70,6 @@ services: class: Drupal\helfi_api_base\DebugDataItemPluginManager parent: default_plugin_manager - Drupal\helfi_api_base\Package\VersionChecker: '@helfi_api_base.package_version_checker' - helfi_api_base.package_version_checker: - class: Drupal\helfi_api_base\Package\VersionChecker - arguments: [] - tags: - - { name: service_collector, call: add, tag: helfi_api_base.version_checker } - - Drupal\helfi_api_base\Package\HelfiPackage: '@helfi_api_base.helfi_package_version_checker' - helfi_api_base.helfi_package_version_checker: - class: Drupal\helfi_api_base\Package\HelfiPackage - arguments: ['@http_client'] - tags: - - { name: helfi_api_base.version_checker } - Drupal\helfi_api_base\EventSubscriber\EnvironmentResponseSubscriber: '@helfi_api_base.environment_response_subscriber' helfi_api_base.environment_response_subscriber: class: Drupal\helfi_api_base\EventSubscriber\EnvironmentResponseSubscriber diff --git a/src/Exception/InvalidPackageException.php b/src/Exception/InvalidPackageException.php deleted file mode 100644 index 8a496930..00000000 --- a/src/Exception/InvalidPackageException.php +++ /dev/null @@ -1,13 +0,0 @@ -client - ->request('GET', sprintf(self::BASE_URL, $packagePath)); - $content = json_decode($response->getBody()->getContents(), TRUE); - } - catch (GuzzleException $e) { - return []; - } - if (!isset($content['packages'][$packageName])) { - throw new InvalidPackageException('Package not found.'); - } - return $content['packages'][$packageName]; - } - - /** - * {@inheritdoc} - */ - public function get(string $packageName, string $version): ? Version { - // Attempt to get package data and fallback to dev version if - // no stable version is found. - if (!$packages = $this->getPackageData($packageName)) { - $packages = $this->getPackageData($packageName, TRUE); - } - usort($packages, function ($package1, $package2) { - return version_compare($package1['version'], $package2['version']); - }); - // Packages are sorted from oldest to newest. - $latest = end($packages); - - if (empty($latest['version']) || !is_string($latest['version'])) { - throw new InvalidPackageException('No version data found.'); - } - return new Version($packageName, $latest['version'], version_compare($version, $latest['version'], '>=')); - } - -} diff --git a/src/Package/Version.php b/src/Package/Version.php deleted file mode 100644 index 665b7690..00000000 --- a/src/Package/Version.php +++ /dev/null @@ -1,43 +0,0 @@ - $this->name, - 'latestVersion' => $this->latestVersion, - 'isLatest' => $this->isLatest, - ]; - } - -} diff --git a/src/Package/VersionChecker.php b/src/Package/VersionChecker.php deleted file mode 100644 index 4703becd..00000000 --- a/src/Package/VersionChecker.php +++ /dev/null @@ -1,56 +0,0 @@ -collectors[] = $versionChecker; - return $this; - } - - /** - * Gets the package version. - * - * @param string $packageName - * The package name. - * @param string $version - * The version. - * - * @return \Drupal\helfi_api_base\Package\Version|null - * The version object or null. - * - * @throws \Drupal\helfi_api_base\Exception\InvalidPackageException - */ - public function get(string $packageName, string $version) : ? Version { - foreach ($this->collectors as $collector) { - if (!$collector->applies($packageName)) { - continue; - } - return $collector->get($packageName, $version); - } - return NULL; - } - -} diff --git a/src/Package/VersionCheckerInterface.php b/src/Package/VersionCheckerInterface.php deleted file mode 100644 index 64a8d119..00000000 --- a/src/Package/VersionCheckerInterface.php +++ /dev/null @@ -1,38 +0,0 @@ -packageVersion = $container->get('helfi_api_base.package_version_checker'); - return $instance; - } +final class PackageVersion extends ResourceBase { /** * Responds to GET requests. * - * @return \Drupal\rest\ResourceResponse - * The response containing the record. - * * @throws \Symfony\Component\HttpKernel\Exception\HttpException */ - public function get(Request $request) : ResourceResponse { - $args = []; - foreach (['name', 'version'] as $arg) { - if (!$value = $request->query->get($arg)) { - throw new BadRequestHttpException(sprintf('Missing required query argument: %s', $arg)); - } - if (is_array($value)) { - $value = reset($value); - } - $args[$arg] = (string) $value; - } - ['name' => $name, 'version' => $version] = $args; - - $data = $this->packageVersion->get($name, $version); - - if (!$data instanceof Version) { - throw new BadRequestHttpException(sprintf('Invalid package name: %s', $name)); - } - - $cacheableMetadata = new CacheableMetadata(); - $cacheableMetadata->addCacheContexts([ - 'url.query_args:name', - 'url.query_args:version', - ]) - ->setCacheMaxAge(180); - - return (new ResourceResponse($data->toArray())) - ->addCacheableDependency($cacheableMetadata); - } - - /** - * {@inheritdoc} - */ - public function calculateDependencies() : array { - return [ - 'module' => ['user'], - ]; + public function get() : ResourceResponse { + throw new BadRequestHttpException(sprintf('Deprecated')); } } diff --git a/tests/src/Functional/DebugUiTest.php b/tests/src/Functional/DebugUiTest.php index 86888d1d..f1588201 100644 --- a/tests/src/Functional/DebugUiTest.php +++ b/tests/src/Functional/DebugUiTest.php @@ -32,7 +32,7 @@ public function testDebugAdminUi() : void { $this->drupalGet('/admin/debug'); $this->assertSession()->statusCodeEquals(403); - $account = $this->createUser(['restful get helfi_debug_package_version']); + $account = $this->createUser(['access debug page']); $this->drupalLogin($account); $this->drupalGet('/admin/debug'); diff --git a/tests/src/Kernel/Controller/DebugControllerTest.php b/tests/src/Kernel/Controller/DebugControllerTest.php index 5dc4395d..c8f1c0a0 100644 --- a/tests/src/Kernel/Controller/DebugControllerTest.php +++ b/tests/src/Kernel/Controller/DebugControllerTest.php @@ -37,7 +37,7 @@ public function testControllerPermission() : void { $this->assertEquals(Response::HTTP_FORBIDDEN, $response->getStatusCode()); - $this->drupalSetUpCurrentUser(permissions: ['restful get helfi_debug_package_version']); + $this->drupalSetUpCurrentUser(permissions: ['access debug page']); $request = $this->getMockedRequest('/admin/debug'); $response = $this->processRequest($request); diff --git a/tests/src/Kernel/Plugin/rest/resource/PackageVersionTest.php b/tests/src/Kernel/Plugin/rest/resource/PackageVersionTest.php deleted file mode 100644 index e67d32bc..00000000 --- a/tests/src/Kernel/Plugin/rest/resource/PackageVersionTest.php +++ /dev/null @@ -1,155 +0,0 @@ -createUser(permissions: ['restful get helfi_debug_package_version']); - $this->drupalSetCurrentUser($user); - } - - /** - * Tests request without permission. - */ - public function testAccessDenied() : void { - $this->drupalSetUpCurrentUser(); - - $request = $this->getMockedRequest('/api/v1/package'); - $response = $this->processRequest($request); - - $this->assertEquals(HttpResponse::HTTP_FORBIDDEN, $response->getStatusCode()); - } - - /** - * Tests required query parameters. - * - * @dataProvider getDataRequiredParameters - */ - public function testGetRequiredParameters(array $parameters, string $expectedErrorMessage) : void { - $this->setupUser(); - $request = $this->getMockedRequest('/api/v1/package', parameters: $parameters); - $response = $this->processRequest($request); - - $this->assertEquals(HttpResponse::HTTP_BAD_REQUEST, $response->getStatusCode()); - $this->assertEquals($expectedErrorMessage, json_decode($response->getContent(), TRUE)['message']); - } - - /** - * A data provider. - * - * @return array[] - * The data. - */ - public function getDataRequiredParameters() : array { - return [ - [ - [], 'Missing required query argument: name', - ], - [ - ['name' => 'drupal/helfi_api_base'], 'Missing required query argument: version', - ], - ]; - } - - /** - * Tests invalid package name. - */ - public function testInvalidPackageName() : void { - $this->setupUser(); - $request = $this->getMockedRequest('/api/v1/package', parameters: [ - 'name' => 'drupal/invalid', - 'version' => '1.0.0', - ]); - $response = $this->processRequest($request); - - $this->assertEquals(HttpResponse::HTTP_BAD_REQUEST, $response->getStatusCode()); - $this->assertEquals('Invalid package name: drupal/invalid', json_decode($response->getContent(), TRUE)['message']); - } - - /** - * Tests requests that lead to InvalidPackageException error. - */ - public function testInvalidPackageException() : void { - \Drupal::service('kernel')->rebuildContainer(); - - $client = $this->createMockHttpClient([ - new RequestException('message', new Request('GET', 'test')), - new RequestException('message', new Request('GET', 'test')), - ]); - $this->container->set('helfi_api_base.helfi_package_version_checker', new HelfiPackage($client)); - - $request = $this->getMockedRequest('/api/v1/package', parameters: [ - 'name' => 'drupal/helfi_api_base', - 'version' => '1.0.0', - ]); - - $this->setupUser(); - $response = $this->processRequest($request); - - $this->assertEquals(HttpResponse::HTTP_BAD_REQUEST, $response->getStatusCode()); - $this->assertEquals('No version data found.', json_decode($response->getContent(), TRUE)['message']); - } - - /** - * Tests get request and cache contexts. - */ - public function testGet() : void { - \Drupal::service('kernel')->rebuildContainer(); - $client = $this->createMockHttpClient([ - new Response(body: json_encode([ - 'packages' => [ - 'drupal/helfi_api_base' => [ - [ - 'version' => '1.2.0', - ], - ], - ], - ])), - ]); - $this->container->set('helfi_api_base.helfi_package_version_checker', new HelfiPackage($client)); - - $request = $this->getMockedRequest('/api/v1/package', parameters: [ - 'name' => 'drupal/helfi_api_base', - 'version' => '1.0.0', - ]); - $this->setupUser(); - $response = $this->processRequest($request); - - $this->assertInstanceOf(CacheableResponse::class, $response); - // Make sure our response is not sent to actual API. - $this->assertEquals('1.2.0', json_decode($response->getContent(), TRUE)['latestVersion']); - $this->assertContains('url.query_args:name', $response->getCacheableMetadata()->getCacheContexts()); - $this->assertContains('url.query_args:version', $response->getCacheableMetadata()->getCacheContexts()); - $this->assertEquals(180, $response->getCacheableMetadata()->getCacheMaxAge()); - } - -} diff --git a/tests/src/Unit/Package/HelfiPackageTest.php b/tests/src/Unit/Package/HelfiPackageTest.php deleted file mode 100644 index e6f712ad..00000000 --- a/tests/src/Unit/Package/HelfiPackageTest.php +++ /dev/null @@ -1,238 +0,0 @@ -prophesize(ClientInterface::class)->reveal()); - $this->assertEquals($expected, $sut->applies($package)); - } - - /** - * Data provider for applies() test. - * - * @return array[] - * The data. - */ - public function appliesData() : array { - return [ - ['drupal/helfi_api_base', TRUE], - ['drupal/hdbt', TRUE], - ['drupal/hdbt_admin', TRUE], - ['city-of-helsinki/hauki', FALSE], - ]; - } - - /** - * Tests empty version. - * - * @covers ::get - * @covers ::__construct - * @covers ::getPackageData - * @dataProvider emptyVersionData - */ - public function testEmptyVersion($version) : void { - $client = $this->createMockHttpClient([ - new Response(body: json_encode([ - 'packages' => [ - 'drupal/helfi_api_base' => [ - [ - 'version' => $version, - ], - ], - ], - ])), - ]); - $sut = new HelfiPackage($client); - $this->expectException(InvalidPackageException::class); - $this->expectExceptionMessage('No version data found.'); - $sut->get('drupal/helfi_api_base', '1.2.0'); - } - - /** - * Data provider for empty version check. - * - * @return array - * The data. - */ - public function emptyVersionData() : array { - return [ - [NULL], - [1], - [''], - ]; - } - - /** - * Tests that client error returns null. - * - * @covers ::get - * @covers ::__construct - * @covers ::getPackageData - */ - public function testException() : void { - // First we try to fetch stable version, then fallback to dev. - $client = $this->createMockHttpClient([ - new RequestException('Stable package', new Request('GET', 'test')), - new RequestException('Dev package', new Request('GET', 'test')), - ]); - $sut = new HelfiPackage($client); - $this->expectException(InvalidPackageException::class); - $this->expectExceptionMessage('No version data found.'); - $sut->get('drupal/helfi_api_base', '1.2.0'); - } - - /** - * Tests empty packages. - * - * @covers ::get - * @covers ::__construct - * @covers ::getPackageData - */ - public function testEmptyPackage() : void { - $client = $this->createMockHttpClient([ - new Response(body: json_encode([ - 'packages' => [], - ])), - ]); - $sut = new HelfiPackage($client); - $this->expectException(InvalidPackageException::class); - $this->expectExceptionMessage('Package not found.'); - $sut->get('drupal/helfi_api_base', '1.2.0'); - } - - /** - * Tests get(). - * - * @covers ::__construct - * @covers ::get - * @covers \Drupal\helfi_api_base\Package\Version - * @covers ::getPackageData - * - * @dataProvider getData - */ - public function testGet( - string $packageName, - array $packageVersions, - string $packageVersion, - string $expectedLatestVersion, - bool $isLatest, - ) : void { - $client = $this->createMockHttpClient([ - new Response(body: json_encode([ - 'packages' => [ - $packageName => $packageVersions, - ], - ])), - ]); - $sut = new HelfiPackage($client); - $result = $sut->get($packageName, $packageVersion); - $this->assertInstanceOf(Version::class, $result); - $this->assertEquals($packageName, $result->name); - $this->assertEquals($expectedLatestVersion, $result->latestVersion); - $this->assertEquals($isLatest, $result->isLatest); - $this->assertNotEmpty($result->toArray()); - } - - /** - * Data provider for get(). - * - * @return array[] - * The data. - */ - public function getData() : array { - return [ - // Test with same version. - [ - 'drupal/helfi_api_base', - [ - [ - 'version' => '1.2.0', - ], - [ - 'version' => '1.3.0', - ], - ], - '1.3.0', - '1.3.0', - TRUE, - ], - // Test with dev-main. - [ - 'drupal/helfi_api_base', - [ - [ - 'version' => '1.2.0', - ], - [ - 'version' => '1.3.0', - ], - ], - 'dev-main', - '1.3.0', - FALSE, - ], - // Test with future release. - [ - 'drupal/helfi_api_base', - [ - [ - 'version' => '1.2.0', - ], - [ - 'version' => '1.3.0', - ], - ], - '1.4.0', - '1.3.0', - TRUE, - ], - // Test older release. - [ - 'drupal/hdbt', - [ - [ - 'version' => '1.2.0', - ], - [ - 'version' => '1.3.0', - ], - ], - '1.2.0', - '1.3.0', - FALSE, - ], - ]; - } - -}