‪TYPO3CMS  ‪main
WebhookExecutionTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
21 use Psr\Http\Message\RequestInterface;
33 use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\ActionService;
34 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
35 
42 final class ‪WebhookExecutionTest extends FunctionalTestCase
43 {
45 
46  protected array ‪$coreExtensionsToLoad = ['webhooks'];
47 
48  private const ‪LANGUAGE_PRESETS = [
49  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
50  ];
51 
52  protected function ‪setUp(): void
53  {
54  parent::setUp();
55 
56  $this->importCSVDataSet(__DIR__ . '/Fixtures/be_users_admin.csv');
57  $this->importCSVDataSet(__DIR__ . '/Fixtures/pages.csv');
58  $this->importCSVDataSet(__DIR__ . '/Fixtures/sys_webhooks.csv');
59 
61  'testing',
62  $this->‪buildSiteConfiguration(1, '/'),
63  [
64  $this->‪buildDefaultLanguageConfiguration('EN', '/'),
65  ]
66  );
67  $backendUser = $this->setUpBackendUser(1);
68  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
69  }
70 
71  private function ‪registerRequestInspector(callable $inspector): void
72  {
73  ‪$GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler']['logger'] = function () use ($inspector) {
74  return function (RequestInterface $request) use ($inspector) {
75  $inspector($request);
76  return (new ‪ResponseFactory())->createResponse()
77  ->withBody((new ‪StreamFactory())->createStream('success'));
78  };
79  };
80  }
81 
82  #[Test]
83  public function ‪requestIsSentOutForMessagesWithAGivenType(): void
84  {
85  $numberOfRequestsFired = 0;
86  $inspector = function (RequestInterface $request) use (&$numberOfRequestsFired) {
87  $payload = json_decode($request->getBody()->getContents(), true);
88  $numberOfRequestsFired++;
89  self::assertSame('modified', $payload['action']);
90  self::assertSame('Dummy Modified', $payload['changedFields']['title']);
91  };
92  $this->‪registerRequestInspector($inspector);
93 
94  // Catch any requests, evaluate their payload
95  (new ActionService())->modifyRecord('pages', 10, ['title' => 'Dummy Modified']);
96  self::assertEquals(1, $numberOfRequestsFired);
97  }
98 
102  #[Test]
104  {
105  $numberOfRequestsFired = 0;
106  $inspector = function (RequestInterface $request) use (&$numberOfRequestsFired) {
107  $payload = json_decode($request->getBody()->getContents(), true);
108  self::assertSame('backend', $payload['context']);
109  self::assertSame('han-solo', $payload['loginData']['uname']);
110  self::assertSame('********', $payload['loginData']['uident']);
111  $numberOfRequestsFired++;
112  };
113  $this->‪registerRequestInspector($inspector);
114  $securityAspect = ‪SecurityAspect::provideIn($this->get(Context::class));
115  $nonce = $securityAspect->provideNonce();
116  $requestToken = ‪RequestToken::create('core/user-auth/be');
117  $securityAspect->setReceivedRequestToken($requestToken);
118 
119  $request = new ‪ServerRequest('https://example.com/site1/', 'POST');
120  $request = $request->withParsedBody([
121  'login_status' => 'login',
122  'username' => 'han-solo',
123  'userident' => 'chewbaka',
124  ‪RequestToken::PARAM_NAME => $requestToken->toHashSignedJwt($nonce),
125  ]);
126 
127  $userRequest = GeneralUtility::makeInstance(BackendUserAuthentication::class);
128  $userRequest->start($request);
129  // second request
130  $userRequest->start($request);
131  self::assertEquals(2, $numberOfRequestsFired);
132  }
133 
134  #[Test]
136  {
137  // Just empty the table for the request, other ways are possible to do this
138  $this->get(ConnectionPool::class)->getConnectionForTable('sys_webhook')->truncate('sys_webhook');
139  $numberOfRequestsFired = 0;
140  $inspector = function () use (&$numberOfRequestsFired) {
141  $numberOfRequestsFired++;
142  };
143  $this->‪registerRequestInspector($inspector);
144 
145  // Catch any requests, evaluate their payload
146  (new ActionService())->modifyRecord('pages', 10, ['title' => 'Dummy Modified']);
147  self::assertEquals(0, $numberOfRequestsFired);
148  }
149 }
‪TYPO3\CMS\Core\Context\SecurityAspect\provideIn
‪static provideIn(Context $context)
Definition: SecurityAspect.php:41
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:37
‪TYPO3\CMS\Webhooks\Tests\Functional\WebhookExecutionTest\registerRequestInspector
‪registerRequestInspector(callable $inspector)
Definition: WebhookExecutionTest.php:70
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:50
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:88
‪TYPO3\CMS\Webhooks\Tests\Functional
Definition: WebhookExecutionTest.php:18
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Webhooks\Tests\Functional\WebhookExecutionTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: WebhookExecutionTest.php:47
‪TYPO3\CMS\Webhooks\Tests\Functional\WebhookExecutionTest
Definition: WebhookExecutionTest.php:43
‪TYPO3\CMS\Core\Security\RequestToken
Definition: RequestToken.php:26
‪TYPO3\CMS\Core\Context\SecurityAspect
Definition: SecurityAspect.php:30
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Http\StreamFactory
Definition: StreamFactory.php:27
‪TYPO3\CMS\Core\Security\RequestToken\PARAM_NAME
‪const PARAM_NAME
Definition: RequestToken.php:28
‪TYPO3\CMS\Webhooks\Tests\Functional\WebhookExecutionTest\requestIsSentOutForMessagesWithAGivenType
‪requestIsSentOutForMessagesWithAGivenType()
Definition: WebhookExecutionTest.php:82
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:98
‪TYPO3\CMS\Core\Http\ResponseFactory
Definition: ResponseFactory.php:27
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Webhooks\Tests\Functional\WebhookExecutionTest\oneMessageWithMultipleRequestsIsTriggeredAndDispatched
‪oneMessageWithMultipleRequestsIsTriggeredAndDispatched()
Definition: WebhookExecutionTest.php:102
‪TYPO3\CMS\Webhooks\Tests\Functional\WebhookExecutionTest\messageWithoutConfiguredTypesDoesNotSendARequest
‪messageWithoutConfiguredTypesDoesNotSendARequest()
Definition: WebhookExecutionTest.php:134
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Security\RequestToken\create
‪static create(string $scope)
Definition: RequestToken.php:43
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Webhooks\Tests\Functional\WebhookExecutionTest\setUp
‪setUp()
Definition: WebhookExecutionTest.php:51
‪TYPO3\CMS\Webhooks\Tests\Functional\WebhookExecutionTest\$coreExtensionsToLoad
‪array $coreExtensionsToLoad
Definition: WebhookExecutionTest.php:45