. /** * HTTPS find and replace Tests * * @package tool_httpsreplace * @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace tool_httpsreplace\tests; defined('MOODLE_INTERNAL') || die(); /** * Tests the httpsreplace tool. * * @package tool_httpsreplace * @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class httpsreplace_test extends \advanced_testcase { /** * Data provider for test_upgrade_http_links */ public function upgrade_http_links_provider() { global $CFG; // Get the http url, since the default test wwwroot is https. $wwwroothttp = preg_replace('/^https:/', 'http:', $CFG->wwwroot); return [ "Test image from another site should be replaced" => [ "content" => '', "outputregex" => '/UPDATE/', "expectedcontent" => '', ], "Test object from another site should be replaced" => [ "content" => '', "outputregex" => '/UPDATE/', "expectedcontent" => '', ], "Test image from a site with international name should be replaced" => [ "content" => '', "outputregex" => '/UPDATE/', "expectedcontent" => '', ], "Link that is from this site should be replaced" => [ "content" => '', "outputregex" => '/UPDATE/', "expectedcontent" => '', ], "Link that is from this site, https new so doesn't need replacing" => [ "content" => '', "outputregex" => '/^$/', "expectedcontent" => '', ], "Unavailable image should be replaced" => [ "content" => '', "outputregex" => '/UPDATE/', "expectedcontent" => '', ], "Https content that has an http url as a param should not be replaced" => [ "content" => '', "outputregex" => '/^$/', "expectedcontent" => '', ], "Search for params should be case insensitive" => [ "content" => '', "outputregex" => '/UPDATE/', "expectedcontent" => '', ], "URL should be case insensitive" => [ "content" => '', "outputregex" => '/UPDATE/', "expectedcontent" => '', ], "More params should not interfere" => [ "content" => 'A picture

', "outputregex" => '/UPDATE/', "expectedcontent" => 'A picture

', ], "Broken URL should not be changed" => [ "content" => '', "outputregex" => '/^$/', "expectedcontent" => '', ], "Link URL should not be changed" => [ "content" => '' . $this->getExternalTestFileUrl('/test.png', false) . '', "outputregex" => '/^$/', "expectedcontent" => '' . $this->getExternalTestFileUrl('/test.png', false) . '', ], "Test image from another site should be replaced but link should not" => [ "content" => '', "outputregex" => '/UPDATE/', "expectedcontent" => '', ], ]; } /** * Convert the HTTP external test file URL to use HTTPS. * * Note: We *must not* use getExternalTestFileUrl with the True option * here, becase it is reasonable to have only one of these set due to * issues with SSL certificates. * * @param string $path Path to be rewritten * @return string */ protected function get_converted_http_link($path) { return preg_replace('/^http:/', 'https:', $this->getExternalTestFileUrl($path, false)); } /** * Test upgrade_http_links * @param string $content Example content that we'll attempt to replace. * @param string $ouputregex Regex for what output we expect. * @param string $expectedcontent What content we are expecting afterwards. * @dataProvider upgrade_http_links_provider */ public function test_upgrade_http_links($content, $ouputregex, $expectedcontent) { global $DB; $this->resetAfterTest(); $this->expectOutputRegex($ouputregex); $finder = new tool_httpreplace_url_finder_test(); $generator = $this->getDataGenerator(); $course = $generator->create_course((object) [ 'summary' => $content, ]); $finder->upgrade_http_links(); $summary = $DB->get_field('course', 'summary', ['id' => $course->id]); $this->assertContains($expectedcontent, $summary); } /** * Data provider for test_http_link_stats */ public function http_link_stats_provider() { global $CFG; // Get the http url, since the default test wwwroot is https. $wwwrootdomain = 'www.example.com'; $wwwroothttp = preg_replace('/^https:/', 'http:', $CFG->wwwroot); $testdomain = $this->get_converted_http_link(''); return [ "Test image from an available site so shouldn't be reported" => [ "content" => '', "domain" => $testdomain, "expectedcount" => 0, ], "Link that is from this site shouldn't be reported" => [ "content" => '', "domain" => $wwwrootdomain, "expectedcount" => 0, ], "Unavailable, but https shouldn't be reported" => [ "content" => '', "domain" => 'intentionally.unavailable', "expectedcount" => 0, ], "Unavailable image should be reported" => [ "content" => '', "domain" => 'intentionally.unavailable', "expectedcount" => 1, ], "Unavailable object should be reported" => [ "content" => '', "domain" => 'intentionally.unavailable', "expectedcount" => 1, ], "Link should not be reported" => [ "content" => 'Link', "domain" => 'intentionally.unavailable', "expectedcount" => 0, ], "Text should not be reported" => [ "content" => 'http://intentionally.unavailable/page.php', "domain" => 'intentionally.unavailable', "expectedcount" => 0, ], ]; } /** * Test http_link_stats * @param string $content Example content that we'll attempt to replace. * @param string $domain The domain we will check was replaced. * @param string $expectedcount Number of urls from that domain that we expect to be replaced. * @dataProvider http_link_stats_provider */ public function test_http_link_stats($content, $domain, $expectedcount) { $this->resetAfterTest(); $finder = new tool_httpreplace_url_finder_test(); $generator = $this->getDataGenerator(); $course = $generator->create_course((object) [ 'summary' => $content, ]); $results = $finder->http_link_stats(); $this->assertEquals($expectedcount, $results[$domain] ?? 0); } /** * Test links and text are not changed */ public function test_links_and_text() { global $DB; $this->resetAfterTest(); $this->expectOutputRegex('/^$/'); $finder = new tool_httpreplace_url_finder_test(); $generator = $this->getDataGenerator(); $course = $generator->create_course((object) [ 'summary' => 'Link http://other.unavailable/page.php', ]); $results = $finder->http_link_stats(); $this->assertCount(0, $results); $finder->upgrade_http_links(); $results = $finder->http_link_stats(); $this->assertCount(0, $results); $summary = $DB->get_field('course', 'summary', ['id' => $course->id]); $this->assertContains('http://intentionally.unavailable/page.php', $summary); $this->assertContains('http://other.unavailable/page.php', $summary); $this->assertNotContains('https://intentionally.unavailable', $summary); $this->assertNotContains('https://other.unavailable', $summary); } /** * If we have an http wwwroot then we shouldn't report it. */ public function test_httpwwwroot() { global $DB, $CFG; $this->resetAfterTest(); $CFG->wwwroot = preg_replace('/^https:/', 'http:', $CFG->wwwroot); $this->expectOutputRegex('/^$/'); $finder = new tool_httpreplace_url_finder_test(); $generator = $this->getDataGenerator(); $course = $generator->create_course((object) [ 'summary' => '', ]); $results = $finder->http_link_stats(); $this->assertCount(0, $results); $finder->upgrade_http_links(); $summary = $DB->get_field('course', 'summary', ['id' => $course->id]); $this->assertContains($CFG->wwwroot, $summary); } /** * Test that links in excluded tables are not replaced */ public function test_upgrade_http_links_excluded_tables() { $this->resetAfterTest(); set_config('test_upgrade_http_links', ''); $finder = new tool_httpreplace_url_finder_test(); ob_start(); $results = $finder->upgrade_http_links(); $output = ob_get_contents(); ob_end_clean(); $this->assertTrue($results); $this->assertNotContains('https://somesite', $output); $testconf = get_config('core', 'test_upgrade_http_links'); $this->assertContains('http://somesite', $testconf); $this->assertNotContains('https://somesite', $testconf); } /** * Test renamed domains */ public function test_renames() { global $DB, $CFG; $this->resetAfterTest(); $this->expectOutputRegex('/UPDATE/'); $renames = [ 'example.com' => 'secure.example.com', ]; set_config('renames', json_encode($renames), 'tool_httpsreplace'); $finder = new tool_httpreplace_url_finder_test(); $generator = $this->getDataGenerator(); $course = $generator->create_course((object) [ 'summary' => '