From 5f9091dfda268c0b5250f4957196ddb05b7acb6f Mon Sep 17 00:00:00 2001 From: Jonathan LELIEVRE Date: Thu, 3 Oct 2024 14:58:32 +0200 Subject: [PATCH] Add endpoint to toggle status of individual module, add related integration tests --- src/ApiPlatform/Resources/Module/Module.php | 11 ++- .../ApiPlatform/ModuleEndpointTest.php | 91 ++++++++++++++++--- 2 files changed, 89 insertions(+), 13 deletions(-) diff --git a/src/ApiPlatform/Resources/Module/Module.php b/src/ApiPlatform/Resources/Module/Module.php index 6de5e77..f43f405 100644 --- a/src/ApiPlatform/Resources/Module/Module.php +++ b/src/ApiPlatform/Resources/Module/Module.php @@ -22,10 +22,11 @@ namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Module; -use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\ApiResource; +use PrestaShop\PrestaShop\Core\Domain\Module\Command\UpdateModuleStatusCommand; use PrestaShop\PrestaShop\Core\Domain\Module\Query\GetModuleInfos; use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet; +use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate; use PrestaShopBundle\ApiPlatform\Metadata\PaginatedList; #[ApiResource( @@ -37,6 +38,14 @@ 'module_read', ], ), + new CQRSUpdate( + uriTemplate: '/module/status/{technicalName}', + CQRSCommand: UpdateModuleStatusCommand::class, + CQRSQuery: GetModuleInfos::class, + scopes: [ + 'module_write', + ], + ), new PaginatedList( uriTemplate: '/modules', scopes: [ diff --git a/tests/Integration/ApiPlatform/ModuleEndpointTest.php b/tests/Integration/ApiPlatform/ModuleEndpointTest.php index d2ecafa..7e630ff 100644 --- a/tests/Integration/ApiPlatform/ModuleEndpointTest.php +++ b/tests/Integration/ApiPlatform/ModuleEndpointTest.php @@ -52,10 +52,15 @@ public function getProtectedEndpoints(): iterable '/modules', ]; - yield 'bulk toggle' => [ + yield 'bulk toggle status' => [ 'PUT', '/modules/toggle-status', ]; + + yield 'toggle module status' => [ + 'PUT', + '/module/status/{technicalName}', + ]; } public function testListModules(): array @@ -71,7 +76,7 @@ public function testListModules(): array $this->assertTrue(version_compare($apiModule['version'], '0.1.0', '>=')); $this->assertGreaterThan(0, $apiModule['moduleId']); - return ['moduleId' => $apiModule['moduleId'], 'technicalName' => $apiModule['technicalName']]; + return ['moduleId' => $apiModule['moduleId'], 'technicalName' => $apiModule['technicalName'], 'version' => $apiModule['version']]; } /** @@ -80,15 +85,11 @@ public function testListModules(): array public function testGetModuleInfos(array $module): array { $moduleInfos = $this->getModuleInfos($module['technicalName']); - - // Returned data has modified fields, the others haven't changed - $this->assertArrayHasKey('version', $moduleInfos); - $version = $moduleInfos['version']; $this->assertEquals( [ 'moduleId' => $module['moduleId'], 'technicalName' => $module['technicalName'], - 'version' => $version, + 'version' => $module['version'], 'enabled' => true, 'installed' => true, ], @@ -101,7 +102,7 @@ public function testGetModuleInfos(array $module): array /** * @depends testGetModuleInfos */ - public function testBulkUpdateStatus(array $module): void + public function testBulkUpdateStatus(array $module): array { // Check number of disabled modules $disabledModules = $this->listItems('/modules', ['module_read'], ['enabled' => false]); @@ -119,8 +120,6 @@ public function testBulkUpdateStatus(array $module): void ], ]); self::assertResponseStatusCodeSame(204); - // Active status is cached so we must clear it before calling the single endpoint - \Module::resetStaticCache(); // Check updated disabled status $moduleInfos = $this->getModuleInfos($module['technicalName']); @@ -141,8 +140,6 @@ public function testBulkUpdateStatus(array $module): void ], ]); self::assertResponseStatusCodeSame(204); - // Active status is cached so we must clear it before calling the single endpoint - \Module::resetStaticCache(); // Check updated enabled status $moduleInfos = $this->getModuleInfos($module['technicalName']); @@ -151,6 +148,76 @@ public function testBulkUpdateStatus(array $module): void // Check number of disabled modules $disabledModules = $this->listItems('/modules', ['module_read'], ['enabled' => false]); $this->assertEquals(0, $disabledModules['totalItems']); + + return $module; + } + + /** + * @depends testBulkUpdateStatus + */ + public function testUpdateModuleStatus(array $module): void + { + // Check number of disabled modules + $disabledModules = $this->listItems('/modules', ['module_read'], ['enabled' => false]); + $this->assertEquals(0, $disabledModules['totalItems']); + + // Disable specific module + $bearerToken = $this->getBearerToken(['module_read', 'module_write']); + $response = static::createClient()->request('PUT', sprintf('/module/status/%s', $module['technicalName']), [ + 'auth_bearer' => $bearerToken, + 'json' => [ + 'enabled' => false, + ], + ]); + self::assertResponseStatusCodeSame(200); + $decodedResponse = json_decode($response->getContent(), true); + $this->assertNotFalse($decodedResponse); + + // Check response from status update request + $expectedModuleInfos = [ + 'moduleId' => $module['moduleId'], + 'technicalName' => $module['technicalName'], + 'version' => $module['version'], + 'enabled' => false, + 'installed' => true, + ]; + $this->assertEquals($expectedModuleInfos, $decodedResponse); + + // Check updated disabled status + $moduleInfos = $this->getModuleInfos($module['technicalName']); + $this->assertEquals($expectedModuleInfos, $moduleInfos); + + // Check number of disabled modules + $disabledModules = $this->listItems('/modules', ['module_read'], ['enabled' => false]); + $this->assertEquals(1, $disabledModules['totalItems']); + + // Enable specific module + $bearerToken = $this->getBearerToken(['module_read', 'module_write']); + $response = static::createClient()->request('PUT', sprintf('/module/status/%s', $module['technicalName']), [ + 'auth_bearer' => $bearerToken, + 'json' => [ + 'enabled' => true, + ], + ]); + self::assertResponseStatusCodeSame(200); + $decodedResponse = json_decode($response->getContent(), true); + $this->assertNotFalse($decodedResponse); + + // Check response from status update request + $expectedModuleInfos['enabled'] = true; + $this->assertEquals($expectedModuleInfos, $decodedResponse); + + // Check updated enabled status + $moduleInfos = $this->getModuleInfos($module['technicalName']); + $this->assertTrue($moduleInfos['enabled']); + + // Check updated enabled status + $moduleInfos = $this->getModuleInfos($module['technicalName']); + $this->assertEquals($expectedModuleInfos, $moduleInfos); + + // Check number of disabled modules + $disabledModules = $this->listItems('/modules', ['module_read'], ['enabled' => false]); + $this->assertEquals(0, $disabledModules['totalItems']); } private function getModuleInfos(string $technicalName): array