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\UpdatePublished;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Symfony\Component\HttpFoundation\Request;
class UpdatePublishedTest extends EntityKernelTestBase {
protected $updatePublished;
protected $clientFactory;
protected $configFactory;
protected $database;
protected $node;
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');
$this->node = Node::create([
'type' => 'article',
'title' => 'Test EN',
'uuid' => '98213529-0000-0001-0000-123456789123',
]);
$this->node
->save();
$this->database = $this->container
->get('database');
$this->updatePublished = new UpdatePublished($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 testUpdatePublished(...$args) {
$key = new Key('id', 'secret');
$request = Request::createFromGlobals();
$payload = [
'crud' => 'update',
'status' => 'successful',
'initiator' => $this->clientFactory
->getSettings()
->getUuid(),
'assets' => [
[
'type' => $args[0],
'uuid' => $args[1],
],
],
];
$event = new HandleWebhookEvent($request, $payload, $key, $this->clientFactory
->getClient());
$this->updatePublished
->onHandleWebhook($event);
$entity_status = $this
->getStatusByUuid($args[1]);
$this
->assertEqual($args[2], $entity_status);
}
public function dataProvider() : array {
return [
[
'test_entity',
'',
'',
],
[
'drupal8_content_entity',
'98213529-0000-0001-0000-123456789123',
'confirmed',
],
];
}
protected function getStatusByUuid(string $uuid) : string {
return $this->database
->select('acquia_contenthub_publisher_export_tracking', 'acpet')
->fields('acpet', [
'status',
])
->condition('acpet.entity_uuid', $uuid)
->execute()
->fetchField();
}
}