Issue #22 - Allowing tests to point to a file when a local URL was found. It will be used to test parsed contents later (parse CSS for example).

This commit is contained in:
Daniel Thee Roperto
2016-11-10 14:03:59 +11:00
parent a5a301869d
commit 24759fe280
4 changed files with 57 additions and 23 deletions

View File

@@ -61,7 +61,7 @@ class maintenance_static_page {
} else if (PHPUNIT_TEST) {
$html = '<html></html>';
} else {
$html = file_get_contents($CFG->wwwroot.'/auth/outage/info.php?auth_outage_hide_warning=1&id='.$outage->id);
$html = self::file_get_contents($CFG->wwwroot.'/auth/outage/info.php?auth_outage_hide_warning=1&id='.$outage->id);
}
return self::create_from_html($html);
@@ -92,6 +92,20 @@ class maintenance_static_page {
return new maintenance_static_page($dom);
}
/**
* Tries to get the contents of the file or URL.
* @param string $file File to get.
* @return string Contents of $file or an empty string if failed.
*/
private static function file_get_contents($file) {
$contents = @file_get_contents($file);
if ($contents === false) {
debugging('Cannot fetch: '.$file);
return ''; // Better a broken link than halting the generation.
}
return $contents;
}
/** @var DOMDocument */
protected $dom;
@@ -259,12 +273,20 @@ class maintenance_static_page {
if (is_null($filename)) {
$url = $href; // Skipped, use original URL.
} else {
$this->update_link_stylesheet_parse($filename);
$url = $this->get_url_for_file($filename);
}
$link->setAttribute('href', $url);
}
}
/**
* Checks for urls inside filename.
* @param string $filename
*/
private function update_link_stylesheet_parse($filename) {
}
/**
* Fetch and fixes the favicon link tag.
*/
@@ -337,12 +359,12 @@ class maintenance_static_page {
return null; // External URL, leave it.
}
// PHPUnit will use www.example.com as wwwroot and we don't to copy the file.
// PHPUnit does not expose a web interface to fetch, point to local file instead.
if (PHPUNIT_TEST) {
$contents = '';
} else {
$contents = file_get_contents($url);
$url = str_replace($CFG->wwwroot, $CFG->dirroot, $url);
}
$contents = self::file_get_contents($url);
$filename = sha1($contents).'.'.$type;
$filepath = $this->get_resources_folder().'/'.$filename;
file_put_contents($filepath, $contents);

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@@ -0,0 +1,7 @@
a {
font-size: 200%;
}
div {
background-color: #abcdef;
}

View File

@@ -75,37 +75,33 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
}
public function test_updatelinkstylesheet() {
$link1 = (string)new moodle_url('/example.css');
$link2 = (string)new moodle_url('/auth/outage/stylesheet');
$link3 = 'http://google.com/coolstyle.css';
$local_css_link = $this->get_fixture_path('simple.css');
$external_css_link = 'http://google.com/coolstuff.css';
$html = "<!DOCTYPE html>\n".
'<html><head><link href="'.$link1.'" rel="stylesheet" /><title>Title</title></head>'.
'<body><link rel="stylesheet" href="'.$link2.'">Content<link rel="stylesheet" href="'.$link3.'"></body></html>';
'<html><head><link href="'.$local_css_link.'" rel="stylesheet" /><title>Title</title></head>'.
'<body>Content<link rel="stylesheet" href="'.$external_css_link.'"></body></html>';
$generated = $this->generated_page_html($html);
self::assertContains('http://www.example.com/moodle/auth/outage/file.php?file=', $generated);
self::assertNotContains($link1, $generated);
self::assertNotContains($link2, $generated);
self::assertContains($link3, $generated);
self::assertNotContains($local_css_link, $generated);
self::assertContains($external_css_link, $generated);
}
public function test_updateimages() {
$link1 = (string)new moodle_url('/example.png');
$link2 = (string)new moodle_url('/auth/outage/imagefile');
$link3 = 'http://google.com/coolstyle.css';
$local_img_link = $this->get_fixture_path('catalyst.png');
$external_img_link = 'http://google.com/coolstyle.css';
$html = "<!DOCTYPE html>\n".
'<html><head><img src="'.$link1.'" alt="an image" /><title>Title</title></head>'.
'<body><img src="'.$link2.'">Content<img src="'.$link3.'" /></body></html>';
'<html><head><title>Title</title></head>'.
'<body><img src="'.$local_img_link.'">Content<img src="'.$external_img_link.'" /></body></html>';
$generated = $this->generated_page_html($html);
self::assertContains('http://www.example.com/moodle/auth/outage/file.php?file=', $generated);
self::assertNotContains($link1, $generated);
self::assertNotContains($link2, $generated);
self::assertContains($link3, $generated);
self::assertNotContains($local_img_link, $generated);
self::assertContains($external_img_link, $generated);
}
public function test_updatelinkfavicon() {
$link = (string)new moodle_url('/favicon.jpg');
$link = $this->get_fixture_path('catalyst.png');
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title><link rel="shortcut icon" href="'.$link.'""></head>'.
'<body>Content</body></html>';
@@ -116,7 +112,7 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
}
public function test_previewpath() {
$link = (string)new moodle_url('/favicon.jpg');
$link = $this->get_fixture_path('catalyst.png');
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title><link rel="shortcut icon" href="'.$link.'""></head>'.
'<body>Content</body></html>';
@@ -162,4 +158,13 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
maintenance_static_page::create_from_outage(null)->generate();
self::assertFileNotExists($file);
}
/**
* Gets a fixture file for this test case.
* @param $file
* @return string
*/
private function get_fixture_path($file) {
return (string)new moodle_url('/auth/outage/tests/phpunit/local/controllers/fixtures/'.$file);
}
}