You are here

public function EntityShareAsyncFunctionalTest::testAsync in Entity Share 8.3

Same name and namespace in other branches
  1. 8.2 modules/entity_share_async/tests/src/Functional/EntityShareAsyncFunctionalTest.php \Drupal\Tests\entity_share_async\Functional\EntityShareAsyncFunctionalTest::testAsync()

Test async feature.

File

modules/entity_share_async/tests/src/Functional/EntityShareAsyncFunctionalTest.php, line 75

Class

EntityShareAsyncFunctionalTest
General functional test class.

Namespace

Drupal\Tests\entity_share_async\Functional

Code

public function testAsync() {
  $queue_factory = $this->container
    ->get('queue');
  $state_storage = $this->container
    ->get('state');
  $channel_id = static::$entityTypeId . '_' . static::$entityBundleId . '_' . static::$entityLangcode;

  /** @var \Drupal\entity_share_async\Service\QueueHelperInterface $queue_helper */
  $queue_helper = $this->container
    ->get('entity_share_async.queue_helper');
  $queue_helper
    ->enqueue($this->remote
    ->id(), $channel_id, $this::IMPORT_CONFIG_ID, [
    'es_test',
  ]);

  // Test that the entity had been enqueued and is present in the state.
  $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.');

  // Test that both contents had been deleted.
  $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();

  // Test that the queue is empty and that the entity is no more in the state.
  $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.');

  // Test that only the enqueued content had been synced.
  $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.');
}