‪TYPO3CMS  ‪main
WebhookExecutionTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use PHPUnit\Framework\Attributes\Test;
19 use Psr\EventDispatcher\EventDispatcherInterface;
20 use Psr\Http\Message\RequestInterface;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
28 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
29 
30 final class ‪WebhookExecutionTest extends FunctionalTestCase
31 {
32  protected array ‪$coreExtensionsToLoad = [
33  'redirects',
34  'webhooks',
35  ];
36 
37  protected function ‪setUp(): void
38  {
39  parent::setUp();
40  $this->importCSVDataSet(__DIR__ . '/Fixtures/sys_webhook.csv');
41  $this->importCSVDataSet(__DIR__ . '/Fixtures/sys_redirect.csv');
42  }
43 
44  #[Test]
45  public function ‪matchedRedirectEmitsWebhookMessage(): void
46  {
47  $payloadSourceUrl = null;
48  $payloadTargetUrl = null;
49  $payloadStatusCode = null;
50  $payloadRedirectUid = null;
51  $numberOfRequestFired = 0;
52  $inspector = function (RequestInterface $request) use (
53  &$numberOfRequestFired,
54  &$payloadSourceUrl,
55  &$payloadTargetUrl,
56  &$payloadStatusCode,
57  &$payloadRedirectUid
58  ) {
59  $payload = json_decode($request->getBody()->getContents(), true);
60  $payloadSourceUrl = $payload['sourceUrl'] ?? null;
61  $payloadTargetUrl = $payload['targetUrl'] ?? null;
62  $payloadStatusCode = $payload['statusCode'] ?? null;
63  $payloadRedirectUid = $payload['redirect']['uid'] ?? null;
64  $numberOfRequestFired++;
65  };
66  $this->‪registerRequestInspector($inspector);
67 
68  // Ensure to disable increment hit counter event listener
69  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['features']['redirects.hitCount'] = true;
70 
71  $matchedRecord = BackendUtility::getRecord('sys_redirect', 1);
72  $this->get(EventDispatcherInterface::class)->dispatch(
74  new ‪ServerRequest('https://acme.com/foo'),
75  new ‪RedirectResponse($matchedRecord['target'], $matchedRecord['target_statuscode']),
76  $matchedRecord,
77  new ‪Uri($matchedRecord['target']),
78  )
79  );
80 
81  self::assertSame(1, $numberOfRequestFired);
82  self::assertSame('https://acme.com/foo', $payloadSourceUrl);
83  self::assertSame('https://example.com/bar', $payloadTargetUrl);
84  self::assertSame(301, $payloadStatusCode);
85  self::assertSame(1, $payloadRedirectUid);
86  }
87 
88  protected function ‪registerRequestInspector(callable $inspector): void
89  {
90  ‪$GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler']['logger'] = function () use ($inspector) {
91  return function (RequestInterface $request) use ($inspector) {
92  $inspector($request);
93  return (new ‪ResponseFactory())->createResponse()
94  ->withBody((new ‪StreamFactory())->createStream('success'));
95  };
96  };
97  }
98 }
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:30
‪TYPO3\CMS\Redirects\Tests\Functional\WebhookExecutionTest\$coreExtensionsToLoad
‪array $coreExtensionsToLoad
Definition: WebhookExecutionTest.php:32
‪TYPO3\CMS\Redirects\Tests\Functional
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪TYPO3\CMS\Core\Http\StreamFactory
Definition: StreamFactory.php:27
‪TYPO3\CMS\Core\Http\ResponseFactory
Definition: ResponseFactory.php:27
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Redirects\Tests\Functional\WebhookExecutionTest\matchedRedirectEmitsWebhookMessage
‪matchedRedirectEmitsWebhookMessage()
Definition: WebhookExecutionTest.php:45
‪TYPO3\CMS\Redirects\Tests\Functional\WebhookExecutionTest\setUp
‪setUp()
Definition: WebhookExecutionTest.php:37
‪TYPO3\CMS\Redirects\Tests\Functional\WebhookExecutionTest\registerRequestInspector
‪registerRequestInspector(callable $inspector)
Definition: WebhookExecutionTest.php:88
‪TYPO3\CMS\Redirects\Tests\Functional\WebhookExecutionTest
Definition: WebhookExecutionTest.php:31
‪TYPO3\CMS\Redirects\Event\RedirectWasHitEvent
Definition: RedirectWasHitEvent.php:33