You are here

public function SchedulerEventsTest::testSchedulerEvents in Scheduler 2.x

Tests six scheduler events for entity types other than node.

Currently this covers Media and Commerce Products.

@dataProvider dataSchedulerEvents()

File

tests/src/Functional/SchedulerEventsTest.php, line 97

Class

SchedulerEventsTest
Tests the six generic events that Scheduler dispatches.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testSchedulerEvents($entityTypeId, $bundle) {
  $this
    ->drupalLogin($this->schedulerUser);
  $storage = $this
    ->entityStorageObject($entityTypeId);
  $title_prefix = strtoupper("API TEST {$entityTypeId}");

  // Create an entity of the required type, scheduled for publishing.
  $entity = $this
    ->createEntity($entityTypeId, $bundle, [
    'title' => $title_prefix,
    'publish_on' => strtotime('-1 day'),
  ]);

  // Run cron and check that the events have been dispatched correctly. The
  // name is first changed by a PRE_PUBLISH event subscriber, then a second
  // time by a PUBLISH event watcher. Checking the final value tests both.
  scheduler_cron();
  $storage
    ->resetCache([
    $entity
      ->id(),
  ]);
  $entity = $storage
    ->load($entity
    ->id());
  $this
    ->assertEquals($title_prefix . ' - altered a second time by "PUBLISH" event', $entity
    ->label());

  // Create an entity of the required type, scheduled for unpublishing.
  $entity = $this
    ->createEntity($entityTypeId, $bundle, [
    'title' => $title_prefix,
    'unpublish_on' => strtotime('-1 day'),
  ]);

  // Run cron and check that the events have been dispatched correctly.
  scheduler_cron();
  $storage
    ->resetCache([
    $entity
      ->id(),
  ]);
  $entity = $storage
    ->load($entity
    ->id());
  $this
    ->assertEquals($title_prefix . ' - altered a second time by "UNPUBLISH" event', $entity
    ->label());

  // Turn on immediate publishing when a publish date is in the past.
  $this
    ->entityTypeObject($entityTypeId, $bundle)
    ->setThirdPartySetting('scheduler', 'publish_past_date', 'publish')
    ->save();

  // Create an unpublished and unscheduled entity.
  $entity = $this
    ->createEntity($entityTypeId, $bundle, [
    'title' => $title_prefix,
    'status' => FALSE,
  ]);

  // Edit the media item, setting a publish-on date in the past.
  $edit = [
    'publish_on[0][value][date]' => date('Y-m-d', strtotime('-2 day', $this->requestTime)),
    'publish_on[0][value][time]' => date('H:i:s', strtotime('-2 day', $this->requestTime)),
  ];
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $this
    ->submitForm($edit, 'Save');

  // Verify that the values have been altered as expected, without cron.
  $storage
    ->resetCache([
    $entity
      ->id(),
  ]);
  $entity = $storage
    ->load($entity
    ->id());
  $this
    ->assertEquals($title_prefix . ' - altered a second time by "PUBLISH_IMMEDIATELY" event', $entity
    ->label());
}