View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_async\Functional;
use Drupal\node\NodeInterface;
use Drupal\Tests\entity_share_client\Functional\EntityShareClientFunctionalTestBase;
class EntityShareAsyncFunctionalTest extends EntityShareClientFunctionalTestBase {
public static $modules = [
'entity_share_async',
];
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
protected function setUp() {
parent::setUp();
$this
->postSetupFixture();
}
protected function getEntitiesDataArray() {
return [
'node' => [
'en' => [
'es_test' => $this
->getCompleteNodeInfos([
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_not_asynced' => $this
->getCompleteNodeInfos([
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
],
],
];
}
public function testAsync() {
$queue_factory = $this->container
->get('queue');
$state_storage = $this->container
->get('state');
$channel_id = static::$entityTypeId . '_' . static::$entityBundleId . '_' . static::$entityLangcode;
$queue_helper = $this->container
->get('entity_share_async.queue_helper');
$queue_helper
->enqueue($this->remote
->id(), $channel_id, [
'es_test',
]);
$queue = $queue_factory
->get('entity_share_async_import');
$this
->assertEquals(1, $queue
->numberOfItems(), 'The entity had been enqueued.');
$async_states = $state_storage
->get('entity_share_async.states', []);
$this
->assertTrue(isset($async_states[$this->remote
->id()][$channel_id]['es_test']), 'The entity is marked for syncing.');
$entity_id = $this
->getEntityId('node', 'es_test');
$this
->assertEmpty($entity_id, 'The node with the UUID es_test had been deleted.');
$entity_id = $this
->getEntityId('node', 'es_test_not_asynced');
$this
->assertEmpty($entity_id, 'The node with the UUID es_test_not_asynced had been deleted.');
$this->container
->get('cron')
->run();
$queue = $queue_factory
->get('entity_share_async_import');
$this
->assertEquals(0, $queue
->numberOfItems(), 'The entity had been processed by the queue.');
$async_states = $state_storage
->get('entity_share_async.states', []);
$this
->assertFalse(isset($async_states[$this->remote
->id()][$channel_id]['es_test']), 'The entity is no more marked for syncing.');
$entity_id = $this
->getEntityId('node', 'es_test');
$this
->assertNotEmpty($entity_id, 'The node with the UUID es_test has been imported.');
$entity_id = $this
->getEntityId('node', 'es_test_not_asynced');
$this
->assertEmpty($entity_id, 'The node with the UUID es_test_not_asynced has not been imported.');
}
protected function populateRequestService() {
parent::populateRequestService();
$this->jsonapiHelper
->setRemote($this->remote);
$http_client = $this->remoteManager
->prepareJsonApiClient($this->remote);
$selected_entities = [
'es_test',
];
$prepared_url = $this
->prepareUrlFilteredOnUuids($selected_entities, 'node_es_test_en');
$this
->discoverJsonApiEndpoints($http_client, $prepared_url);
}
}