headless_cms-1.0.3/modules/headless_cms_notify/tests/src/Kernel/HeadlessNotifyServiceTest.php
modules/headless_cms_notify/tests/src/Kernel/HeadlessNotifyServiceTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\headless_cms_notify\Kernel;
use Drupal\consumers\Entity\Consumer;
use Drupal\headless_cms_notify\Entity\HeadlessNotifyTransport;
use Drupal\headless_cms_notify\Event\BeforeHeadlessNotifyEvent;
use Drupal\headless_cms_notify\HeadlessNotificationEntityOperation;
use Drupal\headless_cms_notify\HeadlessNotifyService;
use Drupal\headless_cms_notify\NotifyMessage\HeadlessNotifyEntityOperationMessage;
use Drupal\headless_cms_notify_test\NotifyTestTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Tests the HeadlessNotifyService.
*
* @group headless_cms_notify
*/
class HeadlessNotifyServiceTest extends KernelTestBase implements EventSubscriberInterface {
use NotifyTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'node',
'user',
'field',
'text',
'image',
'filter',
'file',
'consumers',
'options',
'headless_cms_notify',
'headless_cms_notify_test',
'migrate_utils',
];
/**
* The notification service.
*/
protected HeadlessNotifyService $notifyService;
/**
* Whether the notification was prevented.
*/
protected bool $notificationPrevented = FALSE;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('node');
$this->installEntitySchema('user');
$this->installEntitySchema('consumer');
$this->installConfig(['field', 'filter', 'consumers', 'headless_cms_notify']);
// Create a content type.
$nodeType = NodeType::create([
'type' => 'article',
'name' => 'Article',
]);
$nodeType->save();
// Create transport.
$transport = HeadlessNotifyTransport::create([
'id' => 'memory',
'label' => 'Memory',
'transport_plugin' => 'memory',
'transport_plugin_configuration' => [],
]);
$transport->save();
// Create a consumer.
$consumer = Consumer::create([
'label' => 'Test Consumer',
'client_id' => 'test_consumer',
'is_default' => TRUE,
'headless_cms_notify_enabled' => TRUE,
'headless_cms_notify_transport' => 'memory',
'headless_cms_notify_notification_types' => [
[
'value' => HeadlessNotifyEntityOperationMessage::getMessageTypeId(),
],
],
'headless_cms_notify_entity_types' => [
[
'value' => 'node',
],
],
]);
$consumer->save();
$this->notifyService = $this->container->get('Drupal\headless_cms_notify\HeadlessNotifyService');
$this->container->get('event_dispatcher')->addSubscriber($this);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
BeforeHeadlessNotifyEvent::EVENT_NAME => ['onBeforeNotify'],
];
}
/**
* Event subscriber callback.
*/
public function onBeforeNotify(BeforeHeadlessNotifyEvent $event): void {
if ($this->notificationPrevented) {
$event->abort();
}
}
/**
* Tests that notifications can be prevented via the event system.
*/
public function testNotificationPrevention(): void {
// Create a test node.
$node = Node::create([
'type' => 'article',
'title' => 'Test Article',
]);
$node->save();
// First is sent on creation.
$this->assertNotificationCount(1, 'One notification should have been sent.');
// First test without preventing.
$this->notificationPrevented = FALSE;
$this->notifyService->sendEntityOperationNotification(
HeadlessNotificationEntityOperation::Create,
$node,
);
// Assert that the notification was sent.
$this->assertNotificationCount(2, 'Two notification should have been sent.');
// Now test with preventing.
$this->notificationPrevented = TRUE;
$this->notifyService->sendEntityOperationNotification(
HeadlessNotificationEntityOperation::Create,
$node,
);
// Assert that no additional notification was sent.
$this->assertNotificationCount(2, 'No additional notification should have been sent when prevented.');
}
}
