You are here

public function SchedulerHooksTest::testUnpublishingAllowed in Scheduler 2.x

Covers hook_scheduler_{type}_unpublishing_allowed()

This hook is used to deny the unpublishing of individual entities. This test is simpler than the test sequence for allowed publishing, because the past date 'publish' option is not applicable.

@dataProvider dataCustomEntityTypes()

File

tests/src/Functional/SchedulerHooksTest.php, line 329

Class

SchedulerHooksTest
Tests the API hook functions of the Scheduler module.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testUnpublishingAllowed($entityTypeId, $bundle) {
  $storage = $this
    ->entityStorageObject($entityTypeId);
  $titleField = $entityTypeId == 'media' ? 'name' : 'title';
  $this
    ->drupalLogin($this->webUser);

  // Check the 'approved for unpublishing' field is shown on the entity form.
  $this
    ->drupalGet($this
    ->entityAddUrl($entityTypeId, $bundle));
  $this
    ->assertSession()
    ->fieldExists('edit-field-approved-unpublishing-value');

  // Check that the message is shown when scheduling an entity for
  // unpublishing which is not yet allowed to be unpublished.
  $edit = [
    "{$titleField}[0][value]" => "Red {$entityTypeId} - Set unpublish-on date without approval",
    'unpublish_on[0][value][date]' => date('Y-m-d', time() + 3),
    'unpublish_on[0][value][time]' => date('H:i:s', time() + 3),
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextMatches('/is scheduled for unpublishing.* but will not be unpublished until approved/');

  // Create an entity that is scheduled but not approved for unpublishing, run
  // cron for scheduler, and check that the entity is still published.
  $entity = $this
    ->createUnapprovedEntity($entityTypeId, $bundle, 'unpublish_on');
  scheduler_cron();
  $storage
    ->resetCache([
    $entity
      ->id(),
  ]);
  $entity = $storage
    ->load($entity
    ->id());
  $this
    ->assertTrue($entity
    ->isPublished(), "Unapproved '{$entity->label()}' should not be unpublished during cron processing.");

  // Create an entity and approve it for unpublishing, run cron for scheduler
  // and check that the entity is unpublished.
  $entity = $this
    ->createUnapprovedEntity($entityTypeId, $bundle, 'unpublish_on');
  $this
    ->approveEntity($entityTypeId, $entity
    ->id(), 'field_approved_unpublishing');
  $this
    ->assertTrue($entity
    ->isPublished(), "New approved '{$entity->label()}' should initially remain published.");
  scheduler_cron();
  $storage
    ->resetCache([
    $entity
      ->id(),
  ]);
  $entity = $storage
    ->load($entity
    ->id());
  $this
    ->assertFalse($entity
    ->isPublished(), "Approved '{$entity->label()}' should be unpublished during cron processing.");
}