Skip to content

fix: system config webhook check #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/Webhook/Registration/WebhookSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,39 @@ public function removeSalesChannelWebhookConfiguration(EntityDeletedEvent $event

/**
* system-config should be written by {@see SettingsSaver} only, which checks the webhook on its own.
* Just in case new/changed credentials will be saved via the normal system config save route.
* Just in case new/changed credentials will be saved via the normal system config.
*/
public function checkWebhookBefore(BeforeSystemConfigMultipleChangedEvent $event): void
{
$routeName = (string) $this->requestStack->getMainRequest()?->attributes->getString('_route');

if (!\str_contains($routeName, 'api.action.core.save.system-config')) {
return;
if ($config = $this->getConfigToCheck($event)) {
$this->webhookSystemConfigHelper->checkWebhookBefore([$event->getSalesChannelId() => $config]);
}

/** @var array<string, array<string, mixed>> $config */
$config = $event->getConfig();
$this->webhookSystemConfigHelper->checkWebhookBefore($config);
}

/**
* system-config should be written by {@see SettingsSaver} only, which checks the webhook on its own.
* Just in case new/changed credentials will be saved via the normal system config save route.
* Just in case new/changed credentials will be saved via the normal system config.
*/
public function checkWebhookAfter(SystemConfigMultipleChangedEvent $event): void
{
if ($this->getConfigToCheck($event)) {
$this->webhookSystemConfigHelper->checkWebhookAfter([$event->getSalesChannelId()]);
}
}

/**
* @return array<string, mixed>|null
*/
private function getConfigToCheck(BeforeSystemConfigMultipleChangedEvent|SystemConfigMultipleChangedEvent $event): ?array
{
/** @var array<string, mixed> $config */
$config = $event->getConfig();
$routeName = (string) $this->requestStack->getMainRequest()?->attributes->getString('_route');

if (!\str_contains($routeName, 'api.action.core.save.system-config')) {
return;
if (\str_contains($routeName, 'api.action.paypal.settings.save') || !$this->webhookSystemConfigHelper->needsCheck($config)) {
return null;
}

$this->webhookSystemConfigHelper->checkWebhookAfter(\array_keys($event->getConfig()));
return $config;
}
}
12 changes: 10 additions & 2 deletions src/Webhook/Registration/WebhookSystemConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function checkWebhookBefore(array $newData): array
$errors = [];

foreach ($newData as $salesChannelId => $newSettings) {
if ($salesChannelId === 'null') {
if (!$salesChannelId || $salesChannelId === 'null') {
$salesChannelId = null;
}

Expand Down Expand Up @@ -102,7 +102,7 @@ public function checkWebhookAfter(array $salesChannelIds): array
$errors = [];

foreach ($salesChannelIds as $salesChannelId) {
if ($salesChannelId === 'null') {
if (!$salesChannelId || $salesChannelId === 'null') {
$salesChannelId = null;
}

Expand All @@ -129,6 +129,14 @@ public function checkWebhookAfter(array $salesChannelIds): array
return $errors;
}

/**
* @param array<string, mixed> $config
*/
public function needsCheck(array $config): bool
{
return !empty($this->filterSettings($config));
}

private function fetchSettings(?string $salesChannelId, bool $inherit = false): array
{
$settings = [];
Expand Down
162 changes: 160 additions & 2 deletions tests/Webhook/Registration/WebhookSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Swag\PayPal\Test\Webhook\Registration;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Shopware\Core\Framework\Context;
Expand All @@ -29,6 +30,7 @@
use Swag\PayPal\Webhook\Registration\WebhookSystemConfigHelper;
use Swag\PayPal\Webhook\WebhookRegistry;
use Swag\PayPal\Webhook\WebhookService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;

Expand All @@ -44,9 +46,15 @@ class WebhookSubscriberTest extends TestCase

private SystemConfigService $systemConfigService;

private WebhookSystemConfigHelper&MockObject $webhookSystemConfigHelper;

private RequestStack $requestStack;

protected function setUp(): void
{
$this->systemConfigService = SystemConfigServiceMock::createWithCredentials();
$this->webhookSystemConfigHelper = $this->createMock(WebhookSystemConfigHelper::class);
$this->requestStack = new RequestStack();
}

public function testRemoveWebhookWithInheritedConfiguration(): void
Expand All @@ -73,6 +81,156 @@ public function testRemoveWebhookWithNoConfiguration(): void
static::assertEmpty($this->systemConfigService->getString(Settings::WEBHOOK_ID, TestDefaults::SALES_CHANNEL));
}

public function testCheckWebhookBefore(): void
{
$event = new BeforeSystemConfigMultipleChangedEvent(['some-key' => 'some-value'], null);

$this->webhookSystemConfigHelper
->expects(static::once())
->method('checkWebhookBefore')
->with(['' => ['some-key' => 'some-value']]);

$this->webhookSystemConfigHelper
->expects(static::once())
->method('needsCheck')
->with(['some-key' => 'some-value'])
->willReturn(true);

$this->createWebhookSubscriber(['' => null, TestDefaults::SALES_CHANNEL => null])
->checkWebhookBefore($event);
}

public function testCheckWebhookBeforeWithSalesChannelId(): void
{
$event = new BeforeSystemConfigMultipleChangedEvent(['some-key' => 'some-value'], 'some-sales-channel-id');

$this->webhookSystemConfigHelper
->expects(static::once())
->method('checkWebhookBefore')
->with(['some-sales-channel-id' => ['some-key' => 'some-value']]);

$this->webhookSystemConfigHelper
->expects(static::once())
->method('needsCheck')
->with(['some-key' => 'some-value'])
->willReturn(true);

$this->createWebhookSubscriber(['' => null, TestDefaults::SALES_CHANNEL => null])
->checkWebhookBefore($event);
}

public function testCheckWebhookBeforeWithSaveRoute(): void
{
$request = new Request(attributes: ['_route' => 'api.action.paypal.settings.save']);
$this->requestStack->push($request);

$event = new BeforeSystemConfigMultipleChangedEvent(['some-key' => 'some-value'], 'some-sales-channel-id');

$this->webhookSystemConfigHelper
->expects(static::never())
->method('checkWebhookBefore');

$this->webhookSystemConfigHelper
->expects(static::never())
->method('needsCheck');

$this->createWebhookSubscriber(['' => null, TestDefaults::SALES_CHANNEL => null])
->checkWebhookBefore($event);
}

public function testCheckWebhookBeforeWithNoCheckNeeded(): void
{
$event = new BeforeSystemConfigMultipleChangedEvent(['some-key' => 'some-value'], 'some-sales-channel-id');

$this->webhookSystemConfigHelper
->expects(static::never())
->method('checkWebhookBefore');

$this->webhookSystemConfigHelper
->expects(static::once())
->method('needsCheck')
->with(['some-key' => 'some-value'])
->willReturn(false);

$this->createWebhookSubscriber(['' => null, TestDefaults::SALES_CHANNEL => null])
->checkWebhookBefore($event);
}

public function testCheckWebhookAfter(): void
{
$event = new SystemConfigMultipleChangedEvent(['some-key' => 'some-value'], null);

$this->webhookSystemConfigHelper
->expects(static::once())
->method('checkWebhookAfter')
->with(['']);

$this->webhookSystemConfigHelper
->expects(static::once())
->method('needsCheck')
->with(['some-key' => 'some-value'])
->willReturn(true);

$this->createWebhookSubscriber(['' => null, TestDefaults::SALES_CHANNEL => null])
->checkWebhookAfter($event);
}

public function testCheckWebhookAfterWithSalesChannelId(): void
{
$event = new SystemConfigMultipleChangedEvent(['some-key' => 'some-value'], 'some-sales-channel-id');

$this->webhookSystemConfigHelper
->expects(static::once())
->method('checkWebhookAfter')
->with(['some-sales-channel-id']);

$this->webhookSystemConfigHelper
->expects(static::once())
->method('needsCheck')
->with(['some-key' => 'some-value'])
->willReturn(true);

$this->createWebhookSubscriber(['' => null, TestDefaults::SALES_CHANNEL => null])
->checkWebhookAfter($event);
}

public function testCheckWebhookAfterWithSaveRoute(): void
{
$request = new Request(attributes: ['_route' => 'api.action.paypal.settings.save']);
$this->requestStack->push($request);

$event = new SystemConfigMultipleChangedEvent(['some-key' => 'some-value'], 'some-sales-channel-id');

$this->webhookSystemConfigHelper
->expects(static::never())
->method('checkWebhookAfter');

$this->webhookSystemConfigHelper
->expects(static::never())
->method('needsCheck');

$this->createWebhookSubscriber(['' => null, TestDefaults::SALES_CHANNEL => null])
->checkWebhookAfter($event);
}

public function testCheckWebhookAfterWithNoCheckNeeded(): void
{
$event = new SystemConfigMultipleChangedEvent(['some-key' => 'some-value'], 'some-sales-channel-id');

$this->webhookSystemConfigHelper
->expects(static::never())
->method('checkWebhookAfter');

$this->webhookSystemConfigHelper
->expects(static::once())
->method('needsCheck')
->with(['some-key' => 'some-value'])
->willReturn(false);

$this->createWebhookSubscriber(['' => null, TestDefaults::SALES_CHANNEL => null])
->checkWebhookAfter($event);
}

public function testSubscribedEvents(): void
{
static::assertEqualsCanonicalizing([
Expand Down Expand Up @@ -105,8 +263,8 @@ private function createWebhookSubscriber(array $configuration): WebhookSubscribe
new NullLogger(),
$this->systemConfigService,
$webhookService,
$this->createMock(WebhookSystemConfigHelper::class),
new RequestStack(),
$this->webhookSystemConfigHelper,
$this->requestStack,
);
}

Expand Down
83 changes: 83 additions & 0 deletions tests/Webhook/Registration/WebhookSystemConfigHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php declare(strict_types=1);
/*
* (c) shopware AG <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Swag\PayPal\Test\Webhook\Registration;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Swag\PayPal\Setting\Service\SettingsValidationServiceInterface;
use Swag\PayPal\Setting\Settings;
use Swag\PayPal\Webhook\Registration\WebhookSystemConfigHelper;
use Swag\PayPal\Webhook\WebhookServiceInterface;

/**
* @internal
*/
#[Package('checkout')]
#[CoversClass(WebhookSystemConfigHelper::class)]
class WebhookSystemConfigHelperTest extends TestCase
{
private const WEBHOOK_KEYS = [
Settings::CLIENT_ID,
Settings::CLIENT_SECRET,
Settings::CLIENT_ID_SANDBOX,
Settings::CLIENT_SECRET_SANDBOX,
Settings::SANDBOX,
Settings::WEBHOOK_ID,
];

private WebhookSystemConfigHelper $helper;

protected function setUp(): void
{
$this->helper = new WebhookSystemConfigHelper(
new NullLogger(),
$this->createMock(WebhookServiceInterface::class),
$this->createMock(SystemConfigService::class),
$this->createMock(SettingsValidationServiceInterface::class),
);
}

/**
* @param array<string, mixed> $config
*/
#[DataProvider('providerNeedsCheck')]
public function testNeedsCheck(bool $expected, array $config): void
{
static::assertSame($expected, $this->helper->needsCheck($config));
}

public static function providerNeedsCheck(): \Generator
{
foreach (self::WEBHOOK_KEYS as $key) {
yield \sprintf('needs check for "%s"', $key) => [
true,
[$key => 'some-value'],
];

yield \sprintf('needs check for "%s" with null value', $key) => [
true,
[$key => null],
];
}

foreach (Settings::DEFAULT_VALUES as $key => $value) {
if (\in_array($key, self::WEBHOOK_KEYS, true)) {
continue;
}

yield \sprintf('no check for "%s"', $key) => [
false,
[$key => $value],
];
}
}
}
Loading