|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author JoakimLofgren |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
use PhpXmlRpc\Helper\Charset; |
|
7
|
|
|
|
|
8
|
|
|
include_once __DIR__ . '/PolyfillTestCase.php'; |
|
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
|
|
|
class CharsetTest extends PhpXmlRpc_PolyfillTestCase |
|
19
|
|
|
{ |
|
20
|
|
|
// Consolas font should render these properly |
|
21
|
|
|
protected $runes = "ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ"; |
|
22
|
|
|
protected $greek = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ"; |
|
23
|
|
|
protected $russian = "Река неслася; бедный чёлн"; |
|
24
|
|
|
protected $chinese = "我能吞下玻璃而不伤身体。"; |
|
25
|
|
|
protected $latinString; |
|
26
|
|
|
|
|
27
|
|
|
protected function set_up() |
|
28
|
|
|
{ |
|
29
|
|
|
// construct a latin string with all chars (except control ones) |
|
30
|
|
|
$this->latinString = "\n\r\t"; |
|
31
|
|
|
for($i = 32; $i < 127; $i++) { |
|
32
|
|
|
$this->latinString .= chr($i); |
|
33
|
|
|
} |
|
34
|
|
|
for($i = 160; $i < 256; $i++) { |
|
35
|
|
|
$this->latinString .= chr($i); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function utfToLatin($data) |
|
40
|
|
|
{ |
|
41
|
|
|
return Charset::instance()->encodeEntities( |
|
42
|
|
|
$data, |
|
43
|
|
|
'UTF-8', |
|
44
|
|
|
'ISO-8859-1' |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testUtf8ToLatin1All() |
|
49
|
|
|
{ |
|
50
|
|
|
/*$this->assertEquals( |
|
51
|
|
|
'ISO-8859-1', |
|
52
|
|
|
mb_detect_encoding($this->latinString, 'ISO-8859-1, UTF-8, WINDOWS-1251, ASCII', true), |
|
53
|
|
|
'Setup latinString is not ISO-8859-1 encoded...' |
|
54
|
|
|
);*/ |
|
55
|
|
|
$string = utf8_encode($this->latinString); |
|
56
|
|
|
$encoded = $this->utfToLatin($string); |
|
57
|
|
|
$this->assertEquals(str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $this->latinString), $encoded); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testUtf8ToLatin1EuroSymbol() |
|
61
|
|
|
{ |
|
62
|
|
|
$string = 'a.b.c.å.ä.ö.€.'; |
|
63
|
|
|
$encoded = $this->utfToLatin($string); |
|
64
|
|
|
$this->assertEquals(utf8_decode('a.b.c.å.ä.ö.€.'), $encoded); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testUtf8ToLatin1Runes() |
|
68
|
|
|
{ |
|
69
|
|
|
$string = $this->runes; |
|
70
|
|
|
$encoded = $this->utfToLatin($string); |
|
71
|
|
|
$this->assertEquals('ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ', $encoded); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testUtf8ToLatin1Greek() |
|
75
|
|
|
{ |
|
76
|
|
|
$string = $this->greek; |
|
77
|
|
|
$encoded = $this->utfToLatin($string); |
|
78
|
|
|
$this->assertEquals('Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ', $encoded); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testUtf8ToLatin1Russian() |
|
82
|
|
|
{ |
|
83
|
|
|
$string = $this->russian; |
|
84
|
|
|
$encoded = $this->utfToLatin($string); |
|
85
|
|
|
$this->assertEquals('Река неслася; бедный чёлн', $encoded); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testUtf8ToLatin1Chinese() |
|
89
|
|
|
{ |
|
90
|
|
|
$string = $this->chinese; |
|
91
|
|
|
$encoded = $this->utfToLatin($string); |
|
92
|
|
|
$this->assertEquals('我能吞下玻璃而不伤身体。', $encoded); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|