Skip to content

Commit

Permalink
Merge pull request #33 from jinpresta/reset-module
Browse files Browse the repository at this point in the history
Endpoint reset module
  • Loading branch information
jolelievre authored Oct 24, 2024
2 parents 76c0335 + 0b8206f commit 980488a
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
strategy:
matrix:
php: [ '8.1', '8.2', '8.3' ]
prestashop_version: ['develop', '9.0.x']
fail-fast: false
steps:
- name: Checkout module code
Expand All @@ -30,7 +31,10 @@ jobs:
fetch-depth: 0
repository: PrestaShop/PrestaShop
path: prestashop
ref: ${{ matrix.prestashop_version }}
- name: Build Docker
env:
VERSION: ${{ matrix.php }}-apache
run: |
cd prestashop
USER_ID=$(id -u) GROUP_ID=$(id -g) PS_INSTALL_AUTO=0 docker compose build --no-cache && docker compose up -d --force-recreate
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ concurrency:
jobs:
# Check there is no syntax errors in the project
php-linter:
name: PHP Syntax check 8.1
name: PHP Syntax check
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -15,6 +15,12 @@ jobs:
- name: PHP syntax checker 8.1
uses: prestashop/github-action-php-lint/8.1@master

- name: PHP syntax checker 8.2
uses: prestashop/github-action-php-lint/8.2@master

- name: PHP syntax checker 8.3
uses: prestashop/github-action-php-lint/8.3@master

# Check the PHP code follow the coding standards
php-cs-fixer:
name: PHP-CS-Fixer
Expand Down
2 changes: 2 additions & 0 deletions src/ApiPlatform/Resources/Module/BulkModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Module\Command\BulkToggleModuleStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Module\Exception\ModuleNotFoundException;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;

#[ApiResource(
Expand All @@ -40,6 +41,7 @@
],
),
],
exceptionToStatus: [ModuleNotFoundException::class => 404],
)]
class BulkModules
{
Expand Down
16 changes: 14 additions & 2 deletions src/ApiPlatform/Resources/Module/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Module;

use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Module\Command\ResetModuleCommand;
use PrestaShop\PrestaShop\Core\Domain\Module\Command\UpdateModuleStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Module\Exception\ModuleNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Module\Query\GetModuleInfos;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSPartialUpdate;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
use PrestaShopBundle\ApiPlatform\Metadata\PaginatedList;

Expand All @@ -39,13 +42,21 @@
],
),
new CQRSUpdate(
uriTemplate: '/module/status/{technicalName}',
uriTemplate: '/module/{technicalName}/status',
CQRSCommand: UpdateModuleStatusCommand::class,
CQRSQuery: GetModuleInfos::class,
scopes: [
'module_write',
],
),
new CQRSPartialUpdate(
uriTemplate: '/module/{technicalName}/reset',
CQRSCommand: ResetModuleCommand::class,
CQRSQuery: GetModuleInfos::class,
scopes: [
'module_write',
],
),
new PaginatedList(
uriTemplate: '/modules',
scopes: [
Expand All @@ -54,10 +65,11 @@
gridDataFactory: 'prestashop.core.grid.data_factory.module',
),
],
exceptionToStatus: [ModuleNotFoundException::class => 404],
)]
class Module
{
public int $moduleId;
public ?int $moduleId;

public string $technicalName;

Expand Down
93 changes: 87 additions & 6 deletions tests/Integration/ApiPlatform/ModuleEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class ModuleEndpointTest extends ApiTestCase
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
DatabaseDump::restoreTables(['module', 'module_shop']);
DatabaseDump::restoreMatchingTables('/module/');
self::createApiClient(['module_write', 'module_read']);
}

public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
DatabaseDump::restoreTables(['module', 'module_shop']);
DatabaseDump::restoreMatchingTables('/module/');
}

public function getProtectedEndpoints(): iterable
Expand All @@ -59,10 +59,43 @@ public function getProtectedEndpoints(): iterable

