From 1d09ec33bdcbcc761a6526e8982859600bd6958e Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Tue, 13 Oct 2009 05:37:46 +0000
Subject: [PATCH] #602958 by chx: Added API function to SimpleTest to run cron,
 avoiding fatal errors.

---
 includes/common.inc                         | 7 +++++--
 modules/search/search.test                  | 4 ++--
 modules/simpletest/drupal_web_test_case.php | 7 +++++++
 modules/system/system.test                  | 5 +----
 modules/trigger/trigger.test                | 2 +-
 5 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/includes/common.inc b/includes/common.inc
index aac56ef14b99..118953c538a7 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -4098,9 +4098,12 @@ function drupal_page_set_cache() {
 }
 
 /**
- * Executes a cron run when called
+ * Executes a cron run when called.
+ *
+ * Do not call this function from test, use $this->cronRun() instead.
+ *
  * @return
- * Returns TRUE if ran successfully
+ *   Returns TRUE if ran successfully
  */
 function drupal_cron_run() {
   // Allow execution to continue even if the request gets canceled.
diff --git a/modules/search/search.test b/modules/search/search.test
index 357789039ee9..2e5750a5876d 100644
--- a/modules/search/search.test
+++ b/modules/search/search.test
@@ -499,7 +499,7 @@ class SearchCommentTestCase extends DrupalWebTestCase {
 
     // Invoke search index update.
     $this->drupalLogout();
-    $this->drupalGet($GLOBALS['base_url'] . '/cron.php', array('external' => TRUE, 'query' => array('cron_key' => variable_get('cron_key', 'drupal'))));
+    $this->cronRun();
 
     // Search for $title.
     $edit = array(
@@ -521,7 +521,7 @@ class SearchCommentTestCase extends DrupalWebTestCase {
 
     // Invoke search index update.
     $this->drupalLogout();
-    $this->drupalGet($GLOBALS['base_url'] . '/cron.php', array('external' => TRUE, 'query' => array('cron_key' => variable_get('cron_key', 'drupal'))));
+    $this->cronRun();
 
     // Search for $title.
     $this->drupalPost('', $edit, t('Search'));
diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php
index edab208755e3..37ff8cfa89b0 100644
--- a/modules/simpletest/drupal_web_test_case.php
+++ b/modules/simpletest/drupal_web_test_case.php
@@ -1499,6 +1499,13 @@ protected function drupalPost($path, $edit, $submit, array $options = array(), a
     }
   }
 
+  /**
+   * Runs cron in the Drupal installed by Simpletest.
+   */
+  protected function cronRun() {
+    $this->drupalGet($GLOBALS['base_url'] . '/cron.php', array('external' => TRUE, 'query' => array('cron_key' => variable_get('cron_key', 'drupal'))));
+  }
+
   /**
    * Check for meta refresh tag and if found call drupalGet() recursively. This
    * function looks for the http-equiv attribute to be set to "Refresh"
diff --git a/modules/system/system.test b/modules/system/system.test
index fdf6887bbce8..78bece99a2f2 100644
--- a/modules/system/system.test
+++ b/modules/system/system.test
@@ -386,9 +386,6 @@ class CronRunTestCase extends DrupalWebTestCase {
     $key = variable_get('cron_key', 'drupal');
     $this->drupalGet($base_url . '/cron.php', array('external' => TRUE, 'query' => array('cron_key' => $key)));
     $this->assertResponse(200);
-
-    // Execute cron directly.
-    $this->assertTrue(drupal_cron_run(), t('Cron ran successfully.'));
   }
 
   /**
@@ -475,7 +472,7 @@ class CronRunTestCase extends DrupalWebTestCase {
     $this->assertTrue(file_exists($perm_new->uri), t('New permanent file was created correctly.'));
 
     // Run cron and then ensure that only the old, temp file was deleted.
-    $this->assertTrue(drupal_cron_run(), t('Cron ran successfully.'));
+    $this->cronRun();
     $this->assertFalse(file_exists($temp_old->uri), t('Old temp file was correctly removed.'));
     $this->assertTrue(file_exists($temp_new->uri), t('New temp file was correctly ignored.'));
     $this->assertTrue(file_exists($perm_old->uri), t('Old permanent file was correctly ignored.'));
diff --git a/modules/trigger/trigger.test b/modules/trigger/trigger.test
index 99a001640948..7204dc395670 100644
--- a/modules/trigger/trigger.test
+++ b/modules/trigger/trigger.test
@@ -165,7 +165,7 @@ class TriggerCronTestCase extends DrupalWebTestCase {
     $this->drupalPost('admin/structure/trigger/system', $edit, t('Assign'));
 
     // Force a cron run.
-    drupal_cron_run();
+    $this->cronRun();
 
     // Make sure the non-configurable action has fired.
     $action_run = variable_get('trigger_test_system_cron_action', FALSE);
-- 
GitLab