View source
<?php
namespace Drupal\Tests\acquia_contenthub\Unit\Controller;
use Drupal\acquia_contenthub\Controller\ContentHubExportQueueController;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
class ContentHubExportQueueControllerTest extends UnitTestCase {
protected $container;
protected $configFactory;
protected $queueFactory;
protected $queueManager;
protected $contentHubExportQueueController;
protected $configEntity = [
'dependency_depth' => 3,
'user_role' => AccountInterface::ANONYMOUS_ROLE,
'export_queue_entities_per_item' => 1,
'export_queue_batch_size' => 1,
'export_queue_waiting_time' => 5,
];
protected function setUp() : void {
$this->container = $this
->createMock('Drupal\\Core\\DependencyInjection\\Container');
$logger_factory = $this
->createMock(LoggerChannelFactoryInterface::class);
$logger_factory
->method('get')
->with('acquia_contenthub')
->willReturn($this
->createMock(LoggerChannelInterface::class));
\Drupal::setContainer($this->container);
$this->configFactory = $this
->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
$this->configFactory
->method('get')
->with('acquia_contenthub.entity_config')
->willReturn($this
->createMockForContentHubEntityConfig());
$this->queueFactory = $this
->getMockBuilder('Drupal\\Core\\Queue\\QueueFactory')
->disableOriginalConstructor()
->getMock();
$this->queueManager = $this
->getMockBuilder('Drupal\\Core\\Queue\\QueueWorkerManager')
->disableOriginalConstructor()
->getMock();
$this->contentHubExportQueueController = new ContentHubExportQueueController($this->queueFactory, $this->queueManager, $this->configFactory, $logger_factory);
}
public function testGetQueueCount() {
$queue = $this
->createMock('Drupal\\Core\\Queue\\QueueInterface');
$queue
->method('numberOfItems')
->willReturn(100);
$this->queueFactory
->method('get')
->with('acquia_contenthub_export_queue')
->willReturn($queue);
$count = $this->contentHubExportQueueController
->getQueueCount();
$this
->assertEquals($count, 100);
}
public function testGetWaitingTime() {
$waiting_time = $this->contentHubExportQueueController
->getWaitingTime();
$this
->assertEquals($waiting_time, 5);
}
public function testEnqueueExportEntities() {
$queued_items = [
$this
->createQueueItem('node', 1, '00000000-0000-0000-0000-000000000000'),
$this
->createQueueItem('node', 2, '00000000-0000-1111-0000-000000000000'),
$this
->createQueueItem('node', 3, '00000000-0000-2222-0000-000000000000'),
$this
->createQueueItem('node', 4, '00000000-0000-3333-0000-000000000000'),
$this
->createQueueItem('node', 5, '00000000-0000-4444-0000-000000000000'),
$this
->createQueueItem('node', 6, '00000000-0000-5555-0000-000000000000'),
$this
->createQueueItem('node', 7, '00000000-0000-6666-0000-000000000000'),
];
$queue = $this
->createMock('Drupal\\Core\\Queue\\QueueInterface');
$queue
->method('numberOfItems')
->willReturn(100);
$queue
->expects($this
->at(0))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[0];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$queue
->expects($this
->at(1))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[1];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$queue
->expects($this
->at(2))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[2];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$queue
->expects($this
->at(3))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[3];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$queue
->expects($this
->at(4))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[4];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$queue
->expects($this
->at(5))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[5];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$queue
->expects($this
->at(6))
->method('createItem')
->willReturnCallback(function ($item) use ($queued_items) {
$item = (array) $item;
$queued_item = (array) $queued_items[6];
$this
->assertEquals($item, $queued_item);
return $item == $queued_item ? 1 : FALSE;
});
$this->queueFactory
->method('get')
->with('acquia_contenthub_export_queue')
->willReturn($queue);
$candidate_entities = [
'00000000-0000-0000-0000-000000000000' => $this
->createMockForContentEntity('node', 1, '00000000-0000-0000-0000-000000000000'),
'00000000-0000-1111-0000-000000000000' => $this
->createMockForContentEntity('node', 2, '00000000-0000-1111-0000-000000000000'),
'00000000-0000-2222-0000-000000000000' => $this
->createMockForContentEntity('node', 3, '00000000-0000-2222-0000-000000000000'),
'00000000-0000-3333-0000-000000000000' => $this
->createMockForContentEntity('node', 4, '00000000-0000-3333-0000-000000000000'),
'00000000-0000-4444-0000-000000000000' => $this
->createMockForContentEntity('node', 5, '00000000-0000-4444-0000-000000000000'),
'00000000-0000-5555-0000-000000000000' => $this
->createMockForContentEntity('node', 6, '00000000-0000-5555-0000-000000000000'),
'00000000-0000-6666-0000-000000000000' => $this
->createMockForContentEntity('node', 7, '00000000-0000-6666-0000-000000000000'),
];
$this->contentHubExportQueueController
->enqueueExportEntities($candidate_entities);
}
protected function createMockForContentHubEntityConfig() {
$contenthub_entity_config = $this
->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
->disableOriginalConstructor()
->setMethods([
'get',
])
->getMockForAbstractClass();
$contenthub_entity_config
->method('get')
->willReturnCallback(function ($argument) {
if (isset($this->configEntity[$argument])) {
return $this->configEntity[$argument];
}
return NULL;
});
return $contenthub_entity_config;
}
protected function createMockForContentEntity($entity_type_id, $id, $uuid) {
$enabled_methods = [
'getEntityTypeId',
'id',
'uuid',
'getFields',
];
$content_entity_mock = $this
->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
->disableOriginalConstructor()
->setMethods($enabled_methods)
->getMockForAbstractClass();
$content_entity_mock
->method('getEntityTypeId')
->willReturn($entity_type_id);
$content_entity_mock
->method('id')
->willReturn($id);
$content_entity_mock
->method('uuid')
->willReturn($uuid);
$content_entity_mock
->method('getFields')
->willReturn([]);
return $content_entity_mock;
}
protected function createQueueItem($entity_type, $entity_id, $entity_uuid) {
return (object) [
'data' => [
0 => [
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'entity_uuid' => $entity_uuid,
],
],
];
}
}