You are here

workbench_scheduler.test in Workbench Scheduler 7.2

Same filename and directory in other branches
  1. 7 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 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() {

    // Create schedule.
    $edit = array();
    $edit['label'] = $this
      ->randomName(8);
    $edit['name'] = strtolower($this
      ->randomName(8));
    $edit['transition'] = 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.transition', $edit['transition'], '=')
      ->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 $schedule
   * @param $ts (optional)
   * @return mixed
   */
  private function createScheduledNode($schedule, $ts = null) {

    // Parse arguments.
    if (is_numeric($schedule) && !empty($ts)) {
      $schedule = array(
        $schedule => $ts,
      );
    }

    // 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);

    // Adding schedules. Supports multple.
    foreach ($schedule as $sid => $time) {
      $edit['workbench_scheduler_sid[' . $sid . ']'] = TRUE;
      $edit['workbench_scheduler_date[' . $sid . '][date]'] = date('m/d/Y', $time);
      $edit['workbench_scheduler_date[' . $sid . '][time]'] = date('H:i', $time);
    }
    $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.date', $time, '=')
      ->execute()
      ->rowCount();
    $this
      ->assertTrue($result, $result . " schedule(s) assigned to node {$node->title}");
    return $node;
  }

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

}