You are here

protected function SchedulerDevelGenerateTest::countScheduledEntities in Scheduler 2.x

Helper function to count scheduled entities and assert the expected number.

Parameters

string $type: The machine-name for the entity type to be checked.

string $bundle_field: The name of the field which contains the bundle.

string $bundle: The machine-name for the bundle/content type to be checked.

string $scheduler_field: The field name to count, either 'publish_on' or 'unpublish_on'.

int $num_total: The total number of entities that should exist.

int $num_scheduled: The number of entities which should have a value in $scheduler_field.

int $time_range: Optional time range from the devel form. The generated scheduler dates should be in a range of +/- this value from the current time.

1 call to SchedulerDevelGenerateTest::countScheduledEntities()
SchedulerDevelGenerateTest::testDevelGenerate in tests/src/Functional/SchedulerDevelGenerateTest.php
Test the functionality that Scheduler adds during entity generation.

File

tests/src/Functional/SchedulerDevelGenerateTest.php, line 55

Class

SchedulerDevelGenerateTest
Tests the Scheduler interaction with Devel Generate module.

Namespace

Drupal\Tests\scheduler\Functional

Code

protected function countScheduledEntities($type, $bundle_field, $bundle, $scheduler_field, $num_total, $num_scheduled, $time_range = NULL) {
  $storage = $this
    ->entityStorageObject($type);

  // Check that the expected number of entities have been created.
  $count = $storage
    ->getQuery()
    ->condition($bundle_field, $bundle)
    ->count()
    ->execute();
  $this
    ->assertEquals($num_total, $count, sprintf('The expected number of %s %s is %s, found %s', $bundle, $type, $num_total, $count));

  // Check that the expected number of entities have been scheduled.
  $count = $storage
    ->getQuery()
    ->condition($bundle_field, $bundle)
    ->exists($scheduler_field)
    ->count()
    ->execute();
  $this
    ->assertEquals($num_scheduled, $count, sprintf('The expected number of %s %s with scheduled %s is %s, found %s', $bundle, $type, $scheduler_field, $num_total, $count));
  if (isset($time_range) && $num_scheduled > 0) {

    // Define the minimum and maximum times that we expect the scheduled dates
    // to be within. REQUEST_TIME remains static for the duration of this test
    // but even though devel_generate also uses uses REQUEST_TIME this will
    // slowly creep forward during sucessive calls. Tests can fail incorrectly
    // for this reason, hence the best approximation is to use time() when
    // calculating the upper end of the range.
    $min = $this->requestTime - $time_range;
    $max = time() + $time_range;
    $query = $storage
      ->getAggregateQuery();
    $result = $query
      ->condition($bundle_field, $bundle)
      ->aggregate($scheduler_field, 'min')
      ->aggregate($scheduler_field, 'max')
      ->execute();
    $min_found = $result[0]["{$scheduler_field}_min"];
    $max_found = $result[0]["{$scheduler_field}_max"];

    // Assert that the found values are within the expected range.
    $this
      ->assertGreaterThanOrEqual($min, $min_found, sprintf('The minimum value found for %s is %s, earlier than the expected %s', $scheduler_field, $this->dateFormatter
      ->format($min_found, 'custom', 'j M, H:i:s'), $this->dateFormatter
      ->format($min, 'custom', 'j M, H:i:s')));
    $this
      ->assertLessThanOrEqual($max, $max_found, sprintf('The maximum value found for %s is %s, later than the expected %s', $scheduler_field, $this->dateFormatter
      ->format($max_found, 'custom', 'j M, H:i:s'), $this->dateFormatter
      ->format($max, 'custom', 'j M, H:i:s')));
  }
}