1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author JoakimLofgren |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
include_once __DIR__ . '/PolyfillTestCase.php'; |
7
|
|
|
|
8
|
|
|
use PhpXmlRpc\Helper\Charset; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Test conversion between encodings via usage of the Charset class. |
12
|
|
|
* Note that quite a few other tests testing different classes also test character set conversion. |
13
|
|
|
* |
14
|
|
|
* For Windows if you want to test the output use Consolas font and run the following in cmd: |
15
|
|
|
* chcp 28591 (latin1) |
16
|
|
|
* chcp 65001 (utf8) |
17
|
|
|
* |
18
|
|
|
* @todo add tests for conversion: utf8 -> ascii (incl. chars 0-31 and 127) |
19
|
|
|
* @todo add tests for conversion: latin1 -> utf8 |
20
|
|
|
* @todo add tests for conversion: latin1 -> ascii (incl. chars 0-31 and 127) |
21
|
|
|
*/ |
22
|
|
|
class CharsetTest extends PhpXmlRpc_PolyfillTestCase |
23
|
|
|
{ |
24
|
|
|
// Consolas font should render these properly. Of course this file is encoded in UTF-8 |
25
|
|
|
protected $runes = "ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ"; |
26
|
|
|
protected $greek = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ"; |
27
|
|
|
protected $russian = "Река неслася; бедный чёлн"; |
28
|
|
|
protected $chinese = "我能吞下玻璃而不伤身体。"; |
29
|
|
|
|
30
|
|
|
protected function utf8ToLatin1($data) |
31
|
|
|
{ |
32
|
|
|
return Charset::instance()->encodeEntities( |
33
|
|
|
$data, |
34
|
|
|
'UTF-8', |
35
|
|
|
'ISO-8859-1' |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function utf8ToAscii($data) |
40
|
|
|
{ |
41
|
|
|
return Charset::instance()->encodeEntities( |
42
|
|
|
$data, |
43
|
|
|
'UTF-8', |
44
|
|
|
'US-ASCII' |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testUtf8ToLatin1All() |
49
|
|
|
{ |
50
|
|
|
$latinString = "\n\r\t"; |
51
|
|
|
for($i = 32; $i < 127; $i++) { |
52
|
|
|
$latinString .= chr($i); |
53
|
|
|
} |
54
|
|
|
for($i = 160; $i < 256; $i++) { |
55
|
|
|
$latinString .= chr($i); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// the warning suppression is due to utf8_encode being deprecated in php 8.2 |
59
|
|
|
$string = @utf8_encode($latinString); |
60
|
|
|
$encoded = $this->utf8ToLatin1($string); |
61
|
|
|
$this->assertEquals(str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $latinString), $encoded); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testUtf8ToLatin1EuroSymbol() |
65
|
|
|
{ |
66
|
|
|
$string = 'a.b.c.å.ä.ö.€.'; |
67
|
|
|
$encoded = $this->utf8ToLatin1($string); |
68
|
|
|
// the warning suppression is due to utf8_decode being deprecated in php 8.2 |
69
|
|
|
$this->assertEquals(@utf8_decode('a.b.c.å.ä.ö.€.'), $encoded); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testUtf8ToLatin1Runes() |
73
|
|
|
{ |
74
|
|
|
$string = $this->runes; |
75
|
|
|
$encoded = $this->utf8ToLatin1($string); |
76
|
|
|
$this->assertEquals('ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ', $encoded); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testUtf8ToLatin1Greek() |
80
|
|
|
{ |
81
|
|
|
$string = $this->greek; |
82
|
|
|
$encoded = $this->utf8ToLatin1($string); |
83
|
|
|
$this->assertEquals('Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ', $encoded); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testUtf8ToLatin1Russian() |
87
|
|
|
{ |
88
|
|
|
$string = $this->russian; |
89
|
|
|
$encoded = $this->utf8ToLatin1($string); |
90
|
|
|
$this->assertEquals('Река неслася; бедный чёлн', $encoded); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testUtf8ToLatin1Chinese() |
94
|
|
|
{ |
95
|
|
|
$string = $this->chinese; |
96
|
|
|
$encoded = $this->utf8ToLatin1($string); |
97
|
|
|
$this->assertEquals('我能吞下玻璃而不伤身体。', $encoded); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testLatin15() |
101
|
|
|
{ |
102
|
|
|
if (!function_exists('mb_convert_encoding')) { |
103
|
|
|
$this->markTestSkipped('Miss mbstring extension to test exotic charsets'); |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// euro symbol in ISO-8859-15 |
108
|
|
|
$string = chr(164); |
109
|
|
|
$encoder = Charset::instance(); |
110
|
|
|
$this->assertEquals('€', $encoder->encodeEntities($string, 'ISO-8859-15', 'UTF-8')); |
111
|
|
|
$this->assertEquals('€', $encoder->encodeEntities($string, 'ISO-8859-15', 'US-ASCII')); |
112
|
|
|
$this->assertEquals(chr(164), $encoder->encodeEntities($string, 'ISO-8859-15', 'ISO-8859-15')); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|