Skip to content

Commit

Permalink
Add module GET endpoint and bulk PUT for status
Browse files Browse the repository at this point in the history
  • Loading branch information
jolelievre committed Mar 25, 2024
1 parent 1639d20 commit 55da29e
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/ApiPlatform/Resources/Module/BulkModules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @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;
}
57 changes: 57 additions & 0 deletions src/ApiPlatform/Resources/Module/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @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;
}
135 changes: 135 additions & 0 deletions tests/Integration/ApiPlatform/ModuleEndpointTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @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;
}
}

0 comments on commit 55da29e

Please sign in to comment.