View source
<?php
namespace Drupal\Tests\webhooks\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\webhooks\Entity\WebhookConfig;
use Drupal\webhooks\Webhook;
class WebhooksTest extends BrowserTestBase {
const WEBHOOK_ID_INCOMING = 'webhook_id_incoming';
const WEBHOOK_ID_OUTGOING = 'webhook_id_outgoing';
const WEBHOOK_ID_INCOMING_VERIFIED = 'webhook_id_incoming_verified';
const WEBHOOK_ID_OUTGOING_VERIFIED = 'webhook_id_outgoing_verified';
const WEBHOOK_SECRET = 'iepooleiDahF3eimeikooC2iep1ahqua';
const WEBHOOK_ID_OUTGOING_XML = 'webhook_id_outgoing_xml';
const WEBHOOK_ID_INCOMING_XML = 'webhook_id_incoming_xml';
protected $defaultTheme = 'stark';
public static $modules = [
'webhooks',
'webhooks_test',
];
protected $profile = 'minimal';
protected $webhookService;
protected $state;
protected $uuid;
protected $payload = [
'payload' => 'attribute',
];
protected $headers = [
'header-type' => 'header-value',
];
protected function setUp() : void {
parent::setUp();
$this->state = $this->container
->get('state');
$this->uuid = $this->container
->get('uuid');
$this->webhookService = $this->container
->get('webhooks.service');
WebhookConfig::create([
'id' => self::WEBHOOK_ID_INCOMING,
'label' => 'Webhook Incoming',
'uuid' => $this->uuid
->generate(),
'payload_url' => '',
'type' => 'incoming',
'events' => [],
'content_type' => WebhookConfig::CONTENT_TYPE_JSON,
'secret' => '',
'status' => 1,
])
->save();
WebhookConfig::create([
'id' => self::WEBHOOK_ID_OUTGOING,
'label' => 'Webhook Outgoing',
'uuid' => $this->uuid
->generate(),
'payload_url' => Url::fromRoute('webhooks.webhook_receive', [
'incoming_webhook_name' => self::WEBHOOK_ID_INCOMING,
])
->setAbsolute(TRUE)
->toString(),
'type' => 'outgoing',
'events' => [],
'content_type' => WebhookConfig::CONTENT_TYPE_JSON,
'secret' => '',
'status' => 1,
])
->save();
WebhookConfig::create([
'id' => self::WEBHOOK_ID_INCOMING_VERIFIED,
'label' => 'Webhook Incoming Verified',
'uuid' => $this->uuid
->generate(),
'payload_url' => '',
'type' => 'incoming',
'events' => [],
'content_type' => WebhookConfig::CONTENT_TYPE_JSON,
'secret' => self::WEBHOOK_SECRET,
'status' => 1,
])
->save();
WebhookConfig::create([
'id' => self::WEBHOOK_ID_OUTGOING_VERIFIED,
'label' => 'Webhook Outgoing Verified',
'uuid' => $this->uuid
->generate(),
'payload_url' => Url::fromRoute('webhooks.webhook_receive', [
'incoming_webhook_name' => self::WEBHOOK_ID_INCOMING_VERIFIED,
])
->setAbsolute(TRUE)
->toString(),
'type' => 'outgoing',
'events' => [],
'content_type' => WebhookConfig::CONTENT_TYPE_JSON,
'secret' => self::WEBHOOK_SECRET,
'status' => 1,
])
->save();
WebhookConfig::create([
'id' => self::WEBHOOK_ID_INCOMING_XML,
'label' => 'Webhook Incoming XML',
'uuid' => $this->uuid
->generate(),
'payload_url' => '',
'type' => 'incoming',
'events' => [],
'content_type' => WebhookConfig::CONTENT_TYPE_XML,
'secret' => '',
'status' => 1,
])
->save();
WebhookConfig::create([
'id' => self::WEBHOOK_ID_OUTGOING_XML,
'label' => 'Webhook Outgoing XML',
'uuid' => $this->uuid
->generate(),
'payload_url' => Url::fromRoute('webhooks.webhook_receive', [
'incoming_webhook_name' => self::WEBHOOK_ID_INCOMING_XML,
])
->setAbsolute(TRUE)
->toString(),
'type' => 'outgoing',
'events' => [],
'content_type' => WebhookConfig::CONTENT_TYPE_XML,
'secret' => '',
'status' => 1,
])
->save();
}
public function testIncomingCreated() {
$webhook_config = WebhookConfig::load(self::WEBHOOK_ID_INCOMING);
$this
->assertInstanceOf(WebhookConfig::class, $webhook_config);
}
public function testOutgoingCreated() {
$webhook_config = WebhookConfig::load(self::WEBHOOK_ID_OUTGOING);
$this
->assertInstanceOf(WebhookConfig::class, $webhook_config);
}
public function testEventSend() {
$webhook_config = WebhookConfig::load(self::WEBHOOK_ID_OUTGOING);
$webhook = new Webhook($this->payload);
$this->webhookService
->send($webhook_config, $webhook);
$this
->assertEqual($this->state
->get('onWebhookSend'), TRUE);
}
public function testEventReceive() {
$webhook_config = WebhookConfig::load(self::WEBHOOK_ID_OUTGOING);
$webhook = new Webhook($this->payload);
$this->webhookService
->send($webhook_config, $webhook);
$webhook_received = $this->state
->get('onWebhookReceive');
$this
->assertEqual($webhook_received, TRUE);
}
public function testPayload() {
$webhook_config = WebhookConfig::load(self::WEBHOOK_ID_OUTGOING);
$webhook = new Webhook($this->payload);
$this->webhookService
->send($webhook_config, $webhook);
$webhook_received = $this->state
->get('onWebhookReceive_webhook');
$this
->assertEqual($webhook_received
->getPayload(), $this->payload);
}
public function testHeaders() {
$webhook_config = WebhookConfig::load(self::WEBHOOK_ID_OUTGOING);
$webhook = new Webhook($this->payload, $this->headers);
$this->webhookService
->send($webhook_config, $webhook);
$webhook_received = $this->state
->get('onWebhookReceive_webhook');
$headers_received = $webhook_received
->getHeaders();
$intersection = array_intersect($headers_received, $this->headers);
$this
->assertEqual($intersection, $this->headers);
$this
->assertEqual($headers_received['x-drupal-delivery'], $webhook
->getUuid());
$this
->assertEqual($headers_received['x-drupal-event'], $webhook
->getEvent());
}
public function testSignature() {
$webhook_config = WebhookConfig::load(self::WEBHOOK_ID_OUTGOING_VERIFIED);
$webhook = new Webhook($this->payload);
$this->webhookService
->send($webhook_config, $webhook);
$this
->assertEqual($webhook_config
->getSecret(), self::WEBHOOK_SECRET);
$webhook_receive = $this->state
->get('onWebhookReceive');
$this
->assertEqual($webhook_receive, TRUE);
$webhook_received = $this->state
->get('onWebhookReceive_webhook');
$this
->assertEqual($webhook_received
->getSignature(), $webhook
->getSignature());
}
public function testContentTypeJson() {
$webhook_config = WebhookConfig::load(self::WEBHOOK_ID_OUTGOING);
$webhook = new Webhook($this->payload);
$webhook
->setContentType(WebhookConfig::CONTENT_TYPE_JSON);
$this->webhookService
->send($webhook_config, $webhook);
$webhook_received = $this->state
->get('onWebhookReceive_webhook');
$this
->assertEqual($webhook_received
->getContentType(), WebhookConfig::CONTENT_TYPE_JSON);
}
public function testContentTypeXml() {
$webhook_config = WebhookConfig::load(self::WEBHOOK_ID_OUTGOING_XML);
$webhook = new Webhook($this->payload);
$webhook
->setContentType(WebhookConfig::CONTENT_TYPE_XML);
$this->webhookService
->send($webhook_config, $webhook);
$webhook_received = $this->state
->get('onWebhookReceive_webhook');
$this
->assertEqual($webhook_received
->getContentType(), WebhookConfig::CONTENT_TYPE_XML);
}
public function testDeliveryIdUuid() {
$webhook_config = WebhookConfig::load(self::WEBHOOK_ID_OUTGOING);
$webhook = new Webhook($this->payload);
$this->webhookService
->send($webhook_config, $webhook);
$webhook_received = $this->state
->get('onWebhookReceive_webhook');
$this
->assertEqual($webhook_received
->getUuid(), $webhook
->getUuid());
}
}