You are here

public function SchedulerHooksTest::testPublishingAllowed in Scheduler 2.x

Covers hook_scheduler_{type}_publishing_allowed()

This hook is used to deny the publishing of individual entities. The test uses the customised content type which has checkboxes 'Approved for publishing' and 'Approved for unpublishing'.

@dataProvider dataCustomEntityTypes()

File

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

Class

SchedulerHooksTest
Tests the API hook functions of the Scheduler module.

Namespace

Drupal\Tests\scheduler\Functional

Code

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

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

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

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

  // Create an entity and approve it for publishing, run cron for scheduler
  // and check that the entity is published. This is a stronger test than
  // simply approving the previously used entity above, as we do not know what
  // publish state that may be in after the cron run above.
  $entity = $this
    ->createUnapprovedEntity($entityTypeId, $bundle, 'publish_on');
  $this
    ->approveEntity($entityTypeId, $entity
    ->id(), 'field_approved_publishing');
  $this
    ->assertFalse($entity
    ->isPublished(), "New approved '{$entity->label()}' should not be initially published.");
  scheduler_cron();
  $storage
    ->resetCache([
    $entity
      ->id(),
  ]);
  $entity = $storage
    ->load($entity
    ->id());
  $this
    ->assertTrue($entity
    ->isPublished(), "Approved '{$entity->label()}' should be published during cron processing.");

  // Turn on immediate publishing when the date is in the past and repeat
  // the tests. It is not needed to run cron jobs here.
  $bundle_field_name = $entity
    ->getEntityType()
    ->get('entity_keys')['bundle'];
  $entity->{$bundle_field_name}->entity
    ->setThirdPartySetting('scheduler', 'publish_past_date', 'publish')
    ->save();

  // Check that an entity can be approved and published programatically.
  $entity = $this
    ->createUnapprovedEntity($entityTypeId, $bundle, 'publish_on');
  $this
    ->assertFalse($entity
    ->isPublished(), "New unapproved '{$entity->label()}' with a date in the past should not be published immediately after saving.");
  $this
    ->approveEntity($entityTypeId, $entity
    ->id(), 'field_approved_publishing');
  $storage
    ->resetCache([
    $entity
      ->id(),
  ]);
  $entity = $storage
    ->load($entity
    ->id());
  $this
    ->assertTrue($entity
    ->isPublished(), "New approved '{$entity->label()}' with a date in the past should be published immediately when created programatically.");

  // Check that an entity can be approved and published via edit form.
  $entity = $this
    ->createUnapprovedEntity($entityTypeId, $bundle, 'publish_on');
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $this
    ->submitForm([
    'field_approved_publishing[value]' => '1',
  ], 'Save');
  $storage
    ->resetCache([
    $entity
      ->id(),
  ]);
  $entity = $storage
    ->load($entity
    ->id());
  $this
    ->assertTrue($entity
    ->isPublished(), "Approved '{$entity->label()}' with a date in the past is published immediately after saving via edit form.");
}