Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Pull changes from D7 branch into original master #2

Open
wants to merge 16 commits into from

1 participant

Ben Buckman
Ben Buckman

Fixed verbose() and other bugs, added file dump mechanism, added HTTP auth mechanism to phpunit.xml, other changes.
Split drupal_test_case.php into 2 files, upal.php and DrupalTestCase.class.php.
Does not fully copy the changes in my master branch, because they are a) backported to D6 and b) meant for a ready-made sandbox site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 16 unique commits by 1 author.

Dec 01, 2011
Ben Buckman partial cherry-pick 540678b from master: making verbose() work with p…
…rinter object
805637c
Ben Buckman cherry-pick 90e2413, a35cfd8 from master: handle http auth credential…
…s in phpunit.xml
b54354b
Ben Buckman partial cherry-pick 702ff50 from master: verbose() made static 7461481
Ben Buckman manual [cleaner] replication of 67beb30: move class to its own file, …
…rename drupal_test_case to upal
7a46e96
Ben Buckman modify example phpunit.xml with new commands f7e2be7
Ben Buckman return TRUE on assertTrue for drupalLogin to work bb19fb3
Ben Buckman comment about 'pass' return 11f8809
Ben Buckman partial cherry-pick of c38083a: crude mechanism to dump current CURL'…
…d html to file system
6c0828a
Ben Buckman cherry-pick 720a97b: add AssertNotEqual 4ccbf8f
Ben Buckman partial cherry-pick eaa8a0: line break before and after verbose output 4cba714
Ben Buckman manual c-p 827858b: fix message parameter 3b69acc
Ben Buckman include URL in output on content dump
(cherry picked from commit a47275c)
ee54826
Ben Buckman fix assertFieldById to work without value
(cherry picked from commit 15264c0)
4245649
Ben Buckman manual c-p 90b3f64,ed89e5b: cleanup logic for created nodes and users 4c3ee5c
Ben Buckman description param for dumpContentToFile()
(cherry picked from commit 5d92aa9)
f5f2518
Ben Buckman method to set and restore variables on tearDown
(cherry picked from commit 9cf5764)
61d7c9c
This page is out of date. Refresh to see the latest.
270  drupal_test_case.php → DrupalTestCase.class.php
... ...
@@ -1,27 +1,6 @@
1 1
 <?php
2 2
 
3 3
 /*
4  
- * @file
5  
- *   Test Framework for Drupal based on PHPUnit.
6  
- *
7  
- *   @todo
8  
- *     - Simpletest's assertFalse casts to boolean whereas PHPUnit requires boolean. See testSiteWideContact().
9  
- *     - hard coded TRUE at end of drupalLogin() because PHPUnit doesn't return
10  
- *       anything from an assertion (unlike simpletest). Even if we fix drupalLogin(),
11  
- *       we have to fix this to get 100% compatibility with simpletest.
12  
- *     - setUp() only resets DB for mysql. Probably should use Drush and thus
13  
- *       support postgres and sqlite easily. That buys us auto creation of upal DB
14  
- *       as well.
15  
- *     - Unlikely: Instead of DB restore, clone as per http://drupal.org/node/666956.
16  
- *     - error() could log $caller info.
17  
- *     - Fix verbose().
18  
- *     - Fix random test failures.
19  
- *     - Split into separate class files and add autoloader for upal.
20  
- *     - Compare speed versus simpletest.
21  
- *     - move upal_init() to a class thats called early in the suite.
22  
- */
23  
-
24  
-/*
25 4
  * @todo: Perhaps move these annotations down to the instance classes and tests.
26 5
  *
27 6
  * @runTestsInSeparateProcess
@@ -150,6 +129,19 @@
150 129
    * The number of redirects followed during the handling of a request.
151 130
    */
152 131
   protected $redirect_count;
  132
+  
  133
+  
  134
+  /**
  135
+   * stuff to clean up on tearDown()
  136
+   */
  137
+  protected $cleanup = array();
  138
+
  139
+
  140
+  /**
  141
+   * for verbose output
  142
+   */
  143
+  protected static $printer;
  144
+
153 145
 
