You are here

public function SchedulerDefaultTimeTest::testDefaultTime in Scheduler 8

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

Test the default time functionality during content creation and edit.

This test covers the default scenario where the dates are optional and not required. A javascript test covers the cases where the dates are required.

File

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

Class

SchedulerDefaultTimeTest
Tests the default time functionality.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testDefaultTime() {
  $this
    ->drupalLogin($this->schedulerUser);
  $config = $this
    ->config('scheduler.settings');

  // We cannot easily test the full validation message as they contain the
  // current time which can be one or two seconds in the past. The best we can
  // do is check the fixed part of the message as it is when passed to t() in
  // Datetime::validateDatetime. Tests only needs to work in English anyway.
  $publish_validation_message = 'The Publish on date is invalid.';
  $unpublish_validation_message = 'The Unpublish on date is invalid.';

  // First test with the "date only" functionality disabled.
  $config
    ->set('allow_date_only', FALSE)
    ->save();

  // Test that entering a time is required.
  $edit = [
    'title[0][value]' => 'No time ' . $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 error messages are shown.
  $this
    ->drupalPostForm('node/add/' . $this->type, $edit, 'Save');

  // By default it is required to enter a time when scheduling content for
  // publishing and for unpublishing.
  $this
    ->assertSession()
    ->pageTextNotMatches('/' . $edit['title[0][value]'] . ' is scheduled to be published .* and unpublished .*/');
  $this
    ->assertSession()
    ->pageTextContains($publish_validation_message);
  $this
    ->assertSession()
    ->pageTextContains($unpublish_validation_message);

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

  // Create a node and check that the validation messages are not shown.
  $this
    ->drupalPostForm('node/add/' . $this->type, $edit, 'Save');
  $this
    ->assertSession()
    ->pageTextNotContains($publish_validation_message);
  $this
    ->assertSession()
    ->pageTextNotContains($unpublish_validation_message);

  // 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 scheduled information is shown 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.');

    // Check that the default time has been added to the form on edit.
    $this
      ->drupalGet('node/' . $node
      ->id() . '/edit');
    $this
      ->assertSession()
      ->FieldValueEquals('publish_on[0][value][time]', $this->defaultTime);
    $this
      ->assertSession()
      ->FieldValueEquals('unpublish_on[0][value][time]', $this->defaultTime);
  }
  else {
    $this
      ->fail('The expected node was not found.');
  }
}