You are here

public function EntityShareCronFunctionalTest::testCron in Entity Share Cron 3.0.x

Test cron feature.

File

tests/src/Functional/EntityShareCronFunctionalTest.php, line 77

Class

EntityShareCronFunctionalTest
General functional test class.

Namespace

Drupal\Tests\entity_share_cron\Functional

Code

public function testCron() {
  $queue_factory = $this->container
    ->get('queue');
  $state_storage = $this->container
    ->get('state');
  $channel_id = key($this->channels);

  // Set entity_share_cron.settings to check pagination handling.
  $entity_share_cron_settings = $this
    ->config('entity_share_cron.settings');
  $entity_share_cron_settings
    ->set('page_limit', 2);
  $entity_share_cron_settings
    ->set('remotes', [
    $this->remote
      ->id() => [
      'enabled' => TRUE,
      'channels' => [
        $channel_id => [
          'enabled' => TRUE,
          'import_config' => $this->importConfig
            ->id(),
          'operations' => [
            'create' => TRUE,
            'update' => TRUE,
          ],
        ],
      ],
    ],
  ]);
  $entity_share_cron_settings
    ->save();

  // Check that there are no contents and nothing in queue.
  $number_of_content = $this->entityTypeManager
    ->getStorage('node')
    ->getQuery()
    ->count()
    ->execute();
  $this
    ->assertEquals(0, $number_of_content, 'There are no contents on the client website before cron run.');
  $queue = $queue_factory
    ->get(EntityShareCronServiceInterface::PENDING_QUEUE_NAME);
  $this
    ->assertEquals(0, $queue
    ->numberOfItems(), 'The queue is empty.');
  $this->container
    ->get('cron')
    ->run();

  // Check that after the first cron run, the first two pages had been
  // imported. And the next one is in the queue.
  $number_of_content = $this->entityTypeManager
    ->getStorage('node')
    ->getQuery()
    ->count()
    ->execute();
  $this
    ->assertEquals(100, $number_of_content, 'There are 100 contents on the client website after the first cron run. The first 2 pages had been imported.');
  $queue = $queue_factory
    ->get(EntityShareCronServiceInterface::PENDING_QUEUE_NAME);
  $this
    ->assertEquals(1, $queue
    ->numberOfItems(), 'The next page had been queued.');
  $this->container
    ->get('cron')
    ->run();

  // Check that after the second cron run, the next two pages had been
  // imported. And the next one is in the queue.
  $number_of_content = $this->entityTypeManager
    ->getStorage('node')
    ->getQuery()
    ->count()
    ->execute();
  $this
    ->assertEquals(200, $number_of_content, 'There are 200 contents on the client website after the second cron run. The first 4 pages had been imported.');
  $queue = $queue_factory
    ->get(EntityShareCronServiceInterface::PENDING_QUEUE_NAME);
  $this
    ->assertEquals(1, $queue
    ->numberOfItems(), 'The next page had been queued.');
  $this->container
    ->get('cron')
    ->run();

  // Check that after the third cron run, the last two pages had been
  // imported. And the queue is empty as there is no more pages to import.
  $number_of_content = $this->entityTypeManager
    ->getStorage('node')
    ->getQuery()
    ->count()
    ->execute();
  $this
    ->assertEquals(300, $number_of_content, 'There are 300 contents on the client website after the second cron run. All the pages had been imported.');
  $queue = $queue_factory
    ->get(EntityShareCronServiceInterface::PENDING_QUEUE_NAME);
  $this
    ->assertEquals(0, $queue
    ->numberOfItems(), 'The queue is empty as there is no more pages to import.');

  // Test update operation. Delete one content and change one to see
  // that the deleted content will not be recreated and the changed one will
  // be updated.
  $state_storage
    ->delete(CronHookHandler::STATE_ID);
  $content_to_delete = $this
    ->loadEntity('node', 'es_test_1');
  $content_to_delete
    ->delete();
  $content_to_update = $this
    ->loadEntity('node', 'es_test_2');
  $content_to_update
    ->set('title', 'another title');
  $content_to_update
    ->save();
  $entity_share_cron_settings
    ->set('remotes', [
    $this->remote
      ->id() => [
      'enabled' => TRUE,
      'channels' => [
        $channel_id => [
          'enabled' => TRUE,
          'import_config' => $this->importConfig
            ->id(),
          'operations' => [
            'create' => FALSE,
            'update' => TRUE,
          ],
        ],
      ],
    ],
  ]);
  $entity_share_cron_settings
    ->save();
  $this->container
    ->get('cron')
    ->run();
  $deleted_content = $this
    ->loadEntity('node', 'es_test_1');
  $this
    ->assertNull($deleted_content, 'The deleted content had not been re-imported.');
  $updated_content = $this
    ->loadEntity('node', 'es_test_2');
  $this
    ->assertEquals('Node 2', $updated_content
    ->label(), 'The changed content had been updated.');

  // Test create operation. Change an existing content and see that it will
  // not be updated and that the previously deleted content will be created.
  $state_storage
    ->delete(CronHookHandler::STATE_ID);
  $updated_content
    ->set('title', 'another title 2');
  $updated_content
    ->save();
  $entity_share_cron_settings
    ->set('remotes', [
    $this->remote
      ->id() => [
      'enabled' => TRUE,
      'channels' => [
        $channel_id => [
          'enabled' => TRUE,
          'import_config' => $this->importConfig
            ->id(),
          'operations' => [
            'create' => TRUE,
            'update' => FALSE,
          ],
        ],
      ],
    ],
  ]);
  $entity_share_cron_settings
    ->save();
  $this->container
    ->get('cron')
    ->run();
  $deleted_content = $this
    ->loadEntity('node', 'es_test_1');
  $this
    ->assertNotNull($deleted_content, 'The deleted content had been re-imported.');
  $updated_content = $this
    ->loadEntity('node', 'es_test_2');
  $this
    ->assertEquals('another title 2', $updated_content
    ->label(), 'The changed content had not been updated.');
}