From 5109a588cdee3669e7b863f280da60a36106239b Mon Sep 17 00:00:00 2001
From: partdigital <jbolduc@genuineinteractive.com>
Date: Tue, 16 Dec 2014 11:27:19 -0500
Subject: [PATCH] Apply Patch: workbench_scheduler.simpletests-2343015.7.patch

---
 tests/workbench_scheduler.test | 293 ++++++++++++++++++++++++++++++++++++++++-
 workbench_scheduler.info       |   2 +-
 workbench_scheduler.module     |   4 +-
 3 files changed, 290 insertions(+), 9 deletions(-)

diff --git a/tests/workbench_scheduler.test b/tests/workbench_scheduler.test
index bb3d115..c795d59 100644
--- a/tests/workbench_scheduler.test
+++ b/tests/workbench_scheduler.test
@@ -4,18 +4,299 @@
  * @file
  * Tests for workbench_scheduler.module
  */
+class WorkbenchSchedulerTestCase extends DrupalWebTestCase {
 
-class WorkbenchSchedulerTestCase extends DrupalUnitTestCase {
+  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',
+     );
+
+  }
 
-  /**
-   * One using of this function is to enable the module used for testing, any dependencies
-   * or anything else that might be universal for all tests
-   */
   function setUp($modules = array()) {
-    $modules = array_merge($modules, array('workbench_scheduler'));
     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}'");
+      $result = $query->execute()->rowCount();
+
+      $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.");
+  }
 }
diff --git a/workbench_scheduler.info b/workbench_scheduler.info
index a5abfec..1ac6693 100644
--- a/workbench_scheduler.info
+++ b/workbench_scheduler.info
@@ -13,4 +13,4 @@ files[] = workbench_scheduler.views.inc
 files[] = workbench_scheduler.features.inc
 files[] = includes/workbench_scheduler_handler_filter_state.inc
 files[] = includes/workbench_scheduler_handler_filter_name.inc
-files[] = tests/*.test
+files[] = tests/workbench_scheduler.test
diff --git a/workbench_scheduler.module b/workbench_scheduler.module
index ea1c06a..587c514 100644
--- a/workbench_scheduler.module
+++ b/workbench_scheduler.module
@@ -308,7 +308,7 @@ function workbench_scheduler_form_node_form_alter(&$form, &$form_state, $form_id
         //display message depending on schedule settings.
     $msg = '';
 
-    $type_settings = variable_get('workbench_scheduler_'.$form['#node']->type, FALSE);
+    $type_settings = variable_get('workbench_scheduler_'.$form['#node']->type, array());
     // Check if only process latest revision
     if(in_array('workbench_scheduler_limit_current_revision', $type_settings)){
       $msg = t('Only the schedule for the most recent revision will be run.');
@@ -1198,7 +1198,7 @@ function workbench_scheduler_process_end_dates($timestamp) {
     foreach ($scheduled_nodes as $node_schedule) {
 
       $do_process = FALSE;
-      $type_settings = variable_get('workbench_scheduler_'.$node_schedule->type, FALSE);
+      $type_settings = variable_get('workbench_scheduler_'.$node_schedule->type, array());
 
       // Only process latest revision
       if(in_array('workbench_scheduler_limit_current_revision', $type_settings)){
-- 
1.9.2

