You are here

public function SchedulerNonEnabledTypeTest::testNonEnabledType in Scheduler 2.x

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

Tests the publish_enable and unpublish_enable entity type settings.

@dataProvider dataNonEnabledType()

File

tests/src/Functional/SchedulerNonEnabledTypeTest.php, line 17

Class

SchedulerNonEnabledTypeTest
Tests entity types which are not enabled for scheduling.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testNonEnabledType($id, $entityTypeId, $description, $publishing_enabled, $unpublishing_enabled) {
  $this
    ->drupalLogin($this->adminUser);
  $entityType = $this
    ->entityTypeObject($entityTypeId, 'non-enabled');
  $bundle = $entityType
    ->id();
  $storage = $this
    ->entityStorageObject($entityTypeId);
  $titleField = $entityTypeId == 'media' ? 'name' : 'title';

  // The 'default' case specifically checks the behavior of the unchanged
  // settings, so only change these when not running the default test.
  if ($description != 'Default') {
    $entityType
      ->setThirdPartySetting('scheduler', 'publish_enable', $publishing_enabled)
      ->setThirdPartySetting('scheduler', 'unpublish_enable', $unpublishing_enabled)
      ->save();
  }

  // When publishing and/or unpublishing are not enabled but the 'required'
  // setting remains on, the entity must be able to be saved without a date.
  $entityType
    ->setThirdPartySetting('scheduler', 'publish_required', !$publishing_enabled)
    ->save();
  $entityType
    ->setThirdPartySetting('scheduler', 'unpublish_required', !$unpublishing_enabled)
    ->save();

  // Allow dates in the past to be valid on saving the entity, to simplify the
  // testing process.
  $entityType
    ->setThirdPartySetting('scheduler', 'publish_past_date', 'schedule')
    ->save();

  // Create a new entity via the add/bundle url, and check that the correct
  // fields are displayed on the form depending on the enabled settings.
  $this
    ->drupalGet($this
    ->entityAddUrl($entityTypeId, $bundle));
  if ($publishing_enabled) {
    $this
      ->assertSession()
      ->fieldExists('publish_on[0][value][date]');
  }
  else {
    $this
      ->assertSession()
      ->fieldNotExists('publish_on[0][value][date]');
  }
  if ($unpublishing_enabled) {
    $this
      ->assertSession()
      ->fieldExists('unpublish_on[0][value][date]');
  }
  else {
    $this
      ->assertSession()
      ->fieldNotExists('unpublish_on[0][value][date]');
  }

  // Fill in the title field and check that the entity can be saved OK.
  $title = $id . 'a - ' . $description;
  $this
    ->submitForm([
    "{$titleField}[0][value]" => $title,
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextMatches('/' . preg_quote($title, '/') . ' has been (created|successfully saved)/');

  // Create an unpublished entity with a publishing date, which mimics what
  // could be done by a third-party module, or a by-product of the entity type
  // being enabled for publishing then being disabled before it got published.
  $title = $id . 'b - ' . $description;
  $values = [
    "{$titleField}" => $title,
    'status' => FALSE,
    'publish_on' => $this->requestTime - 120,
  ];
  $entity = $this
    ->createEntity($entityTypeId, $bundle, $values);

  // Check that the entity can be edited and saved OK.
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextMatches('/' . preg_quote($title, '/') . ' has been (updated|successfully saved)/');

  // Run cron and display the dblog.
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/reports/dblog');

  // Reload the entity.
  $storage
    ->resetCache([
    $entity
      ->id(),
  ]);
  $entity = $storage
    ->load($entity
    ->id());

  // Check if the entity has been published or remains unpublished.
  if ($publishing_enabled) {
    $this
      ->assertTrue($entity
      ->isPublished(), "The unpublished entity '{$title}' should now be published");
  }
  else {
    $this
      ->assertFalse($entity
      ->isPublished(), "The unpublished entity '{$title}' should remain unpublished");
  }

  // Do the same for unpublishing - create a published entity with an
  // unpublishing date in the future, to be valid for editing and saving.
  $title = $id . 'c - ' . $description;
  $values = [
    "{$titleField}" => $title,
    'status' => TRUE,
    'unpublish_on' => $this->requestTime + 180,
  ];
  $entity = $this
    ->createEntity($entityTypeId, $bundle, $values);

  // Check that the entity can be edited and saved.
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextMatches('/' . preg_quote($title, '/') . ' has been (updated|successfully saved)/');

  // Create a published entity with a date in the past, then run cron.
  $title = $id . 'd - ' . $description;
  $values = [
    "{$titleField}" => $title,
    'status' => TRUE,
    'unpublish_on' => $this->requestTime - 120,
  ];
  $entity = $this
    ->createEntity($entityTypeId, $bundle, $values);
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/reports/dblog');

  // Reload the entity.
  $storage
    ->resetCache([
    $entity
      ->id(),
  ]);
  $entity = $storage
    ->load($entity
    ->id());

  // Check if the entity has been unpublished or remains published.
  if ($unpublishing_enabled) {
    $this
      ->assertFalse($entity
      ->isPublished(), "The published entity '{$title}' should now be unpublished");
  }
  else {
    $this
      ->assertTrue($entity
      ->isPublished(), "The published entity '{$title}' should remain published");
  }

  // Display the full content list and the scheduled list. Calls to these
  // pages are for information and debug only.
  switch ($entityTypeId) {
    case 'node':
      $this
        ->drupalGet('admin/content');
      $this
        ->drupalGet('admin/content/scheduled');
      break;
    case 'media':
      $this
        ->drupalGet('admin/content/media');
      $this
        ->drupalGet('admin/content/media/scheduled');
      break;
    case 'commerce_product':
      $this
        ->drupalGet('admin/commerce/products');
      $this
        ->drupalGet('admin/commerce/products/scheduled');
      break;
  }
}