You are here

workbench_scheduler.test in Workbench Scheduler 7

Same filename and directory in other branches
  1. 7.2 tests/workbench_scheduler.test

File

tests/workbench_scheduler.test
View source
<?php

/**
 * @file
 * Tests for workbench_scheduler.module.
 */
class WorkbenchSchedulerTestCase extends DrupalWebTestCase {
  protected $admin_user;
  protected $content_type;
  function setUp($modules = array()) {
    $modules = array_merge($modules, array(
      'workbench_scheduler',
      'workbench_moderation',
    ));
    parent::setUp($modules);

    // Create a new content type and enable moderation on it.
    $type = $this
      ->drupalCreateContentType();
    $this->content_type = $type->name;
    variable_set('node_options_' . $this->content_type, array(
      'revision',
      'moderation',
    ));
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'bypass node access',
      'administer nodes',
      'view revisions',
      'view all unpublished content',
      'view moderation history',
      'view moderation messages',
      'bypass workbench moderation',
      "create {$this->content_type} content",
      'administer workbench schedules',
      'administer content types',
      'set workbench schedule',
      'set any workbench schedule',
    ));
  }

}

/**
 * Create Schedule.
 */
class WorkbenchSchedulerScheduleTestCase extends WorkbenchSchedulerTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Create Workbench Schedule',
      'description' => 'Create a new workbench schedule',
      'group' => 'Workbench Scheduler',
    );
  }
  function setUp($modules = array()) {
    parent::setUp($modules);
    $this
      ->drupalLogin($this->admin_user);
  }
  function testScheduleCreate() {
    $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['#schedule'] = new stdClass;
    $edit["types[{$this->content_type}]"] = $this->content_type;
    $this
      ->drupalPost('admin/config/workbench/scheduler/schedules/add', $edit, t('Save'));

    // Checking database integrity to see if it was created successfully.
    $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'], '=')
      ->range(0, 1);

    // Checking table relationships.
    $query
      ->join('workbench_scheduler_types', 'wst', "wss.name = wst.name AND wst.type ='{$this->content_type}'");
    $query
      ->execute();
    $this
      ->assertTrue($query, 'Workbench Schedule saved');
  }

}

/**
 * Assign node to schedule.
 */
class WorkbenchSchedulerNodeScheduleTestCase extends WorkbenchSchedulerTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Node Save Schedule',
      'description' => 'Apply a schedule to a node',
      'group' => 'Workbench Scheduler',
    );
  }
  public function setUp($modules = array()) {
    parent::setUp($modules);
    $this
      ->drupalLogin($this->admin_user);
  }

  /**
   * Create Node With Schedule.
   * @param $sid
   * @param $time_start
   * @param $time_end
   * @return mixed
   */
  private function createScheduledNode($sid, $time_start, $time_end) {

    // Create node to edit.
    $langcode = LANGUAGE_NONE;
    $body_key = "body[{$langcode}][0][value]";
    $edit = array();
    $edit['title'] = $this
      ->randomName(8);
    $edit[$body_key] = $this
      ->randomName(16);
    $edit['workbench_scheduler_sid'] = $sid;
    $edit['workbench_scheduler_start_date[date]'] = date('Y-m-d', $time_start);
    $edit['workbench_scheduler_start_date[time]'] = date('H:i', $time_start);
    $edit['workbench_scheduler_end_date[date]'] = date('Y-m-d', $time_end);
    $edit['workbench_scheduler_end_date[time]'] = date('H:i', $time_end);
    $this
      ->drupalPost('node/add/' . $this->content_type, $edit, t('Save'));

    // Checking the node.
    $node = $this
      ->drupalGetNodeByTitle($edit['title']);
    $this
      ->assertTrue($node, "Node {$node->title} created");
    $result = db_select('workbench_scheduler_nodes', 'wsn')
      ->fields('wsn')
      ->condition('wsn.nid', $node->nid, '=')
      ->condition('wsn.sid', $sid, '=')
      ->condition('wsn.start_date', $time_start, '=')
      ->condition('wsn.end_date', $time_end, '=')
      ->range(0, 1)
      ->execute()
      ->rowCount();
    $this
      ->assertTrue($result, "Schedule assigned to node {$node->title}");
    return $node;
  }

  /**
   * Test Node Schedule.
   */
  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.");
  }

}