diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..481f1c8 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,13 @@ +name: CI + +on: + pull_request: null + +jobs: + Silverstripe: + name: 'Silverstripe (bundle)' + uses: nswdpc/ci-files/.github/workflows/silverstripe_53_83.yml@v-4 + PHPStan: + name: 'PHPStan (analyse)' + uses: nswdpc/ci-files/.github/workflows/phpstan.silverstripe_83.yml@v-4 + needs: Silverstripe diff --git a/.gitignore b/.gitignore index afc1cdc..d5ff5da 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,7 @@ /vendor/ .DS_Store /.php-cs-fixer.cache +/.phpactor.json +/app/ +/public/ +/composer.lock diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index f9b7107..0000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,21 +0,0 @@ -in(__DIR__); - -$config = new PhpCsFixer\Config(); -return $config->setRules([ - '@PSR2' => true, - 'array_indentation' => true, - 'array_syntax' => ['syntax' => 'short'], - 'blank_line_after_namespace' => true, - 'blank_line_after_opening_tag' => true, - 'full_opening_tag' => true, - 'no_closing_tag' => true, - ]) - ->setIndent(" ") - ->setFinder($finder); diff --git a/README.md b/README.md index a1a68d8..199facc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Datawrapper support for Silverstripe -This module provides an iframe element to embed [Datawrapper](https://datawrapper.de) charts and maps on a page, along with the script to enable responsiveness. +This module provides an iframe element to embed [Datawrapper](https://datawrapper.de) charts and maps on a page, as an Elemental content block, along with the script to enable responsiveness. ### Features @@ -15,7 +15,7 @@ See [composer.json](./composer.json) ## Installation -``` +```sh composer require nswdpc/silverstripe-datawrapper ``` @@ -25,34 +25,27 @@ composer require nswdpc/silverstripe-datawrapper ## Documentation - Further [documentation for content authors](./docs/en/001_index.md) is available. ## Configuration -### Webhooks +### Enable the element -```yaml -Name: 'app-datawrapper' -After: - - '#nswdpc-datawrapper' -NSWDPC\Datawrapper\WebhookController: - webhooks_enabled: true|false - webhooks_random_code: 'a random unguessable code' -``` +Add the following to a project configuration,e.g. 'app/_config/datawrapper.yml` to enable the content block on Page records: -If you are using Datwrapper custom webhooks, add a `webhooks_random_code` value. - -As there is no shared webhook signing key, anyone with the webhook URL and the Datawrapper Id of an element on your website will be able to publish elements. -You can change this random code at any time but you must ensure the custom webhook URL value at Datawrapper is updated to match. +```yml +Page: + allowed_elements: + - 'NSWDPC\Elemental\Models\Datawrapper\ElementDatawrapper' +``` -You can set elements to ignore webhook publishing requests by unchecking the "Auto publish" value on an element. +### Webhooks -Datawrapper custom webhook URLs are Team-based. Only charts in that team will receive a webhook request when they are published. +[See webhooks documentation](./docs/en/002_webhooks.md) for further information. ## Maintainers -+ [dpcdigital@NSWDPC:~$](https://dpc.nsw.gov.au) ++ PD Web Team ## Bugtracker diff --git a/composer.json b/composer.json index 5fb32d7..ae83d8d 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,6 @@ "authors": [ { "name": "James Ellis", - "homepage": "https://dpc.nsw.gov.au", "role": "Developer" } ], @@ -26,7 +25,13 @@ }, "require-dev": { "phpunit/phpunit": "^9.5", - "friendsofphp/php-cs-fixer": "^3" + "friendsofphp/php-cs-fixer": "^3", + "cambis/silverstripe-rector": "^2", + "cambis/silverstan": "^2", + "nswdpc/ci-files": "dev-v-4", + "phpstan/phpstan": "^2", + "phpstan/phpstan-phpunit": "^2", + "rector/rector": "^2" }, "autoload": { "psr-4": { @@ -44,9 +49,23 @@ ] } }, + "scripts": { + "phpstan-analyse": "./vendor/bin/phpstan analyse --ansi --no-progress --no-interaction --configuration vendor/nswdpc/ci-files/phpstan/.phpstan.silverstripe.neon src/ tests/", + "rector-dryrun": "./vendor/bin/rector process --dry-run --ansi --config vendor/nswdpc/ci-files/rector/.rector.silverstripe_53_83.php src/ tests/", + "rector-process": "./vendor/bin/rector process --no-diffs --ansi --config vendor/nswdpc/ci-files/rector/.rector.silverstripe_53_83.php src/ tests/", + "phpcsfixer-fix": "./vendor/bin/php-cs-fixer fix --ansi --no-interaction --config vendor/nswdpc/ci-files/php-cs-fixer/.php-cs-fixer.php src/ tests/" + }, "extra": { "expose": [ "client/static" ] + }, + "config": { + "allow-plugins": { + "composer/installers": true, + "silverstripe/vendor-plugin": true, + "silverstripe/recipe-plugin": true, + "phpstan/extension-installer": true + } } } diff --git a/docs/en/001_index.md b/docs/en/001_index.md index fcff302..bed4c4e 100644 --- a/docs/en/001_index.md +++ b/docs/en/001_index.md @@ -1,5 +1,7 @@ # Documentation +> This requires the Datawrapper content block to be enabled by a developer. + ## How to create a element > Audience: content editors, authors and creators @@ -19,7 +21,10 @@ In the "Share & Embed" section on your chart/map: * You will see a URL like "https://datawrapper.dwcdn.net/abcd1/5/" image showing URL to be copied per instructions + +## Page editing +Add a "Datawrapper visualisation" content block to the Page. In the "Datawrapper" CMS element you have created, add the URL value provided. You can right click and copy the URL then paste it in the field. @@ -43,4 +48,6 @@ To update the version on your website, simply set the "Datawrapper version" fiel Then save the element to review changes or, optionally, publish. +## Automatic updates on publish + See [webhook setup for options to automatically publish](.002_webhooks.md). diff --git a/docs/en/002_webhooks.md b/docs/en/002_webhooks.md index 8bcfcc9..549ed1f 100644 --- a/docs/en/002_webhooks.md +++ b/docs/en/002_webhooks.md @@ -23,7 +23,7 @@ POST /webhook HTTP/1.1 content-type: application/json content-length: 32 -{"id":"abcd1","publicVersion":6} +{"id":"abcd1","publicVersion":6, "user":{"id":123,"email":"user@your-org.com"}} ``` You can use a system such as Pipedream to inspect and debug webhook requests. @@ -33,12 +33,19 @@ You can use a system such as Pipedream to inspect and debug webhook requests. Turn off|on via project configuration, the default is `true` -```yaml -NSWDPC\Datawrapper\Webhook: - webhooks_enabled: true|false +```yml +--- +Name: app-datawrapper +After: + - '#nswdpc-datawrapper' +--- +NSWDPC\Datawrapper\WebHookController: + webhooks_enabled: true webhooks_random_code: 'some_random_code_string' ``` Use `webhooks_random_code` to randomise the URL. Using this example, the submission URL will look something like `https://mysite.example.com/_datawrapperwebhook/submit/some_random_code_string/` This is empty by default. It's a good idea to have this value set as anyone who knows the URL and a Datawrapper chart ID you are using will be able to submit webhook requests. + +To invalidate webhook requests, change the `webhooks_random_code` value and flush the configuration. Any webhooks configured at DataWrapper will need to be updated in this event. diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3f44535..bb17480 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,14 @@ - - - tests/ - - - - src/ - - tests/ - - - + + + + + src/ + + + tests/ + + + + tests/ + diff --git a/src/Controllers/WebHookController.php b/src/Controllers/WebHookController.php index 20323b3..0ccc01c 100644 --- a/src/Controllers/WebHookController.php +++ b/src/Controllers/WebHookController.php @@ -7,29 +7,20 @@ use SilverStripe\Control\Director; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; -use Silverstripe\Versioned\Versioned; +use SilverStripe\Versioned\Versioned; /** * Controller for handling webhook submissions from Datawrapper * To create a webhook URL, see the README.md - * @author James + * @author James */ class WebHookController extends Controller { - /** - * @var bool - */ - private static $webhooks_enabled = true; + private static bool $webhooks_enabled = true; - /** - * @var string - */ - private static $webhooks_random_code = ''; + private static string $webhooks_random_code = ''; - /** - * @var array - */ - private static $allowed_actions = [ + private static array $allowed_actions = [ 'submit' => true ]; @@ -37,20 +28,21 @@ class WebHookController extends Controller * Return link to this controller * @return string */ + #[\Override] public function Link($action = null) { if ($link = self::getWebhookURL()) { return $link; } + return ""; } /** * Mis-named public method retained for BC * @deprecated 1.0 - * @return string|null */ - public static function getWebookURL() : ?string + public static function getWebookURL(): ?string { return static::getWebhookURL(); } @@ -58,19 +50,20 @@ public static function getWebookURL() : ?string /** * Return the URL (absolute) for webhook submissions * If webhooks are not enabled, this will return boolean false - * @return string|null */ - public static function getWebhookURL() : ?string + public static function getWebhookURL(): ?string { $enabled = self::config()->get('webhooks_enabled'); if (!$enabled) { return null; } + $code = self::config()->get('webhooks_random_code'); $path = "_datawrapperwebhook/submit/"; if ($code) { $path .= "{$code}/"; } + return Director::absoluteURL($path); } @@ -79,7 +72,7 @@ public static function getWebhookURL() : ?string * The two keys are 'success' being a boolean, count being the number of items changed * @return string JSON encoded value */ - protected function getResponseBody($success = true, $count = 0) : string + protected function getResponseBody($success = true, $count = 0): string { $data = [ 'success' => $success, @@ -90,9 +83,8 @@ protected function getResponseBody($success = true, $count = 0) : string /** * We have done something wrong - * @return HTTPResponse */ - protected function serverError($status_code = 503, $message = "") : HTTPResponse + protected function serverError($status_code = 503, $message = ""): HTTPResponse { $response = HTTPResponse::create($this->getResponseBody(false), $status_code); $response->addHeader('Content-Type', 'application/json'); @@ -101,7 +93,6 @@ protected function serverError($status_code = 503, $message = "") : HTTPResponse /** * Client (being Mailgun user agent) has done something wrong - * @return HTTPResponse */ protected function clientError($status_code = 400, $message = ""): HTTPResponse { @@ -112,9 +103,8 @@ protected function clientError($status_code = 400, $message = ""): HTTPResponse /** * All is good - * @return HTTPResponse */ - protected function returnOK($status_code = 200, $message = "OK", $count = 0) : HTTPResponse + protected function returnOK($status_code = 200, $message = "OK", $count = 0): HTTPResponse { $response = HTTPResponse::create($this->getResponseBody(true, $count), $status_code); $response->addHeader('Content-Type', 'application/json'); @@ -123,32 +113,30 @@ protected function returnOK($status_code = 200, $message = "OK", $count = 0) : /** * Ignore requests to / - * @return HTTPResponse */ - public function index($request) : HTTPResponse + public function index($request): HTTPResponse { return $this->clientError(404, "Not Found"); } /** * Returns whether webhooks are enabled in Configuration - * @return bool */ - protected function webhooksEnabled() : bool + protected function webhooksEnabled(): bool { return $this->config()->get('webhooks_enabled'); } /** * Test whether the random code sent in the request matches what is configured - * @return bool */ - protected function webhookRandomCodeMatch(HTTPRequest $request) : bool + protected function webhookRandomCodeMatch(HTTPRequest $request): bool { $code = $this->config()->get('webhooks_random_code'); if (!$code) { return true; } + $request_code = $request->param('ID'); return $request_code == $code; } @@ -157,7 +145,7 @@ protected function webhookRandomCodeMatch(HTTPRequest $request) : bool * Primary handler for submitted webooks * @throws \Exception */ - public function submit(HTTPRequest $request = null) : HTTPResponse + public function submit(HTTPRequest $request = null): HTTPResponse { try { if (!$this->webhooksEnabled()) { @@ -180,7 +168,7 @@ public function submit(HTTPRequest $request = null) : HTTPResponse } // POST body - $payload = json_decode($request->getBody(), true); + $payload = json_decode((string) $request->getBody(), true); if (!$payload) { throw new \Exception("No payload found"); } @@ -216,8 +204,8 @@ public function submit(HTTPRequest $request = null) : HTTPResponse } return $this->returnOK(200, "OK", $count); - } catch (\Exception $e) { - return $this->clientError($e->getCode(), $e->getMessage()); + } catch (\Exception $exception) { + return $this->clientError($exception->getCode(), $exception->getMessage()); } } } diff --git a/src/Models/Elements/ElementDatawrapper.php b/src/Models/Elements/ElementDatawrapper.php index a259560..eea080e 100644 --- a/src/Models/Elements/ElementDatawrapper.php +++ b/src/Models/Elements/ElementDatawrapper.php @@ -3,77 +3,48 @@ namespace NSWDPC\Elemental\Models\Datawrapper; use Codem\Utilities\HTML5\UrlField; -use NSWDPC\Datawrapper\WebhookController; +use NSWDPC\Datawrapper\WebHookController; use NSWDPC\Elemental\Models\Iframe\ElementIframe; -use SilverStripe\Forms\NumericField; use SilverStripe\Forms\CheckboxField; -use SilverStripe\Forms\TextField; use SilverStripe\Forms\RequiredFields; use SilverStripe\ORM\ValidationException; -use Silverstripe\View\ArrayData; +use SilverStripe\View\ArrayData; use SilverStripe\View\Requirements; /** * Datawrapper Element - * @author James Ellis + * @author James + * @property ?string $Content + * @property ?string $DatawrapperId + * @property int $DatawrapperVersion + * @property bool $AutoPublish */ class ElementDatawrapper extends ElementIframe { + private static string $table_name = 'ElementDatawrapper'; - /** - * @var string - */ - private static $table_name = 'ElementDatawrapper'; + private static string $icon = 'font-icon-code'; - /** - * @var string - */ - private static $icon = 'font-icon-code'; + private static bool $inline_editable = false; - /** - * @var bool - */ - private static $inline_editable = false; + private static string $singular_name = 'Datawrapper visualisation'; - /** - * @var string - */ - private static $singular_name = 'Datawrapper visualisation'; + private static string $plural_name = 'Datawrapper visualisations'; - /** - * @var string - */ - private static $plural_name = 'Datawrapper visualisations'; + private static string $title = 'Datawrapper visualisation'; - /** - * @var string - */ - private static $title = 'Datawrapper visualisation'; + private static string $description = 'Display a Datawrapper visualisation'; - /** - * @var string - */ - private static $description = 'Display a Datawrapper visualisation'; + private static string $default_host = 'datawrapper.dwcdn.net'; - /** - * @var string - */ - private static $default_host = 'datawrapper.dwcdn.net'; - - /** - * @var array - */ - private static $db = [ + private static array $db = [ 'Content' => 'HTMLText', 'DatawrapperId' => 'Varchar(5)',// dw IDs are 5 chr long 'DatawrapperVersion' => 'Int', 'AutoPublish' => 'Boolean', ]; - /** - * @var array - */ - private static $defaults = [ + private static array $defaults = [ 'DatawrapperVersion' => 1, 'AutoPublish' => 0, ]; @@ -82,7 +53,7 @@ class ElementDatawrapper extends ElementIframe * @var array * Provide indexes for fields used in queries */ - private static $indexes = [ + private static array $indexes = [ 'DatawrapperVersion' => true, 'DatawrapperId' => true, 'AutoPublish' => true @@ -91,14 +62,16 @@ class ElementDatawrapper extends ElementIframe /** * @return string */ + #[\Override] public function getType() { - return _t(__CLASS__ . '.BlockType', 'Datawrapper visualisation'); + return _t(self::class . '.BlockType', 'Datawrapper visualisation'); } /** * Apply requirements when templating */ + #[\Override] public function forTemplate($holder = true) { Requirements::customScript( @@ -115,6 +88,7 @@ public function forTemplate($holder = true) /** * Handle default settings prior to write */ + #[\Override] public function onBeforeWrite() { parent::onBeforeWrite(); @@ -133,12 +107,12 @@ public function onBeforeWrite() protected function setPartsFromUrl() { if (!empty($this->InputURL)) { - $path = trim(trim(parse_url($this->InputURL, PHP_URL_PATH), "/")); + $path = trim(trim(parse_url((string) $this->InputURL, PHP_URL_PATH), "/")); $path_parts = explode("/", $path); - if (count($path_parts) != 2) { - throw new ValidationException( + if (count($path_parts) !== 2) { + throw ValidationException::create( _t( - __CLASS__ . '.DW_URL_NOT_VALID', + self::class . '.DW_URL_NOT_VALID', 'The Datawrapper path must have a 5 character Datawrapper chart Id and a version number. The URL provided was {url}', [ 'url' => $this->InputURL @@ -147,10 +121,10 @@ protected function setPartsFromUrl() ); } - if (strlen($path_parts[0]) != 5) { - throw new ValidationException( + if (strlen($path_parts[0]) !== 5) { + throw ValidationException::create( _t( - __CLASS__ . '.DW_ID_CHR_LENGTH', + self::class . '.DW_ID_CHR_LENGTH', 'The Datawrapper chart Id must be 5 characters long' ) ); @@ -158,9 +132,9 @@ protected function setPartsFromUrl() $version = intval($path_parts[1]); if ($version < 1) { - throw new ValidationException( + throw ValidationException::create( _t( - __CLASS__ . '.DW_URL_VERSION_FAILURE', + self::class . '.DW_URL_VERSION_FAILURE', 'The Datawrapper version must be >= 1' ) ); @@ -173,35 +147,33 @@ protected function setPartsFromUrl() /** * Return the datawrapper URL - * @return string */ - public function DatawrapperURL() : string + public function DatawrapperURL(): string { if (!$this->DatawrapperId) { return ""; } + if (!$this->DatawrapperVersion || $this->DatawrapperVersion <= 1) { $this->DatawrapperVersion = 1; } - $url = "https://" + + return "https://" . $this->config()->get('default_host') . "/" . $this->DatawrapperId . "/" . $this->DatawrapperVersion . "/"; - return $url; } /** * Return the "id" attribute for a DW element * Note that only one element per DatawrapperId can exist on a single page or "id" clashes will happen - * @return string */ - public function DatawrapperIdAttribute() : string + public function DatawrapperIdAttribute(): string { - $id = "datawrapper-chart-{$this->DatawrapperId}"; - return $id; + return "datawrapper-chart-{$this->DatawrapperId}"; } /** @@ -209,12 +181,13 @@ public function DatawrapperIdAttribute() : string */ public function getCMSValidator() { - return new RequiredFields('InputURL'); + return RequiredFields::create(['InputURL']); } /** * @inheritdoc */ + #[\Override] public function getCMSFields() { $fields = parent::getCMSFields(); @@ -235,8 +208,8 @@ public function getCMSFields() UrlField::create( 'InputURL', _t( - __CLASS__ . ".DW_URL_LINK_TO_VISUALISATION", - 'The Datawrapper \'Link to your visualisation:\' URL (Visualisation only option)' + self::class . ".DW_URL_LINK_TO_VISUALISATION", + "The Datawrapper 'Link to your visualisation:' URL (Visualisation only option)" ), $this->DatawrapperURL() )->setDescription("In the format https://datawrapper.dwcdn.net/abc12/1/") @@ -256,7 +229,7 @@ public function getCMSFields() 'Auto publish' )->setDescription( _t( - __CLASS__ . '.DW_AUTOPUBLISH', + self::class . '.DW_AUTOPUBLISH', "If checked, this element will be published when the chart is published at Datawrapper, " . "
" . "The parent item of this element will not be published at the same time" diff --git a/templates/NSWDPC/Elemental/Models/Datawrapper/ElementDatawrapper.ss b/templates/NSWDPC/Elemental/Models/Datawrapper/ElementDatawrapper.ss index edf934a..f004692 100644 --- a/templates/NSWDPC/Elemental/Models/Datawrapper/ElementDatawrapper.ss +++ b/templates/NSWDPC/Elemental/Models/Datawrapper/ElementDatawrapper.ss @@ -4,7 +4,7 @@ {$Content} <% end_if %>
- <% if $IsLazy %><% end_if %>
diff --git a/tests/DatawrapperTest.php b/tests/DatawrapperTest.php index e7a2bd7..e121ed6 100644 --- a/tests/DatawrapperTest.php +++ b/tests/DatawrapperTest.php @@ -11,7 +11,6 @@ */ class DatawrapperTest extends SapphireTest { - /** * @inheritdoc */ @@ -33,7 +32,7 @@ private function createTestRecord() /** * Test that the InputURL value results in the expected values being saved */ - public function testUrlCreation() + public function testUrlCreation(): void { $record = [ 'DatawrapperId' => 'abcd4', @@ -59,7 +58,7 @@ public function testUrlCreation() /** * Test iframe save/update */ - public function testIframe() + public function testIframe(): void { $width = 300; $height = 200; @@ -96,16 +95,16 @@ public function testIframe() $strings = [ "id=\"{$record->DatawrapperIdAttribute()}", - "allow=\"fullscreen\"", - "loading=\"lazy\"", + 'allow="fullscreen"', + 'loading="lazy"', "height=\"{$iframe_height}\"", "

{$record->Title}

", "title=\"{$record->AlternateContent}\"", - "src=\"" . htmlspecialchars($record->DatawrapperURL()) . "\"" + 'src="' . htmlspecialchars((string) $record->DatawrapperURL()) . '"' ]; foreach ($strings as $string) { - $this->assertTrue(strpos($template, $string) !== false, "{$string} should appear in the template"); + $this->assertTrue(str_contains((string) $template, $string), "{$string} should appear in the template"); } } } diff --git a/tests/DatawrapperWebhookTest.php b/tests/DatawrapperWebhookTest.php index 6ddf5f6..a6122ce 100644 --- a/tests/DatawrapperWebhookTest.php +++ b/tests/DatawrapperWebhookTest.php @@ -14,7 +14,6 @@ */ class DatawrapperWebhookTest extends FunctionalTest { - /** * @inheritdoc */ @@ -28,11 +27,11 @@ class DatawrapperWebhookTest extends FunctionalTest /** * @inheritdoc */ - public function setUp() : void + public function setUp(): void { parent::setUp(); - Config::inst()->set(WebHookController::class, 'webhooks_random_code', 'randomecodeforurl'); - Config::inst()->set(WebHookController::class, 'webhooks_enabled', true); + Config::modify()->set(WebHookController::class, 'webhooks_random_code', 'randomecodeforurl'); + Config::modify()->set(WebHookController::class, 'webhooks_enabled', true); } /** @@ -46,7 +45,7 @@ private function createTestRecord() /** * Test webhook controller POST */ - public function testWebHook() + public function testWebHook(): void { $width = 300; $height = 200; @@ -78,7 +77,7 @@ public function testWebHook() 'publicVersion' => 3 // publishing to a later version ]; $cookies = null; - $body = json_encode($data, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT); + $body = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); $request = $this->post($url, $data, $headers, $session, $body, $cookies); $response = json_decode($request->getBody()); @@ -111,7 +110,7 @@ public function testWebHook() 'publicVersion' => 1 ]; $cookies = null; - $body = json_encode($data, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT); + $body = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); $request = $this->post($url, $data, $headers, $session, $body, $cookies); $response = json_decode($request->getBody()); @@ -132,7 +131,7 @@ public function testWebHook() /** * Test webhook controller POST */ - public function testWebHookAvoidRollback() + public function testWebHookAvoidRollback(): void { $width = 300; $height = 200; @@ -164,7 +163,7 @@ public function testWebHookAvoidRollback() 'publicVersion' => 3 // DW sends request with this version ]; $cookies = null; - $body = json_encode($data, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT); + $body = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); $request = $this->post($url, $data, $headers, $session, $body, $cookies); $response = json_decode($request->getBody()); @@ -184,7 +183,7 @@ public function testWebHookAvoidRollback() /** * Test bad webhook controller POST */ - public function testBadWebhook() + public function testBadWebhook(): void { $headers = [ 'Content-Type' => "application/json" @@ -195,14 +194,13 @@ public function testBadWebhook() 'publicVersion' => 1 ]; $cookies = null; - $body = json_encode($data, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT); + $body = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); // put together a request with a fake code $path = "/_datawrapperwebhook/submit/notarealcode/"; $url = Director::absoluteURL($path); $request = $this->post($url, $data, $headers, $session, $body, $cookies); - $response = json_decode($request->getBody()); // should not be a 200 OK $this->assertNotEquals( @@ -216,7 +214,6 @@ public function testBadWebhook() $request = $this->get($url); - $response = json_decode($request->getBody()); // GET should not be a 200 OK $this->assertNotEquals( @@ -226,10 +223,10 @@ public function testBadWebhook() ); // turn off webhooks - Config::inst()->set(WebHookController::class, 'webhooks_enabled', false); + Config::modify()->set(WebHookController::class, 'webhooks_enabled', false); $request = $this->post($url, $data, $headers, $session, $body, $cookies); - $response = json_decode($request->getBody()); + json_decode($request->getBody()); // should not be a 200 OK $this->assertNotEquals(