You are here

public function SchedulerJavascriptDefaultTimeTest::testTimeWhenSchedulingIsRequired in Scheduler 2.x

Same name and namespace in other branches
  1. 8 tests/src/FunctionalJavascript/SchedulerJavascriptDefaultTimeTest.php \Drupal\Tests\scheduler\FunctionalJavascript\SchedulerJavascriptDefaultTimeTest::testTimeWhenSchedulingIsRequired()

Test the default time functionality when scheduling dates are required.

@dataProvider dataTimeWhenSchedulingIsRequired()

File

tests/src/FunctionalJavascript/SchedulerJavascriptDefaultTimeTest.php, line 53

Class

SchedulerJavascriptDefaultTimeTest
Tests the JavaScript functionality for default dates.

Namespace

Drupal\Tests\scheduler\FunctionalJavascript

Code

public function testTimeWhenSchedulingIsRequired($entityTypeId, $bundle, $field) {
  $config = $this
    ->config('scheduler.settings');
  $titleField = $entityTypeId == 'media' ? 'name' : 'title';
  $entityType = $this
    ->entityTypeObject($entityTypeId);

  // This test is only relevant when the configuration allows a date only with
  // a default time specified. Testing with 'allow_date_only' = false is
  // covered in the browser test SchedulerDefaultTimeTest.
  $config
    ->set('allow_date_only', TRUE)
    ->save();

  // Use a default time of 19:30:20 (7:30pm and 20 seconds).
  $default_time = '19:30:20';
  $config
    ->set('default_time', $default_time)
    ->save();

  // Create a DateTime object to hold the scheduling date. This is better than
  // using a raw unix timestamp because it caters for daylight-saving.
  $scheduling_time = new \DateTime();
  $scheduling_time
    ->add(new \DateInterval('P1D'))
    ->setTime(19, 30, 20);

  // Node and Media entities are revisionable and the 'Revision Information'
  // tab is the default active one, so needs a click on 'Scheduling Options'.
  // Products do not have this link, so the click would fail. A simple way to
  // resolve this is display the scheduler options as a separate fieldset.
  $entityType
    ->setThirdPartySetting('scheduler', 'fields_display_mode', 'fieldset')
    ->save();
  foreach ([
    TRUE,
    FALSE,
  ] as $required) {

    // Set the publish_on/unpublish_on required setting.
    $entityType
      ->setThirdPartySetting('scheduler', $field . '_required', $required)
      ->save();

    // Create an entity.
    $this
      ->drupalGet($this
      ->entityAddUrl($entityTypeId, $bundle));
    $page = $this
      ->getSession()
      ->getPage();
    $title = ucfirst($field) . ($required ? ' required' : ' not required') . ', datepickerFormat = ' . $this->datepickerFormat;
    $page
      ->fillField("edit-{$titleField}-0-value", $title);
    if ($required) {

      // Fill in the date value but do nothing with the time field.
      $page
        ->fillField('edit-' . $field . '-on-0-value-date', $scheduling_time
        ->format($this->datepickerFormat));
    }
    $page
      ->pressButton('Save');

    // Test that the content has saved properly.
    $this
      ->assertSession()
      ->pageTextMatches('/' . preg_quote($title, '/') . ' has been (created|successfully saved)/');
    $entity = $this
      ->getEntityByTitle($entityTypeId, $title);
    $this
      ->assertNotEmpty($entity, 'The entity object can be found by title');
    if ($required) {

      // Check that the scheduled date and time are correct.
      $this
        ->assertEquals($scheduling_time
        ->getTimestamp(), (int) $entity->{$field . '_on'}->value);
    }
    else {

      // Check that no scheduled date was stored.
      $this
        ->assertEmpty($entity->{$field . '_on'}->value);
    }
  }
}