From d83a9292549c79668252d2946bced52072a569b3 Mon Sep 17 00:00:00 2001 From: nathangray Date: Tue, 7 Feb 2017 16:02:06 -0700 Subject: [PATCH] Get tests to not fail if DB is missing - we skip the ones that need a DB --- api/src/Db/test/SchemaTest.php | 3 +++ api/src/loader/test/SecurityTest.php | 27 +++++++++++++++++++++++---- api/src/test/DateTimeTest.php | 3 +++ api/src/test/LoggedInTest.php | 26 ++++++++++++++++++-------- doc/phpunit.xml | 9 ++++----- 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/api/src/Db/test/SchemaTest.php b/api/src/Db/test/SchemaTest.php index d113ae84a1..622393a759 100644 --- a/api/src/Db/test/SchemaTest.php +++ b/api/src/Db/test/SchemaTest.php @@ -302,6 +302,9 @@ class SchemaTest extends LoggedInTest { )); } + /** + * @depends testInsertMoreContent + */ public function testDropTable() { foreach(self::$adodb->MetaTables() as $table) diff --git a/api/src/loader/test/SecurityTest.php b/api/src/loader/test/SecurityTest.php index 7f2c95a51a..e2cbfcd223 100644 --- a/api/src/loader/test/SecurityTest.php +++ b/api/src/loader/test/SecurityTest.php @@ -22,9 +22,21 @@ use PHPUnit_Framework_TestCase as TestCase; class SecurityTest extends TestCase { + public function setUp() + { + // _check_script_tag uses HtmLawed, which calls GLOBALS['egw']->link() + $GLOBALS['egw'] = $this->getMockBuilder('Egw') + ->disableOriginalConstructor() + ->setMethods(['link', 'setup']) + ->getMock(); + } + public function tearDown() { unset($GLOBALS['egw_inset_vars']); + + // Must remember to clear this, or other tests may break + unset($GLOBALS['egw']); } /** @@ -105,10 +117,17 @@ class SecurityTest extends TestCase { // we currently fail 44 of 140 tests, thought they seem not to apply to our use case, as we check request data 'https://html5sec.org/' => call_user_func(function() { $payloads = $items = null; - if (!($items_js = file_get_contents('https://html5sec.org/items.js')) || - !preg_match_all("|^\s+'data'\s+:\s+'(.*)',$|m", $items_js, $items, PREG_PATTERN_ORDER) || - !($payload_js = file_get_contents('https://html5sec.org/payloads.js')) || - !preg_match_all("|^\s+'([^']+)'\s+:\s+'(.*)',$|m", $payload_js, $payloads, PREG_PATTERN_ORDER)) + try + { + if (!($items_js = file_get_contents('https://html5sec.org/items.js')) || + !preg_match_all("|^\s+'data'\s+:\s+'(.*)',$|m", $items_js, $items, PREG_PATTERN_ORDER) || + !($payload_js = file_get_contents('https://html5sec.org/payloads.js')) || + !preg_match_all("|^\s+'([^']+)'\s+:\s+'(.*)',$|m", $payload_js, $payloads, PREG_PATTERN_ORDER)) + { + return false; + } + } + catch (Exception $e) { return false; } diff --git a/api/src/test/DateTimeTest.php b/api/src/test/DateTimeTest.php index ee99bb50cd..70bd47d564 100644 --- a/api/src/test/DateTimeTest.php +++ b/api/src/test/DateTimeTest.php @@ -40,6 +40,9 @@ class DateTimeTest extends TestCase { { // Reset DateTime::setUserPrefs(static::$usertime->getName()); + + unset($GLOBALS['egw']); + parent::tearDownAfterClass(); } /** diff --git a/api/src/test/LoggedInTest.php b/api/src/test/LoggedInTest.php index ce7e922fc6..b6e5e5c46c 100644 --- a/api/src/test/LoggedInTest.php +++ b/api/src/test/LoggedInTest.php @@ -45,9 +45,6 @@ abstract class LoggedInTest extends TestCase // These globals pulled from the test config (phpunit.xml) static::load_egw($GLOBALS['EGW_USER'],$GLOBALS['EGW_PASSWORD'], $GLOBALS['EGW_DOMAIN']); - // Re-init config, since it doesn't get handled by loading Egw - Api\Config::init_static(); - if($GLOBALS['egw']->db) { $GLOBALS['egw']->db->connect(); @@ -57,11 +54,14 @@ abstract class LoggedInTest extends TestCase static::markTestSkipped('No $GLOBALS[egw]->db'); die(); } + + // Re-init config, since it doesn't get handled by loading Egw + Api\Config::init_static(); } catch(Exception $e) { - static::markTestSkipped('Unable to connect to Egroupware'); - throw $e; + static::markTestSkipped('Unable to connect to Egroupware - ' . $e->getMessage()); + return; } // Do some checks to make sure things we expect are there @@ -101,7 +101,7 @@ abstract class LoggedInTest extends TestCase } /** - * Start the eGW session, exits on wrong credentials + * Start the eGW session, skips the test if there are problems connecting * * @param string $user * @param string $passwd @@ -125,13 +125,23 @@ abstract class LoggedInTest extends TestCase 'flags' => array( 'currentapp' => 'api', 'noheader' => true, - 'autocreate_session_callback' => __NAMESPACE__ . '\AppTest::create_session', + 'autocreate_session_callback' => __CLASS__ .'::create_session', 'no_exception_handler' => 'cli', 'noapi' => false, ) ); - include(realpath(__DIR__ . '/../../../header.inc.php')); + try + { + include(realpath(__DIR__ . '/../../../header.inc.php')); + } + catch (Exception $e) + { + // Something went wrong setting up egw environment - don't try + // to do the test + static::markTestSkipped($e->getMessage()); + return; + } require_once realpath(__DIR__.'/../loader/common.php'); // autoloader & check_load_extension diff --git a/doc/phpunit.xml b/doc/phpunit.xml index 8e30c097cb..f018020893 100644 --- a/doc/phpunit.xml +++ b/doc/phpunit.xml @@ -1,7 +1,9 @@ + colors="true" + backupGlobals="false" +> @@ -10,10 +12,7 @@ ../api/src/test/ - - - ../api/src/Mail/test/CredentialsTest.php - + ../api/src/*/test/ ../*/test/