Skip to content

Commit

Permalink
MDL-41707 allow custom location of external test files used from unit…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
skodak committed Sep 27, 2013
1 parent d45e65c commit a9d2f1b
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 38 deletions.
17 changes: 13 additions & 4 deletions lib/componentlib.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ function check_requisites() {
$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;
}
Expand Down Expand Up @@ -286,7 +286,12 @@ function install() {
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)) {
Expand Down Expand Up @@ -474,7 +479,11 @@ function get_all_components_md5() {
$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])) {
Expand Down
2 changes: 1 addition & 1 deletion lib/filestorage/tests/file_storage_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ public function test_create_file_from_url() {
'itemid' => 0,
'filepath' => '/downloadtest/',
);
$url = 'http://download.moodle.org/unittest/test.html';
$url = $this->getExternalTestFileUrl('/test.html');

$fs = get_file_storage();

Expand Down
40 changes: 40 additions & 0 deletions lib/phpunit/classes/advanced_testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,46 @@ public static function getDataGenerator() {
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.
Expand Down
3 changes: 2 additions & 1 deletion lib/tests/componentlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
25 changes: 12 additions & 13 deletions lib/tests/filelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function test_download_file_content() {
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));
Expand All @@ -109,7 +109,7 @@ public function test_download_file_content() {
$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));
Expand All @@ -118,7 +118,7 @@ public function test_download_file_content() {
$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);
Expand All @@ -133,13 +133,14 @@ public function test_download_file_content() {
$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);
Expand All @@ -152,13 +153,11 @@ public function test_download_file_content() {
$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);
Expand All @@ -168,7 +167,7 @@ public function test_download_file_content() {
*/

// 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);
Expand All @@ -184,7 +183,7 @@ public function test_curl_class() {
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);
Expand Down Expand Up @@ -212,7 +211,7 @@ public function test_curl_class() {
@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));
Expand Down Expand Up @@ -294,7 +293,7 @@ public function test_curl_class() {
@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);
Expand All @@ -310,7 +309,7 @@ public function test_curl_class() {
$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");
Expand Down
18 changes: 6 additions & 12 deletions lib/tests/rsslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,8 @@
require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');


class core_rsslib_testcase extends basic_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';
class core_rsslib_testcase extends advanced_testcase {

// The number of seconds tests should wait for the server to respond (high to prevent false positives).
const TIMEOUT = 10;

Expand All @@ -51,7 +45,7 @@ protected function setUp() {
}

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);

Expand Down Expand Up @@ -105,7 +99,7 @@ public function test_getfeed() {
* 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());
}
Expand All @@ -119,7 +113,7 @@ public function test_failproxy() {
$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());
Expand All @@ -130,7 +124,7 @@ public function test_failproxy() {
* 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());
Expand Down
8 changes: 1 addition & 7 deletions lib/tests/weblib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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-->
Expand Down

0 comments on commit a9d2f1b

Please sign in to comment.