154 146
   public function run(PHPUnit_Framework_TestResult $result = NULL) {
155 147
     $this->setPreserveGlobalState(FALSE);
@@ -206,14 +198,27 @@ protected function error($message = '', $group = 'Other', array $caller = NULL)
206 198
     return $this->fail('exception: ' . $message, $group);
207 199
   }
208 200
 
209  
-  function assertEqual($expected, $actual, $msg = NULL) {
210  
-    return $this->assertEquals($expected, $actual);
  201
+  // legacy supports
  202
+
  203
+  public static function assertTrue($condition, $message = '') {
  204
+    parent::assertTrue($condition, $message);
  205
+    return TRUE;  // needed for simpletest back-comp (e.g. in drupalLogin), but dumb / always true.
  206
+  }
  207
+
  208
+  function assertEqual($expected, $actual, $message = '') {
  209
+    return $this->assertEquals($expected, $actual, $message);
  210
+  }
  211
+
  212
+  function assertNotEqual($expected, $actual, $message = '') {
  213
+    return $this->assertNotEquals($expected, $actual, $message);
211 214
   }
212 215
 
213 216
   function assertIdentical($first, $second, $message = '', $group = 'Other') {
214 217
     return $this->assertSame($first, $second, $message);
215 218
   }
216 219
 
  220
+
  221
+
217 222
   /**
218 223
    * Pass if the internal browser's URL matches the given path.
219 224
    *
@@ -517,7 +522,7 @@ protected function assertFieldByXPath($xpath, $value = NULL, $message = '', $gro
517 522
 
518 523
     // If value specified then check array for match.
519 524
     $found = TRUE;
520  
-    if (isset($value)) {
  525
+    if (!empty($value)) {
521 526
       $found = FALSE;
522 527
       if ($fields) {
523 528
         foreach ($fields as $field) {
@@ -653,7 +658,7 @@ protected function assertNoFieldByName($name, $value = '', $message = '') {
653 658
    * @return
654 659
    *   TRUE on pass, FALSE on fail.
655 660
    */
656  
-  protected function assertFieldById($id, $value = '', $message = '') {
  661
+  protected function assertFieldById($id, $value = NULL, $message = '') {
657 662
     return $this->assertFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : t('Found field by id @id', array('@id' => $id)), t('Browser'));
658 663
   }
659 664
 
@@ -1096,18 +1101,42 @@ public static function generatePermutations($parameters) {
1096 1101
     return $all_permutations;
1097 1102
   }
1098 1103
 
1099  
-  function verbose($message) {
1100  
-    if (strlen($message) < 500) {
1101  
-      // $this->log($message, 'verbose');
  1104
+
  1105
+  public static function verbose($message, $cutoff = 500) {
  1106
+
  1107
+    // init printer on first time
  1108
+    if (! self::$printer instanceof PHPUnit_TextUI_ResultPrinter) {
  1109
+      self::$printer = new PHPUnit_TextUI_ResultPrinter('php://stdout', TRUE, TRUE, TRUE);   // can change to stderr
  1110
+      //echo "SET UP PRINTER!!!\n";  // (this works too, but not as good...?)
1102 1111
     }
  1112
+
  1113
+    // default limit to arbitrary length
  1114
+    if (is_int($cutoff) && strlen($message) > $cutoff) {
  1115
+      $message = truncate_utf8($message, $cutoff, FALSE, TRUE);
  1116
+    }
  1117
+
  1118
+    //$this->log($message, 'verbose');      // this doesn't do anything
  1119
+    //echo "[verbose] " . $message . "\n";  // this works but is crude
  1120
+
  1121
+    self::$printer->write("\n" . $message . "\n");  // seems to be a more native approach
  1122
+  }
  1123
+
  1124
+  /**
  1125
+   * output objects
  1126
+   */
  1127
+  public function debug($obj, $heading = '') {
  1128
+   self::verbose( (empty($heading) ? '' : "* {$heading}:\n") . print_r($obj,TRUE), NULL );
1103 1129
   }
1104 1130
 
  1131
+
  1132
+
1105 1133
   /**
1106 1134
    * Create a user with a given set of permissions. The permissions correspond to the
1107 1135
    * names given on the privileges page.
1108 1136
    *
1109 1137
    * @param $permissions
1110 1138
    *   Array of permission names to assign to user.
  1139
+   * @param $cleanup if TRUE, gets deleted in tearDown()
1111 1140
    * @return
1112 1141
    *   A fully loaded user object with pass_raw property, or FALSE if account
1113 1142
    *   creation fails.
@@ -1132,6 +1161,11 @@ protected function drupalCreateUser($permissions = array('access comments', 'acc
1132 1161
     if (empty($account->uid)) {
1133 1162
       return FALSE;
1134 1163
     }
  1164
+    
  1165
+    // mark for cleanup on tearDown()
  1166
+    if ($cleanup) {
  1167
+      $this->cleanup['users'][] = $account->uid;
  1168
+    }
1135 1169
 
1136 1170
     // Add the raw password so that we can log in as this user.
1137 1171
     $account->pass_raw = $edit['pass'];
@@ -1211,7 +1245,7 @@ protected function drupalGet($path, array $options = array(), array $headers = a
1211 1245
   /**
1212 1246
    * Initializes the cURL connection.
1213 1247
    *
1214  
-   * If the simpletest_httpauth_credentials variable is set, this function will
  1248
+   * If the UPAL_HTTP_USER and UPAL_HTTP_PASS config vars are set, this function will
1215 1249
    * add HTTP authentication headers. This is necessary for testing sites that
1216 1250
    * are protected by login credentials from public access.
1217 1251
    * See the description of $curl_options for other options.
@@ -1231,10 +1265,16 @@ protected function curlInitialize() {
1231 1265
         CURLOPT_HEADERFUNCTION => array(&$this, 'curlHeaderCallback'),
1232 1266
         CURLOPT_USERAGENT => 'Upal',
1233 1267
       );
1234  
-      if (isset($this->httpauth_credentials)) {
  1268
+
  1269
+      // http credentials optionally in phpunit.xml
  1270
+      if (isset($GLOBALS['UPAL_HTTP_USER']) && isset($GLOBALS['UPAL_HTTP_PASS']) && !empty($GLOBALS['UPAL_HTTP_USER']) && !empty($GLOBALS['UPAL_HTTP_PASS'])) {
  1271
+        $this->httpauth_credentials = $GLOBALS['UPAL_HTTP_USER'] . ':' . $GLOBALS['UPAL_HTTP_PASS'];
  1272
+
1235 1273
         $curl_options[CURLOPT_HTTPAUTH] = $this->httpauth_method;
1236 1274
         $curl_options[CURLOPT_USERPWD] = $this->httpauth_credentials;
1237 1275
       }
  1276
+
  1277
+
1238 1278
       curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options);
1239 1279
 
1240 1280
       // By default, the child session name should be the same as the parent.
@@ -1651,6 +1691,7 @@ protected function drupalLogout() {
1651 1691
     $pass = $this->assertField('name', t('Username field found.'), t('Logout'));
1652 1692
     $pass = $pass && $this->assertField('pass', t('Password field found.'), t('Logout'));
1653 1693
 
  1694
+    // [bb] $pass here is forced to TRUE, but really phpunit doesn't return TRUE on success, so this runs either way:
1654 1695
     if ($pass) {
1655 1696
       $this->loggedInUser = FALSE;
1656 1697
     }
@@ -1662,10 +1703,11 @@ protected function drupalLogout() {
1662 1703
    * @param $settings
1663 1704
    *   An associative array of settings to change from the defaults, keys are
1664 1705
    *   node properties, for example 'title' => 'Hello, world!'.
  1706
+   * @param $cleanup if TRUE, gets deleted in tearDown()
1665 1707
    * @return
1666 1708
    *   Created node object.
1667 1709
    */
1668  
-  protected function drupalCreateNode($settings = array()) {
  1710
+  protected function drupalCreateNode($settings = array(), $cleanup = TRUE) {
1669 1711
     // Populate defaults array.
1670 1712
     $settings += array(
1671 1713
       'body'      => array(LANGUAGE_NONE => array(array())),
@@ -1709,6 +1751,11 @@ protected function drupalCreateNode($settings = array()) {
1709 1751
 
1710 1752
     $node = (object) $settings;
1711 1753
     node_save($node);
  1754
+    
  1755
+    // mark for cleanup on tearDown()
  1756
+    if ($cleanup) {
  1757
+      $this->cleanup['nodes'][] = $node->nid;
  1758
+    }
1712 1759
 
1713 1760
     // Small hack to link revisions to our test user.
1714 1761
     db_update('node_revision')
@@ -1717,6 +1764,19 @@ protected function drupalCreateNode($settings = array()) {
1717 1764
       ->execute();
1718 1765
     return $node;
1719 1766
   }
  1767
+  
  1768
+  
  1769
+  /**
  1770
+   * [added] wrapper around variable_set, saves original value for restore on tearDown.
  1771
+   */
  1772
+  function drupalVariableSet($key, $value, $cleanup = TRUE) {
  1773
+    if ($cleanup) {
  1774
+      $this->cleanup['variables'][$key] = variable_get($key, 0);
  1775
+    }
  1776
+    
  1777
+    variable_set($key, $value);
  1778
+  }
  1779
+  
1720 1780
 
1721 1781
   /**
1722 1782
    * Creates a custom content type based on default settings.
@@ -2315,10 +2375,102 @@ protected function drupalSetSettings($settings) {
2315 2375
     $this->drupalSettings = $settings;
2316 2376
   }
2317 2377
 
2318  
- }
  2378
+
  2379
+
  2380
+  /**
  2381
+   * crude mechanism to dump current CURL'd html to file system
  2382
+   * dir set in phpunit.xml as DUMP_DIR. 
  2383
+   * [tried and removed: create subdir for this test run and file for each dump]
  2384
+   *
  2385
+   * @param $description optional description of file dump for the logs
  2386
+   */
  2387
+  public function dumpContentToFile($description = '') {
  2388
+    $dump_dir = isset($GLOBALS['DUMP_DIR']) ? $GLOBALS['DUMP_DIR'] : NULL;
  2389
+    if (empty($dump_dir) || !file_exists($dump_dir)) {
  2390
+      $this->error("Missing or invalid DUMP_DIR in " . __FUNCTION__);
  2391
+      return;
  2392
+    }
  2393
+
  2394
+
  2395
+    // tried per-run subdir, but dropped; should be handled separately in jenkins/bash
  2396
+    //static $run_ts = NULL;
  2397
+    //  if (! $run_ts) $run_ts = date('Y-m-d_H:m:s');
  2398
+
  2399
+    //$dump_dir .= '/' . $run_ts;
  2400
+    //if (! file_exists($dump_dir)) {
  2401
+    //  $made = mkdir($dump_dir);
  2402
+    //  if (! $made) {
  2403
+    //    $this->error("Unable to create dump subdir $dump_dir");
  2404
+    //    return;
  2405
+    //  }
  2406
+    //}
  2407
+
  2408
+    $filename = $this->getUrl();
  2409
+    $filename = str_replace('/', '-', $filename);
  2410
+    $filename = str_replace(':', '', $filename);
  2411
+    $filename = str_replace('.', '_', $filename);
  2412
+
  2413
+    // add counter to identify order and prevent dups/overrides of same URL
  2414
+    static $count = 0;
  2415
+    $filename = (++$count) . '-' . $filename . '.html';
  2416
+
  2417
+    $filepath = realpath($dump_dir) . '/' . $filename;
  2418
+    
  2419
+    //$this->verbose(sprintf("Dumping content to %s", $filepath));
  2420
+
  2421
+    $put = file_put_contents($filepath, $this->drupalGetContent());
  2422
+    if ($put === FALSE) $this->error("Unable to dump content to $filepath.");
  2423
+    else $this->verbose("Dumped content of " . $this->getUrl() . " to $filepath." . (empty($description) ? '' : " ($description)") );
  2424
+  }
  2425
+
  2426
+
  2427
+  /**
  2428
+   * tear down after tests: added for cleanup.
  2429
+   */
  2430
+  function tearDown() {
  2431
+    
  2432
+    // nodes
  2433
+    if (is_array($this->cleanup['nodes'])) {
  2434
+      foreach($this->cleanup['nodes'] as $nid) {
  2435
+        if (is_numeric($nid)) {
  2436
+          $this->verbose("Cleanup: deleting node [{$nid}]");
  2437
+          node_delete($nid);
  2438
+        }
  2439
+      }
  2440
+    }
  2441
+    
  2442
+    // users
  2443
+    if (is_array($this->cleanup['users'])) {
  2444
+      foreach($this->cleanup['users'] as $uid) {
  2445
+        if (is_numeric($uid)) {
  2446
+          $this->verbose("Cleanup: deleting user [{$uid}]");
  2447
+          user_delete(array(), $uid);          
  2448
+        }
  2449
+      }
  2450
+    }
  2451
+    
  2452
+    // [restore] variables
  2453
+    if (is_array($this->cleanup['variables'])) {
  2454
+      foreach($this->cleanup['variables'] as $key => $value) {
  2455
+        $this->verbose("Cleanup: restoring variable [{$key}]");
  2456
+        variable_set($key, $value);
  2457
+      }
  2458
+    }
  2459
+    
  2460
+    // @todo apply this $cleanup logic to other created stuff.
  2461
+    
  2462
+    parent::tearDown();
  2463
+  }
  2464
+
  2465
+
  2466
+
  2467
+} // abstract class DrupalTestCase
  2468
+
2319 2469
 
2320 2470
 class DrupalUnitTestCase extends DrupalTestCase {
2321 2471
   function setUp() {
  2472
+    $this->verbose("Setting up " . get_class($this));
  2473
+
2322 2474
     parent::setUp();
2323 2475
 
2324 2476
     if (!defined('DRUPAL_ROOT')) {
@@ -2331,6 +2483,8 @@ function setUp() {
2331 2483
 
2332 2484
 class DrupalWebTestCase extends DrupalTestCase {
2333 2485
   public function setUp() {
  2486
+    $this->verbose("Setting up " . get_class($this));
  2487
+
2334 2488
     parent::setUp();
2335 2489
 
2336 2490
     if (!defined('DRUPAL_ROOT')) {
@@ -2409,55 +2563,3 @@ public function setUp() {
2409 2563
 
2410 2564
   }
2411 2565
 }
2412  
-
2413  
-/*
2414  
- * Initialize our environment at the start of each run (i.e. suite).
2415  
- */
2416  
-function upal_init() {
2417  
-  // UNISH_DRUSH value can come from phpunit.xml or `which drush`.
2418  
-  if (!defined('UNISH_DRUSH')) {
2419  
-    // Let the UNISH_DRUSH environment variable override if set.
2420  
-    $unish_drush = isset($_SERVER['UNISH_DRUSH']) ? $_SERVER['UNISH_DRUSH'] : NULL;
2421  
-    $unish_drush = isset($GLOBALS['UNISH_DRUSH']) ? $GLOBALS['UNISH_DRUSH'] : $unish_drush;
2422  
-    if (empty($unish_drush)) {
2423  
-      // $unish_drush = Drush_TestCase::is_windows() ? exec('for %i in (drush) do @echo.   %~$PATH:i') : trim(`which drush`);
2424  
-      $unish_drush = trim(`which drush`);
2425  
-    }
2426  
-    define('UNISH_DRUSH', $unish_drush);
2427  
-  }
2428  
-
2429  
-  // We read from globals here because env can be empty and ini did not work in quick test.
2430  
-  define('UPAL_DB_URL', getenv('UPAL_DB_URL') ? getenv('UPAL_DB_URL') : (!empty($GLOBALS['UPAL_DB_URL']) ? $GLOBALS['UPAL_DB_URL'] : 'mysql://root:@127.0.0.1/upal'));
2431  
-
2432  
-  // Make sure we use the right Drupal codebase.
2433  
-  define('UPAL_ROOT', getenv('UPAL_ROOT') ? getenv('UPAL_ROOT') : (isset($GLOBALS['UPAL_ROOT']) ? $GLOBALS['UPAL_ROOT'] : realpath('.')));
2434  
-  chdir(UPAL_ROOT);
2435  
-
2436  
-  // The URL that browser based tests (ewwwww) should use.
2437  
-  define('UPAL_WEB_URL', getenv('UPAL_WEB_URL') ? getenv('UPAL_WEB_URL') : (isset($GLOBALS['UPAL_WEB_URL']) ? $GLOBALS['UPAL_WEB_URL'] : 'http://upal'));
2438  
-
2439  
-
2440  
-  // Set the env vars that Derupal expects. Largely copied from drush.
2441  
-  $url = parse_url(UPAL_WEB_URL);
2442  
-
2443  
-  if (array_key_exists('path', $url)) {
2444  
-    $_SERVER['PHP_SELF'] = $url['path'] . '/index.php';
2445  
-  }
2446  
-  else {
2447  
-    $_SERVER['PHP_SELF'] = '/index.php';
2448  
-  }
2449  
-
2450  
-  $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
2451  
-  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
2452  
-  $_SERVER['REQUEST_METHOD']  = NULL;
2453  
-
2454  
-  $_SERVER['SERVER_SOFTWARE'] = NULL;
2455  
-  $_SERVER['HTTP_USER_AGENT'] = NULL;
2456  
-
2457  
-  $_SERVER['HTTP_HOST'] = $url['host'];
2458  
-  $_SERVER['SERVER_PORT'] = array_key_exists('port', $url) ? $url['port'] : NULL;
2459  
-}
2460  
-
2461  
- // This code is in global scope.
2462  
- // TODO: I would rather this code at top of file, but I get Fatal error: Class 'Drush_TestCase' not found
2463  
- upal_init();
15  phpunit.xml.dist
@@ -2,7 +2,7 @@
2 2
 <phpunit backupGlobals="false"
3 3
     backupStaticAttributes="false"
4 4
     syntaxCheck="false"
5  
-    bootstrap="drupal_test_case.php">
  5
+    bootstrap="upal.php">
6 6
   <php>
7 7
     <!-- These variables may alternatively be set as bash environment variables. -->
8 8
 
@@ -15,11 +15,20 @@
15 15
     <!-- <var name="UPAL_WEB_URL" value="http://example.com/upal"/> -->
16 16
 
17 17
     <!--Hard code a fileystem path to the to-be-tested Drupal. Defaults to cwd.-->
18  
-    <!--<var name="UPAL_ROOT" value="/Users/mw/htd/drupal"/>-->
  18
+    <!--<var name="UPAL_ROOT" value="/Users/example/drupal"/>-->
19 19
 
20 20
     <!--Uncomment the line below if your path to drush differs from `which drush`. Use absolute path.-->
21 21
     <!--You currently need 'master' branch of drush after 2011.07.21. Drush 4.6 will be OK - http://drupal.org/node/1105514-->
22  
-    <!--<var name="UNISH_DRUSH" value="/Users/mw/bin/drush"/>-->
  22
+    <!--<var name="UNISH_DRUSH" value="/Users/example/bin/drush"/>-->
  23
+
  24
+
  25
+    <!-- optional HTTP authentication to sandbox -->
  26
+    <var name="UPAL_HTTP_USER" value="user"/>
  27
+    <var name="UPAL_HTTP_PASS" value="pass"/>
  28
+    
  29
+    <!-- optional for html/content dumps -->
  30
+    <var name="DUMP_DIR" value="/Users/example/tests-dump" />
  31
+    
23 32
 
24 33
     <includePath>.</includePath>
25 34
   </php>
79  upal.php
... ...
@@ -0,0 +1,79 @@
  1
+<?php
  2
+
  3
+/*
  4
+ * @file
  5
+ *   Test Framework for Drupal based on PHPUnit.
  6
+ *
  7
+ *   @todo
  8
+ *     - Simpletest's assertFalse casts to boolean whereas PHPUnit requires boolean. See testSiteWideContact().
  9
+ *     - hard coded TRUE at end of drupalLogin() because PHPUnit doesn't return
  10
+ *       anything from an assertion (unlike simpletest). Even if we fix drupalLogin(),
  11
+ *       we have to fix this to get 100% compatibility with simpletest.
  12
+ *     - setUp() only resets DB for mysql. Probably should use Drush and thus
  13
+ *       support postgres and sqlite easily. That buys us auto creation of upal DB
  14
+ *       as well.
  15
+ *     - Unlikely: Instead of DB restore, clone as per http://drupal.org/node/666956.
  16
+ *     - error() could log $caller info.
  17
+ *     - Fix random test failures.
  18
+ *     - Split into separate class files and add autoloader for upal.
  19
+ *     - Compare speed versus simpletest.
  20
+ *     - move upal_init() to a class thats called early in the suite.
  21
+ */
  22
+
  23
+// class moved to its own file
  24
+include_once 'DrupalTestCase.class.php';
  25
+
  26
+upal_init();
  27
+
  28
+
  29
+/*
  30
+ * Initialize our environment at the start of each run (i.e. suite).
  31
+ */
  32
+function upal_init() {
  33
+  // UNISH_DRUSH value can come from phpunit.xml or `which drush`.
  34
+  if (!defined('UNISH_DRUSH')) {
  35
+    // Let the UNISH_DRUSH environment variable override if set.
  36
+    $unish_drush = isset($_SERVER['UNISH_DRUSH']) ? $_SERVER['UNISH_DRUSH'] : NULL;
  37
+    $unish_drush = isset($GLOBALS['UNISH_DRUSH']) ? $GLOBALS['UNISH_DRUSH'] : $unish_drush;
  38
+    if (empty($unish_drush)) {
  39
+      // $unish_drush = Drush_TestCase::is_windows() ? exec('for %i in (drush) do @echo.   %~$PATH:i') : trim(`which drush`);
  40
+      $unish_drush = trim(`which drush`);
  41
+    }
  42
+    define('UNISH_DRUSH', $unish_drush);
  43
+  }
  44
+
  45
+  // We read from globals here because env can be empty and ini did not work in quick test.
  46
+  define('UPAL_DB_URL', getenv('UPAL_DB_URL') ? getenv('UPAL_DB_URL') : (!empty($GLOBALS['UPAL_DB_URL']) ? $GLOBALS['UPAL_DB_URL'] : 'mysql://root:@127.0.0.1/upal'));
  47
+
  48
+  // Make sure we use the right Drupal codebase.
  49
+  define('UPAL_ROOT', getenv('UPAL_ROOT') ? getenv('UPAL_ROOT') : (isset($GLOBALS['UPAL_ROOT']) ? $GLOBALS['UPAL_ROOT'] : realpath('.')));
  50
+  chdir(UPAL_ROOT);
  51
+
  52
+  // The URL that browser based tests (ewwwww) should use.
  53
+  define('UPAL_WEB_URL', getenv('UPAL_WEB_URL') ? getenv('UPAL_WEB_URL') : (isset($GLOBALS['UPAL_WEB_URL']) ? $GLOBALS['UPAL_WEB_URL'] : 'http://upal'));
  54
+
  55
+
  56
+  // http credentials (optional)
  57
+  $GLOBALS['UPAL_HTTP_USER'] = getenv('UPAL_HTTP_USER') ? getenv('UPAL_HTTP_USER') : (isset($GLOBALS['UPAL_HTTP_USER']) ? $GLOBALS['UPAL_HTTP_USER'] : NULL);
  58
+  $GLOBALS['UPAL_HTTP_PASS'] = getenv('UPAL_HTTP_PASS') ? getenv('UPAL_HTTP_PASS') : (isset($GLOBALS['UPAL_HTTP_PASS']) ? $GLOBALS['UPAL_HTTP_PASS'] : NULL);
  59
+
  60
+  // Set the env vars that Drupal expects. Largely copied from drush.
  61
+  $url = parse_url(UPAL_WEB_URL);
  62
+
  63
+  if (array_key_exists('path', $url)) {
  64
+    $_SERVER['PHP_SELF'] = $url['path'] . '/index.php';
  65
+  }
  66
+  else {
  67
+    $_SERVER['PHP_SELF'] = '/index.php';
  68
+  }
  69
+
  70
+  $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
  71
+  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  72
+  $_SERVER['REQUEST_METHOD']  = NULL;
  73
+
  74
+  $_SERVER['SERVER_SOFTWARE'] = NULL;
  75
+  $_SERVER['HTTP_USER_AGENT'] = NULL;
  76
+
  77
+  $_SERVER['HTTP_HOST'] = $url['host'];
  78
+  $_SERVER['SERVER_PORT'] = array_key_exists('port', $url) ? $url['port'] : NULL;
  79
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.