class ContentHubReindexTest in Acquia Content Hub 8
@coversDefaultClass \Drupal\acquia_contenthub\Controller\ContentHubReindex
@group acquia_contenthub
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\acquia_contenthub\Unit\Controller\ContentHubReindexTest
Expanded class hierarchy of ContentHubReindexTest
File
- tests/
src/ Unit/ Controller/ ContentHubReindexTest.php, line 15
Namespace
Drupal\Tests\acquia_contenthub\Unit\ControllerView source
class ContentHubReindexTest extends UnitTestCase {
/**
* The Reindex State.
*
* @var string
*/
protected $reindexState;
/**
* Number of Entities to Reindex.
*
* @var int
*/
protected $numEntities;
/**
* Drupal State.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* The Translation Interface.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationInterface;
/**
* The Entity Type Manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The Entity Storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $entityStorage;
/**
* The dependency injection container.
*
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
*/
protected $container;
/**
* Content Hub Client Manager.
*
* @var \Drupal\acquia_contenthub\Client\ClientManager
*/
protected $clientManager;
/**
* Content Hub Entity Manager.
*
* @var \Drupal\acquia_contenthub\EntityManager
*/
protected $entityManager;
/**
* The Content Hub Entities Tracking Service.
*
* @var \Drupal\acquia_contenthub\ContentHubEntitiesTracking
*/
protected $contentHubEntitiesTracking;
/**
* The Content Hub Reindex Service.
*
* @var \Drupal\acquia_contenthub\Controller\ContentHubReindex
*/
protected $contentHubReindex;
/**
* {@inheritdoc}
*/
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();
// Setting up Entity Type Manager.
$this->entityTypeManager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$this->entityStorage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$this->entityTypeManager
->method('getStorage')
->willReturn($this->entityStorage);
// Setting up the Container.
$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();
// Defining a certain number of entities to be reindexed.
$this->numEntities = 5;
$this->contentHubEntitiesTracking
->method('getCountEntitiesToReindex')
->willReturn($this->numEntities);
// Creating the Test Object.
$this->contentHubReindex = new ContentHubReindex($this->contentHubEntitiesTracking, $this->clientManager, $this->state);
}
/**
* Obtains entities sample data.
*
* @return array
* An array of entities.
*/
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,
],
];
}
/**
* Test the isReindexNone function.
*
* @covers ::isReindexNone
*/
public function testIsReindexNone() {
$this->reindexState = ContentHubReindex::REINDEX_NONE;
$this
->assertTrue($this->contentHubReindex
->isReindexNone());
}
/**
* Test the setReindexStateNone function.
*
* @covers ::setReindexStateNone
*/
public function testSetReindexStateNone() {
$this->reindexState = ContentHubReindex::REINDEX_FINISHED;
$this->contentHubReindex
->setReindexStateNone();
$this
->assertTrue($this->contentHubReindex
->isReindexNone());
}
/**
* Test the isReindexSent function.
*
* @covers ::isReindexSent
*/
public function testIsReindexSent() {
$this->reindexState = ContentHubReindex::REINDEX_SENT;
$this
->assertTrue($this->contentHubReindex
->isReindexSent());
}
/**
* Test the setReindexStateSent function.
*
* @covers ::setReindexStateSent
*/
public function testSetReindexStateSent() {
$this->reindexState = ContentHubReindex::REINDEX_NONE;
$this->contentHubReindex
->setReindexStateSent();
$this
->assertTrue($this->contentHubReindex
->isReindexSent());
}
/**
* Test the isReindexFailed function.
*
* @covers ::isReindexFailed
*/
public function testIsReindexFailed() {
$this->reindexState = ContentHubReindex::REINDEX_FAILED;
$this
->assertTrue($this->contentHubReindex
->isReindexFailed());
}
/**
* Test the setReindexStateFailed function.
*
* @covers ::setReindexStateFailed
*/
public function testSetReindexStateFailed() {
$this->reindexState = ContentHubReindex::REINDEX_NONE;
$this->contentHubReindex
->setReindexStateFailed();
$this
->assertTrue($this->contentHubReindex
->isReindexFailed());
}
/**
* Test the isReindexFinished function.
*
* @covers ::isReindexFinished
*/
public function testIsReindexFinished() {
$this->reindexState = ContentHubReindex::REINDEX_FINISHED;
$this
->assertTrue($this->contentHubReindex
->isReindexFinished());
}
/**
* Test the setReindexStateFinished function.
*
* @covers ::setReindexStateFinished
*/
public function testSetReindexStateFinished() {
$this->reindexState = ContentHubReindex::REINDEX_NONE;
$this->contentHubReindex
->setReindexStateFinished();
$this
->assertTrue($this->contentHubReindex
->isReindexFinished());
}
/**
* Test the getCountReExportEntities function.
*
* @covers ::getCountReExportEntities
*/
public function testGetCountReExportEntities() {
$entities = $this->contentHubReindex
->getCountReExportEntities();
$this
->assertEquals($this->numEntities, $entities);
}
/**
* Test the getReExportEntities function.
*
* @covers ::getReExportEntities
*/
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);
}
/**
* Test the setExportedEntitiesToReindex function.
*
* @covers ::setExportedEntitiesToReindex
*/
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,
]);
// Initially the system is not set to reindex.
$this
->assertTrue($this->contentHubReindex
->isReindexNone());
$this->contentHubReindex
->setExportedEntitiesToReindex('node');
$this
->assertTrue($this->contentHubReindex
->isReindexSent());
}
/**
* Test the reExportEntities function.
*
* @covers ::reExportEntities
*/
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());
}
/**
* Test the getExportedEntitiesNotOwnedByThisSite function.
*
* @covers ::getExportedEntitiesNotOwnedByThisSite
*/
public function testGetExportedEntitiesNotOwnedByThisSite() {
$origin = '00000000-0000-0000-0000-000000000000';
$entity_type_id = 'media';
$this->contentHubEntitiesTracking
->expects($this
->once())
->method('getSiteOrigin')
->willReturn($origin);
// Some entities do not belong to this origin.
$entities = [
'success' => TRUE,
'data' => [
// Entities owned by this site origin.
[
'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,
],
// Entities that do not belong to this site origin.
[
'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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentHubReindexTest:: |
protected | property | Content Hub Client Manager. | |
ContentHubReindexTest:: |
protected | property | The dependency injection container. | |
ContentHubReindexTest:: |
protected | property | The Content Hub Entities Tracking Service. | |
ContentHubReindexTest:: |
protected | property | The Content Hub Reindex Service. | |
ContentHubReindexTest:: |
protected | property | Content Hub Entity Manager. | |
ContentHubReindexTest:: |
protected | property | The Entity Storage. | |
ContentHubReindexTest:: |
protected | property | The Entity Type Manager. | |
ContentHubReindexTest:: |
protected | property | Number of Entities to Reindex. | |
ContentHubReindexTest:: |
protected | property | The Reindex State. | |
ContentHubReindexTest:: |
protected | property | Drupal State. | |
ContentHubReindexTest:: |
protected | property | The Translation Interface. | |
ContentHubReindexTest:: |
private | function | Obtains entities sample data. | |
ContentHubReindexTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ContentHubReindexTest:: |
public | function | Test the getCountReExportEntities function. | |
ContentHubReindexTest:: |
public | function | Test the getExportedEntitiesNotOwnedByThisSite function. | |
ContentHubReindexTest:: |
public | function | Test the getReExportEntities function. | |
ContentHubReindexTest:: |
public | function | Test the isReindexFailed function. | |
ContentHubReindexTest:: |
public | function | Test the isReindexFinished function. | |
ContentHubReindexTest:: |
public | function | Test the isReindexNone function. | |
ContentHubReindexTest:: |
public | function | Test the isReindexSent function. | |
ContentHubReindexTest:: |
public | function | Test the reExportEntities function. | |
ContentHubReindexTest:: |
public | function | Test the setExportedEntitiesToReindex function. | |
ContentHubReindexTest:: |
public | function | Test the setReindexStateFailed function. | |
ContentHubReindexTest:: |
public | function | Test the setReindexStateFinished function. | |
ContentHubReindexTest:: |
public | function | Test the setReindexStateNone function. | |
ContentHubReindexTest:: |
public | function | Test the setReindexStateSent function. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |