View source
<?php
namespace Drupal\Tests\acquia_contenthub\Kernel;
use Acquia\ContentHubClient\CDF\CDFObject;
use Acquia\ContentHubClient\ContentHubClient;
use Acquia\ContentHubClient\Settings;
use Acquia\Hmac\Key;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Client\ClientFactory;
use Drupal\acquia_contenthub\Event\HandleWebhookEvent;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Symfony\Component\HttpFoundation\Request;
class ImportTrackingTest extends EntityKernelTestBase {
public static $modules = [
'user',
'file',
'node',
'field',
'depcalc',
'acquia_contenthub',
'acquia_contenthub_subscriber',
'acquia_contenthub_server_test',
];
protected $dispatcher;
protected $clientFactory;
protected $importQueue;
protected $settingsClientUuid = '00000000-0000-460b-ac74-b6bed08b4441';
protected $initiatorID = '00000000-abba-47f1-6a54-1e485293a301';
protected function setUp() {
parent::setUp();
$this
->installSchema('acquia_contenthub_subscriber', [
'acquia_contenthub_subscriber_import_tracking',
]);
$this->importQueue = $this->container
->get('acquia_contenthub_subscriber.acquia_contenthub_import_queue');
$this->dispatcher = $this->container
->get('event_dispatcher');
$cdf_object = $this
->getMockBuilder(CDFObject::class)
->disableOriginalConstructor()
->getMock();
$cdf_object
->method('getMetadata')
->willReturn([]);
$content_hub_settings = $this
->getMockBuilder(Settings::class)
->disableOriginalConstructor()
->getMock();
$content_hub_settings
->method('getUuid')
->willReturn($this->settingsClientUuid);
$content_hub_client = $this
->getMockBuilder(ContentHubClient::class)
->disableOriginalConstructor()
->getMock();
$content_hub_client
->method('getSettings')
->willReturn($content_hub_settings);
$content_hub_client
->method('getEntity')
->willReturn($cdf_object);
$client_factory = $this
->getMockBuilder(ClientFactory::class)
->disableOriginalConstructor()
->getMock();
$client_factory
->method('getClient')
->willReturn($content_hub_client);
$this->clientFactory = $client_factory;
}
public function testImportQueueOperations() {
$this
->assertEquals(0, $this->importQueue
->getQueueCount(), 'By default import queue should be empty.');
$payload = [
'status' => 'successful',
'crud' => 'update',
'assets' => [
[
'uuid' => '00000000-0001-460b-ac74-b6bed08b4441',
'type' => 'drupal8_content_entity',
],
[
'uuid' => '00000000-0002-4be4-8bf0-c86d895721e9',
'type' => 'drupal8_config_entity',
],
],
'initiator' => $this->initiatorID,
];
$this
->invokeImportUpdateAssets($payload);
$this
->assertEquals(1, $this->importQueue
->getQueueCount());
$payload = [
'status' => 'successful',
'crud' => 'update',
'assets' => [
[
'uuid' => '00000000-0003-460b-ac74-b6bed08b4441',
'type' => 'drupal8_content_entity',
],
],
'initiator' => $this->initiatorID,
];
$this
->invokeImportUpdateAssets($payload);
$this
->assertEquals(2, $this->importQueue
->getQueueCount());
}
public function testTrackingWithWrongAssetType() {
$payload = [
'status' => 'successful',
'crud' => 'update',
'assets' => [
[
'uuid' => '00000000-0003-460b-ac74-b6bed08b4441',
'type' => 'unknown_entity_type',
],
],
'initiator' => $this->initiatorID,
];
$this
->invokeImportUpdateAssets($payload);
$this
->assertEquals(0, $this->importQueue
->getQueueCount());
}
public function testTrackingWithEmptyAssets() {
$payload = [
'status' => 'successful',
'crud' => 'update',
'assets' => [],
'initiator' => $this->initiatorID,
];
$this
->invokeImportUpdateAssets($payload);
$this
->assertEquals(0, $this->importQueue
->getQueueCount());
}
public function testTrackingWhenInitiatorAndAssetUuidIsIdentical() {
$payload = [
'status' => 'successful',
'crud' => 'update',
'assets' => [
[
'uuid' => '00000000-0003-460b-ac74-b6bed08b4441',
'type' => 'drupal8_content_entity',
],
],
'initiator' => $this->settingsClientUuid,
];
$this
->invokeImportUpdateAssets($payload);
$this
->assertEquals(0, $this->importQueue
->getQueueCount());
}
public function testTrackingWithWrongPayloadStatus() {
$payload = [
'status' => 'any_status_except_successful',
'crud' => 'update',
'assets' => [],
'initiator' => $this->initiatorID,
];
$this
->invokeImportUpdateAssets($payload);
$this
->assertEquals(0, $this->importQueue
->getQueueCount());
}
protected function invokeImportUpdateAssets(array $payload) {
$request = Request::createFromGlobals();
$key = new Key('id', 'secret');
$event = new HandleWebhookEvent($request, $payload, $key, $this->clientFactory
->getClient());
$this->dispatcher
->dispatch(AcquiaContentHubEvents::HANDLE_WEBHOOK, $event);
}
}