diff --git a/classes/local/controllers/maintenance_static_page.php b/classes/local/controllers/maintenance_static_page.php index 0cad11e..ad5b2e4 100644 --- a/classes/local/controllers/maintenance_static_page.php +++ b/classes/local/controllers/maintenance_static_page.php @@ -61,7 +61,7 @@ class maintenance_static_page { } else if (PHPUNIT_TEST) { $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); diff --git a/tests/phpunit/local/controllers/fixtures/catalyst.png b/tests/phpunit/local/controllers/fixtures/catalyst.png new file mode 100644 index 0000000..e241f49 Binary files /dev/null and b/tests/phpunit/local/controllers/fixtures/catalyst.png differ diff --git a/tests/phpunit/local/controllers/fixtures/simple.css b/tests/phpunit/local/controllers/fixtures/simple.css new file mode 100644 index 0000000..93095ef --- /dev/null +++ b/tests/phpunit/local/controllers/fixtures/simple.css @@ -0,0 +1,7 @@ +a { + font-size: 200%; +} + +div { + background-color: #abcdef; +} \ No newline at end of file diff --git a/tests/phpunit/local/controllers/maintenance_static_page_test.php b/tests/phpunit/local/controllers/maintenance_static_page_test.php index d334b38..9d3f9ce 100644 --- a/tests/phpunit/local/controllers/maintenance_static_page_test.php +++ b/tests/phpunit/local/controllers/maintenance_static_page_test.php @@ -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 = "\n". - 'Title'. - 'Content'; + 'Title'. + 'Content'; $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 = "\n". - 'an imageTitle'. - 'Content'; + 'Title'. + 'Content'; $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 = "\n". 'Title'. 'Content'; @@ -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 = "\n". 'Title'. 'Content'; @@ -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); + } }