From 55da29e496825adfb17750af963a6fa0a485547c Mon Sep 17 00:00:00 2001 From: Jonathan Lelievre Date: Thu, 21 Mar 2024 17:09:00 +0100 Subject: [PATCH] Add module GET endpoint and bulk PUT for status --- .../Resources/Module/BulkModules.php | 58 ++++++++ src/ApiPlatform/Resources/Module/Module.php | 57 ++++++++ .../ApiPlatform/ModuleEndpointTest.php | 135 ++++++++++++++++++ 3 files changed, 250 insertions(+) create mode 100644 src/ApiPlatform/Resources/Module/BulkModules.php create mode 100644 src/ApiPlatform/Resources/Module/Module.php create mode 100644 tests/Integration/ApiPlatform/ModuleEndpointTest.php diff --git a/src/ApiPlatform/Resources/Module/BulkModules.php b/src/ApiPlatform/Resources/Module/BulkModules.php new file mode 100644 index 0000000..3fbb706 --- /dev/null +++ b/src/ApiPlatform/Resources/Module/BulkModules.php @@ -0,0 +1,58 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ + +declare(strict_types=1); + +namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Module; + +use ApiPlatform\Metadata\ApiResource; +use PrestaShop\PrestaShop\Core\Domain\Module\Command\BulkToggleModuleStatusCommand; +use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate; + +#[ApiResource( + operations: [ + new CQRSUpdate( + uriTemplate: '/modules/toggle-status', + output: false, + CQRSCommand: BulkToggleModuleStatusCommand::class, + scopes: [ + 'module_write', + ], + CQRSCommandMapping: [ + '[enabled]' => '[expectedStatus]', + ], + ), + ], +)] +class BulkModules +{ + /** + * @var string[] + */ + public array $modules; + + public bool $enabled; +} diff --git a/src/ApiPlatform/Resources/Module/Module.php b/src/ApiPlatform/Resources/Module/Module.php new file mode 100644 index 0000000..14fec4f --- /dev/null +++ b/src/ApiPlatform/Resources/Module/Module.php @@ -0,0 +1,57 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ + +declare(strict_types=1); + +namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Module; + +use ApiPlatform\Metadata\ApiProperty; +use ApiPlatform\Metadata\ApiResource; +use PrestaShop\PrestaShop\Core\Domain\Module\Query\GetModuleInfos; +use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet; + +#[ApiResource( + operations: [ + new CQRSGet( + uriTemplate: '/module/{moduleId}', + CQRSQuery: GetModuleInfos::class, + scopes: [ + 'module_read', + ], + ), + ], +)] +class Module +{ + #[ApiProperty(identifier: true)] + public int $moduleId; + + public string $technicalName; + + public string $version; + + public bool $enabled; +} diff --git a/tests/Integration/ApiPlatform/ModuleEndpointTest.php b/tests/Integration/ApiPlatform/ModuleEndpointTest.php new file mode 100644 index 0000000..5994758 --- /dev/null +++ b/tests/Integration/ApiPlatform/ModuleEndpointTest.php @@ -0,0 +1,135 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ + +declare(strict_types=1); + +namespace PsApiResourcesTest\Integration\ApiPlatform; + +use Tests\Resources\DatabaseDump; + +class ModuleEndpointTest extends ApiTestCase +{ + public static function setUpBeforeClass(): void + { + parent::setUpBeforeClass(); + DatabaseDump::restoreTables(['module']); + self::createApiClient(['module_write', 'module_read']); + } + + public static function tearDownAfterClass(): void + { + parent::tearDownAfterClass(); + DatabaseDump::restoreTables(['module']); + } + + public function getProtectedEndpoints(): iterable + { + yield 'get endpoint' => [ + 'GET', + '/module/1', + ]; + + yield 'bulk toggle' => [ + 'PUT', + '/modules/toggle-status', + ]; + } + + public function testGetModuleInfos(): string + { + // Based on core fixtures and default data after install ps_languageselector should have the ID 6 + // This ID fetching can be improved when the listing ill be available + $moduleInfos = $this->getModuleInfos(6); + + // Returned data has modified fields, the others haven't changed + $this->assertEquals( + [ + 'moduleId' => 6, + 'technical_name' => 'ps_languageselector', + 'version' => '2.1.3', + 'enabled' => true, + ], + $moduleInfos + ); + + return $moduleInfos['technical_name']; + } + + /** + * @depends testGetModuleInfos + * + * @param string $technicalName + */ + public function testBulkUpdateStatus(string $technicalName): void + { + // Bulk disable on one module + $bearerToken = $this->getBearerToken(['module_read', 'module_write']); + static::createClient()->request('PUT', '/modules/toggle-status', [ + 'auth_bearer' => $bearerToken, + 'json' => [ + 'modules' => [ + $technicalName, + ], + 'enabled' => false, + ], + ]); + self::assertResponseStatusCodeSame(204); + + // Check updated disabled status + $moduleInfos = $this->getModuleInfos(6); + $this->assertFalse($moduleInfos['enabled']); + + // Bulk enable on one module + static::createClient()->request('PUT', '/modules/toggle-status', [ + 'auth_bearer' => $bearerToken, + 'json' => [ + 'modules' => [ + $technicalName, + ], + 'enabled' => true, + ], + ]); + self::assertResponseStatusCodeSame(204); + + // Check updated enabled status + $moduleInfos = $this->getModuleInfos(6); + $this->assertTrue($moduleInfos['enabled']); + } + + private function getModuleInfos(int $moduleId): array + { + $bearerToken = $this->getBearerToken(['module_read']); + $response = static::createClient()->request('GET', '/module/' . $moduleId, [ + 'auth_bearer' => $bearerToken, + ]); + self::assertResponseStatusCodeSame(200); + + $decodedResponse = json_decode($response->getContent(), true); + $this->assertNotFalse($decodedResponse); + + return $decodedResponse; + } +}