You are here

public function SchedulerDevelGenerateTest::testDevelGenerate in Scheduler 2.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/SchedulerDevelGenerateTest.php \Drupal\Tests\scheduler\Functional\SchedulerDevelGenerateTest::testDevelGenerate()

Test the functionality that Scheduler adds during entity generation.

@dataProvider dataDevelGenerate()

File

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

Class

SchedulerDevelGenerateTest
Tests the Scheduler interaction with Devel Generate module.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testDevelGenerate($entityTypeId, $url_part, $enabled) {
  $this
    ->drupalLogin($this->adminUser);
  $entityType = $this
    ->entityTypeObject($entityTypeId, $enabled ? NULL : 'non-enabled');
  $bundle = $entityType
    ->id();
  $bundle_field = $this->container
    ->get('entity_type.manager')
    ->getDefinition($entityTypeId)
    ->get('entity_keys')['bundle'];

  // Use the minimum required settings to see what happens when everything
  // else is left as default.
  $generate_settings = [
    "{$entityTypeId}_types[{$bundle}]" => TRUE,
  ];
  $this
    ->drupalGet("admin/config/development/generate/{$url_part}");
  $this
    ->submitForm($generate_settings, 'Generate');

  // Display the full content list and the scheduled list for the entity type
  // being generated. Calls to these pages are for information and debug only.
  if ($entityTypeId == 'media') {
    $admin_content_urls = [
      'admin/content/media',
      'admin/content/media/scheduled',
    ];
  }
  else {
    $admin_content_urls = [
      'admin/content',
      'admin/content/scheduled',
    ];
  }
  foreach ($admin_content_urls as $url) {
    $this
      ->drupalGet($url);
  }

  // Delete all content for this type and generate new content with only
  // publish-on dates. Use 100% as this is how we can count the expected
  // number of scheduled entities. The time range of 3600 is one hour.
  // The number of entities has to be lower than 50 until the Devel issue with
  // undefined index 'users' is available and we switch to using 8.x-3.0
  // See https://www.drupal.org/project/devel/issues/3076613
  $generate_settings = [
    "{$entityTypeId}_types[{$bundle}]" => TRUE,
    'num' => 40,
    'kill' => TRUE,
    'time_range' => 3600,
    'scheduler_publishing' => 100,
    'scheduler_unpublishing' => 0,
  ];
  $this
    ->drupalGet("admin/config/development/generate/{$url_part}");
  $this
    ->submitForm($generate_settings, 'Generate');

  // Display the full content list and the scheduled content list.
  foreach ($admin_content_urls as $url) {
    $this
      ->drupalGet($url);
  }

  // Check we have the expected number of entities scheduled for publishing
  // only, and verify that that the dates are within the time range specified.
  $this
    ->countScheduledEntities($entityTypeId, $bundle_field, $bundle, 'publish_on', 40, $enabled ? 40 : 0, $generate_settings['time_range']);
  $this
    ->countScheduledEntities($entityTypeId, $bundle_field, $bundle, 'unpublish_on', 40, 0);

  // Do similar for unpublish_on date. Delete all then generate new content
  // with only unpublish-on dates. Time range 86400 is one day.
  $generate_settings = [
    "{$entityTypeId}_types[{$bundle}]" => TRUE,
    'num' => 30,
    'kill' => TRUE,
    'time_range' => 86400,
    'scheduler_publishing' => 0,
    'scheduler_unpublishing' => 100,
  ];
  $this
    ->drupalGet("admin/config/development/generate/{$url_part}");
  $this
    ->submitForm($generate_settings, 'Generate');

  // Display the full content list and the scheduled content list.
  foreach ($admin_content_urls as $url) {
    $this
      ->drupalGet($url);
  }

  // Check we have the expected number of entities scheduled for unpublishing
  // only, and verify that that the dates are within the time range specified.
  $this
    ->countScheduledEntities($entityTypeId, $bundle_field, $bundle, 'publish_on', 30, 0);
  $this
    ->countScheduledEntities($entityTypeId, $bundle_field, $bundle, 'unpublish_on', 30, $enabled ? 30 : 0, $generate_settings['time_range']);
}