MDL-41707 allow custom location of external test files used from unit tests

This commit is contained in:
Petr Škoda 2013-09-24 21:11:10 +02:00
parent d45e65ccad
commit a9d2f1b414
8 changed files with 78 additions and 37 deletions

View File

@ -220,12 +220,12 @@ class component_installer {
$this->requisitesok = false;
/// Check that everything we need is present
if (empty($this->sourcebase) || empty($this->zippath) || empty($this->zipfilename)) {
if (empty($this->sourcebase) || empty($this->zipfilename)) {
$this->errorstring='missingrequiredfield';
return false;
}
/// Check for correct sourcebase (this will be out in the future)
if ($this->sourcebase != 'http://download.moodle.org') {
if (!PHPUNIT_TEST and $this->sourcebase != 'http://download.moodle.org') {
$this->errorstring='wrongsourcebase';
return false;
}
@ -286,7 +286,12 @@ class component_installer {
return COMPONENT_ERROR;
}
/// Download zip file and save it to temp
$source = $this->sourcebase.'/'.$this->zippath.'/'.$this->zipfilename;
if ($this->zippath) {
$source = $this->sourcebase.'/'.$this->zippath.'/'.$this->zipfilename;
} else {
$source = $this->sourcebase.'/'.$this->zipfilename;
}
$zipfile= $CFG->tempdir.'/'.$this->zipfilename;
if($contents = download_file_content($source)) {
@ -474,7 +479,11 @@ class component_installer {
$comp_arr = array();
/// Define and retrieve the full md5 file
$source = $this->sourcebase.'/'.$this->zippath.'/'.$this->md5filename;
if ($this->zippath) {
$source = $this->sourcebase.'/'.$this->zippath.'/'.$this->md5filename;
} else {
$source = $this->sourcebase.'/'.$this->md5filename;
}
/// Check if we have downloaded the md5 file before (per request cache)
if (!empty($this->cachedmd5components[$source])) {

View File

@ -865,7 +865,7 @@ class core_files_file_storage_testcase extends advanced_testcase {
'itemid' => 0,
'filepath' => '/downloadtest/',
);
$url = 'http://download.moodle.org/unittest/test.html';
$url = $this->getExternalTestFileUrl('/test.html');
$fs = get_file_storage();

View File

@ -452,6 +452,46 @@ abstract class advanced_testcase extends PHPUnit_Framework_TestCase {
return phpunit_util::get_data_generator();
}
/**
* Returns UTL of the external test file.
*
* The result depends on the value of following constants:
* - TEST_EXTERNAL_FILES_HTTP_URL
* - TEST_EXTERNAL_FILES_HTTPS_URL
*
* They should point to standard external test files repository,
* it defaults to 'http://download.moodle.org/unittest'.
*
* False value means skip tests that require external files.
*
* @param string $path
* @param bool $https true if https required
* @return string url
*/
public function getExternalTestFileUrl($path, $https = false) {
$path = ltrim($path, '/');
if ($path) {
$path = '/'.$path;
}
if ($https) {
if (defined('TEST_EXTERNAL_FILES_HTTPS_URL')) {
if (!TEST_EXTERNAL_FILES_HTTPS_URL) {
$this->markTestSkipped('Tests using external https test files are disabled');
}
return TEST_EXTERNAL_FILES_HTTPS_URL.$path;
}
return 'https://download.moodle.org/unittest'.$path;
}
if (defined('TEST_EXTERNAL_FILES_HTTP_URL')) {
if (!TEST_EXTERNAL_FILES_HTTP_URL) {
$this->markTestSkipped('Tests using external http test files are disabled');
}
return TEST_EXTERNAL_FILES_HTTP_URL.$path;
}
return 'http://download.moodle.org/unittest'.$path;
}
/**
* Recursively visit all the files in the source tree. Calls the callback
* function with the pathname of each file found.

View File

@ -33,7 +33,8 @@ class core_componentlib_testcase extends advanced_testcase {
public function test_component_installer() {
global $CFG;
$ci = new component_installer('http://download.moodle.org', 'unittest', 'downloadtests.zip');
$url = $this->getExternalTestFileUrl('');
$ci = new component_installer($url, '', 'downloadtests.zip');
$this->assertTrue($ci->check_requisites());
$destpath = $CFG->dataroot.'/downloadtests';

View File

@ -84,7 +84,7 @@ class core_filelib_testcase extends advanced_testcase {
global $CFG;
// Test http success first.
$testhtml = "http://download.moodle.org/unittest/test.html";
$testhtml = $this->getExternalTestFileUrl('/test.html');
$contents = download_file_content($testhtml);
$this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
@ -109,7 +109,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame('', $response->error);
// Test https success.
$testhtml = "https://download.moodle.org/unittest/test.html";
$testhtml = $this->getExternalTestFileUrl('/test.html', true);
$contents = download_file_content($testhtml, null, null, false, 300, 20, true);
$this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
@ -118,7 +118,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
// Now 404.
$testhtml = "http://download.moodle.org/unittest/test.html_nonexistent";
$testhtml = $this->getExternalTestFileUrl('/test.html_nonexistent');
$contents = download_file_content($testhtml);
$this->assertFalse($contents);
@ -133,13 +133,14 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame('', $response->error);
// Invalid url.
$testhtml = "ftp://download.moodle.org/unittest/test.html";
$testhtml = $this->getExternalTestFileUrl('/test.html');
$testhtml = str_replace('http://', 'ftp://', $testhtml);
$contents = download_file_content($testhtml);
$this->assertFalse($contents);
// Test standard redirects.
$testurl = 'http://download.moodle.org/unittest/test_redir.php';
$testurl = $this->getExternalTestFileUrl('/test_redir.php');
$contents = download_file_content("$testurl?redir=2");
$this->assertSame('done', $contents);
@ -152,13 +153,11 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame('done', $response->results);
$this->assertSame('', $response->error);
// Commented out this block if there are performance problems.
/*
// Commented out for performance reasons.
$contents = download_file_content("$testurl?redir=6");
$this->assertFalse(false, $contents);
$this->assertDebuggingCalled();
$response = download_file_content("$testurl?redir=6", null, null, true);
$this->assertInstanceOf('stdClass', $response);
$this->assertSame('0', $response->status);
@ -168,7 +167,7 @@ class core_filelib_testcase extends advanced_testcase {
*/
// Test relative redirects.
$testurl = 'http://download.moodle.org/unittest/test_relative_redir.php';
$testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
$contents = download_file_content("$testurl");
$this->assertSame('done', $contents);
@ -184,7 +183,7 @@ class core_filelib_testcase extends advanced_testcase {
global $CFG;
// Test https success.
$testhtml = "https://download.moodle.org/unittest/test.html";
$testhtml = $this->getExternalTestFileUrl('/test.html');
$curl = new curl();
$contents = $curl->get($testhtml);
@ -212,7 +211,7 @@ class core_filelib_testcase extends advanced_testcase {
@unlink($tofile);
// Test full URL redirects.
$testurl = 'http://download.moodle.org/unittest/test_redir.php';
$testurl = $this->getExternalTestFileUrl('/test_redir.php');
$curl = new curl();
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
@ -294,7 +293,7 @@ class core_filelib_testcase extends advanced_testcase {
@unlink($tofile);
// Test relative location redirects.
$testurl = 'http://download.moodle.org/unittest/test_relative_redir.php';
$testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
$curl = new curl();
$contents = $curl->get($testurl);
@ -310,7 +309,7 @@ class core_filelib_testcase extends advanced_testcase {
$this->assertSame('done', $contents);
// Test different redirect types.
$testurl = 'http://download.moodle.org/unittest/test_relative_redir.php';
$testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
$curl = new curl();
$contents = $curl->get("$testurl?type=301");

View File

@ -35,14 +35,8 @@ global $CFG;
require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
class core_rsslib_testcase extends basic_testcase {
class core_rsslib_testcase extends advanced_testcase {
// A url we know exists and is valid.
const VALIDURL = 'http://download.moodle.org/unittest/rsstest.xml';
// A url which we know doesn't exist.
const INVALIDURL = 'http://download.moodle.org/unittest/rsstest-which-doesnt-exist.xml';
// This tinyurl redirects to th rsstest.xml file.
const REDIRECTURL = 'http://tinyurl.com/lvyslv';
// The number of seconds tests should wait for the server to respond (high to prevent false positives).
const TIMEOUT = 10;
@ -51,7 +45,7 @@ class core_rsslib_testcase extends basic_testcase {
}
public function test_getfeed() {
$feed = new moodle_simplepie(self::VALIDURL, self::TIMEOUT);
$feed = new moodle_simplepie($this->getExternalTestFileUrl('/rsstest.xml'), self::TIMEOUT);
$this->assertInstanceOf('moodle_simplepie', $feed);
@ -105,7 +99,7 @@ EOD;
* Test retrieving a url which doesn't exist.
*/
public function test_failurl() {
$feed = @new moodle_simplepie(self::INVALIDURL, self::TIMEOUT); // We do not want this in php error log.
$feed = @new moodle_simplepie($this->getExternalTestFileUrl('/rsstest-which-doesnt-exist.xml'), self::TIMEOUT); // We do not want this in php error log.
$this->assertNotEmpty($feed->error());
}
@ -119,7 +113,7 @@ EOD;
$oldproxy = $CFG->proxyhost;
$CFG->proxyhost = 'xxxxxxxxxxxxxxx.moodle.org';
$feed = new moodle_simplepie(self::VALIDURL);
$feed = new moodle_simplepie($this->getExternalTestFileUrl('/rsstest.xml'));
$this->assertNotEmpty($feed->error());
$this->assertEmpty($feed->get_title());
@ -130,7 +124,7 @@ EOD;
* Test retrieving a url which sends a redirect to another valid feed.
*/
public function test_redirect() {
$feed = new moodle_simplepie(self::REDIRECTURL, self::TIMEOUT);
$feed = new moodle_simplepie($this->getExternalTestFileUrl('/rss_redir.php'), self::TIMEOUT);
$this->assertNull($feed->error());
$this->assertSame('Moodle News', $feed->get_title());

View File

@ -15,13 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* These tests rely on the rsstest.xml file on download.moodle.org,
* from eloys listing:
* rsstest.xml: One valid rss feed.
* md5: 8fd047914863bf9b3a4b1514ec51c32c
* size: 32188
*
* If networking/proxy configuration is wrong these tests will fail..
* Weblib tests.
*
* @package core
* @category phpunit

View File

@ -20,6 +20,10 @@
<php>
<!--<const name="PHPUNIT_LONGTEST" value="1"/> uncomment to execute also slow or otherwise expensive tests-->
<!--Following constants instruct tests to fetch external test files from alternative location or skip tests if empty, clone https://github.com/moodlehq/moodle-exttests to local web server-->
<!--<const name="TEST_EXTERNAL_FILES_HTTP_URL" value="http://download.moodle.org/unittest"/> uncomment and alter to fetch external test files from alternative location-->
<!--<const name="TEST_EXTERNAL_FILES_HTTPS_URL" value="https://download.moodle.org/unittest"/> uncomment and alter to fetch external test files from alternative location-->
</php>
<!--All core suites need to be manually added here-->