You are here

BaseTest.php in TMGMT Extension Suite 8.2

File

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

namespace Drupal\Tests\tmgmt_extension_suit\Kernel;

use Drupal\Tests\tmgmt\Kernel\TMGMTKernelTestBase;
use Drupal\tmgmt\JobInterface;

// Note we have to disable the SYMFONY_DEPRECATIONS_HELPER to ensure deprecation
// notices are not triggered.
// TODO: remove this and fix all the deprecations before Drupal 9.0.0.
putenv('SYMFONY_DEPRECATIONS_HELPER=disabled');

/**
 * TMGMT Extension Suit kernel test base class.
 *
 * @group tmgmt_extension_suit
 */
class BaseTest extends TMGMTKernelTestBase {
  public static $modules = [
    'tmgmt_extension_suit',
  ];
  protected $requestTime;
  protected $checkStatusQueue;
  protected $uniqueQueueItemMock;

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

    // Suppress deprecation errors.
    set_error_handler(function ($errno) {
      if ($errno == E_USER_DEPRECATED) {
        return TRUE;
      }
      return FALSE;
    });
    $this->checkStatusQueue = \Drupal::service('queue')
      ->get('tmgmt_extension_suit_check_status');
    $this->uniqueQueueItemMock = $this
      ->getMockBuilder('\\Drupal\\tmgmt_extension_suit\\Utils\\UniqueQueueItem')
      ->setMethods([
      'addItem',
    ])
      ->disableOriginalConstructor()
      ->getMock();
    \Drupal::getContainer()
      ->set('tmgmt_extension_suit.utils.queue_unique_item', $this->uniqueQueueItemMock);
    $this
      ->installConfig([
      'tmgmt_extension_suit',
    ]);
    $this
      ->createJobWithItems([]);
    $this->requestTime = time();
  }

  /**
   * {@inheritdoc}
   */
  protected function tearDown() {
    restore_error_handler();
  }

  /**
   * Creates a job.
   */
  protected function createJobWithItems() {
    $job = parent::createJob();
    $job->state = JobInterface::STATE_ACTIVE;
    $job
      ->save();
    for ($i = 1; $i < 3; $i++) {
      $job
        ->addItem('test_source', 'test', $i);
    }
  }
  public function testCheckDefaultValue() {
    $three_hours_in_seconds = 3600 * 3;
    $this
      ->assertEquals(\Drupal::config('tmgmt_extension_suit.settings')
      ->get('interval'), $three_hours_in_seconds);
  }
  public function testThrottlingInterval3hLastRun1hAgo() {
    $this->uniqueQueueItemMock
      ->expects($this
      ->never())
      ->method('addItem');
    \Drupal::state()
      ->set('tmgmt_extension_suit.check_status_cron_last', $this->requestTime - 3600);
    tmgmt_extension_suit_cron();
  }
  public function testThrottlingInterval3hLastRun3hAgo() {
    $this->uniqueQueueItemMock
      ->expects($this
      ->once())
      ->method('addItem');
    \Drupal::state()
      ->set('tmgmt_extension_suit.check_status_cron_last', $this->requestTime - 3600 * 3);
    tmgmt_extension_suit_cron();
  }
  public function testThrottlingInterval3hLastRun4hAgo() {
    $this->uniqueQueueItemMock
      ->expects($this
      ->once())
      ->method('addItem');
    \Drupal::state()
      ->set('tmgmt_extension_suit.check_status_cron_last', $this->requestTime - 3600 * 4);
    tmgmt_extension_suit_cron();
  }
  public function testThrottleCronRun() {
    $config = \Drupal::service('config.factory')
      ->getEditable('tmgmt_extension_suit.settings');
    $config
      ->set('interval', 5);
    $config
      ->save();
    $this->uniqueQueueItemMock
      ->expects($this
      ->never())
      ->method('addItem');
    \Drupal::state()
      ->set('tmgmt_extension_suit.check_status_cron_last', $this->requestTime);
    sleep(4);
    tmgmt_extension_suit_cron();
  }
  public function testThrottle12of15CronRuns() {
    $config = \Drupal::service('config.factory')
      ->getEditable('tmgmt_extension_suit.settings');
    $config
      ->set('interval', 5);
    $config
      ->save();
    $this->uniqueQueueItemMock
      ->expects($this
      ->exactly(3))
      ->method('addItem');
    \Drupal::state()
      ->set('tmgmt_extension_suit.check_status_cron_last', $this->requestTime);
    for ($i = 0; $i < 15; $i++) {
      sleep(1);
      tmgmt_extension_suit_cron();
    }
  }

}

Classes

Namesort descending Description
BaseTest TMGMT Extension Suit kernel test base class.