View source
<?php
namespace Drupal\Tests\acquia_contenthub\Unit\Controller;
use Drupal\acquia_contenthub\Controller\ContentHubReindex;
use Drupal\Core\State\State;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\State\StateInterface;
class ContentHubReindexTest extends UnitTestCase {
protected $reindexState;
protected $numEntities;
protected $state;
protected $translationInterface;
protected $entityTypeManager;
protected $entityStorage;
protected $container;
protected $clientManager;
protected $entityManager;
protected $contentHubEntitiesTracking;
protected $contentHubReindex;
protected function setUp() : void {
parent::setUp();
$this->state = $this
->createMock('Drupal\\Core\\State\\StateInterface');
$this->state
->method('get')
->willReturnCallback(function ($name, $value) {
if ($name == ContentHubReindex::REINDEXING_STATE) {
if (!empty($this->reindexState)) {
return $this->reindexState;
}
return $value;
}
return NULL;
});
$this->state
->method('set')
->willReturnCallback(function ($name, $value) {
if ($name === ContentHubReindex::REINDEXING_STATE) {
$this->reindexState = $value;
}
else {
$this->reindexState = ContentHubReindex::REINDEX_NONE;
}
return $this->reindexState;
});
$this->translationInterface = $this
->createMock('Drupal\\Core\\StringTranslation\\TranslationInterface');
$this->entityManager = $this
->getMockBuilder('Drupal\\acquia_contenthub\\EntityManager')
->disableOriginalConstructor()
->getMock();
$this->entityTypeManager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$this->entityStorage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$this->entityTypeManager
->method('getStorage')
->willReturn($this->entityStorage);
$this->container = $this
->createMock('Drupal\\Core\\DependencyInjection\\Container');
$this->container
->method('get')
->willReturnCallback(function ($name) {
switch ($name) {
case 'state':
return $this->state;
case 'acquia_contenthub.acquia_contenthub_reindex':
return $this->contentHubReindex;
case 'acquia_contenthub.entity_manager':
return $this->entityManager;
case 'entity_type.manager':
return $this->entityTypeManager;
case 'string_translation':
return $this->translationInterface;
}
return NULL;
});
\Drupal::setContainer($this->container);
$this->clientManager = $this
->createMock('\\Drupal\\acquia_contenthub\\Client\\ClientManagerInterface');
$this->contentHubEntitiesTracking = $this
->getMockBuilder('Drupal\\acquia_contenthub\\ContentHubEntitiesTracking')
->disableOriginalConstructor()
->getMock();
$this->numEntities = 5;
$this->contentHubEntitiesTracking
->method('getCountEntitiesToReindex')
->willReturn($this->numEntities);
$this->contentHubReindex = new ContentHubReindex($this->contentHubEntitiesTracking, $this->clientManager, $this->state);
}
private function getEntitiesData() {
return [
(object) [
'entity_uuid' => '00000000-0000-0000-0000-000000000000',
'entity_type' => 'node',
'entity_id' => 1,
],
(object) [
'entity_uuid' => '00000000-0000-0000-1111-000000000000',
'entity_type' => 'node',
'entity_id' => 2,
],
(object) [
'entity_uuid' => '00000000-0000-0000-2222-000000000000',
'entity_type' => 'node',
'entity_id' => 3,
],
(object) [
'entity_uuid' => '00000000-0000-0000-3333-000000000000',
'entity_type' => 'node',
'entity_id' => 4,
],
(object) [
'entity_uuid' => '00000000-0000-0000-4444-000000000000',
'entity_type' => 'node',
'entity_id' => 5,
],
];
}
public function testIsReindexNone() {
$this->reindexState = ContentHubReindex::REINDEX_NONE;
$this
->assertTrue($this->contentHubReindex
->isReindexNone());
}
public function testSetReindexStateNone() {
$this->reindexState = ContentHubReindex::REINDEX_FINISHED;
$this->contentHubReindex
->setReindexStateNone();
$this
->assertTrue($this->contentHubReindex
->isReindexNone());
}
public function testIsReindexSent() {
$this->reindexState = ContentHubReindex::REINDEX_SENT;
$this
->assertTrue($this->contentHubReindex
->isReindexSent());
}
public function testSetReindexStateSent() {
$this->reindexState = ContentHubReindex::REINDEX_NONE;
$this->contentHubReindex
->setReindexStateSent();
$this
->assertTrue($this->contentHubReindex
->isReindexSent());
}
public function testIsReindexFailed() {
$this->reindexState = ContentHubReindex::REINDEX_FAILED;
$this
->assertTrue($this->contentHubReindex
->isReindexFailed());
}
public function testSetReindexStateFailed() {
$this->reindexState = ContentHubReindex::REINDEX_NONE;
$this->contentHubReindex
->setReindexStateFailed();
$this
->assertTrue($this->contentHubReindex
->isReindexFailed());
}
public function testIsReindexFinished() {
$this->reindexState = ContentHubReindex::REINDEX_FINISHED;
$this
->assertTrue($this->contentHubReindex
->isReindexFinished());
}
public function testSetReindexStateFinished() {
$this->reindexState = ContentHubReindex::REINDEX_NONE;
$this->contentHubReindex
->setReindexStateFinished();
$this
->assertTrue($this->contentHubReindex
->isReindexFinished());
}
public function testGetCountReExportEntities() {
$entities = $this->contentHubReindex
->getCountReExportEntities();
$this
->assertEquals($this->numEntities, $entities);
}
public function testGetReExportEntities() {
$entities = $this
->getEntitiesData();
$offset = 1;
$limit = 3;
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('getEntitiesToReindex')
->willReturn($entities);
$result = $this->contentHubReindex
->getReExportEntities($offset, $limit);
$this
->assertEquals(array_slice($entities, $offset, $limit), $result);
}
public function testSetExportedEntitiesToReindex() {
$entities = $this
->getEntitiesData();
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('setExportedEntitiesForReindex')
->with('node')
->willReturn(TRUE);
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('getEntitiesToReindex')
->willReturn($entities);
foreach ($entities as $key => $entity) {
$this->clientManager
->expects($this
->at($key))
->method('createRequest')
->with('deleteEntity', [
$entity->entity_uuid,
])
->willReturn(TRUE);
}
$this->clientManager
->expects($this
->at(count($entities)))
->method('createRequest')
->with('reindex')
->willReturn([
'success' => TRUE,
]);
$this
->assertTrue($this->contentHubReindex
->isReindexNone());
$this->contentHubReindex
->setExportedEntitiesToReindex('node');
$this
->assertTrue($this->contentHubReindex
->isReindexSent());
}
public function testReExportEntities() {
$limit = 3;
$entities_list = $this
->getEntitiesData();
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('getEntitiesToReindex')
->willReturn($entities_list);
$entity = $this
->createMock('Drupal\\Core\\Entity\\EntityInterface');
foreach ($entities_list as $key => $entity_data) {
if ($key < $limit) {
$this->entityStorage
->expects($this
->at($key))
->method('load')
->with($entity_data->entity_id)
->willReturn($entity);
$this->entityManager
->expects($this
->at($key))
->method('enqueueCandidateEntity')
->with($entity)
->willReturn(NULL);
}
}
$context = [];
ContentHubReindex::reExportEntities($limit, $context);
$expected_context = [
'sandbox' => [
'progress' => 3,
'max' => 5,
],
'results' => [
0 => 1,
1 => 2,
2 => 3,
],
'message' => 'Exporting entities: @entities',
];
$this
->assertEquals($expected_context['sandbox'], $context['sandbox']);
$this
->assertEquals($expected_context['results'], $context['results']);
$this
->assertEquals($expected_context['message'], $context['message']
->getUntranslatedString());
}
public function testGetExportedEntitiesNotOwnedByThisSite() {
$origin = '00000000-0000-0000-0000-000000000000';
$entity_type_id = 'media';
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('getSiteOrigin')
->willReturn($origin);
$entities = [
'success' => TRUE,
'data' => [
[
'uuid' => '00000000-0000-0000-1111-000000000000',
'origin' => $origin,
'modified' => '2017-09-07T22:45:41+00:00',
'type' => $entity_type_id,
],
[
'uuid' => '00000000-0000-0000-2222-000000000000',
'origin' => $origin,
'modified' => '2017-09-07T22:45:41+00:00',
'type' => $entity_type_id,
],
[
'uuid' => '00000000-0000-0000-3333-000000000000',
'origin' => $origin,
'modified' => '2017-09-07T22:45:41+00:00',
'type' => $entity_type_id,
],
[
'uuid' => '00000000-0000-0000-4444-000000000000',
'origin' => '11111111-0000-0000-0000-000000000000',
'modified' => '2017-09-07T22:45:41+00:00',
'type' => $entity_type_id,
],
[
'uuid' => '00000000-0000-0000-5555-000000000000',
'origin' => '22222222-0000-0000-0000-000000000000',
'modified' => '2017-09-07T22:45:41+00:00',
'type' => $entity_type_id,
],
[
'uuid' => '00000000-0000-0000-6666-000000000000',
'origin' => '33333333-0000-0000-0000-000000000000',
'modified' => '2017-09-07T22:45:41+00:00',
'type' => $entity_type_id,
],
],
];
$options = [
'type' => $entity_type_id,
];
$this->clientManager
->expects($this
->once())
->method('createRequest')
->with('listEntities', [
$options,
])
->willReturn($entities);
$external_ownership = $this->contentHubReindex
->getExportedEntitiesNotOwnedByThisSite($entity_type_id);
$expected_output = array_slice($entities['data'], 3, 3);
$this
->assertEquals($expected_output, $external_ownership);
}
}