You are here

public function WorkbenchSchedulerNodeScheduleTestCase::testNodeSchedule in Workbench Scheduler 7.2

Same name and namespace in other branches
  1. 7 tests/workbench_scheduler.test \WorkbenchSchedulerNodeScheduleTestCase::testNodeSchedule()

Test Node Schedule.

File

tests/workbench_scheduler.test, line 147
Tests for workbench_scheduler.module.

Class

WorkbenchSchedulerNodeScheduleTestCase
Assign node to schedule.

Code

public function testNodeSchedule() {

  // Create two schedules.
  // We do this because each node can schedule multiple schedules.
  $schedule_transitions = array(
    1,
    2,
  );
  foreach ($schedule_transitions as $id) {
    $edit = array();
    $edit['label'] = $this
      ->randomName(8);
    $edit['name'] = strtolower($this
      ->randomName(8));
    $edit['transition'] = $id;
    $edit["types[{$this->content_type}]"] = $this->content_type;
    $this
      ->drupalPost('admin/config/workbench/scheduler/schedules/add', $edit, t('Save'));
  }

  // Get all schedules.
  $schedule_query = db_select('workbench_scheduler_schedules', 'wss')
    ->fields('wss')
    ->execute()
    ->fetchAllAssoc('sid');

  // Get the first sid. We'll use this for most of the tests.
  $schedule = $schedule_query[1];

  // Default behavior - one schedule.
  // Running a test to make sure that a schedule runs when applied to a node.
  $ts = mktime(date('H'), date('i'), 0, date('m'), date('d'), date('Y'));
  $node = $this
    ->createScheduledNode($schedule->sid, $ts);

  // Run the schedule.
  $this
    ->cronRun();

  // There should only be one row.
  $result = db_select('workbench_scheduler_nodes', 'wsn')
    ->fields('wsn')
    ->condition('wsn.nid', $node->nid, '=')
    ->condition('wsn.completed', 1, '=')
    ->execute()
    ->rowCount();
  $this
    ->assertTrue($result, "Cron run for {$node->title}: One Schedule was run for one revision.");

  // Default behavior - 2 revisions each with a schedule.
  // Two revisions, each have their onw schedule.
  $ts = mktime(date('H'), date('i'), 0, date('m'), date('d'), date('Y'));
  $node = $this
    ->createScheduledNode($schedule->sid, $ts);

  // Add a new schedule that happens after.
  $edit = array();
  $edit['workbench_scheduler_sid[' . $schedule->sid . ']'] = TRUE;
  $edit['workbench_scheduler_date[' . $schedule->sid . '][date]'] = date('m/d/Y', $ts + 60);
  $edit['workbench_scheduler_date[' . $schedule->sid . '][time]'] = date('H:i', $ts + 60);
  $this
    ->drupalPost('node/add/' . $this->content_type, $edit, t('Save'));
  $this
    ->cronRun();

  // Run cron again. (This is to make sure no other schedules are run).
  $this
    ->cronRun();

  // There should be one completed row.
  $result = db_select('workbench_scheduler_nodes', 'wsn')
    ->fields('wsn')
    ->condition('wsn.nid', $node->nid, '=')
    ->condition('wsn.completed', 1, '=')
    ->condition('wsn.vid', $node->vid)
    ->execute()
    ->rowCount();
  $this
    ->assertEqual($result, 1, "Cron run for {$node->title}: Only schedule for latest revision run.");

  // Default behavior - 1 revision with multiple schedules.
  // Make timestamp in the past so it will run.
  $ts = mktime(date('H'), date('i'), 0, date('m'), date('d'), date('Y')) - 120;

  // Add multiple schedules.
  $schedules = array();
  foreach ($schedule_query as $schedule_data) {
    $schedules[$schedule_data->sid] = $ts;
    $ts += 60;
  }
  $node = $this
    ->createScheduledNode($schedules);

  // Change state once.
  $this
    ->cronRun();

  // Change state again.
  $this
    ->cronRun();

  // There should be two completed schedules.
  $result = db_select('workbench_scheduler_nodes', 'wsn')
    ->fields('wsn')
    ->condition('wsn.nid', $node->nid, '=')
    ->condition('wsn.vid', $node->vid, '=')
    ->condition('wsn.completed', 1, '=')
    ->execute()
    ->rowCount();
  $this
    ->assertEqual($result, 2, "Cron run for {$node->title}: Two schedules were run.");

  // Secondary behavior - one schedule.
  // Update content type settings.
  // One revision has a schedule, another revision doesn't.
  $ts = mktime(date('H'), date('i'), 0, date('m'), date('d'), date('Y'));
  $node = $this
    ->createScheduledNode($schedule->sid, $ts);

  // Add a new schedule that happens after.
  $edit = array();
  $edit['workbench_scheduler_sid[' . $schedule->sid . ']'] = FALSE;
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $this
    ->cronRun();

  // Getting the latest revision.
  $rev_list = node_revision_list($node);
  $latest_vid = max(array_keys($rev_list));

  // Should be no results returned.
  $result = db_select('workbench_scheduler_nodes', 'wsn')
    ->fields('wsn')
    ->condition('wsn.nid', $node->nid, '=')
    ->condition('wsn.completed', 1, '=')
    ->condition('wsn.vid', $latest_vid)
    ->execute()
    ->rowCount();
  $this
    ->assertFalse($result, "Cron run for {$node->title}: No schedules run.");
}