You are here

trait SchedulerUiTrait in Lightning Scheduler 8

Contains methods for interacting with the scheduler UI.

Hierarchy

5 files declare their use of SchedulerUiTrait
InlineEntityFormTest.php in tests/src/Functional/InlineEntityFormTest.php
ScheduledTransitionTest.php in tests/src/Functional/ScheduledTransitionTest.php
TimeStepTest.php in tests/src/FunctionalJavascript/TimeStepTest.php
TransitionTest.php in tests/src/FunctionalJavascript/TransitionTest.php
UiTest.php in tests/src/FunctionalJavascript/UiTest.php

File

tests/src/Traits/SchedulerUiTrait.php, line 12

Namespace

Drupal\Tests\lightning_scheduler\Traits
View source
trait SchedulerUiTrait {

  /**
   * Sets the time zone.
   *
   * Functional tests normally run in the Sydney, Australia time zone in order
   * to catch time zone-related edge cases and bugs. However, the scheduler UI
   * is extremely sensitive to time zones, so to reduce craziness it's best to
   * set it to the time zone configured in php.ini.
   *
   * @param string $default
   *   (optional) The time zone to set if none is configured in php.ini.
   *   Defaults to UTC.
   */
  protected function setUpTimeZone($default = 'UTC') {
    $this
      ->config('system.date')
      ->clear('timezone.default')
      ->save();
    date_default_timezone_set(ini_get('date.timezone') ?: $default);
  }

  /**
   * Creates a scheduled state transition.
   *
   * @param string $to_state
   *   The label of the state to transition to.
   * @param int $ts
   *   The localized time stamp at which the transition should take place. This
   *   should be generated using mktime(), not gmmktime().
   * @param bool $save
   *   (optional) Whether to save the transition, or just enter it into the UI
   *   without saving. Defaults to TRUE.
   */
  protected function createTransition($to_state, $ts, $save = TRUE) {
    $page = $this
      ->getSession()
      ->getPage();
    try {
      $page
        ->clickLink('add another');
    } catch (ElementNotFoundException $e) {
      $page
        ->clickLink('Schedule a status change');
    }
    $page
      ->selectFieldOption('Scheduled moderation state', $to_state);
    $page
      ->fillField('Scheduled transition date', date('mdY', $ts));
    $page
      ->fillField('Scheduled transition time', date('h:i:sA', $ts));
    if ($save) {
      $page
        ->pressButton('Save transition');
      $text = sprintf("Change to {$to_state} on %s at %s", date('F j, Y', $ts), date('g:i A', $ts));
      $this
        ->assertSession()
        ->pageTextContains($text);
    }
    $this
      ->addToAssertionCount(1);
  }

  /**
   * Sets the scheduled transition data.
   *
   * @param string $field
   *   The hidden field in which to store the transitions.
   * @param array[] $data
   *   The scheduled transitions.
   */
  protected function setTransitionData($field, array $data) {
    $data = Json::encode($data);
    $this
      ->assertSession()
      ->hiddenFieldExists($field)
      ->setValue($data);
  }

  /**
   * Asserts that a set of transitions is present.
   *
   * @param string $field
   *   The hidden field which contains the transitions.
   * @param array[] $data
   *   The scheduled transitions.
   */
  protected function assertTransitionData($field, array $data) {
    array_walk($data, function (array &$transition) {
      $transition['when'] = gmdate('c', $transition['when']);
    });
    $this
      ->assertSession()
      ->hiddenFieldValueEquals($field, Json::encode($data));
  }

  /**
   * Sets the time input's step attribute.
   *
   * @param int $time_step
   *   (optional) The time step.
   */
  protected function setTimeStep($time_step = 1) {
    $this
      ->config('lightning_scheduler.settings')
      ->set('time_step', $time_step)
      ->save();
  }

  /**
   * Sets the time of the request, according to the datetime.time service.
   *
   * @param int $request_time
   *   The time stamp to set.
   */
  protected function setRequestTime($request_time) {
    $this->container
      ->get('state')
      ->set('lightning_scheduler.request_time', $request_time);
  }

  /**
   * Creates the editorial workflow.
   *
   * @return \Drupal\workflows\WorkflowInterface
   *   The editorial workflow entity.
   */
  protected function createEditorialWorkflow() {
    $workflow = Workflow::create([
      'type' => 'content_moderation',
      'id' => 'editorial',
      'label' => 'Editorial',
      'type_settings' => [
        'states' => [
          'archived' => [
            'label' => 'Archived',
            'weight' => 5,
            'published' => FALSE,
            'default_revision' => TRUE,
          ],
          'draft' => [
            'label' => 'Draft',
            'published' => FALSE,
            'default_revision' => FALSE,
            'weight' => -5,
          ],
          'published' => [
            'label' => 'Published',
            'published' => TRUE,
            'default_revision' => TRUE,
            'weight' => 0,
          ],
          'review' => [
            'label' => 'In review',
            'weight' => -1,
            'published' => FALSE,
            'default_revision' => FALSE,
          ],
        ],
        'transitions' => [
          'archive' => [
            'label' => 'Archive',
            'from' => [
              'published',
            ],
            'to' => 'archived',
            'weight' => 2,
          ],
          'archived_published' => [
            'label' => 'Restore from archive',
            'from' => [
              'archived',
            ],
            'to' => 'published',
            'weight' => 4,
          ],
          'create_new_draft' => [
            'label' => 'Create New Draft',
            'to' => 'draft',
            'weight' => 0,
            'from' => [
              'archived',
              'draft',
              'published',
              'review',
            ],
          ],
          'publish' => [
            'label' => 'Publish',
            'to' => 'published',
            'weight' => 1,
            'from' => [
              'draft',
              'published',
              'review',
            ],
          ],
          'review' => [
            'label' => 'Send to review',
            'to' => 'review',
            'weight' => 0,
            'from' => [
              'draft',
              'review',
            ],
          ],
        ],
      ],
    ]);
    $workflow
      ->save();
    return $workflow;
  }

  /**
   * Clicks the link to edit an entity.
   *
   * @see lightning_scheduler_local_tasks_alter()
   */
  protected function clickEditLink() {
    $this
      ->assertSession()
      ->elementExists('named', [
      'link',
      'edit-form',
    ])
      ->click();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SchedulerUiTrait::assertTransitionData protected function Asserts that a set of transitions is present.
SchedulerUiTrait::clickEditLink protected function Clicks the link to edit an entity.
SchedulerUiTrait::createEditorialWorkflow protected function Creates the editorial workflow.
SchedulerUiTrait::createTransition protected function Creates a scheduled state transition.
SchedulerUiTrait::setRequestTime protected function Sets the time of the request, according to the datetime.time service.
SchedulerUiTrait::setTimeStep protected function Sets the time input's step attribute.
SchedulerUiTrait::setTransitionData protected function Sets the scheduled transition data.
SchedulerUiTrait::setUpTimeZone protected function Sets the time zone.