-
Notifications
You must be signed in to change notification settings - Fork 131
/
test-sanitize.php
251 lines (182 loc) · 8.19 KB
/
test-sanitize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
class SanitizeTest extends \WP_UnitTestCase {
public function test_html5_to_xhtml11() {
$html = '<article style="font-weight:bold;">Foo</article><h1>Hello!</h1><command>Bar</command>';
$html = \Pressbooks\Sanitize\html5_to_xhtml11( $html );
$this->assertEquals(
'<div class=\'bc-article article\' style="font-weight:bold;">Foo</div><h1>Hello!</h1><div class=\'bc-command command\' >Bar</div>',
$html
);
}
public function test_html5_to_epub3() {
$html = '<article style="font-weight:bold;">Foo</article><h1>Hello!</h1><command>Bar</command>';
$html = \Pressbooks\Sanitize\html5_to_epub3( $html );
$this->assertEquals(
'<article style="font-weight:bold;">Foo</article><h1>Hello!</h1><div class=\'bc-command command\' >Bar</div>',
$html
);
}
public function test_fix_audio_shortcode() {
\Pressbooks\Sanitize\fix_audio_shortcode();
$this->assertTrue( has_filter( 'wp_audio_shortcode' ) );
// Verify that style attribute is empty.
$var = wp_audio_shortcode( [ 'src' => 'http://foo/audio.mp3' ] );
$this->assertFalse( strpos( $var, 'style=' ) );
}
public function test_sanitize_xml_attribute() {
$var = 'Hello-World!';
$this->assertEquals( $var, \Pressbooks\Sanitize\sanitize_xml_attribute( $var ) );
$var = "\t <Hello&World!> ";
$this->assertEquals( '<Hello&World!>', \Pressbooks\Sanitize\sanitize_xml_attribute( $var ) );
$var = " te\fst";
$this->assertEquals( 'test', \Pressbooks\Sanitize\sanitize_xml_attribute( $var ) );
}
public function test_sanitize_xml_id() {
$var = 'Hello-World!';
$test = $this->_generateControlCharacters() . $var;
$test = \Pressbooks\Sanitize\sanitize_xml_id( $test );
$this->assertEquals( 'Hello-World', $test );
$var = ' Héllö Wôrld! ';
$test = \Pressbooks\Sanitize\sanitize_xml_id( $var );
$this->assertEquals( 'HelloWorld', $test );
$var = '123';
$test = \Pressbooks\Sanitize\sanitize_xml_id( $var );
$this->assertStringStartsWith( 'slug-123', $test );
$var = 'こんにちは世界!';
$test = \Pressbooks\Sanitize\sanitize_xml_id( $var );
$this->assertStringStartsWith( 'slug-', $test );
}
public function test_remove_control_characters() {
$var = 'Hello-World!';
$test = $this->_generateControlCharacters() . $var;
$test = \Pressbooks\Sanitize\remove_control_characters( $test );
$this->assertEquals( 12, strlen( $test ) );
$var = 'Héllö Wôrld!';
$test = \Pressbooks\Sanitize\remove_control_characters( $var );
$this->assertEquals( 12, mb_strlen( $test, 'UTF-8' ) );
$var = 'こんにちは世界!';
$test = \Pressbooks\Sanitize\remove_control_characters( $var );
$this->assertEquals( 8, mb_strlen( $test, 'UTF-8' ) );
}
public function test_force_ascii() {
$var = 'Hello-World!';
$test = $this->_generateControlCharacters() . $var;
$test = \Pressbooks\Sanitize\force_ascii( $test );
$this->assertEquals( 12, strlen( $test ) );
$var = 'Héllö Wôrld!';
$test = \Pressbooks\Sanitize\force_ascii( $var );
$this->assertEquals( 9, strlen( $test ) );
$var = 'こんにちは世界!';
$test = \Pressbooks\Sanitize\force_ascii( $var );
$this->assertEquals( 1, strlen( $test ) );
}
/**
* Generate a string containing all the ASCII control characters
*
* @return string
*/
private function _generateControlCharacters() {
$controlCharacters = chr( 127 );
for ( $i = 0; $i < 32; ++$i ) {
$controlCharacters .= chr( $i );
}
return $controlCharacters;
}
public function test_decode() {
$test = '012345&6789';
$test = \Pressbooks\Sanitize\decode( $test );
$this->assertEquals( '012345&6789', $test );
$test = '012345&6789';
$test = \Pressbooks\Sanitize\decode( $test );
$this->assertEquals( '012345&6789', $test );
}
public function test_strip_br() {
$test = 'Hello <br /> World!';
$test = \Pressbooks\Sanitize\strip_br( $test );
$this->assertEquals( 'Hello World!', $test );
$test = 'Hello <br/><br /> World!';
$test = \Pressbooks\Sanitize\strip_br( $test );
$this->assertEquals( 'Hello World!', $test );
$test = 'Hello <br /> World!';
$test = \Pressbooks\Sanitize\strip_br( $test );
$this->assertEquals( 'Hello World!', $test );
$test = 'Hello <br/><br /> World!';
$test = \Pressbooks\Sanitize\strip_br( $test );
$this->assertEquals( 'Hello World!', $test );
}
public function test_filter_title() {
// Acceptable Tags: <br />, <span> with class, <em>, and <strong>.
$test = '<h1><em>Hello</em><br/><strong>World!</strong></h1>';
$test = \Pressbooks\Sanitize\filter_title( $test );
$this->assertEquals( '<em>Hello</em><br /><strong>World!</strong>', $test );
$test = '<span class="pb" style="font-weight:bold;"><i><b>Foobar</b></i></span><p /><div>Foobaz</div>';
$test = \Pressbooks\Sanitize\filter_title( $test );
$this->assertEquals( '<span class="pb">Foobar</span>Foobaz', $test );
$test = '<del><strike>Keep me</strike></del>';
$test = \Pressbooks\Sanitize\filter_title( $test );
$this->assertEquals( '<del>Keep me</del>', $test );
}
public function test_canonicalize_url() {
$url = 'pressbooks.com/';
$this->assertEquals( 'http://pressbooks.com', \Pressbooks\Sanitize\canonicalize_url( $url ) );
$url = 'https://pressbooks.com/';
$this->assertEquals( 'https://pressbooks.com', \Pressbooks\Sanitize\canonicalize_url( $url ) );
$url = 'HTTPS://PRESSBOOKS.COM/FOO/BAR/';
$this->assertEquals( 'https://pressbooks.com/FOO/BAR', \Pressbooks\Sanitize\canonicalize_url( $url ) );
$url = 'ftp://PRESSBOOKS.COM/foo/BAR�/?hello=world&TESTING=��123';
$this->assertEquals( 'http://pressbooks.com/foo/BAR/?hello=world&TESTING=123', \Pressbooks\Sanitize\canonicalize_url( $url ) );
$url = 'MAILTO:^accepts�!mostly,garb@ge.../';
$this->assertEquals( 'MAILTO:^accepts!mostly,garb@ge...', \Pressbooks\Sanitize\canonicalize_url( $url ) );
$url = 'mailto:miranda@yourcompany.com?bcc=eventsteam@yourcompany.com&subject=Excited%20to%20meet%20at%20the%20event!&body=Hi%20Miranda,';
$this->assertEquals( $url, \Pressbooks\Sanitize\canonicalize_url( $url ) );
}
public function test_maybe_https() {
if ( isset( $_SERVER['HTTPS'] ) ) {
$old = $_SERVER['HTTPS'];
}
$_SERVER['HTTPS'] = true;
$url = 'http://pressbooks.com';
$url = \Pressbooks\Sanitize\maybe_https( $url );
$this->assertStringStartsWith( 'https://', $url );
$url = 'http://https.org';
$url = \Pressbooks\Sanitize\maybe_https( $url );
$this->assertEquals( 'https://https.org', $url );
$_SERVER['HTTPS'] = false;
$url = 'http://pressbooks.com';
$url = \Pressbooks\Sanitize\maybe_https( $url );
$this->assertStringStartsNotWith( 'https://', $url );
$url = 'https://http.org';
$url = \Pressbooks\Sanitize\maybe_https( $url );
$this->assertEquals( 'https://http.org', $url );
if ( isset( $old ) ) {
$_SERVER['HTTPS'] = $old;
} else {
unset( $_SERVER['HTTPS'] );
}
}
public function test_normalize_css_urls() {
$css = '@font-face { font-family: "Bergamot Ornaments"; src: url(themes-book/pressbooks-book/fonts/Bergamot-Ornaments.ttf) format("truetype"); font-weight: normal; font-style: normal; }';
$css = \Pressbooks\Sanitize\normalize_css_urls( $css );
$template_directory_uri = get_template_directory_uri();
$this->assertContains( $template_directory_uri . '/assets/book/typography/fonts/Bergamot-Ornaments.ttf', $css );
$css = 'url(/fonts/foo.garbage)';
$css = \Pressbooks\Sanitize\normalize_css_urls( $css );
$this->assertNotContains( 'themes/pressbooks-book/assets/typography/fonts/foo.garbage', $css );
}
public function test_allow_post_content() {
global $allowedposttags;
\Pressbooks\Sanitize\allow_post_content();
$this->assertTrue( $allowedposttags['h1']['xml:lang'] );
}
public function test_clean_filename() {
$file = '../../hacker.php';
$file = \Pressbooks\Sanitize\clean_filename( $file );
$this->assertEquals( $file, 'hacker.php' );
$file = '../../hacker.php;../../~more-hacks.php...';
$file = \Pressbooks\Sanitize\clean_filename( $file );
$this->assertEquals( $file, 'hacker.php;~more-hacks.php' );
$file = 'フランス語.txt'; // UTF-8
$file = \Pressbooks\Sanitize\clean_filename( $file );
$this->assertEquals( $file, 'フランス語.txt' );
}
}