You are here

public function WorkbenchSchedulerNodeScheduleTestCase::testNodeSchedule in Workbench Scheduler 7

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

Test Node Schedule.

File

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

Class

WorkbenchSchedulerNodeScheduleTestCase
Assign node to schedule.

Code

public function testNodeSchedule() {
  $states = workbench_scheduler_state_labels();
  $indexes = array_rand($states, 2);

  // Create schedule.
  $edit = array();
  $edit['label'] = $this
    ->randomName(8);
  $edit['name'] = strtolower($this
    ->randomName(8));
  $edit['start_state'] = $indexes[0];
  $edit['end_state'] = $indexes[1];
  $edit["types[{$this->content_type}]"] = $this->content_type;
  $this
    ->drupalPost('admin/config/workbench/scheduler/schedules/add', $edit, t('Save'));

  // Get the sid.
  $query = db_select('workbench_scheduler_schedules', 'wss')
    ->fields('wss')
    ->condition('wss.name', $edit['name'], '=')
    ->condition('wss.label', $edit['label'], '=')
    ->condition('wss.start_state', $edit['start_state'], '=')
    ->condition('wss.end_state', $edit['end_state'], '=')
    ->execute()
    ->fetchAssoc();

  // Default behavior - one schedule.
  // Running a test to make sure that a schedule runs when applied to a node.
  $start_ts = mktime(date('H'), date('i'), 0, date('m'), date('d'), date('Y')) - 60;
  $end_ts = mktime(date('H'), date('i'), 0, date('m'), date('d'), date('Y'));
  $node = $this
    ->createScheduledNode($query['sid'], $start_ts, $end_ts);
  $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 - two schedules.
  // Running a test to see that all schedules run when a node as two or more schedules (one for each revision).
  $start_ts = mktime(date('H'), date('i'), 0, date('m'), date('d'), date('Y')) - 120;
  $end_ts = mktime(date('H'), date('i'), 0, date('m'), date('d'), date('Y'));
  $node = $this
    ->createScheduledNode($query['sid'], $start_ts, $end_ts);

  // Add a new schedule that happens after.
  $edit = array();
  $edit['workbench_scheduler_sid'] = $query['sid'];
  $edit['workbench_scheduler_start_date[date]'] = date('Y-m-d', $start_ts + 60);
  $edit['workbench_scheduler_start_date[time]'] = date('H:i', $start_ts + 60);
  $edit['workbench_scheduler_end_date[date]'] = date('Y-m-d', $end_ts);
  $edit['workbench_scheduler_end_date[time]'] = date('H:i', $end_ts);
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $this
    ->cronRun();

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

  // Secondary behavior - one schedule.
  // Update content type settings.
  // TODO: This is returning a minor error, need to fix
  $edit = array();
  $edit['workbench_scheduler[workbench_scheduler_limit_current_revision]'] = 1;
  $this
    ->drupalPost('admin/structure/types/manage/' . $this->content_type, $edit, t('Save content type'));
  $this
    ->assert(TRUE, "Settings updated for content type {$this->content_type}. Run schedules on latest revisions only.");

  // Two revisions, each have their onw schedule.
  $start_ts = mktime(date('H'), date('i'), 0, date('m'), date('d'), date('Y')) - 120;
  $end_ts = mktime(date('H'), date('i'), 0, date('m'), date('d'), date('Y'));
  $node = $this
    ->createScheduledNode($query['sid'], $start_ts, $end_ts);

  // Add a new schedule that happens after.
  $edit = array();
  $edit['workbench_scheduler_sid'] = $query['sid'];
  $edit['workbench_scheduler_start_date[date]'] = date('Y-m-d', $start_ts + 60);
  $edit['workbench_scheduler_start_date[time]'] = date('H:i', $start_ts + 60);
  $edit['workbench_scheduler_end_date[date]'] = date('Y-m-d', $end_ts);
  $edit['workbench_scheduler_end_date[time]'] = date('H:i', $end_ts);
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));

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

  // There should be two completed rows.
  $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
    ->assertTrue($result, "Cron run for {$node->title}: Only schedule for latest revision run.");

  // One revision has a schedule, another revision doesn't.
  $start_ts = mktime(date('H'), date('i'), 0, date('m'), date('d'), date('Y')) - 120;
  $end_ts = mktime(date('H'), date('i'), 0, date('m'), date('d'), date('Y'));
  $node = $this
    ->createScheduledNode($query['sid'], $start_ts, $end_ts);

  // Save new revision (no schedule).
  $edit = array();
  $edit['workbench_scheduler_sid'] = 0;
  $edit['workbench_scheduler_start_date[date]'] = 0;
  $edit['workbench_scheduler_start_date[time]'] = 0;
  $edit['workbench_scheduler_end_date[date]'] = 0;
  $edit['workbench_scheduler_end_date[time]'] = 0;
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));

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

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