Skip to content

Commit

Permalink
Add endpoint to toggle status of individual module, add related integ…
Browse files Browse the repository at this point in the history
…ration tests
  • Loading branch information
jolelievre committed Oct 3, 2024
1 parent bcd6ed4 commit 5f9091d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 13 deletions.
11 changes: 10 additions & 1 deletion src/ApiPlatform/Resources/Module/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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: [
Expand Down
91 changes: 79 additions & 12 deletions tests/Integration/ApiPlatform/ModuleEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']];
}

/**
Expand All @@ -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,
],
Expand All @@ -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]);
Expand All @@ -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']);
Expand All @@ -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']);
Expand All @@ -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
Expand Down

0 comments on commit 5f9091d

Please sign in to comment.