You are here

protected function SchedulerDevelGenerateTest::countScheduledNodes in Scheduler 8

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

Parameters

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

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

int $num_nodes: The total number of nodes that should exist.

int $num_scheduled: The number of those nodes which should be scheduled with a $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::countScheduledNodes()
SchedulerDevelGenerateTest::testDevelGenerate in tests/src/Functional/SchedulerDevelGenerateTest.php
Test the functionality that Scheduler adds during content generation.

File

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

Class

SchedulerDevelGenerateTest
Tests the Scheduler interaction with Devel Generate module.

Namespace

Drupal\Tests\scheduler\Functional

Code

protected function countScheduledNodes($type, $field, $num_nodes, $num_scheduled, $time_range = NULL) {

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

  // Check that the expected number of nodes have been scheduled.
  $count = $this->nodeStorage
    ->getQuery()
    ->condition('type', $type)
    ->exists($field)
    ->count()
    ->execute();
  $this
    ->assertEquals($num_scheduled, $count, sprintf('The expected number of scheduled %s is %s, found %s', $field, $num_scheduled, $count));
  if (isset($time_range)) {

    // 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 = $this->nodeStorage
      ->getAggregateQuery();
    $result = $query
      ->condition('type', $type)
      ->aggregate($field, 'min')
      ->aggregate($field, 'max')
      ->execute();
    $min_found = $result[0]["{$field}_min"];
    $max_found = $result[0]["{$field}_max"];

    // Assert that the found values are within the expcted range.
    $this
      ->assertGreaterThanOrEqual($min, $min_found, sprintf('The minimum value for %s is %s, smaller than the expected %s', $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 for %s is %s which is larger than expected %s', $field, $this->dateFormatter
      ->format($max_found, 'custom', 'j M, H:i:s'), $this->dateFormatter
      ->format($max, 'custom', 'j M, H:i:s')));
  }
}