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 |
12
|
|
|
* |
13
|
|
|
* For Windows if you want to test the output use Consolas font |
14
|
|
|
* 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 |
25
|
|
|
protected $runes = "ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ"; |
26
|
|
|
protected $greek = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ"; |
27
|
|
|
protected $russian = "Река неслася; бедный чёлн"; |
28
|
|
|
protected $chinese = "我能吞下玻璃而不伤身体。"; |
29
|
|
|
|
30
|
|
|
protected $latinString; |
31
|
|
|
|
32
|
|
|
/// @todo move to usage of a dataProvider and create the latinString there |
33
|
|
|
protected function set_up() |
34
|
|
|
{ |
35
|
|
|
// construct a latin string with all chars (except control ones) |
36
|
|
|
$this->latinString = "\n\r\t"; |
37
|
|
|
for($i = 32; $i < 127; $i++) { |
38
|
|
|
$this->latinString .= chr($i); |
39
|
|
|
} |
40
|
|
|
for($i = 160; $i < 256; $i++) { |
41
|
|
|
$this->latinString .= chr($i); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function utf8ToLatin1($data) |
46
|
|
|
{ |
47
|
|
|
return Charset::instance()->encodeEntities( |
48
|
|
|
$data, |
49
|
|
|
'UTF-8', |
50
|
|
|
'ISO-8859-1' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function utf8ToAscii($data) |
55
|
|
|
{ |
56
|
|
|
return Charset::instance()->encodeEntities( |
57
|
|
|
$data, |
58
|
|
|
'UTF-8', |
59
|
|
|
'US-ASCII' |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testUtf8ToLatin1All() |
64
|
|
|
{ |
65
|
|
|
/*$this->assertEquals( |
66
|
|
|
'ISO-8859-1', |
67
|
|
|
mb_detect_encoding($this->latinString, 'ISO-8859-1, UTF-8, WINDOWS-1251, ASCII', true), |
68
|
|
|
'Setup latinString is not ISO-8859-1 encoded...' |
69
|
|
|
);*/ |
70
|
|
|
// the warning suppression is due to utf8_encode being deprecated in php 8.2 |
71
|
|
|
$string = @utf8_encode($this->latinString); |
72
|
|
|
$encoded = $this->utf8ToLatin1($string); |
73
|
|
|
$this->assertEquals(str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $this->latinString), $encoded); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testUtf8ToLatin1EuroSymbol() |
77
|
|
|
{ |
78
|
|
|
$string = 'a.b.c.å.ä.ö.€.'; |
79
|
|
|
$encoded = $this->utf8ToLatin1($string); |
80
|
|
|
// the warning suppression is due to utf8_decode being deprecated in php 8.2 |
81
|
|
|
$this->assertEquals(@utf8_decode('a.b.c.å.ä.ö.€.'), $encoded); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testUtf8ToLatin1Runes() |
85
|
|
|
{ |
86
|
|
|
$string = $this->runes; |
87
|
|
|
$encoded = $this->utf8ToLatin1($string); |
88
|
|
|
$this->assertEquals('ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ', $encoded); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testUtf8ToLatin1Greek() |
92
|
|
|
{ |
93
|
|
|
$string = $this->greek; |
94
|
|
|
$encoded = $this->utf8ToLatin1($string); |
95
|
|
|
$this->assertEquals('Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ', $encoded); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testUtf8ToLatin1Russian() |
99
|
|
|
{ |
100
|
|
|
$string = $this->russian; |
101
|
|
|
$encoded = $this->utf8ToLatin1($string); |
102
|
|
|
$this->assertEquals('Река неслася; бедный чёлн', $encoded); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testUtf8ToLatin1Chinese() |
106
|
|
|
{ |
107
|
|
|
$string = $this->chinese; |
108
|
|
|
$encoded = $this->utf8ToLatin1($string); |
109
|
|
|
$this->assertEquals('我能吞下玻璃而不伤身体。', $encoded); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testLatin15() |
113
|
|
|
{ |
114
|
|
|
if (!function_exists('mb_convert_encoding')) { |
115
|
|
|
$this->markTestSkipped('Miss mbstring extension to test exotic charsets'); |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// euro symbol in ISO-8859-15 |
120
|
|
|
$string = chr(164); |
121
|
|
|
$encoder = Charset::instance(); |
122
|
|
|
$this->assertEquals('€', $encoder->encodeEntities($string, 'ISO-8859-15', 'UTF-8')); |
123
|
|
|
$this->assertEquals('€', $encoder->encodeEntities($string, 'ISO-8859-15', 'US-ASCII')); |
124
|
|
|
$this->assertEquals(chr(164), $encoder->encodeEntities($string, 'ISO-8859-15', 'ISO-8859-15')); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|