View source
<?php
namespace Drupal\Tests\acquia_contenthub\Kernel;
use Acquia\ContentHubClient\ContentHubClient;
use Acquia\ContentHubClient\Settings;
use Drupal\acquia_contenthub\Client\ClientFactory;
use Drupal\acquia_contenthub_publisher\Controller\StatusReportController;
use Drupal\acquia_contenthub_test\MockDataProvider;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Symfony\Component\HttpFoundation\Request;
class ContentHubStatusReportTest extends EntityKernelTestBase {
public static $modules = [
'depcalc',
'acquia_contenthub',
'acquia_contenthub_test',
'acquia_contenthub_publisher',
'user',
];
protected $clientFactory;
protected $client;
protected function setUp() {
parent::setUp();
$settings = $this
->prophesize(Settings::class);
$settings
->getUuid()
->willReturn('3a89ff1b-8869-419d-b931-f2282aca3e88');
$settings
->getName()
->willReturn('foo');
$settings
->getUrl()
->willReturn('http://www.example.com');
$settings
->getApiKey()
->willReturn('apikey');
$settings
->getSecretKey()
->willReturn('apisecret');
$this->client = $this
->prophesize(ContentHubClient::class);
$clientFactory = $this
->prophesize(ClientFactory::class);
$clientFactory
->getSettings()
->willReturn($settings
->reveal());
$this->client
->getSettings()
->willReturn($settings
->reveal());
$this->clientFactory = $clientFactory;
}
public function testContentHubStatusReportWithNoClients() {
$options = [
"from" => 0,
"query" => [
"bool" => [
"filter" => [
[
"term" => [
"data.type" => 'client',
],
],
],
],
],
"size" => 50,
"sort" => [
"data.modified" => "desc",
],
];
$this->client
->searchEntity($options)
->willReturn([]);
$this->clientFactory
->getClient()
->willReturn($this->client
->reveal());
$this->container
->set('acquia_contenthub.client.factory', $this->clientFactory
->reveal());
$controller = new StatusReportController($this->container
->get('acquia_contenthub.client.factory'), $this->container
->has('pager.manager') ? $this->container
->get('pager.manager') : NULL);
$request = new Request();
$build = $controller
->statusReportPage($request);
$this
->assertFalse(array_key_exists(0, $build[0]['clients_table']), 'Empty Search Response returns no data.');
}
public function testContentHubStatusReportWithClients() {
$options = [
"from" => 0,
"query" => [
"bool" => [
"filter" => [
[
"term" => [
"data.type" => 'client',
],
],
],
],
],
"size" => 50,
"sort" => [
"data.modified" => "desc",
],
];
$this->client
->searchEntity($options)
->willReturn(MockDataProvider::searchResponse());
$this->client
->getInterestsByWebhook('00000000-55aa-42f5-50d1-2e35b72ae26d')
->willReturn([]);
$this->client
->getInterestsByWebhook('00000000-4b0b-4c99-5b1d-0177597c2ca7')
->willReturn([
'00000000-12bc-442f-46f5-d2694d553429',
]);
$this->client
->getInterestsByWebhook('00000000-72b6-4df8-710b-59790112588e')
->willReturn([
'00000000-42bf-4860-6d03-4e3411ee32b4',
]);
$this->client
->getInterestsByWebhook('00000000-5ac3-4e9f-7fe9-776b56a389c0')
->willReturn([
'00000000-9987-4b2a-74b9-d758c8b60d12',
]);
$this->clientFactory
->getClient()
->willReturn($this->client
->reveal());
$this->container
->set('acquia_contenthub.client.factory', $this->clientFactory
->reveal());
$controller = new StatusReportController($this->container
->get('acquia_contenthub.client.factory'), $this->container
->has('pager.manager') ? $this->container
->get('pager.manager') : NULL);
$request = new Request();
$build = $controller
->statusReportPage($request);
$r = new \ReflectionClass($controller);
$method = $r
->getMethod('getTimeAgo');
$method
->setAccessible(TRUE);
$last_updated_time = $method
->invoke($controller, 1483228800);
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][3]['name']['#markup']) === 'pub', 'Publisher contains correct name.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][3]['type']['#markup']) === 'Publisher', 'Publisher contains expected type.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][3]['uuid']['#markup']) === 'http://pubonline.example.com', 'Publisher webhook domain is correct.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][3]['status']['#markup']) === 'In Progress', 'Publisher status is correct.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][3]['percent_exported']['#markup']) === '86%', 'Publisher contains expected publisher export percent.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][3]['percent_imported']['#markup']) === 'Not Available', 'Publisher correctly contains no imported data.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][3]['updated']['#markup']) === $last_updated_time, 'Publisher updated date is correct.');
$this
->assertTrue(strpos((string) $build[0]['clients_table'][3]['details']['#markup'], '00000000-9987-4b2a-74b9-d758c8b60d12') !== FALSE, 'Publisher contains correct uuid in more details link.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][2]['name']['#markup']) === 'sub', 'Subscriber contains correct name.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][2]['type']['#markup']) === 'Subscriber', 'Subscriber contains expected type.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][2]['uuid']['#markup']) === 'http://subonline.example.com', 'Subscriber webhook domain is correct.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][2]['status']['#markup']) === 'In Progress', 'Subscriber status is correct.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][2]['percent_exported']['#markup']) === 'Not Available', 'Subscriber contains expected publisher export percent.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][2]['percent_imported']['#markup']) === '60%', 'Subscriber correctly contains no imported data.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][2]['updated']['#markup']) === $last_updated_time, 'Subscriber updated date is correct.');
$this
->assertTrue(strpos((string) $build[0]['clients_table'][2]['details']['#markup'], '00000000-42bf-4860-6d03-4e3411ee32b4') !== FALSE, 'Subscriber contains correct uuid in more details link.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][1]['name']['#markup']) === 'pubsub', 'PubSub contains correct name.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][1]['type']['#markup']) === 'Publisher, Subscriber', 'PubSub contains expected type.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][1]['uuid']['#markup']) === 'http://pubsubonline.example.com', 'PubSub webhook domain is correct.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][1]['status']['#markup']) === 'In Progress', 'PubSub status is correct.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][1]['percent_exported']['#markup']) === '100%', 'PubSub contains expected publisher export percent.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][1]['percent_imported']['#markup']) === '92%', 'PubSub correctly contains no imported data.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][1]['updated']['#markup']) === $last_updated_time, 'PubSub updated date is correct.');
$this
->assertTrue(strpos((string) $build[0]['clients_table'][1]['details']['#markup'], '00000000-12bc-442f-46f5-d2694d553429') !== FALSE, 'PubSub contains correct uuid in more details link.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][0]['name']['#markup']) === 'localpubsub', 'Local client contains correct name.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][0]['type']['#markup']) === 'Publisher, Subscriber', 'Local client contains expected type.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][0]['uuid']['#markup']) === 'http://pubsub.example.com', 'Local client webhook domain is correct.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][0]['status']['#markup']) === 'Not Available', 'Local client status is correct.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][0]['percent_exported']['#markup']) === 'Not Available', 'Local client contains expected publisher export percent.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][0]['percent_imported']['#markup']) === 'Not Available', 'Local client correctly contains no imported data.');
$this
->assertTrue(strip_tags((string) $build[0]['clients_table'][0]['updated']['#markup']) === 'Not Available', 'Local client updated date is correct.');
$this
->assertTrue(strpos((string) $build[0]['clients_table'][0]['details']['#markup'], '00000000-00ab-489f-52fa-404bdf8df699') !== FALSE, 'Local client contains correct uuid in more details link.');
}
protected function getMarkupValue($element) : string {
return strip_tags((string) $element);
}
private function getClient() {
return clone $this->client;
}
}