From 805637c3580dffe37d1f926b6c7aeaaa6cc6bd9f Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Thu, 1 Dec 2011 12:22:45 -0300 Subject: [PATCH 01/16] partial cherry-pick 540678bffc9 from master: making verbose() work with printer object --- drupal_test_case.php | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/drupal_test_case.php b/drupal_test_case.php index 26408db..80ca7f9 100644 --- a/drupal_test_case.php +++ b/drupal_test_case.php @@ -151,6 +151,13 @@ abstract class DrupalTestCase extends PHPUnit_Framework_TestCase { */ protected $redirect_count; + + /** + * for verbose output + */ + protected $printer; + + public function run(PHPUnit_Framework_TestResult $result = NULL) { $this->setPreserveGlobalState(FALSE); return parent::run($result); @@ -1096,12 +1103,35 @@ public static function generatePermutations($parameters) { return $all_permutations; } - function verbose($message) { - if (strlen($message) < 500) { - // $this->log($message, 'verbose'); + + public function verbose($message, $cutoff = 500) { + + // init printer on first time + if (! $this->printer instanceof PHPUnit_TextUI_ResultPrinter) { + $this->printer = new PHPUnit_TextUI_ResultPrinter('php://stdout', TRUE, TRUE, TRUE); // can change to stderr + //echo "SET UP PRINTER!!!\n"; // (this works too, but not as good...?) } + + // default limit to arbitrary length + if (is_int($cutoff) && strlen($message) > $cutoff) { + $message = truncate_utf8($message, $cutoff, FALSE, TRUE); + } + + //$this->log($message, 'verbose'); // this doesn't do anything + //echo "[verbose] " . $message . "\n"; // this works but is crude + + $this->printer->write($message . "\n\n"); // seems to be a more native approach } + /** + * output objects + */ + public function debug($obj, $heading = '') { + $this->verbose( (empty($heading) ? '' : "* {$heading}:\n") . print_r($obj,TRUE), NULL ); + } + + + /** * Create a user with a given set of permissions. The permissions correspond to the * names given on the privileges page. @@ -2319,6 +2349,8 @@ protected function drupalSetSettings($settings) { class DrupalUnitTestCase extends DrupalTestCase { function setUp() { + $this->verbose("Setting up " . get_class($this)); + parent::setUp(); if (!defined('DRUPAL_ROOT')) { @@ -2331,6 +2363,8 @@ function setUp() { class DrupalWebTestCase extends DrupalTestCase { public function setUp() { + $this->verbose("Setting up " . get_class($this)); + parent::setUp(); if (!defined('DRUPAL_ROOT')) { From b54354b82b35c0ace346f8598d4f2f67f5d9eeca Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Thu, 1 Dec 2011 12:25:20 -0300 Subject: [PATCH 02/16] cherry-pick 90e241339f, a35cfd8d1 from master: handle http auth credentials in phpunit.xml --- drupal_test_case.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/drupal_test_case.php b/drupal_test_case.php index 80ca7f9..4af4806 100644 --- a/drupal_test_case.php +++ b/drupal_test_case.php @@ -1241,7 +1241,7 @@ protected function drupalGet($path, array $options = array(), array $headers = a /** * Initializes the cURL connection. * - * If the simpletest_httpauth_credentials variable is set, this function will + * If the UPAL_HTTP_USER and UPAL_HTTP_PASS config vars are set, this function will * add HTTP authentication headers. This is necessary for testing sites that * are protected by login credentials from public access. * See the description of $curl_options for other options. @@ -1261,10 +1261,16 @@ protected function curlInitialize() { CURLOPT_HEADERFUNCTION => array(&$this, 'curlHeaderCallback'), CURLOPT_USERAGENT => 'Upal', ); - if (isset($this->httpauth_credentials)) { + + // http credentials optionally in phpunit.xml + if (isset($GLOBALS['UPAL_HTTP_USER']) && isset($GLOBALS['UPAL_HTTP_PASS']) && !empty($GLOBALS['UPAL_HTTP_USER']) && !empty($GLOBALS['UPAL_HTTP_PASS'])) { + $this->httpauth_credentials = $GLOBALS['UPAL_HTTP_USER'] . ':' . $GLOBALS['UPAL_HTTP_PASS']; + $curl_options[CURLOPT_HTTPAUTH] = $this->httpauth_method; $curl_options[CURLOPT_USERPWD] = $this->httpauth_credentials; } + + curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options); // By default, the child session name should be the same as the parent. @@ -2471,7 +2477,11 @@ function upal_init() { define('UPAL_WEB_URL', getenv('UPAL_WEB_URL') ? getenv('UPAL_WEB_URL') : (isset($GLOBALS['UPAL_WEB_URL']) ? $GLOBALS['UPAL_WEB_URL'] : 'http://upal')); - // Set the env vars that Derupal expects. Largely copied from drush. + // http credentials (optional) + $GLOBALS['UPAL_HTTP_USER'] = getenv('UPAL_HTTP_USER') ? getenv('UPAL_HTTP_USER') : (isset($GLOBALS['UPAL_HTTP_USER']) ? $GLOBALS['UPAL_HTTP_USER'] : NULL); + $GLOBALS['UPAL_HTTP_PASS'] = getenv('UPAL_HTTP_PASS') ? getenv('UPAL_HTTP_PASS') : (isset($GLOBALS['UPAL_HTTP_PASS']) ? $GLOBALS['UPAL_HTTP_PASS'] : NULL); + + // Set the env vars that Drupal expects. Largely copied from drush. $url = parse_url(UPAL_WEB_URL); if (array_key_exists('path', $url)) { From 7461481a41957f8dfb3653d073053af8ed997543 Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Thu, 1 Dec 2011 12:28:28 -0300 Subject: [PATCH 03/16] partial cherry-pick 702ff5037eabd from master: verbose() made static --- drupal_test_case.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/drupal_test_case.php b/drupal_test_case.php index 4af4806..63af118 100644 --- a/drupal_test_case.php +++ b/drupal_test_case.php @@ -14,7 +14,6 @@ * as well. * - Unlikely: Instead of DB restore, clone as per http://drupal.org/node/666956. * - error() could log $caller info. - * - Fix verbose(). * - Fix random test failures. * - Split into separate class files and add autoloader for upal. * - Compare speed versus simpletest. @@ -155,7 +154,7 @@ abstract class DrupalTestCase extends PHPUnit_Framework_TestCase { /** * for verbose output */ - protected $printer; + protected static $printer; public function run(PHPUnit_Framework_TestResult $result = NULL) { @@ -1104,11 +1103,11 @@ public static function generatePermutations($parameters) { } - public function verbose($message, $cutoff = 500) { + public static function verbose($message, $cutoff = 500) { // init printer on first time - if (! $this->printer instanceof PHPUnit_TextUI_ResultPrinter) { - $this->printer = new PHPUnit_TextUI_ResultPrinter('php://stdout', TRUE, TRUE, TRUE); // can change to stderr + if (! self::$printer instanceof PHPUnit_TextUI_ResultPrinter) { + self::$printer = new PHPUnit_TextUI_ResultPrinter('php://stdout', TRUE, TRUE, TRUE); // can change to stderr //echo "SET UP PRINTER!!!\n"; // (this works too, but not as good...?) } @@ -1120,14 +1119,14 @@ public function verbose($message, $cutoff = 500) { //$this->log($message, 'verbose'); // this doesn't do anything //echo "[verbose] " . $message . "\n"; // this works but is crude - $this->printer->write($message . "\n\n"); // seems to be a more native approach + self::$printer->write($message . "\n\n"); // seems to be a more native approach } /** * output objects */ public function debug($obj, $heading = '') { - $this->verbose( (empty($heading) ? '' : "* {$heading}:\n") . print_r($obj,TRUE), NULL ); + self::verbose( (empty($heading) ? '' : "* {$heading}:\n") . print_r($obj,TRUE), NULL ); } From 7a46e96e7dd7a859193934da496efaced75a2d21 Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Thu, 1 Dec 2011 12:35:53 -0300 Subject: [PATCH 04/16] manual [cleaner] replication of 67beb30c3cff: move class to its own file, rename drupal_test_case to upal --- ..._test_case.php => DrupalTestCase.class.php | 76 ------------------ upal.php | 79 +++++++++++++++++++ 2 files changed, 79 insertions(+), 76 deletions(-) rename drupal_test_case.php => DrupalTestCase.class.php (96%) create mode 100644 upal.php diff --git a/drupal_test_case.php b/DrupalTestCase.class.php similarity index 96% rename from drupal_test_case.php rename to DrupalTestCase.class.php index 63af118..9d1267e 100644 --- a/drupal_test_case.php +++ b/DrupalTestCase.class.php @@ -1,25 +1,5 @@ Date: Thu, 1 Dec 2011 12:37:58 -0300 Subject: [PATCH 05/16] modify example phpunit.xml with new commands --- phpunit.xml.dist | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index fcc8c5b..beb3211 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,7 +2,7 @@ + bootstrap="upal.php"> @@ -15,11 +15,20 @@ - + - + + + + + + + + + + . From bb19fb34ebae82096f0b5ac3dcea0b21854ac067 Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Thu, 1 Dec 2011 13:01:25 -0300 Subject: [PATCH 06/16] return TRUE on assertTrue for drupalLogin to work --- DrupalTestCase.class.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DrupalTestCase.class.php b/DrupalTestCase.class.php index 9d1267e..d21d51c 100644 --- a/DrupalTestCase.class.php +++ b/DrupalTestCase.class.php @@ -199,6 +199,12 @@ function assertEqual($expected, $actual, $msg = NULL) { function assertIdentical($first, $second, $message = '', $group = 'Other') { return $this->assertSame($first, $second, $message); } + + public static function assertTrue($condition, $message = '') { + parent::assertTrue($condition, $message = ''); + return TRUE; // needed for simpletest back-comp (e.g. in drupalLogin), but dumb / always true. + } + /** * Pass if the internal browser's URL matches the given path. From 11f8809a886ae2fdc0c520109f773d4df3779ef7 Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Thu, 1 Dec 2011 13:04:13 -0300 Subject: [PATCH 07/16] comment about 'pass' return --- DrupalTestCase.class.php | 1 + 1 file changed, 1 insertion(+) diff --git a/DrupalTestCase.class.php b/DrupalTestCase.class.php index d21d51c..064570b 100644 --- a/DrupalTestCase.class.php +++ b/DrupalTestCase.class.php @@ -1672,6 +1672,7 @@ protected function drupalLogout() { $pass = $this->assertField('name', t('Username field found.'), t('Logout')); $pass = $pass && $this->assertField('pass', t('Password field found.'), t('Logout')); + // [bb] $pass here is forced to TRUE, but really phpunit doesn't return TRUE on success, so this runs either way: if ($pass) { $this->loggedInUser = FALSE; } From 6c0828aed7f7389de28bf6b9f0943566512358d7 Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Thu, 1 Dec 2011 13:06:54 -0300 Subject: [PATCH 08/16] partial cherry-pick of c38083a8: crude mechanism to dump current CURL'd html to file system --- DrupalTestCase.class.php | 50 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/DrupalTestCase.class.php b/DrupalTestCase.class.php index 064570b..7ab6561 100644 --- a/DrupalTestCase.class.php +++ b/DrupalTestCase.class.php @@ -2337,7 +2337,55 @@ protected function drupalSetSettings($settings) { $this->drupalSettings = $settings; } - } + + + /** + * crude mechanism to dump current CURL'd html to file system + * dir set in phpunit.xml as DUMP_DIR. + * [tried and removed: create subdir for this test run and file for each dump] + */ + public function dumpContentToFile() { + $dump_dir = isset($GLOBALS['DUMP_DIR']) ? $GLOBALS['DUMP_DIR'] : NULL; + if (empty($dump_dir) || !file_exists($dump_dir)) { + $this->error("Missing or invalid DUMP_DIR in " . __FUNCTION__); + return; + } + + + // tried per-run subdir, but dropped; should be handled separately in jenkins/bash + //static $run_ts = NULL; + // if (! $run_ts) $run_ts = date('Y-m-d_H:m:s'); + + //$dump_dir .= '/' . $run_ts; + //if (! file_exists($dump_dir)) { + // $made = mkdir($dump_dir); + // if (! $made) { + // $this->error("Unable to create dump subdir $dump_dir"); + // return; + // } + //} + + $filename = $this->getUrl(); + $filename = str_replace('/', '-', $filename); + $filename = str_replace(':', '', $filename); + $filename = str_replace('.', '_', $filename); + + // add counter to identify order and prevent dups/overrides of same URL + static $count = 0; + $filename = (++$count) . '-' . $filename . '.html'; + + $filepath = realpath($dump_dir) . '/' . $filename; + + //$this->verbose(sprintf("Dumping content to %s", $filepath)); + + $put = file_put_contents($filepath, $this->drupalGetContent()); + if ($put === FALSE) $this->error("Unable to dump content to $filepath."); + else $this->verbose("Dumped content to $filepath."); + } + +} // abstract class DrupalTestCase + + class DrupalUnitTestCase extends DrupalTestCase { function setUp() { From 4ccbf8f9c436284640e064626e0a2b62e901fb68 Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Thu, 1 Dec 2011 13:11:36 -0300 Subject: [PATCH 09/16] cherry-pick 720a97b2f0: add AssertNotEqual --- DrupalTestCase.class.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/DrupalTestCase.class.php b/DrupalTestCase.class.php index 7ab6561..85b2c49 100644 --- a/DrupalTestCase.class.php +++ b/DrupalTestCase.class.php @@ -206,6 +206,22 @@ public static function assertTrue($condition, $message = '') { } + // legacy supports + + function assertEqual($expected, $actual, $message = '') { + return $this->assertEquals($expected, $actual, $message); + } + + function assertNotEqual($expected, $actual, $message = '') { + return $this->assertNotEquals($expected, $actual, $message); + } + + function assertIdentical($first, $second, $message = '', $group = 'Other') { + return $this->assertSame($first, $second, $message); + } + + + /** * Pass if the internal browser's URL matches the given path. * From 4cba714a498cae72d644c05043b010fc35df84e8 Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Thu, 1 Dec 2011 13:13:31 -0300 Subject: [PATCH 10/16] partial cherry-pick eaa8a0: line break before and after verbose output --- DrupalTestCase.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DrupalTestCase.class.php b/DrupalTestCase.class.php index 85b2c49..34d9e0e 100644 --- a/DrupalTestCase.class.php +++ b/DrupalTestCase.class.php @@ -1121,7 +1121,7 @@ public static function verbose($message, $cutoff = 500) { //$this->log($message, 'verbose'); // this doesn't do anything //echo "[verbose] " . $message . "\n"; // this works but is crude - self::$printer->write($message . "\n\n"); // seems to be a more native approach + self::$printer->write("\n" . $message . "\n"); // seems to be a more native approach } /** From 3b69accf28da3c7c3739e0e4335b806f5a5ee6a7 Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Thu, 1 Dec 2011 13:14:46 -0300 Subject: [PATCH 11/16] manual c-p 827858b1044: fix message parameter --- DrupalTestCase.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DrupalTestCase.class.php b/DrupalTestCase.class.php index 34d9e0e..92672f9 100644 --- a/DrupalTestCase.class.php +++ b/DrupalTestCase.class.php @@ -201,7 +201,7 @@ function assertIdentical($first, $second, $message = '', $group = 'Other') { } public static function assertTrue($condition, $message = '') { - parent::assertTrue($condition, $message = ''); + parent::assertTrue($condition, $message); return TRUE; // needed for simpletest back-comp (e.g. in drupalLogin), but dumb / always true. } From ee54826d64cfad5165fa09f05802b180c8289024 Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Tue, 29 Nov 2011 00:19:34 -0300 Subject: [PATCH 12/16] include URL in output on content dump (cherry picked from commit a47275ca2dec1a0537f2b41c392bd354e522bea8) --- DrupalTestCase.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DrupalTestCase.class.php b/DrupalTestCase.class.php index 92672f9..308f2a5 100644 --- a/DrupalTestCase.class.php +++ b/DrupalTestCase.class.php @@ -2396,7 +2396,7 @@ public function dumpContentToFile() { $put = file_put_contents($filepath, $this->drupalGetContent()); if ($put === FALSE) $this->error("Unable to dump content to $filepath."); - else $this->verbose("Dumped content to $filepath."); + else $this->verbose("Dumped content of " . $this->getUrl() . " to $filepath."); } } // abstract class DrupalTestCase From 424564934967be02b81e99de6dcd23edfccbb751 Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Tue, 29 Nov 2011 00:47:58 -0300 Subject: [PATCH 13/16] fix assertFieldById to work without value (cherry picked from commit 15264c026cbaba581f2ab2da6b3cf866bb402196) --- DrupalTestCase.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DrupalTestCase.class.php b/DrupalTestCase.class.php index 308f2a5..37193b2 100644 --- a/DrupalTestCase.class.php +++ b/DrupalTestCase.class.php @@ -525,7 +525,7 @@ protected function assertFieldByXPath($xpath, $value = NULL, $message = '', $gro // If value specified then check array for match. $found = TRUE; - if (isset($value)) { + if (!empty($value)) { $found = FALSE; if ($fields) { foreach ($fields as $field) { @@ -661,7 +661,7 @@ protected function assertNoFieldByName($name, $value = '', $message = '') { * @return * TRUE on pass, FALSE on fail. */ - protected function assertFieldById($id, $value = '', $message = '') { + protected function assertFieldById($id, $value = NULL, $message = '') { return $this->assertFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : t('Found field by id @id', array('@id' => $id)), t('Browser')); } From 4c3ee5c9a76e13e1e17ca2b42a31bf01b15f0039 Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Thu, 1 Dec 2011 13:20:55 -0300 Subject: [PATCH 14/16] manual c-p 90b3f64,ed89e5b: cleanup logic for created nodes and users --- DrupalTestCase.class.php | 62 ++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/DrupalTestCase.class.php b/DrupalTestCase.class.php index 37193b2..600e504 100644 --- a/DrupalTestCase.class.php +++ b/DrupalTestCase.class.php @@ -129,6 +129,12 @@ abstract class DrupalTestCase extends PHPUnit_Framework_TestCase { * The number of redirects followed during the handling of a request. */ protected $redirect_count; + + + /** + * stuff to clean up on tearDown() + */ + protected $cleanup = array(); /** @@ -192,21 +198,12 @@ protected function error($message = '', $group = 'Other', array $caller = NULL) return $this->fail('exception: ' . $message, $group); } - function assertEqual($expected, $actual, $msg = NULL) { - return $this->assertEquals($expected, $actual); - } + // legacy supports - function assertIdentical($first, $second, $message = '', $group = 'Other') { - return $this->assertSame($first, $second, $message); - } - public static function assertTrue($condition, $message = '') { parent::assertTrue($condition, $message); return TRUE; // needed for simpletest back-comp (e.g. in drupalLogin), but dumb / always true. } - - - // legacy supports function assertEqual($expected, $actual, $message = '') { return $this->assertEquals($expected, $actual, $message); @@ -1139,6 +1136,7 @@ public function debug($obj, $heading = '') { * * @param $permissions * Array of permission names to assign to user. + * @param $cleanup if TRUE, gets deleted in tearDown() * @return * A fully loaded user object with pass_raw property, or FALSE if account * creation fails. @@ -1163,6 +1161,11 @@ protected function drupalCreateUser($permissions = array('access comments', 'acc if (empty($account->uid)) { return FALSE; } + + // mark for cleanup on tearDown() + if ($cleanup) { + $this->cleanup['users'][] = $account->uid; + } // Add the raw password so that we can log in as this user. $account->pass_raw = $edit['pass']; @@ -1700,10 +1703,11 @@ protected function drupalLogout() { * @param $settings * An associative array of settings to change from the defaults, keys are * node properties, for example 'title' => 'Hello, world!'. + * @param $cleanup if TRUE, gets deleted in tearDown() * @return * Created node object. */ - protected function drupalCreateNode($settings = array()) { + protected function drupalCreateNode($settings = array(), $cleanup = TRUE) { // Populate defaults array. $settings += array( 'body' => array(LANGUAGE_NONE => array(array())), @@ -1747,6 +1751,11 @@ protected function drupalCreateNode($settings = array()) { $node = (object) $settings; node_save($node); + + // mark for cleanup on tearDown() + if ($cleanup) { + $this->cleanup['nodes'][] = $node->nid; + } // Small hack to link revisions to our test user. db_update('node_revision') @@ -2399,8 +2408,37 @@ public function dumpContentToFile() { else $this->verbose("Dumped content of " . $this->getUrl() . " to $filepath."); } -} // abstract class DrupalTestCase + /** + * tear down after tests: added for cleanup. + */ + function tearDown() { + if (is_array($this->cleanup['nodes'])) { + foreach($this->cleanup['nodes'] as $nid) { + if (is_numeric($nid)) { + $this->verbose("Cleanup: deleting node [{$nid}]"); + node_delete($nid); + } + } + } + + if (is_array($this->cleanup['users'])) { + foreach($this->cleanup['users'] as $uid) { + if (is_numeric($uid)) { + $this->verbose("Cleanup: deleting user [{$uid}]"); + user_delete(array(), $uid); + } + } + } + + // @todo apply this $cleanup logic to other created stuff. + + parent::tearDown(); + } + + + +} // abstract class DrupalTestCase class DrupalUnitTestCase extends DrupalTestCase { From f5f2518fec18baf662b1f6053dec01260fdc6c7d Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Tue, 29 Nov 2011 18:08:49 -0300 Subject: [PATCH 15/16] description param for dumpContentToFile() (cherry picked from commit 5d92aa9edf6668e702a318fb302976e197924400) --- DrupalTestCase.class.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/DrupalTestCase.class.php b/DrupalTestCase.class.php index 600e504..066db57 100644 --- a/DrupalTestCase.class.php +++ b/DrupalTestCase.class.php @@ -2368,8 +2368,10 @@ protected function drupalSetSettings($settings) { * crude mechanism to dump current CURL'd html to file system * dir set in phpunit.xml as DUMP_DIR. * [tried and removed: create subdir for this test run and file for each dump] + * + * @param $description optional description of file dump for the logs */ - public function dumpContentToFile() { + public function dumpContentToFile($description = '') { $dump_dir = isset($GLOBALS['DUMP_DIR']) ? $GLOBALS['DUMP_DIR'] : NULL; if (empty($dump_dir) || !file_exists($dump_dir)) { $this->error("Missing or invalid DUMP_DIR in " . __FUNCTION__); @@ -2405,7 +2407,7 @@ public function dumpContentToFile() { $put = file_put_contents($filepath, $this->drupalGetContent()); if ($put === FALSE) $this->error("Unable to dump content to $filepath."); - else $this->verbose("Dumped content of " . $this->getUrl() . " to $filepath."); + else $this->verbose("Dumped content of " . $this->getUrl() . " to $filepath." . (empty($description) ? '' : " ($description)") ); } From 61d7c9cf5e9d549716069af58fa705ce0ae06f22 Mon Sep 17 00:00:00 2001 From: "Ben Buckman, New Leaf Digital" Date: Tue, 29 Nov 2011 19:21:38 -0300 Subject: [PATCH 16/16] method to set and restore variables on tearDown (cherry picked from commit 9cf57640482649ba465a3b4e2600c8010d20188e) --- DrupalTestCase.class.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/DrupalTestCase.class.php b/DrupalTestCase.class.php index 066db57..40096a8 100644 --- a/DrupalTestCase.class.php +++ b/DrupalTestCase.class.php @@ -1764,6 +1764,19 @@ protected function drupalCreateNode($settings = array(), $cleanup = TRUE) { ->execute(); return $node; } + + + /** + * [added] wrapper around variable_set, saves original value for restore on tearDown. + */ + function drupalVariableSet($key, $value, $cleanup = TRUE) { + if ($cleanup) { + $this->cleanup['variables'][$key] = variable_get($key, 0); + } + + variable_set($key, $value); + } + /** * Creates a custom content type based on default settings. @@ -2415,6 +2428,8 @@ public function dumpContentToFile($description = '') { * tear down after tests: added for cleanup. */ function tearDown() { + + // nodes if (is_array($this->cleanup['nodes'])) { foreach($this->cleanup['nodes'] as $nid) { if (is_numeric($nid)) { @@ -2424,6 +2439,7 @@ function tearDown() { } } + // users if (is_array($this->cleanup['users'])) { foreach($this->cleanup['users'] as $uid) { if (is_numeric($uid)) { @@ -2433,6 +2449,14 @@ function tearDown() { } } + // [restore] variables + if (is_array($this->cleanup['variables'])) { + foreach($this->cleanup['variables'] as $key => $value) { + $this->verbose("Cleanup: restoring variable [{$key}]"); + variable_set($key, $value); + } + } + // @todo apply this $cleanup logic to other created stuff. parent::tearDown();