View source
<?php
namespace Drupal\Tests\acquia_contenthub\Kernel\EventSubscriber\HandleWebhook;
use Acquia\Hmac\Key;
use Drupal\acquia_contenthub\Event\HandleWebhookEvent;
use Drupal\acquia_contenthub_publisher\EventSubscriber\HandleWebhook\Purge;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Symfony\Component\HttpFoundation\Request;
class PurgeTest extends EntityKernelTestBase {
protected $purge;
protected $clientFactory;
protected $configFactory;
protected $queueFactory;
protected $database;
protected $loggerChannel;
public static $modules = [
'acquia_contenthub',
'acquia_contenthub_publisher',
'acquia_contenthub_server_test',
'depcalc',
'node',
];
protected function setUp() : void {
parent::setUp();
$this
->installSchema('acquia_contenthub_publisher', [
'acquia_contenthub_publisher_export_tracking',
]);
$this->configFactory = $this->container
->get('config.factory');
$this
->createAcquiaContentHubAdminSettings();
$this->clientFactory = $this->container
->get('acquia_contenthub.client.factory');
Node::create([
'type' => 'article',
'title' => 'Test EN',
])
->save();
$this->queueFactory = $this->container
->get('queue');
$this->database = $this->container
->get('database');
$this->loggerChannel = $this
->prophesize(LoggerChannelInterface::class)
->reveal();
$this->purge = new Purge($this->queueFactory, $this->loggerChannel, $this->database);
}
public function createAcquiaContentHubAdminSettings() {
$admin_settings = $this->configFactory
->getEditable('acquia_contenthub.admin_settings');
return $admin_settings
->set('client_name', 'test-client')
->set('origin', '00000000-0000-0001-0000-123456789123')
->set('api_key', '12312321312321')
->set('secret_key', '12312321312321')
->set('hostname', 'https://example.com')
->set('shared_secret', '12312321312321')
->save();
}
public function testQueueAndTablePurge() {
$queue = $this->queueFactory
->get('acquia_contenthub_publish_export');
$this
->assertGreaterThan(0, $queue
->numberOfItems());
$this
->assertGreaterThan(0, $this
->getExportTableCount());
$request = Request::createFromGlobals();
$key = new Key('id', 'secret');
$payload = [
'crud' => 'purge',
'status' => 'successful',
];
$event = new HandleWebhookEvent($request, $payload, $key, $this->clientFactory
->getClient());
$this->purge
->onHandleWebhook($event);
$this
->assertEqual(0, $queue
->numberOfItems());
$this
->assertEqual(0, $this
->getExportTableCount());
}
protected function getExportTableCount() : int {
return $this->database
->select('acquia_contenthub_publisher_export_tracking', 'pet')
->fields('pet', [])
->countQuery()
->execute()
->fetchField();
}
}