diff --git a/src/ApiPlatform/Resources/Hook.php b/src/ApiPlatform/Resources/Hook.php index 52fc8de..be23386 100644 --- a/src/ApiPlatform/Resources/Hook.php +++ b/src/ApiPlatform/Resources/Hook.php @@ -25,14 +25,21 @@ use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\ApiResource; +use PrestaShop\PrestaShop\Core\Domain\Hook\Command\UpdateHookStatusCommand; use PrestaShop\PrestaShop\Core\Domain\Hook\Exception\HookNotFoundException; use PrestaShop\PrestaShop\Core\Domain\Hook\Query\GetHook; use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet; +use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate; use PrestaShopBundle\ApiPlatform\Metadata\PaginatedList; use PrestaShopBundle\ApiPlatform\Provider\QueryListProvider; #[ApiResource( operations: [ + new CQRSUpdate( + uriTemplate: '/hook-status', + CQRSCommand: UpdateHookStatusCommand::class, + scopes: ['hook_write'] + ), new CQRSGet( uriTemplate: '/hook/{id}', requirements: ['id' => '\d+'], diff --git a/tests/Integration/ApiPlatform/GetHookStatusTest.php b/tests/Integration/ApiPlatform/GetHookStatusTest.php new file mode 100644 index 0000000..79e861a --- /dev/null +++ b/tests/Integration/ApiPlatform/GetHookStatusTest.php @@ -0,0 +1,93 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +declare(strict_types=1); + +namespace PsApiResourcesTest\Integration\ApiPlatform; + +use Tests\Resources\DatabaseDump; + +class GetHookStatusTest extends ApiTestCase +{ + public static function setUpBeforeClass(): void + { + parent::setUpBeforeClass(); + DatabaseDump::restoreTables(['hook']); + self::createApiClient(['hook_write', 'hook_read']); + } + + public static function tearDownAfterClass(): void + { + parent::tearDownAfterClass(); + DatabaseDump::restoreTables(['hook']); + } + + public function getProtectedEndpoints(): iterable + { + yield 'put endpoint' => [ + 'PUT', + '/hook-status', + ]; + } + + public function testDisableHook(): void + { + $hook = new \Hook(); + $hook->name = 'disableHook'; + $hook->active = true; + $hook->add(); + + $bearerToken = $this->getBearerToken([ + 'hook_read', + 'hook_write', + ]); + static::createClient()->request('PUT', '/hook-status', [ + 'auth_bearer' => $bearerToken, + 'json' => ['id' => (int) $hook->id, 'active' => false], + ]); + self::assertResponseStatusCodeSame(200); + + $response = static::createClient()->request('GET', '/hook/' . (int) $hook->id, ['auth_bearer' => $bearerToken]); + self::assertEquals(json_decode($response->getContent())->active, false); + self::assertResponseStatusCodeSame(200); + } + + public function testEnableHook(): void + { + $hook = new \Hook(); + $hook->name = 'enableHook'; + $hook->active = false; + $hook->add(); + + $bearerToken = $this->getBearerToken([ + 'hook_read', + 'hook_write', + ]); + static::createClient()->request('PUT', '/hook-status', [ + 'auth_bearer' => $bearerToken, + 'json' => ['id' => (int) $hook->id, 'active' => true], + ]); + self::assertResponseStatusCodeSame(200); + + $response = static::createClient()->request('GET', '/hook/' . (int) $hook->id, ['auth_bearer' => $bearerToken]); + self::assertEquals(json_decode($response->getContent())->active, true); + self::assertResponseStatusCodeSame(200); + } +}