You are here

public function SchedulerDefaultTimeTest::testDefaultWithHiddenTime in Scheduler 2.x

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

This test uses the 'scheduler_extras' helper module, which hides the time elements of both of the scheduler date input fields.

@dataProvider dataStandardEntityTypes()

File

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

Class

SchedulerDefaultTimeTest
Tests the default time functionality.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testDefaultWithHiddenTime($entityTypeId, $bundle) {
  \Drupal::service('module_installer')
    ->install([
    'scheduler_extras',
  ]);
  $titleField = $entityTypeId == 'media' ? 'name' : 'title';
  $this
    ->drupalLogin($this->schedulerUser);

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

  // Define date values but no time values.
  $title = 'Hidden Time Elements ' . $this
    ->randomMachineName(8);
  $edit = [
    "{$titleField}[0][value]" => $title,
    'publish_on[0][value][date]' => $this->publishTime
      ->format('Y-m-d'),
    'unpublish_on[0][value][date]' => $this->unpublishTime
      ->format('Y-m-d'),
  ];

  // Create an entity and check that the time fields are hidden.
  $this
    ->drupalGet($this
    ->entityAddUrl($entityTypeId, $bundle));
  $this
    ->assertSession()
    ->FieldExists('publish_on[0][value][date]');
  $this
    ->assertSession()
    ->FieldExists('unpublish_on[0][value][date]');
  $this
    ->assertSession()
    ->FieldNotExists('publish_on[0][value][time]');
  $this
    ->assertSession()
    ->FieldNotExists('unpublish_on[0][value][time]');
  $this
    ->submitForm($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', $title, $this->publishTime
    ->format($long_pattern), $this->unpublishTime
    ->format($long_pattern)));
  if ($entity = $this
    ->getEntityByTitle($entityTypeId, $title)) {

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