View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_cron\Functional;
use Drupal\entity_share_cron\EntityShareCronServiceInterface;
use Drupal\entity_share_cron\HookHandler\CronHookHandler;
use Drupal\node\NodeInterface;
use Drupal\Tests\entity_share_client\Functional\EntityShareClientFunctionalTestBase;
class EntityShareCronFunctionalTest extends EntityShareClientFunctionalTestBase {
public static $modules = [
'entity_share_cron',
];
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
protected function setUp() : void {
parent::setUp();
$this
->postSetupFixture();
}
protected function getEntitiesDataArray() {
$nodes_infos = [];
for ($i = 1; $i <= 300; $i++) {
$nodes_infos['es_test_' . $i] = $this
->getCompleteNodeInfos([
'title' => [
'value' => 'Node ' . $i,
'checker_callback' => 'getValue',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]);
}
return [
'node' => [
'en' => $nodes_infos,
],
];
}
public function testCron() {
$queue_factory = $this->container
->get('queue');
$state_storage = $this->container
->get('state');
$channel_id = key($this->channels);
$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();
$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();
$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();
$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();
$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.');
$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.');
$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.');
}
}