You are here

public function SchedulerNonEnabledTypeTest::testNonEnabledType in Scheduler 8

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

Tests the publish_enable and unpublish_enable node type settings.

@dataProvider dataNonEnabledType()

File

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

Class

SchedulerNonEnabledTypeTest
Tests a content type which is not enabled for scheduling.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testNonEnabledType($id, $description, $publishing_enabled, $unpublishing_enabled) {
  $this
    ->drupalLogin($this->adminUser);

  // The first test case specifically checks the behavior of the default
  // unchanged settings, so only change these settings for later runs.
  if ($id > 0) {
    $this->nonSchedulerNodeType
      ->setThirdPartySetting('scheduler', 'publish_enable', $publishing_enabled)
      ->setThirdPartySetting('scheduler', 'unpublish_enable', $unpublishing_enabled)
      ->save();
  }

  // Create info string to show what combinations are being tested.
  $info = 'Publishing ' . ($publishing_enabled ? 'enabled' : 'not enabled') . ', Unpublishing ' . ($unpublishing_enabled ? 'enabled' : 'not enabled') . ', ' . $description;

  // Check that the field(s) are displayed only for the correct settings.
  $title = $id . 'a - ' . $info;
  $this
    ->drupalGet('node/add/' . $this->nonSchedulerNodeType
    ->id());
  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]');
  }

  // When publishing and/or unpublishing are not enabled but the 'required'
  // setting remains on, the node must be able to be saved without a date.
  $this->nonSchedulerNodeType
    ->setThirdPartySetting('scheduler', 'publish_required', !$publishing_enabled)
    ->save();
  $this->nonSchedulerNodeType
    ->setThirdPartySetting('scheduler', 'unpublish_required', !$unpublishing_enabled)
    ->save();
  $this
    ->drupalPostForm('node/add/' . $this->nonSchedulerNodeType
    ->id(), [
    'title[0][value]' => $title,
  ], 'Save');

  // Check that the node has saved OK.
  $string = sprintf('%s %s has been created.', $this->nonSchedulerNodeType
    ->get('name'), $title);
  $this
    ->assertSession()
    ->pageTextContains($string);

  // Create an unpublished node with a publishing date, which mimics what
  // could be done by a third-party module, or a by-product of the node type
  // being enabled for publishing then being disabled before it got published.
  $title = $id . 'b - ' . $info;
  $edit = [
    'title' => $title,
    'status' => 0,
    'type' => $this->nonSchedulerNodeType
      ->id(),
    'publish_on' => $this->requestTime - 2,
  ];
  $node = $this
    ->drupalCreateNode($edit);

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

  // Reload the node.
  $this->nodeStorage
    ->resetCache([
    $node
      ->id(),
  ]);
  $node = $this->nodeStorage
    ->load($node
    ->id());

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

  // Do the same for unpublishing.
  $title = $id . 'c - ' . $info;
  $edit = [
    'title' => $title,
    'status' => 1,
    'type' => $this->nonSchedulerNodeType
      ->id(),
    'unpublish_on' => $this->requestTime - 1,
  ];
  $node = $this
    ->drupalCreateNode($edit);

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

  // Reload the node.
  $this->nodeStorage
    ->resetCache([
    $node
      ->id(),
  ]);
  $node = $this->nodeStorage
    ->load($node
    ->id());

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

  // Display the full content list and the scheduled list. Calls to these
  // pages are for information and debug only. They could be removed.
  $this
    ->drupalGet('admin/content');
  $this
    ->drupalGet('admin/content/scheduled');
}