From 0575ef794acb5a08bcc4c5a070d640ab537f630c Mon Sep 17 00:00:00 2001
From: Russell Smith <russell.smith@catalyst-au.net>
Date: Tue, 10 Sep 2013 17:59:21 +1000
Subject: [PATCH] MDL-41707 unittests: Allow disabling of external unit tests

Unit tests should be able to be executed without contacting external
servers.  Allow that to happen if you choose.
---
 lib/filestorage/tests/file_storage_test.php |    5 +++++
 lib/phpunit/bootstrap.php                   |    5 +++++
 lib/tests/componentlib_test.php             |    4 ++++
 lib/tests/filelib_test.php                  |    8 +++++---
 lib/tests/rsslib_test.php                   |   12 ++++++++++++
 phpunit.xml.dist                            |    1 +
 6 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/lib/filestorage/tests/file_storage_test.php b/lib/filestorage/tests/file_storage_test.php
index 049acfe..6422b37 100644
--- a/lib/filestorage/tests/file_storage_test.php
+++ b/lib/filestorage/tests/file_storage_test.php
@@ -693,6 +693,11 @@ class filestoragelib_testcase extends advanced_testcase {
     }
 
     public function test_create_file_from_url() {
+
+        if (!PHPUNIT_EXTERNALTESTS) {
+            return;
+        }
+
         $this->resetAfterTest(true);
 
         $syscontext = context_system::instance();
diff --git a/lib/phpunit/bootstrap.php b/lib/phpunit/bootstrap.php
index 138f27e..7fc7640 100644
--- a/lib/phpunit/bootstrap.php
+++ b/lib/phpunit/bootstrap.php
@@ -76,6 +76,11 @@ if (!defined('PHPUNIT_LONGTEST')) {
     define('PHPUNIT_LONGTEST', false);
 }
 
+if (!defined('PHPUNIT_EXTERNALTESTS')) {
+    /** Execute external unit tests by default */
+    define('PHPUNIT_EXTERNALTESTS', true);
+}
+
 // remove error handling overrides done in config.php
 error_reporting(E_ALL | E_STRICT);
 ini_set('display_errors', '1');
diff --git a/lib/tests/componentlib_test.php b/lib/tests/componentlib_test.php
index cbe0713..fdb83f3 100644
--- a/lib/tests/componentlib_test.php
+++ b/lib/tests/componentlib_test.php
@@ -33,6 +33,10 @@ class componentlib_testcase extends advanced_testcase {
     public function test_component_installer() {
         global $CFG;
 
+        if (!PHPUNIT_EXTERNALTESTS) {
+            return;
+        }
+
         $ci = new component_installer('http://download.moodle.org', 'unittest', 'downloadtests.zip');
         $this->assertTrue($ci->check_requisites());
 
diff --git a/lib/tests/filelib_test.php b/lib/tests/filelib_test.php
index 7365a44..b46d814 100644
--- a/lib/tests/filelib_test.php
+++ b/lib/tests/filelib_test.php
@@ -82,9 +82,11 @@ class filelib_testcase extends advanced_testcase {
     }
 
     public function test_download_file_content() {
-        $testhtml = "http://download.moodle.org/unittest/test.html";
-        $contents = download_file_content($testhtml);
-        $this->assertEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
+        if (PHPUNIT_EXTERNALTESTS) {
+            $testhtml = "http://download.moodle.org/unittest/test.html";
+            $contents = download_file_content($testhtml);
+            $this->assertEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
+        }
     }
 
     /**
diff --git a/lib/tests/rsslib_test.php b/lib/tests/rsslib_test.php
index 7225220..9c09fca 100644
--- a/lib/tests/rsslib_test.php
+++ b/lib/tests/rsslib_test.php
@@ -51,6 +51,10 @@ class moodlesimplepie_testcase extends basic_testcase {
     }
 
     function test_getfeed() {
+        if (!PHPUNIT_EXTERNALTESTS) {
+            return;
+        }
+
         $feed = new moodle_simplepie();
         $feed->set_timeout(self::TIMEOUT);
         $feed->set_feed_url(self::VALIDURL);
@@ -126,6 +130,10 @@ EOD;
     function test_failproxy() {
         global $CFG;
 
+        if (!PHPUNIT_EXTERNALTESTS) {
+            return;
+        }
+
         $oldproxy = $CFG->proxyhost;
         $CFG->proxyhost = 'xxxxxxxxxxxxxxx.moodle.org';
 
@@ -142,6 +150,10 @@ EOD;
     function test_redirect() {
         global $CFG;
 
+        if (!PHPUNIT_EXTERNALTESTS) {
+            return;
+        }
+
         $feed = new moodle_simplepie();
         $feed->set_timeout(self::TIMEOUT);
         $feed->set_feed_url(self::REDIRECTURL);
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index e66cc9a..12d55f0 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -19,6 +19,7 @@
 
     <php>
         <!--<const name="PHPUNIT_LONGTEST" value="1"/> uncomment to execute also slow or otherwise expensive tests-->
+        <!--<const name="PHPUNIT_EXTERNALTESTS" value="0"/> uncomment to disable tests that communicate with other servers -->
     </php>
 
 <!--All core suites need to be manually added here-->
-- 
1.7.9.5

