You are here

public function SchedulerRulesEventsTest::testRulesEventsBoth in Scheduler 2.x

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

Tests all six events related to publishing and unpublishing an entity.

@dataProvider dataStandardEntityTypes()

File

tests/src/Functional/SchedulerRulesEventsTest.php, line 215

Class

SchedulerRulesEventsTest
Tests the six events that Scheduler provides for use in Rules module.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testRulesEventsBoth($entityTypeId, $bundle) {

  // Allow dates in the past.
  $this
    ->entityTypeObject($entityTypeId, $bundle)
    ->setThirdPartySetting('scheduler', 'publish_past_date', 'schedule')
    ->save();

  // Create an entity with both publish-on and unpublish-on dates, and check
  // that both event 1 and event 4 are triggered.
  $titleField = $entityTypeId == 'media' ? 'name' : 'title';
  $title = 'G. Create with both dates';
  $edit = [
    "{$titleField}[0][value]" => $title,
    'publish_on[0][value][date]' => date('Y-m-d', time() - 60),
    'publish_on[0][value][time]' => date('H:i:s', time() - 60),
    'unpublish_on[0][value][date]' => date('Y-m-d', time() + 5),
    'unpublish_on[0][value][time]' => date('H:i:s', time() + 5),
  ];
  $this
    ->drupalGet($this
    ->entityAddUrl($entityTypeId, $bundle));
  $this
    ->submitForm($edit, 'Save');
  $this
    ->checkMessages($entityTypeId, [
    1,
    4,
  ]);

  // Edit the entity and check that only events 2 and 5 are triggered.
  $entity = $this
    ->getEntityByTitle($entityTypeId, $title);
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $this
    ->submitForm([
    "{$titleField}[0][value]" => 'H. Edit with both dates',
  ], 'Save');
  $this
    ->checkMessages($entityTypeId, [
    2,
    5,
  ]);

  // Delay to ensure that the dates are in the past so that the entity will be
  // processed during cron, and assert that events 3, 5 and 6 are triggered.
  sleep(6);
  $this
    ->cronRun();
  $this
    ->drupalGet($entity
    ->toUrl());
  $this
    ->checkMessages($entityTypeId, [
    3,
    5,
    6,
  ]);
}