You are here

HookCronTest.php in Entity Share Cron 8.2

Same filename and directory in other branches
  1. 8 tests/src/Kernel/HookCronTest.php

File

tests/src/Kernel/HookCronTest.php
View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\entity_share_cron\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\entity_share_cron\EntityShareCronServiceInterface;

/**
 * Tests the hook_cron() implementation.
 *
 * @group entity_share_cron
 */
class HookCronTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'serialization',
    'jsonapi',
    'entity_share_client',
    'entity_share_cron',
  ];

  /**
   * Editable module settings.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * Drupal state.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Queue of pending channels.
   *
   * @var \Drupal\Core\Queue\QueueInterface
   */
  protected $queue;

  /**
   * Tests if channels are enqueued.
   */
  public function testEnqueueChannels() {

    // Adjusts configuration to force a synchronization.
    $this->config
      ->set('cron_interval', 1);
    $this->state
      ->set('entity_share_cron.cron_last_run', -999999);

    // Configuree some remotes and channels.
    $this->config
      ->set('remotes', [
      'remote1' => [
        'enabled' => FALSE,
        'channels' => [
          'channel1' => [
            'enabled' => FALSE,
            'url' => 'url1',
          ],
        ],
      ],
      'remote2' => [
        'enabled' => TRUE,
        'channels' => [
          'channel2' => [
            'enabled' => TRUE,
            'url' => 'url2',
          ],
          'channel3' => [
            'enabled' => FALSE,
            'url' => 'url3',
          ],
          'channel4' => [
            'enabled' => TRUE,
            'url' => 'url4',
          ],
        ],
      ],
      'remote3' => [
        'enabled' => FALSE,
      ],
      'remote4' => [
        'enabled' => TRUE,
        'channels' => [
          'channel5' => [
            'enabled' => TRUE,
            'url' => 'url5',
          ],
        ],
      ],
    ]);

    // Saves changes to configuration.
    $this->config
      ->save();

    // Invokes the hook.
    \entity_share_cron_cron();

    // Makes assertions.
    $this
      ->assertEquals(3, $this->queue
      ->numberOfItems());
    $item = $this->queue
      ->claimItem();
    $this
      ->assertEquals('remote2', $item->data['remote_id']);
    $this
      ->assertEquals('channel2', $item->data['channel_id']);
    $this
      ->assertEquals('url2', $item->data['channel_info']['url']);
    $item = $this->queue
      ->claimItem();
    $this
      ->assertEquals('remote2', $item->data['remote_id']);
    $this
      ->assertEquals('channel4', $item->data['channel_id']);
    $this
      ->assertEquals('url4', $item->data['channel_info']['url']);
    $item = $this->queue
      ->claimItem();
    $this
      ->assertEquals('remote4', $item->data['remote_id']);
    $this
      ->assertEquals('channel5', $item->data['channel_id']);
    $this
      ->assertEquals('url5', $item->data['channel_info']['url']);
    $last_run = $this->state
      ->get('entity_share_cron.cron_last_run') ? $this->state
      ->get('entity_share_cron.cron_last_run') : -99999;
    $this
      ->assertGreaterThan(-99999, $last_run);
    $this
      ->assertLessThanOrEqual(time(), $last_run);
  }

  /**
   * Tests when no remotes are enabled.
   */
  public function testNoRemotesEnabled() {

    // Adjusts configuration to force a synchronization.
    $this->config
      ->set('cron_interval', 1);
    $this->state
      ->set('entity_share_cron.cron_last_run', -999999);

    // Configure some remotes and channels.
    $this->config
      ->set('remotes', [
      'remote1' => [
        'enabled' => FALSE,
        'channels' => [
          'channel1' => [
            'enabled' => FALSE,
            'url' => 'url1',
          ],
        ],
      ],
      'remote2' => [
        'enabled' => FALSE,
      ],
    ]);

    // Saves changes to configuration.
    $this->config
      ->save();

    // Invokes the hook.
    \entity_share_cron_cron();

    // Checks if the queue is empty.
    $this
      ->assertEquals(0, $this->queue
      ->numberOfItems());
    $last_run = $this->state
      ->get('entity_share_cron.cron_last_run') ? $this->state
      ->get('entity_share_cron.cron_last_run') : -99999;
    $this
      ->assertGreaterThan(-99999, $last_run);
    $this
      ->assertLessThanOrEqual(time(), $last_run);
  }

  /**
   * Tests if no channels are enqueuend when the interval has not elapsed.
   */
  public function testRecentExecution() {

    // Adjusts configuration as though a recent synchronization happened.
    $last_run = time();
    $this->config
      ->set('cron_interval', 9999999);
    $this->state
      ->set('entity_share_cron.cron_last_run', $last_run);

    // Configure some remotes and channels.
    $this->config
      ->set('remotes', [
      'remote1' => [
        'enabled' => TRUE,
        'channels' => [
          'channel1' => [
            'enabled' => TRUE,
            'url' => 'url1',
          ],
        ],
      ],
    ]);

    // Saves changes to configuration.
    $this->config
      ->save();

    // Invokes the hook.
    \entity_share_cron_cron();

    // Checks if the queue is empty.
    $this
      ->assertEquals(0, $this->queue
      ->numberOfItems());
    $this
      ->assertEquals($last_run, $this->state
      ->get('entity_share_cron.cron_last_run'));
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Installs and gets module configuration.
    $this
      ->installConfig([
      'serialization',
      'jsonapi',
      'entity_share_client',
      'entity_share_cron',
    ]);
    $this->config = \Drupal::configFactory()
      ->getEditable('entity_share_cron.settings');
    $this->state = \Drupal::state();

    // Gets the queue.
    $this->queue = \Drupal::queue(EntityShareCronServiceInterface::PENDING_QUEUE_NAME);
  }

}

Classes

Namesort descending Description
HookCronTest Tests the hook_cron() implementation.