You are here

public function SchedulerBasicMediaTest::testMediaUnpublishing in Scheduler 2.x

Tests scheduled unpublishing of a media entity.

Covers scheduler_entity_presave(), scheduler_cron(), schedulerManager::unpublish.

File

tests/src/Functional/SchedulerBasicMediaTest.php, line 51

Class

SchedulerBasicMediaTest
Tests the modules primary functions with a Media entity type.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testMediaUnpublishing() {

  // Specify values for the entity.
  $values = [
    'name' => 'Unpublish This Media',
    'unpublish_on' => $this->requestTime + 3600,
  ];

  // Create a media entity with the scheduler fields populated as required.
  $entity = $this
    ->createMediaItem($values);
  $this
    ->assertNotEmpty($entity, 'The entity was created sucessfully.');

  // Assert that the entity has an unpublish_on date.
  $this
    ->assertNotEmpty($entity->unpublish_on, 'The entity has an unpublish_on date');

  // Assert that the entity is published before cron.
  $this
    ->assertTrue($entity
    ->isPublished(), 'The entity is published before cron run');

  // Modify the scheduler field to a time in the past, then run cron.
  $entity->unpublish_on = $this->requestTime - 1;
  $entity
    ->save();
  $this
    ->cronRun();

  // Refresh the cache, reload the entity and check the entity is unpublished.
  $this->mediaStorage
    ->resetCache([
    $entity
      ->id(),
  ]);
  $entity = $this->mediaStorage
    ->load($entity
    ->id());
  $this
    ->assertFalse($entity
    ->isPublished(), 'The entity is unpublished after cron run');
}