Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 31... Línea 31...
31
 *
31
 *
32
 * @package   tool_httpsreplace
32
 * @package   tool_httpsreplace
33
 * @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com)
33
 * @copyright Copyright (c) 2016 Blackboard Inc. (http://www.blackboard.com)
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
35
 */
36
class httpsreplace_test extends \advanced_testcase {
36
final class httpsreplace_test extends \advanced_testcase {
Línea 37... Línea 37...
37
 
37
 
38
    /**
38
    /**
39
     * Data provider for test_upgrade_http_links
39
     * Data provider for test_upgrade_http_links
40
     */
40
     */
41
    public function upgrade_http_links_provider() {
41
    public static function upgrade_http_links_provider(): array {
42
        global $CFG;
42
        global $CFG;
43
        // Get the http url, since the default test wwwroot is https.
43
        // Get the http url, since the default test wwwroot is https.
44
        $wwwroothttp = preg_replace('/^https:/', 'http:', $CFG->wwwroot);
44
        $wwwroothttp = preg_replace('/^https:/', 'http:', $CFG->wwwroot);
45
        return [
45
        return [
46
            "Test image from another site should be replaced" => [
46
            "Test image from another site should be replaced" => [
47
                "content" => '<img src="' . $this->getExternalTestFileUrl('/test.jpg', false) . '">',
47
                "content" => '<img src="' . self::getExternalTestFileUrl('/test.jpg', false) . '">',
48
                "outputregex" => '/UPDATE/',
48
                "outputregex" => '/UPDATE/',
49
                "expectedcontent" => '<img src="' . $this->get_converted_http_link('/test.jpg') . '">',
49
                "expectedcontent" => '<img src="' . self::get_converted_http_link('/test.jpg') . '">',
50
            ],
50
            ],
51
            "Test object from another site should be replaced" => [
51
            "Test object from another site should be replaced" => [
52
                "content" => '<object data="' . $this->getExternalTestFileUrl('/test.swf', false) . '">',
52
                "content" => '<object data="' . self::getExternalTestFileUrl('/test.swf', false) . '">',
53
                "outputregex" => '/UPDATE/',
53
                "outputregex" => '/UPDATE/',
54
                "expectedcontent" => '<object data="' . $this->get_converted_http_link('/test.swf') . '">',
54
                "expectedcontent" => '<object data="' . self::get_converted_http_link('/test.swf') . '">',
55
            ],
55
            ],
56
            "Test image from a site with international name should be replaced" => [
56
            "Test image from a site with international name should be replaced" => [
57
                "content" => '<img src="http://中国互联网络信息中心.中国/logosy/201706/W01.png">',
57
                "content" => '<img src="http://中国互联网络信息中心.中国/logosy/201706/W01.png">',
58
                "outputregex" => '/UPDATE/',
58
                "outputregex" => '/UPDATE/',
Línea 77... Línea 77...
77
                "content" => '<img src="https://anothersite.com?param=http://asdf.com">',
77
                "content" => '<img src="https://anothersite.com?param=http://asdf.com">',
78
                "outputregex" => '/^$/',
78
                "outputregex" => '/^$/',
79
                "expectedcontent" => '<img src="https://anothersite.com?param=http://asdf.com">',
79
                "expectedcontent" => '<img src="https://anothersite.com?param=http://asdf.com">',
80
            ],
80
            ],
81
            "Search for params should be case insensitive" => [
81
            "Search for params should be case insensitive" => [
82
                "content" => '<object DATA="' . $this->getExternalTestFileUrl('/test.swf', false) . '">',
82
                "content" => '<object DATA="' . self::getExternalTestFileUrl('/test.swf', false) . '">',
83
                "outputregex" => '/UPDATE/',
83
                "outputregex" => '/UPDATE/',
84
                "expectedcontent" => '<object DATA="' . $this->get_converted_http_link('/test.swf') . '">',
84
                "expectedcontent" => '<object DATA="' . self::get_converted_http_link('/test.swf') . '">',
85
            ],
85
            ],
86
            "URL should be case insensitive" => [
86
            "URL should be case insensitive" => [
87
                "content" => '<object data="HTTP://some.site/path?query">',
87
                "content" => '<object data="HTTP://some.site/path?query">',
88
                "outputregex" => '/UPDATE/',
88
                "outputregex" => '/UPDATE/',
89
                "expectedcontent" => '<object data="https://some.site/path?query">',
89
                "expectedcontent" => '<object data="https://some.site/path?query">',
90
            ],
90
            ],
91
            "More params should not interfere" => [
91
            "More params should not interfere" => [
92
                "content" => '<img alt="A picture" src="' . $this->getExternalTestFileUrl('/test.png', false) .
92
                "content" => '<img alt="A picture" src="' . self::getExternalTestFileUrl('/test.png', false) .
93
                    '" width="1”><p style="font-size: \'20px\'"></p>',
93
                    '" width="1”><p style="font-size: \'20px\'"></p>',
94
                "outputregex" => '/UPDATE/',
94
                "outputregex" => '/UPDATE/',
95
                "expectedcontent" => '<img alt="A picture" src="' . $this->get_converted_http_link('/test.png') .
95
                "expectedcontent" => '<img alt="A picture" src="' . self::get_converted_http_link('/test.png') .
96
                    '" width="1”><p style="font-size: \'20px\'"></p>',
96
                    '" width="1”><p style="font-size: \'20px\'"></p>',
97
            ],
97
            ],
98
            "Broken URL should not be changed" => [
98
            "Broken URL should not be changed" => [
99
                "content" => '<img src="broken.' . $this->getExternalTestFileUrl('/test.png', false) . '">',
99
                "content" => '<img src="broken.' . self::getExternalTestFileUrl('/test.png', false) . '">',
100
                "outputregex" => '/^$/',
100
                "outputregex" => '/^$/',
101
                "expectedcontent" => '<img src="broken.' . $this->getExternalTestFileUrl('/test.png', false) . '">',
101
                "expectedcontent" => '<img src="broken.' . self::getExternalTestFileUrl('/test.png', false) . '">',
102
            ],
102
            ],
103
            "Link URL should not be changed" => [
103
            "Link URL should not be changed" => [
104
                "content" => '<a href="' . $this->getExternalTestFileUrl('/test.png', false) . '">' .
104
                "content" => '<a href="' . self::getExternalTestFileUrl('/test.png', false) . '">' .
105
                    $this->getExternalTestFileUrl('/test.png', false) . '</a>',
105
                    self::getExternalTestFileUrl('/test.png', false) . '</a>',
106
                "outputregex" => '/^$/',
106
                "outputregex" => '/^$/',
107
                "expectedcontent" => '<a href="' . $this->getExternalTestFileUrl('/test.png', false) . '">' .
107
                "expectedcontent" => '<a href="' . self::getExternalTestFileUrl('/test.png', false) . '">' .
108
                    $this->getExternalTestFileUrl('/test.png', false) . '</a>',
108
                    self::getExternalTestFileUrl('/test.png', false) . '</a>',
109
            ],
109
            ],
110
            "Test image from another site should be replaced but link should not" => [
110
            "Test image from another site should be replaced but link should not" => [
111
                "content" => '<a href="' . $this->getExternalTestFileUrl('/test.png', false) . '"><img src="' .
111
                "content" => '<a href="' . self::getExternalTestFileUrl('/test.png', false) . '"><img src="' .
112
                    $this->getExternalTestFileUrl('/test.jpg', false) . '"></a>',
112
                    self::getExternalTestFileUrl('/test.jpg', false) . '"></a>',
113
                "outputregex" => '/UPDATE/',
113
                "outputregex" => '/UPDATE/',
114
                "expectedcontent" => '<a href="' . $this->getExternalTestFileUrl('/test.png', false) . '"><img src="' .
114
                "expectedcontent" => '<a href="' . self::getExternalTestFileUrl('/test.png', false) . '"><img src="' .
115
                    $this->get_converted_http_link('/test.jpg') . '"></a>',
115
                    self::get_converted_http_link('/test.jpg') . '"></a>',
116
            ],
116
            ],
117
        ];
117
        ];
118
    }
118
    }
Línea 119... Línea 119...
119
 
119
 
Línea 125... Línea 125...
125
     * issues with SSL certificates.
125
     * issues with SSL certificates.
126
     *
126
     *
127
     * @param   string  $path Path to be rewritten
127
     * @param   string  $path Path to be rewritten
128
     * @return  string
128
     * @return  string
129
     */
129
     */
130
    protected function get_converted_http_link($path) {
130
    protected static function get_converted_http_link($path) {
131
        return preg_replace('/^http:/', 'https:', $this->getExternalTestFileUrl($path, false));
131
        return preg_replace('/^http:/', 'https:', self::getExternalTestFileUrl($path, false));
132
    }
132
    }
Línea 133... Línea 133...
133
 
133
 
134
    /**
134
    /**
135
     * Test upgrade_http_links
135
     * Test upgrade_http_links
136
     * @param string $content Example content that we'll attempt to replace.
136
     * @param string $content Example content that we'll attempt to replace.
137
     * @param string $ouputregex Regex for what output we expect.
137
     * @param string $outputregex Regex for what output we expect.
138
     * @param string $expectedcontent What content we are expecting afterwards.
138
     * @param string $expectedcontent What content we are expecting afterwards.
139
     * @dataProvider upgrade_http_links_provider
139
     * @dataProvider upgrade_http_links_provider
140
     */
140
     */
141
    public function test_upgrade_http_links($content, $ouputregex, $expectedcontent): void {
141
    public function test_upgrade_http_links($content, $outputregex, $expectedcontent): void {
Línea 142... Línea 142...
142
        global $DB;
142
        global $DB;
143
 
143
 
Línea 144... Línea 144...
144
        $this->resetAfterTest();
144
        $this->resetAfterTest();
Línea 145... Línea 145...
145
        $this->expectOutputRegex($ouputregex);
145
        $this->expectOutputRegex($outputregex);
146
 
146
 
Línea 158... Línea 158...
158
    }
158
    }
Línea 159... Línea 159...
159
 
159
 
160
    /**
160
    /**
161
     * Data provider for test_http_link_stats
161
     * Data provider for test_http_link_stats
162
     */
162
     */
163
    public function http_link_stats_provider() {
163
    public static function http_link_stats_provider(): array {
164
        global $CFG;
164
        global $CFG;
165
        // Get the http url, since the default test wwwroot is https.
165
        // Get the http url, since the default test wwwroot is https.
166
        $wwwrootdomain = 'www.example.com';
166
        $wwwrootdomain = 'www.example.com';
167
        $wwwroothttp = preg_replace('/^https:/', 'http:', $CFG->wwwroot);
167
        $wwwroothttp = preg_replace('/^https:/', 'http:', $CFG->wwwroot);
168
        $testdomain = $this->get_converted_http_link('');
168
        $testdomain = self::get_converted_http_link('');
169
        return [
169
        return [
170
            "Test image from an available site so shouldn't be reported" => [
170
            "Test image from an available site so shouldn't be reported" => [
171
                "content" => '<img src="' . $this->getExternalTestFileUrl('/test.jpg', false) . '">',
171
                "content" => '<img src="' . self::getExternalTestFileUrl('/test.jpg', false) . '">',
172
                "domain" => $testdomain,
172
                "domain" => $testdomain,
173
                "expectedcount" => 0,
173
                "expectedcount" => 0,
174
            ],
174
            ],
175
            "Link that is from this site shouldn't be reported" => [
175
            "Link that is from this site shouldn't be reported" => [