forked from movabletype/movabletype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-util.t
501 lines (446 loc) · 20.9 KB
/
08-util.t
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# $Id: 08-util.t 3531 2009-03-12 09:11:52Z fumiakiy $
use utf8;
use lib 't/lib', 'extlib', 'lib', '../lib', '../extlib';
use Test::More;
use MT;
use MT::Test;
use MT::Util qw( start_end_day start_end_week start_end_month start_end_year
start_end_period week2ymd munge_comment
rich_text_transform html_text_transform encode_html decode_html
iso2ts ts2iso offset_time offset_time_list first_n_words
archive_file_for format_ts dirify remove_html
days_in wday_from_ts encode_js decode_js get_entry spam_protect
is_valid_email encode_php encode_url decode_url encode_xml
decode_xml is_valid_url is_url discover_tb convert_high_ascii
mark_odd_rows dsa_verify perl_sha1_digest relative_date
perl_sha1_digest_hex dec2bin bin2dec xliterate_utf8
start_background_task launch_background_tasks substr_wref
extract_urls extract_domain extract_domains is_valid_date
epoch2ts ts2epoch escape_unicode unescape_unicode
sax_parser trim ltrim rtrim asset_cleanup caturl multi_iter
weaken log_time make_string_csv browser_language sanitize_embed
extract_url_path break_up_text dir_separator deep_do );
use MT::I18N qw( encode_text );
use strict;
my $mt = MT->new;
$mt->config('NoHTMLEntities', 1);
BEGIN { plan tests => 208 };
is(substr_wref("Sabado", 0, 3), "Sab"); #1
is(substr_wref("Sàbado", 0, 3), "Sàb"); #2
is(substr_wref("Sàbado", 0, 6), "Sàbado"); #3
is(convert_high_ascii("\xd8"), 'O'); #4
is(convert_high_ascii("Febr\xf9ary"), 'February'); #5
my $str = '';
for (my $i = 0; $i < 256; $i++) {
$str .= chr($i);
}
$mt->config('PublishCharset', 'iso-8859-1');
is(dirify($str), '_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyzaaaaaaaeceeeeiiiinoooooouuuuyssaaaaaaaeceeeeiiiinoooooouuuuyy'); #6
$mt->config('PublishCharset', 'utf-8');
my $utf8_str = qq!\x{c0}\x{c1}\x{c2}\x{c3}\x{c4}\x{c5}\x{100}\x{104}\x{102}\x{c6}\x{c7}\x{106}\x{10c}\x{108}\x{10a}\x{10e}\x{110}\x{c8}\x{c9}\x{ca}\x{cb}\x{112}\x{118}\x{11a}\x{114}\x{116}\x{11c}\x{11e}\x{120}\x{122}\x{124}\x{126}\x{cc}\x{cd}\x{ce}\x{cf}\x{12a}\x{128}\x{12c}\x{12e}\x{130}\x{132}\x{134}\x{136}\x{141}\x{13d}\x{139}\x{13b}\x{13f}\x{d1}\x{143}\x{147}\x{145}\x{14a}\x{d2}\x{d3}\x{d4}\x{d5}\x{d6}\x{d8}\x{14c}\x{150}\x{14e}\x{152}\x{154}\x{158}\x{156}\x{15a}\x{160}\x{15e}\x{15c}\x{218}\x{164}\x{162}\x{166}\x{21a}\x{d9}\x{da}\x{db}\x{dc}\x{16a}\x{16e}\x{170}\x{16c}\x{168}\x{172}\x{174}\x{dd}\x{176}\x{178}\x{179}\x{17d}\x{17b}\x{e0}\x{e1}\x{e2}\x{e3}\x{e4}\x{e5}\x{101}\x{105}\x{103}\x{e6}\x{e7}\x{107}\x{10d}\x{109}\x{10b}\x{10f}\x{111}\x{e8}\x{e9}\x{ea}\x{eb}\x{113}\x{119}\x{11b}\x{115}\x{117}\x{192}\x{11d}\x{11f}\x{121}\x{123}\x{125}\x{127}\x{ec}\x{ed}\x{ee}\x{ef}\x{12b}\x{129}\x{12d}\x{12f}\x{131}\x{133}\x{135}\x{137}\x{138}\x{142}\x{13e}\x{13a}\x{13c}\x{140}\x{f1}\x{144}\x{148}\x{146}\x{149}\x{14b}\x{f2}\x{f3}\x{f4}\x{f5}\x{f6}\x{f8}\x{14d}\x{151}\x{14f}\x{153}\x{155}\x{159}\x{157}\x{15b}\x{161}\x{15f}\x{15d}\x{219}\x{165}\x{163}\x{167}\x{21b}\x{f9}\x{fa}\x{fb}\x{fc}\x{16b}\x{16f}\x{171}\x{16d}\x{169}\x{173}\x{175}\x{fd}\x{ff}\x{177}\x{17e}\x{17c}\x{17a}\x{de}\x{fe}\x{df}\x{17f}\x{d0}\x{f0}!;
is(dirify($utf8_str), 'aaaaaaaaaaecccccddeeeeeeeeegggghhiiiiiiiiiijjklllllnnnnnooooooooooerrrsssssttttuuuuuuuuuuwyyyzzzaaaaaaaaaaecccccddeeeeeeeeefgggghhiiiiiiiiiijjkklllllnnnnnnooooooooooerrrsssssttttuuuuuuuuuuwyyyzzzss'); #7
my $ts = '19770908153005';
is(format_ts('%a', $ts), 'Thu'); #8
is(format_ts('%A', $ts), 'Thursday'); #9
is(format_ts('%b', $ts), 'Sep'); #10
is(format_ts('%B', $ts), 'September'); #11
is(format_ts('%d', $ts), '08'); #12
is(format_ts('%e', $ts), ' 8'); #13
is(format_ts('%H', $ts), '15'); #14
is(format_ts('%I', $ts), '03'); #15
is(format_ts('%j', $ts), '251'); #16
is(format_ts('%k', $ts), '15'); #17
is(format_ts('%l', $ts), ' 3'); #18
is(format_ts('%m', $ts), '09'); #19
is(format_ts('%M', $ts), '30'); #20
is(format_ts('%p', $ts), 'PM'); #21
is(format_ts('%S', $ts), '05'); #22
is(format_ts('%x', $ts), 'September 8, 1977'); #23
is(format_ts('%X', $ts), ' 3:30 PM'); #24
is(format_ts('%y', $ts), '77'); #25
is(format_ts('%Y', $ts), '1977'); #26
is(encode_html('<foo>'), '<foo>'); #27
is(encode_html('>'), '>'); #28
is(encode_html('>', 1), '&gt;'); #29
is(encode_html("foo & bar &baz"), "foo & bar &baz"); #30
is(decode_html(encode_html('<foo>')), '<foo>'); #31
is(encode_html(), ''); #32
is(encode_html("<"), "<"); #33
is(encode_html("ƒ"), "ƒ"); #34
is(encode_html("ƒ"), "ƒ"); #35
is(encode_html("À"), "À"); #36
is(encode_html('"'), '"'); #37
is(encode_html('&'), '&'); #38
is(encode_html('>'), '>'); #39
is(encode_html('<'), '<'); #40
is(encode_html("<foo>\cM\n"), "<foo>\n"); #41
ok(wday_from_ts(1964,1,3) == 5); #42
ok(wday_from_ts(1995,11,13) == 1); #43
ok(wday_from_ts(1995,11,14) == 2); #44
ok(wday_from_ts(1995,11,15) == 3); #45
ok(wday_from_ts(1995,11,16) == 4); #46
ok(wday_from_ts(1995,11,17) == 5); #47
ok(wday_from_ts(1995,11,18) == 6); #48
ok(wday_from_ts(1995,11,19) == 0); #49
ok(wday_from_ts(1995,11,20) == 1); #50
ok(wday_from_ts(1995,2,28) == 2); #51
ok(wday_from_ts(1946,12,26) == 4); #52
my %xml_tests = (
'foo' => 'foo', #53 #54 #55
'x < y' => 'x < y', #56 #57 #58
'foo & bar' => 'foo & bar', #59 #60 #61
'foo\'s bar' => 'foo's bar', #62 #63 #64
'<title>my title</title>' => #65 #66 #67 #68
[ '<![CDATA[<title>my title</title>]]>',
'<title>my title</title>', ],
'<foo>]]>' => #69 #70 #71 #72
[ '<![CDATA[<foo>]]>]]>',
'<foo>]]>', ],
'x < y' => #73 #74 #75 #76
[ '<![CDATA[x < y]]>',
'x &lt; y', ],
'foobár' => #77 #78 #79 #80
[ '<![CDATA[foobár]]>',
'foob&aacute;r', ],
);
for my $test (keys %xml_tests) {
if (ref($xml_tests{$test}) eq 'ARRAY') {
is(encode_xml($test), $xml_tests{$test}[0]); #65 #69 #73 #77
is(decode_xml($xml_tests{$test}[0]), $test); #66 #70 #74 #78
is(decode_xml(encode_xml($test)), $test); #67 #71 #75 #79
MT::ConfigMgr->instance->NoCDATA(1);
is(encode_xml($test), $xml_tests{$test}[1]); #68 #72 #76 #80
MT::ConfigMgr->instance->NoCDATA(0);
} else {
is(encode_xml($test), $xml_tests{$test}); #53 #56 #59 #62
is(decode_xml($xml_tests{$test}), $test); #54 #57 #60 #63
is(decode_xml(encode_xml($test)), $test); #55 #58 #61 #64
}
}
### tests for trim
is(ltrim(' sunday'), 'sunday'); #81
is(ltrim(' sunday monday'), 'sunday monday'); #82
is(ltrim(' sunday monday tuesday '), 'sunday monday tuesday '); #83
is(ltrim('sunday'), 'sunday'); #84
is(rtrim('sunday'), 'sunday'); #85
is(rtrim('sunday '), 'sunday'); #86
is(rtrim(' sunday monday '), ' sunday monday'); #87
is(rtrim('sunday monday tuesday '), 'sunday monday tuesday'); #88
is(trim('sunday'), 'sunday'); #89
is(trim(' sunday'), 'sunday'); #90
is(trim(' sunday '), 'sunday'); #91
is(trim(' sunday monday '), 'sunday monday'); #92
is(remove_html('<![CDATA[foo]]>'), '<![CDATA[foo]]>', "remove html preserves CDATA");
is(remove_html('<![CDATA[]]><script>alert("foo")</script><![CDATA[]]>'), '<![CDATA[]]>alert("foo")<![CDATA[]]>', "remove html prevents abuse");
is(remove_html('<![CDATA[one]]><script>alert("foo")</script><![CDATA[two]]>'), '<![CDATA[one]]>alert("foo")<![CDATA[two]]>', "remove html prevents abuse, saves plain text");
is(remove_html('<![CDATA[<foo>]]><script>alert("foo")</script><![CDATA[two]]>'), '<![CDATA[<foo>]]>alert("foo")<![CDATA[two]]>', "remove html prevents abuse, saves plain text, escapes inner < characters");
is(MT::Util::to_json({'foo' => 2}), '{"foo":2}');
is(MT::Util::to_json({'foo' => 1}), '{"foo":1}');
is(MT::Util::to_json({'foo' => 0}), '{"foo":0}');
is(MT::Util::to_json({'foo' => 'hoge'}), '{"foo":"hoge"}');
is(MT::Util::to_json({'foo' => 'ho1ge'}), '{"foo":"ho1ge"}');
is(MT::Util::to_json(['foo', 'bar', 'baz']), '["foo","bar","baz"]');
is(MT::Util::to_json(['foo', 1, 'bar', 2, 3, 4]), '["foo",1,"bar",2,3,4]');
is(MT::Util::to_json(['foo', 1, 'bar', { hoge => 1, moge => 'a' }]), '["foo",1,"bar",{"hoge":1,"moge":"a"}]');
### start_end_*
is(start_end_day('19770908153005'), '19770908000000', 'Want to get a scalar value from start_end_day()');
is_deeply([start_end_day('19770908153005')], ['19770908000000', '19770908235959'], 'Want to get a list value from start_end_day()');
is(start_end_week('19770908153005'), '19770904000000', 'Want to get a scalar value from start_end_week()');
is_deeply([start_end_week('19770908153005')], ['19770904000000', '19770910235959'], 'Want to get a list value from start_end_week()');
is(start_end_month('19770908153005'), '19770901000000', 'Want to get a scalar value from start_end_month()');
is_deeply([start_end_month('19770908153005')], ['19770901000000', '19770930235959'], 'Want to get a list value from start_end_month()');
is(start_end_year('19770908153005'), '19770101000000', 'Want to get a scalar value from start_end_year()');
is_deeply([start_end_year('19770908153005')], ['19770101000000', '19771231235959'], 'Want to get a list value from start_end_year()');
is(start_end_period('Individual', $ts), $ts, 'start_end_period() for Individual');
is(start_end_period('Daily', $ts), start_end_day($ts), 'start_end_period() for Daily');
is(start_end_period('Weekly', $ts), start_end_week($ts), 'start_end_period() for Weekly');
is(start_end_period('Monthly', $ts), start_end_month($ts), 'start_end_period() for Monthly');
is_deeply([week2ymd(1977, 36)], [1977,9, 4], 'week2ymd()');
### format text
my $content = <<__HTML__;
Foo
<div>Bar</div>
http://example.com/
__HTML__
is(rich_text_transform($content), $content, 'rich_text_transform()');
my $content_html_text_transformed = <<__HTML__;
<p>Foo</p>
<div>Bar</div>
<p>http://example.com/<br />
</p>
__HTML__
chomp($content_html_text_transformed);
is(html_text_transform($content), $content_html_text_transformed, 'html_text_transform()');
is(first_n_words($content, 2), 'Foo Bar', 'first_n_words()');
### time conversion
my $blog = $mt->model('blog')->new;
$blog->server_offset(0);
my $timestamp = '20021224103045';
my $epoch = 1040725845;
is(iso2ts($blog, '20021224T10:30:45'), '20021224103045', 'iso2ts()');
is(ts2iso($blog, '20021224103045'), '2002-12-24T10:30:45Z', 'ts2iso() (server_offset is 0)');
is(epoch2ts($blog, $epoch), $timestamp, 'epoch2ts() (server_offset is 0)');
is(ts2epoch($blog, $timestamp), $epoch, 'ts2epoch() (server_offset is 0)');
is(offset_time($epoch, $blog), $epoch, 'offset_time() (server_offset is 0)');
is(offset_time($epoch, $blog, '-'), $epoch, 'offset_time() (dir is "-" and server_offset is 0)');
is(offset_time_list($epoch, $blog), 'Tue Dec 24 10:30:45 2002', 'offset_time_list() (server_offset is 0)');
is(offset_time_list($epoch, $blog, '-'), 'Tue Dec 24 10:30:45 2002', 'offset_time_list() (dir is "-" and server_offset is 0)');
$blog->server_offset(1);
is(ts2iso($blog, $timestamp), '2002-12-24T09:30:45Z', 'ts2iso() (server_offset is 1)');
is(epoch2ts($blog, $epoch), '20021224113045', 'epoch2ts() (server_offset is 1)');
is(ts2epoch($blog, $timestamp), $epoch-60*60, 'ts2epoch() (server_offset is 1)');
is(offset_time($epoch, $blog), $epoch+60*60, 'offset_time() (server_offset is 1)');
is(offset_time($epoch, $blog, '-'), $epoch-60*60, 'offset_time() (dir is "-" and server_offset is 1)');
is(offset_time_list($epoch, $blog), 'Tue Dec 24 11:30:45 2002', 'offset_time_list() (server_offset is 1)');
is(offset_time_list($epoch, $blog, '-'), 'Tue Dec 24 09:30:45 2002', 'offset_time_list() (dir is "-" and server_offset is 1)');
$blog->server_offset(-1);
is(ts2iso($blog, $timestamp), '2002-12-24T11:30:45Z', 'ts2iso() (server_offset is -1)');
is(epoch2ts($blog, $epoch), '20021224093045', 'epoch2ts() (server_offset is -1)');
is(ts2epoch($blog, $timestamp), $epoch+60*60, 'ts2epoch() (server_offset is -1)');
is(offset_time($epoch, $blog), $epoch-60*60, 'offset_time() (server_offset is -1)');
is(offset_time($epoch, $blog, '-'), $epoch+60*60, 'offset_time() (dir is "-" and server_offset is -1)');
is(offset_time_list($epoch, $blog), 'Tue Dec 24 09:30:45 2002', 'offset_time_list() (server_offset is -1)');
is(offset_time_list($epoch, $blog, '-'), 'Tue Dec 24 11:30:45 2002', 'offset_time_list() (dir is "-" and server_offset is -1)');
### format relative date
$blog->server_offset(0);
my @relative_date_data = (
{
offset => 0,
style => undef,
expected => 'moments ago',
},
{
offset => 60*60+10,
style => undef,
expected => '1 hour ago',
},
{
offset => 60*60*24+10,
style => undef,
expected => '1 day ago',
},
{
offset => 60*60*24*7+10,
style => undef,
expected => '2002/12/24',
},
{
offset => 0,
style => 2,
expected => 'less than 1 minute ago',
},
{
offset => 60*60+10,
style => 2,
expected => '1 hour ago',
},
{
offset => 60*60*24+10,
style => 2,
expected => '1 day ago',
},
{
offset => 60*60*24*7+10,
style => 2,
expected => '2002/12/24',
},
{
offset => 0,
style => 3,
expected => '0 seconds',
},
{
offset => 60*60+10,
style => 3,
expected => '1 hour',
},
{
offset => 60*60*24+10,
style => 3,
expected => '1 day',
},
{
offset => 60*60*24*7+10,
style => 3,
expected => '2002/12/24',
},
);
for my $d (@relative_date_data) {
is(relative_date($timestamp, $epoch+$d->{offset}, $blog, '%Y/%m/%d', $d->{style}), $d->{expected}, "relative_date() (offset:$d->{offset}, style:$d->{style})");
}
is(days_in(2, 2002), '28', 'days_in()');
is(days_in(2, 2000), '29', 'days_in() (leap year)');
### encode and decode
my $script_tag = "<script>alert('test');alert(\"test\");</script>";
my $script_tag_encoded = '\\<s\\cript\\>alert(\\\'test\\\');alert(\\"test\\");\\<\\/s\\cript\\>';
is(encode_js($script_tag), $script_tag_encoded, 'encode_js()');
is(decode_js($script_tag_encoded), $script_tag, 'decode_js()');
is(encode_php("\\\$\"\n\r\t"), "\\\\\$\"\n\r\t", 'encode_php()');
is(encode_php("\\\$\"\n\r\t", 'qq'), '\\\\\\$\\"\\n\\r\\t', 'encode_php() (qq)');
is(encode_php("\\\$\"\n\r\t", 'here'), '\\\\\\$"\\n\\r\\t', 'encode_php() (here)');
my $url = 'http://www.example.com/?foo=bar&baz=10%';
my $url_encoded = 'http%3A%2F%2Fwww.example.com%2F%3Ffoo%3Dbar%26baz%3D10%25';
is(encode_url($url), $url_encoded, 'encode_url()');
is(decode_url($url_encoded), $url, 'decode_url()');
### escape
is(spam_protect('foo+bar@example.com'), 'foo+bar@example.com', 'spam_protect()');
### validation
is(is_valid_email('foo+bar@example.com'), 'foo+bar@example.com', 'is_valid_email() for valid value');
is(is_valid_email('foo:bar@example.com'), '0', 'is_valid_email() for invalid value');
is(is_valid_url('http://www.example.com/'), 'http://www.example.com/', 'is_valid_url() normal url');
is(is_valid_url('http;//www.example.com/'), 'http://www.example.com/', 'is_valid_url() fixing typo (1)');
is(is_valid_url('http//www.example.com/'), 'http://www.example.com/', 'is_valid_url() fixing typo (2');
#is(is_valid_url('ftp://www.example.com/'), 'ftp://www.example.com/', 'is_valid_url() for ftp'); # Not support.
is(is_valid_url('https://www.example.com/'), 'https://www.example.com/', 'is_valid_url() ssl');
is(is_valid_url('https;//www.example.com/'), 'https://www.example.com/', 'is_valid_url() fixing typo(3)');
is(is_valid_url('http://www.example.com:8080/'), 'http://www.example.com:8080/', 'is_valid_url() port');
ok(is_url('http://www.example.com/?foo=bar&baz=10%'), 'is_url() normal url');
ok(is_url('https://www.example.com/?foo=bar&baz=10%'), 'is_url() ssl');
ok(is_url('http://www.example.com:8080/?foo=bar&baz=10%'), 'is_url() port');
ok(! is_url('not a url'), 'is_url() not a url');
ok(! is_url('not http://_'), 'is_url() invalid url');
### other utilities
my $tb_content = <<__HTML__;
<html>
<head>
<!--
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<rdf:Description
rdf:about="http://example.com/2002/12/24/1/index.html"
trackback:ping="http://example.com/mt/mt-tb.cgi/9999"
dc:title="Foo"
dc:identifier="http://example.com/2002/12/24/1/index.html"
dc:subject="Bar"
dc:description="Foo Bar Foo Bar ..."
dc:creator="Baz"
dc:date="2002-12-24T10:30:45Z" />
</rdf:RDF>
-->
</head>
<body>
</body>
</html>
__HTML__
is_deeply(
discover_tb(
'http://example.com/2002/12/24/1/index.html',
undef, \$tb_content
),
{
'permalink' => 'http://example.com/2002/12/24/1/index.html',
'title' => 'Foo',
'ping_url' => 'http://example.com/mt/mt-tb.cgi/9999',
'dc:identifier' => 'http://example.com/2002/12/24/1/index.html',
'dc:title' => 'Foo',
'dc:date' => '2002-12-24T10:30:45Z',
'dc:subject' => 'Bar',
'dc:description' => 'Foo Bar Foo Bar ...',
'dc:creator' => 'Baz',
'rdf:about' => 'http://example.com/2002/12/24/1/index.html',
'trackback:ping' => 'http://example.com/mt/mt-tb.cgi/9999',
},
'discover_tb()'
);
my @list = (
{v => 1},
{v => 2},
{v => 3},
{v => 4},
);
mark_odd_rows(\@list);
is_deeply(\@list, [{v=>1,is_odd=>1},{v=>2,is_odd=>''},{v=>3,is_odd=>1},{v=>4,is_odd=>''}], 'mark_odd_rows()');
is(xliterate_utf8('Ã'), 'A', 'xliterate_utf8()');
{
require File::Temp;
my ($fh, $file) = File::Temp::tempfile(undef, DIR => MT->config->TempDir);
close($fh);
unlink($file);
start_background_task(sub {
open(my $fh, '>', $file);
close($fh);
});
CORE::sleep(1);
is(-e $file, '1', 'start_background_task()');
unlink($file) if $file;
}
my $urls_content = <<__URLS__;
Foo
http://example.com/path/to/index.html
Bar
http://www.example.com/foo/bar/index.html
__URLS__
my $urls_extracted = {
'example.com/path/to/index.html' => 'example.com',
'example.com/foo/bar/index.html' => 'example.com'
};
is_deeply({extract_urls($urls_content)}, $urls_extracted, 'extract_urls()');
is(extract_domain('example.com/foo/bar/index.html'), 'example.com', 'extract_domain()');
is_deeply([extract_domains($urls_content)], ['example.com', 'example.com'], 'extract_domains()');
is(is_valid_date('2002-12-24 09:30:45'), '1', 'valid date for is_valid_date()');
is(is_valid_date('2002-12-24T09:30:45'), '0', 'invalid string for is_valid_date()');
is(is_valid_date('2002-12-24 99:99:99'), '0', 'invalid value for is_valid_date()');
# Japanese character "あ"
my $utf8_e38182 = Encode::decode('utf8', "\xe3\x81\x82");
is(escape_unicode(Encode::encode('utf8', $utf8_e38182)), 'あ', 'escape_unicode()');
is(unescape_unicode('あ'), $utf8_e38182, 'unescape_unicode()');
isa_ok(sax_parser(), 'XML::LibXML::SAX', 'sax_parser()');
is(asset_cleanup('<form mt:asset-id="1" contenteditable="false"><img src="http://example.com/img/foo.jpg" /></form>'), '<span><img src="http://example.com/img/foo.jpg" /></span>', 'asset_cleanup()');
is(caturl('http://example.com/', 'foo', '/bar', 'baz/', '/foo/'), 'http://example.com/foo/bar/baz/foo/', 'caturl()');
{
my $ref;
{
my $str = 'Foo';
$ref = \$str;
weaken($ref);
}
is($ref, undef, 'weaken()');
}
ok(log_time(), 'log_time()');
is(make_string_csv('Foo,Bar'), '"Foo,Bar"', 'make_string_csv()');
my %accept_languages = qw(
en-us en-us
ja-jp ja
de-DE de
es-ve es
fr-ca fr
nl-nl nl
);
while (my($env, $expected) = each(%accept_languages)) {
$ENV{HTTP_ACCEPT_LANGUAGE} = $env;
is(browser_language(), $expected, "browser_language() for \"$env\"");
}
MT->config('EmbedDomainWhitelist', 'example.com');
my $taint_embed = <<__HTML__;
Content
<a href="http://example.com/foo.html" onclick="alert('test');">Foo</a>
<script type="text/javascript">
alert('test');
</script>
__HTML__
my $sanitized_embed = <<__HTML__;
Content
<a href="http://example.com/foo.html">Foo</a>
<script type="text/javascript"></script>
__HTML__
is(sanitize_embed($taint_embed), $sanitized_embed, 'sanitize_embed()');
is(extract_url_path('http://www.example.com/foo/bar.html?baz'), '/foo/bar.html', 'extract_url_path() with query');
is(extract_url_path('http://www.example.com/foo/bar.html#baz'), '/foo/bar.html', 'extract_url_path() with fragment');
is(break_up_text('FooBarBaz', 2), 'Fo oB ar Ba z', 'break_up_text()');
ok(dir_separator(), 'dir_separator()');
{
my @members = ();
my $counter = 0;
my $sub = sub {
$counter++;
push(@members, ${ $_[0] });
};
deep_do({
key1 => 'Foo',
key2 => ['Bar', 'Baz'],
key3 => undef,
}, $sub);
is_deeply([sort(grep($_, @members))], ['Bar', 'Baz', 'Foo'], 'result of deep_do()');
is($counter, 4, 'a subroutine called 4 times in deep_do()');
}