| 1 | # $Id$ |
|---|
| 2 | |
|---|
| 3 | use lib 't/lib', 'extlib', 'lib', '../lib', '../extlib'; |
|---|
| 4 | use Test; |
|---|
| 5 | use MT; |
|---|
| 6 | use MT::Test; |
|---|
| 7 | use MT::Util qw( encode_html decode_html wday_from_ts format_ts dirify |
|---|
| 8 | convert_high_ascii encode_xml decode_xml substr_wref |
|---|
| 9 | trim ltrim rtrim remove_html ); |
|---|
| 10 | use MT::I18N qw( encode_text ); |
|---|
| 11 | use strict; |
|---|
| 12 | |
|---|
| 13 | my $mt = MT->new; |
|---|
| 14 | $mt->config('NoHTMLEntities', 1); |
|---|
| 15 | |
|---|
| 16 | BEGIN { plan tests => 104 }; |
|---|
| 17 | |
|---|
| 18 | ok(substr_wref("Sabado", 0, 3), "Sab"); #1 |
|---|
| 19 | ok(substr_wref("Sàbado", 0, 3), "Sàb"); #2 |
|---|
| 20 | ok(substr_wref("Sàbado", 0, 6), "Sàbado"); #3 |
|---|
| 21 | |
|---|
| 22 | ok(convert_high_ascii("\xd8"), 'O'); #4 |
|---|
| 23 | ok(convert_high_ascii("Febr\xf9ary"), 'February'); #5 |
|---|
| 24 | |
|---|
| 25 | my $str = ''; |
|---|
| 26 | for (my $i = 0; $i < 256; $i++) { |
|---|
| 27 | $str .= chr($i); |
|---|
| 28 | } |
|---|
| 29 | $mt->config('PublishCharset', 'iso-8859-1'); |
|---|
| 30 | ok(dirify($str), '_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyzaaaaaaaeceeeeiiiinoooooouuuuyssaaaaaaaeceeeeiiiinoooooouuuuyy'); #6 |
|---|
| 31 | $mt->config('PublishCharset', 'utf-8'); |
|---|
| 32 | use bytes; |
|---|
| 33 | my $utf8_str = q{ÃÃÃÃÃÃ
ÄÄÄÃÃÄÄÄÄÄÄÃÃÃÃÄÄÄÄÄÄÄÄ Ä¢Ä€ÄŠÃÃÃÃĪĚĬĮİIJĎĶÅĜĹĻĿÃÅÅÅ
ÅÃÃÃÃÃÃÅÅÅÅÅÅÅÅÅ ÅÅÈŀŢŊÈÃÃÃÃŪŮŰŬŚŲŎÃŶŞŹŜŻà áâãÀåÄÄ
ÄÊçÄÄÄÄÄÄÚéêëÄÄÄÄÄÆÄÄġģĥħìÃîïīĩÄįıijĵķĞÅğĺČÅñÅÅÅÅÅòóÎõöÞÅÅÅÅÅÅÅÅÅ¡ÅÅÈťţŧÈùúûÌūůűÅũųŵÜÿŷşŌźÃßÃÅ¿Ãð}; |
|---|
| 34 | ok(dirify($utf8_str), 'aaaaaaaaaaecccccddeeeeeeeeegggghhiiiiiiiiiijjklllllnnnnnooooooooooerrrsssssttttuuuuuuuuuuwyyyzzzaaaaaaaaaaecccccddeeeeeeeeefgggghhiiiiiiiiiijjkklllllnnnnnnooooooooooerrrsssssttttuuuuuuuuuuwyyyzzzss'); #7 |
|---|
| 35 | |
|---|
| 36 | my $ts = '19770908153005'; |
|---|
| 37 | ok(format_ts('%a', $ts), 'Thu'); #8 |
|---|
| 38 | ok(format_ts('%A', $ts), 'Thursday'); #9 |
|---|
| 39 | ok(format_ts('%b', $ts), 'Sep'); #10 |
|---|
| 40 | ok(format_ts('%B', $ts), 'September'); #11 |
|---|
| 41 | ok(format_ts('%d', $ts), '08'); #12 |
|---|
| 42 | ok(format_ts('%e', $ts), ' 8'); #13 |
|---|
| 43 | ok(format_ts('%H', $ts), '15'); #14 |
|---|
| 44 | ok(format_ts('%I', $ts), '03'); #15 |
|---|
| 45 | ok(format_ts('%j', $ts), '251'); #16 |
|---|
| 46 | ok(format_ts('%k', $ts), '15'); #17 |
|---|
| 47 | ok(format_ts('%l', $ts), ' 3'); #18 |
|---|
| 48 | ok(format_ts('%m', $ts), '09'); #19 |
|---|
| 49 | ok(format_ts('%M', $ts), '30'); #20 |
|---|
| 50 | ok(format_ts('%p', $ts), 'PM'); #21 |
|---|
| 51 | ok(format_ts('%S', $ts), '05'); #22 |
|---|
| 52 | ok(format_ts('%x', $ts), 'September 8, 1977'); #23 |
|---|
| 53 | ok(format_ts('%X', $ts), ' 3:30 PM'); #24 |
|---|
| 54 | ok(format_ts('%y', $ts), '77'); #25 |
|---|
| 55 | ok(format_ts('%Y', $ts), '1977'); #26 |
|---|
| 56 | |
|---|
| 57 | ok(encode_html('<foo>'), '<foo>'); #27 |
|---|
| 58 | ok(encode_html('>'), '>'); #28 |
|---|
| 59 | ok(encode_html('>', 1), '&gt;'); #29 |
|---|
| 60 | ok(encode_html("foo & bar &baz"), "foo & bar &baz"); #30 |
|---|
| 61 | ok(decode_html(encode_html('<foo>')), '<foo>'); #31 |
|---|
| 62 | ok(encode_html(), ''); #32 |
|---|
| 63 | ok(encode_html("<"), "<"); #33 |
|---|
| 64 | ok(encode_html("ƒ"), "ƒ"); #34 |
|---|
| 65 | ok(encode_html("ƒ"), "ƒ"); #35 |
|---|
| 66 | ok(encode_html("À"), "À"); #36 |
|---|
| 67 | ok(encode_html('"'), '"'); #37 |
|---|
| 68 | ok(encode_html('&'), '&'); #38 |
|---|
| 69 | ok(encode_html('>'), '>'); #39 |
|---|
| 70 | ok(encode_html('<'), '<'); #40 |
|---|
| 71 | ok(encode_html("<foo>\cM\n"), "<foo>\n"); #41 |
|---|
| 72 | |
|---|
| 73 | ok(wday_from_ts(1964,1,3) == 5); #42 |
|---|
| 74 | ok(wday_from_ts(1995,11,13) == 1); #43 |
|---|
| 75 | ok(wday_from_ts(1995,11,14) == 2); #44 |
|---|
| 76 | ok(wday_from_ts(1995,11,15) == 3); #45 |
|---|
| 77 | ok(wday_from_ts(1995,11,16) == 4); #46 |
|---|
| 78 | ok(wday_from_ts(1995,11,17) == 5); #47 |
|---|
| 79 | ok(wday_from_ts(1995,11,18) == 6); #48 |
|---|
| 80 | ok(wday_from_ts(1995,11,19) == 0); #49 |
|---|
| 81 | ok(wday_from_ts(1995,11,20) == 1); #50 |
|---|
| 82 | ok(wday_from_ts(1995,2,28) == 2); #51 |
|---|
| 83 | ok(wday_from_ts(1946,12,26) == 4); #52 |
|---|
| 84 | |
|---|
| 85 | my %xml_tests = ( |
|---|
| 86 | 'foo' => 'foo', #53 #54 #55 |
|---|
| 87 | 'x < y' => 'x < y', #56 #57 #58 |
|---|
| 88 | 'foo & bar' => 'foo & bar', #59 #60 #61 |
|---|
| 89 | 'foo\'s bar' => 'foo's bar', #62 #63 #64 |
|---|
| 90 | '<title>my title</title>' => #65 #66 #67 #68 |
|---|
| 91 | [ '<![CDATA[<title>my title</title>]]>', |
|---|
| 92 | '<title>my title</title>', ], |
|---|
| 93 | '<foo>]]>' => #69 #70 #71 #72 |
|---|
| 94 | [ '<![CDATA[<foo>]]>]]>', |
|---|
| 95 | '<foo>]]>', ], |
|---|
| 96 | 'x < y' => #73 #74 #75 #76 |
|---|
| 97 | [ '<![CDATA[x < y]]>', |
|---|
| 98 | 'x &lt; y', ], |
|---|
| 99 | 'foobár' => #77 #78 #79 #80 |
|---|
| 100 | [ '<![CDATA[foobár]]>', |
|---|
| 101 | 'foob&aacute;r', ], |
|---|
| 102 | ); |
|---|
| 103 | |
|---|
| 104 | for my $test (keys %xml_tests) { |
|---|
| 105 | if (ref($xml_tests{$test}) eq 'ARRAY') { |
|---|
| 106 | ok(encode_xml($test), $xml_tests{$test}[0]); #65 #69 #73 #77 |
|---|
| 107 | ok(decode_xml($xml_tests{$test}[0]), $test); #66 #70 #74 #78 |
|---|
| 108 | ok(decode_xml(encode_xml($test)), $test); #67 #71 #75 #79 |
|---|
| 109 | MT::ConfigMgr->instance->NoCDATA(1); |
|---|
| 110 | ok(encode_xml($test), $xml_tests{$test}[1]); #68 #72 #76 #80 |
|---|
| 111 | MT::ConfigMgr->instance->NoCDATA(0); |
|---|
| 112 | } else { |
|---|
| 113 | ok(encode_xml($test), $xml_tests{$test}); #53 #56 #59 #62 |
|---|
| 114 | ok(decode_xml($xml_tests{$test}), $test); #54 #57 #60 #63 |
|---|
| 115 | ok(decode_xml(encode_xml($test)), $test); #55 #58 #61 #64 |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | ### tests for trim |
|---|
| 120 | ok(ltrim(' sunday'), 'sunday'); #81 |
|---|
| 121 | ok(ltrim(' sunday monday'), 'sunday monday'); #82 |
|---|
| 122 | ok(ltrim(' sunday monday tuesday '), 'sunday monday tuesday '); #83 |
|---|
| 123 | ok(ltrim('sunday'), 'sunday'); #84 |
|---|
| 124 | ok(rtrim('sunday'), 'sunday'); #85 |
|---|
| 125 | ok(rtrim('sunday '), 'sunday'); #86 |
|---|
| 126 | ok(rtrim(' sunday monday '), ' sunday monday'); #87 |
|---|
| 127 | ok(rtrim('sunday monday tuesday '), 'sunday monday tuesday'); #88 |
|---|
| 128 | ok(trim('sunday'), 'sunday'); #89 |
|---|
| 129 | ok(trim(' sunday'), 'sunday'); #90 |
|---|
| 130 | ok(trim(' sunday '), 'sunday'); #91 |
|---|
| 131 | ok(trim(' sunday monday '), 'sunday monday'); #92 |
|---|
| 132 | |
|---|
| 133 | ok(remove_html('<![CDATA[foo]]>'), '<![CDATA[foo]]>', "remove html preserves CDATA"); |
|---|
| 134 | ok(remove_html('<![CDATA[]]><script>alert("foo")</script><![CDATA[]]>'), '<![CDATA[]]>alert("foo")<![CDATA[]]>', "remove html prevents abuse"); |
|---|
| 135 | ok(remove_html('<![CDATA[one]]><script>alert("foo")</script><![CDATA[two]]>'), '<![CDATA[one]]>alert("foo")<![CDATA[two]]>', "remove html prevents abuse, saves plain text"); |
|---|
| 136 | ok(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"); |
|---|
| 137 | |
|---|
| 138 | ok(MT::Util::to_json({'foo' => 2}), '{"foo":2}'); |
|---|
| 139 | ok(MT::Util::to_json({'foo' => 1}), '{"foo":1}'); |
|---|
| 140 | ok(MT::Util::to_json({'foo' => 0}), '{"foo":0}'); |
|---|
| 141 | ok(MT::Util::to_json({'foo' => 'hoge'}), '{"foo":"hoge"}'); |
|---|
| 142 | ok(MT::Util::to_json({'foo' => 'ho1ge'}), '{"foo":"ho1ge"}'); |
|---|
| 143 | ok(MT::Util::to_json(['foo', 'bar', 'baz']), '["foo","bar","baz"]'); |
|---|
| 144 | ok(MT::Util::to_json(['foo', 1, 'bar', 2, 3, 4]), '["foo",1,"bar",2,3,4]'); |
|---|
| 145 | ok(MT::Util::to_json(['foo', 1, 'bar', { hoge => 1, moge => 'a' }]), '["foo",1,"bar",{"hoge":1,"moge":"a"}]'); |
|---|
| 146 | |
|---|
| 147 | =pod |
|---|
| 148 | |
|---|
| 149 | my %dates = ( |
|---|
| 150 | '20021224103045' => '20021224T10:30:45', |
|---|
| 151 | '20021224T10:30:45' => '20021224T10:30:45', |
|---|
| 152 | '20021224T103045Z' => '20021224T02:30:45', |
|---|
| 153 | '20021224T10:30:45-0000' => '20021224T02:30:45', |
|---|
| 154 | '20021224T10:30:45+0030' => '20021224T02:00:45', |
|---|
| 155 | '2002-12-24T103045+02' => '20021224T00:30:45', |
|---|
| 156 | '2002-12-24T10:30:45-08:00' => '20021224T10:30:45', |
|---|
| 157 | '20020615103045' => '20020615T10:30:45', |
|---|
| 158 | '20020615103045-08' => '20020615T11:30:45', |
|---|
| 159 | '20020615103045+02' => '20020615T01:30:45', |
|---|
| 160 | ); |
|---|
| 161 | for my $date (keys %dates) { |
|---|
| 162 | ok(parse_iso8601_date($date, -8, 'gmtime'), $dates{$date}); |
|---|
| 163 | #for my $tz ((":America/Los_Angeles", ":Europe/Helsinki")) { |
|---|
| 164 | # $ENV{"TZ"} = $tz; |
|---|
| 165 | # for my $lt (("localtime", "gmtime")) { |
|---|
| 166 | # my $fmt = $lt eq "localtime" ? " %18s " : "%20s"; |
|---|
| 167 | # printf "%-25s -> $fmt %s\n", $i, parseISO8601Date($i, $lt), $tz; |
|---|
| 168 | # } |
|---|
| 169 | #} |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | =cut |
|---|