yield 'toggle module status' => [
'PUT',
'/module/status/{technicalName}',
'/module/{technicalName}/status',
];

yield 'reset module' => [
'PATCH',
'/module/{technicalName}/reset',
];
}

public function testModuleNotFound(): void
{
$bearerToken = $this->getBearerToken(['module_read', 'module_write']);
// GET on non existent module returns a 404
static::createClient()->request('GET', '/module/ps_falsemodule', [
'auth_bearer' => $bearerToken,
]);
self::assertResponseStatusCodeSame(404);

// PUT status on non existent module returns a 404
static::createClient()->request('PUT', '/module/ps_falsemodule/status', [
'auth_bearer' => $bearerToken,
'json' => [
'enabled' => true,
],
]);
self::assertResponseStatusCodeSame(404);

// PATCH reset on non existent module returns a 404
static::createClient()->request('PATCH', '/module/ps_falsemodule/reset', [
'auth_bearer' => $bearerToken,
'json' => [
'keepData' => true,
],
]);
self::assertResponseStatusCodeSame(404);
}

public function testListModules(): array
{
$modules = $this->listItems('/modules', ['module_read']);
Expand Down Expand Up @@ -155,15 +188,15 @@ public function testBulkUpdateStatus(array $module): array
/**
* @depends testBulkUpdateStatus
*/
public function testUpdateModuleStatus(array $module): void
public function testUpdateModuleStatusDisable(array $module): array
{
// 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']), [
$response = static::createClient()->request('PUT', sprintf('/module/%s/status', $module['technicalName']), [
'auth_bearer' => $bearerToken,
'json' => [
'enabled' => false,
Expand Down Expand Up @@ -193,7 +226,7 @@ public function testUpdateModuleStatus(array $module): void

// Enable specific module
$bearerToken = $this->getBearerToken(['module_read', 'module_write']);
$response = static::createClient()->request('PUT', sprintf('/module/status/%s', $module['technicalName']), [
$response = static::createClient()->request('PUT', sprintf('/module/%s/status', $module['technicalName']), [
'auth_bearer' => $bearerToken,
'json' => [
'enabled' => true,
Expand All @@ -218,6 +251,54 @@ public function testUpdateModuleStatus(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 testResetModule(array $module): void
{
// Reset specific module
$bearerToken = $this->getBearerToken(['module_read', 'module_write']);
$response = static::createClient()->request('PATCH', sprintf('/module/%s/reset', $module['technicalName']), [
'auth_bearer' => $bearerToken,
'json' => [
'keepData' => false,
],
]);
self::assertResponseStatusCodeSame(200);
$decodedResponse = json_decode($response->getContent(), true);
$this->assertNotFalse($decodedResponse);

// Module ID has been modified because the module was uninstalled the reinstalled
$this->assertNotEquals($module['moduleId'], $decodedResponse['moduleId']);
$moduleInfos = $this->getModuleInfos($module['technicalName']);
$this->assertNotEquals($module['moduleId'], $moduleInfos['moduleId']);
$module['moduleId'] = $decodedResponse['moduleId'];

// Check response from status update request
$expectedModuleInfos = [
'moduleId' => $module['moduleId'],
'technicalName' => $module['technicalName'],
'version' => $module['version'],
'enabled' => true,
'installed' => true,
];
$this->assertEquals($expectedModuleInfos, $decodedResponse);
}

/**
* @depends testUpdateModuleStatusDisable
*/
public function restResetModuleNotActive(array $module): void
{
$bearerToken = $this->getBearerToken(['module_read', 'module_write']);
static::createClient()->request('PATCH', sprintf('/module/%s/reset', $module['technicalName']), [
'auth_bearer' => $bearerToken,
]);
self::assertResponseStatusCodeSame(400);
}

private function getModuleInfos(string $technicalName): array
Expand Down

0 comments on commit 980488a

Please sign in to comment.