You are here

public function SchedulerDefaultTimeTest::testDefaultTimeWithHiddenTime in Scheduler 8

Test that the default times are set if the form time elements are hidden.

File

tests/src/Functional/SchedulerDefaultTimeTest.php, line 131

Class

SchedulerDefaultTimeTest
Tests the default time functionality.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testDefaultTimeWithHiddenTime() {

  // Create a content type that will have both of the time form elements
  // hidden. See hook_form_node_form_alter() in scheduler_extras test module.
  $type = 'hidden_time';
  $typeName = 'Content with hidden times';

  /** @var NodeTypeInterface $nodetype */
  $nodetype = $this
    ->drupalCreateContentType([
    'type' => $type,
    'name' => $typeName,
  ]);

  // Add scheduler functionality to the content type.
  $nodetype
    ->setThirdPartySetting('scheduler', 'publish_enable', TRUE)
    ->setThirdPartySetting('scheduler', 'unpublish_enable', TRUE)
    ->save();

  // Log in as a user with permission to create and schedule this type.
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'create ' . $type . ' content',
    'edit own ' . $type . ' content',
    'delete own ' . $type . ' content',
    'view own unpublished content',
    'schedule publishing of nodes',
    'view scheduled content',
  ]));

  // Allow the user to enter only a date with no time.
  $this
    ->config('scheduler.settings')
    ->set('allow_date_only', TRUE)
    ->save();

  // Test that entering a time is required.
  $edit = [
    'title[0][value]' => 'Hidden Time Elements ' . $this
      ->randomMachineName(8),
    'publish_on[0][value][date]' => $this->publishTime
      ->format('Y-m-d'),
    'unpublish_on[0][value][date]' => $this->unpublishTime
      ->format('Y-m-d'),
  ];

  // Create a node and check that the expected default time has been saved.
  $this
    ->drupalPostForm('node/add/' . $type, $edit, 'Save');

  // Get the pattern of the 'long' default date format.
  $date_format_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('date_format');
  $long_pattern = $date_format_storage
    ->load('long')
    ->getPattern();

  // Check that the message has the correct default time after saving.
  $this
    ->assertSession()
    ->pageTextContains(sprintf('%s is scheduled to be published %s and unpublished %s', $edit['title[0][value]'], $this->publishTime
    ->format($long_pattern), $this->unpublishTime
    ->format($long_pattern)));

  // Protect this section in case the node was not created.
  if ($node = $this
    ->drupalGetNodeByTitle($edit['title[0][value]'])) {

    // Check that the correct scheduled dates are stored in the node.
    $this
      ->assertEquals($this->publishTime
      ->getTimestamp(), (int) $node->publish_on->value, 'The node publish_on value is stored correctly.');
    $this
      ->assertEquals($this->unpublishTime
      ->getTimestamp(), (int) $node->unpublish_on->value, 'The node unpublish_on value is stored correctly.');
  }
  else {
    $this
      ->fail('The expected node was not found.');
  }
}