View source
<?php
namespace Drupal\Tests\acquia_contenthub\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Prophecy\Argument;
class ImportQueueWorkerLoggingTest extends UnserializationTest {
public static $modules = [
'dblog',
];
protected function setUp() {
parent::setUp();
$this
->installSchema('dblog', [
'watchdog',
]);
}
public function testImporQueueWorkerLogging() {
$error_msg = 'Some error from service.';
$this->contentHubClient
->getInterestsByWebhook(Argument::type('string'))
->willThrow(new \Exception($error_msg));
$this
->runImportQueueWorker([
self::CLIENT_UUID_1,
]);
$this
->assertLogMessage('acquia_contenthub_subscriber', sprintf('Following error occurred while we were trying to get the interest list: %s', $error_msg));
$this->contentHubClient
->getInterestsByWebhook(Argument::type('string'))
->willReturn([]);
$this
->runImportQueueWorker([
self::CLIENT_UUID_1,
]);
$message = sprintf('Skipped importing the following missing entities: %s. This occurs when entities are deleted at the Publisher before importing.', self::CLIENT_UUID_1);
$this
->assertLogMessage('acquia_contenthub_subscriber', $message);
$this->contentHubClient
->getInterestsByWebhook(Argument::type('string'))
->willReturn([]);
$this
->runImportQueueWorker([]);
$this
->assertLogMessage('acquia_contenthub_subscriber', 'There are no matching entities in the queues and the site interest list.');
$cdf_document = $this
->createCDFDocumentFromFixture('view_modes.json');
$this->contentHubClient
->getEntities([
self::CLIENT_UUID_1 => self::CLIENT_UUID_1,
])
->willReturn($cdf_document);
$this->contentHubClient
->getInterestsByWebhook(Argument::type('string'))
->willReturn([
'fefd7eda-4244-4fe4-b9b5-b15b89c61aa8',
]);
$this->contentHubClient
->addEntitiesToInterestList(Argument::any(), Argument::any())
->willReturn();
$this
->runImportQueueWorker([
self::CLIENT_UUID_1,
]);
$this
->assertLogMessage('acquia_contenthub_subscriber', 'The following imported entities have been added to the interest ');
$this->contentHubClient
->getEntities([
self::CLIENT_UUID_1 => self::CLIENT_UUID_1,
])
->willReturn($cdf_document);
$this->contentHubClient
->getInterestsByWebhook(Argument::type('string'))
->willReturn([
'fefd7eda-4244-4fe4-b9b5-b15b89c61aa8',
]);
$this->contentHubClient
->addEntitiesToInterestList(Argument::any(), Argument::any())
->willThrow(new \Exception('error'));
$this
->runImportQueueWorker([
self::CLIENT_UUID_1,
]);
$this
->assertLogMessage('acquia_contenthub_subscriber', 'Error adding the following entities to the interest list for webhook');
}
protected function runImportQueueWorker(array $uuids) {
$item = new \stdClass();
$item->uuids = implode(', ', $uuids);
$this->contentHubImportQueueWorker
->processItem($item);
}
public function assertLogMessage(string $type, string $message) {
$count = Database::getConnection()
->select('watchdog', 'w')
->condition('type', $type)
->condition('message', '%' . $message . '%', 'LIKE')
->countQuery()
->execute()
->fetchField();
$this
->assertTrue($count > 0, new FormattableMarkup('watchdog table contains @count rows for @message', [
'@count' => $count,
'@message' => new FormattableMarkup($message, []),
]));
}
}