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
|
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Tests for Charset Conversions
*
* @package PhpMyAdmin-test
*/
declare(strict_types=1);
namespace PhpMyAdmin\Tests;
use PhpMyAdmin\Encoding;
use PHPUnit\Framework\TestCase;
/**
* Tests for Charset Conversions
*
* @package PhpMyAdmin-test
*/
class EncodingTest extends TestCase
{
/**
* @return void
*/
protected function setUp(): void
{
Encoding::initEngine();
}
/**
* @return void
*/
protected function tearDown(): void
{
Encoding::initEngine();
}
/**
* Test for Encoding::convertString
*
* @return void
* @test
*
* @group medium
*/
public function testNoConversion()
{
$this->assertEquals(
'test',
Encoding::convertString('UTF-8', 'UTF-8', 'test')
);
}
/**
* @return void
*/
public function testInvalidConversion()
{
// Invalid value to use default case
Encoding::setEngine(-1);
$this->assertEquals(
'test',
Encoding::convertString('UTF-8', 'anything', 'test')
);
}
/**
* @return void
*/
public function testRecode()
{
if (! function_exists('recode_string')) {
$this->markTestSkipped('recode extension missing');
}
Encoding::setEngine(Encoding::ENGINE_RECODE);
$this->assertEquals(
'Only That ecole & Can Be My Blame',
Encoding::convertString(
'UTF-8',
'flat',
'Only That école & Can Be My Blame'
)
);
}
/**
* This group is used on debian packaging to exclude the test
* @see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=854821#27
* @group extension-iconv
* @return void
*/
public function testIconv()
{
if (! function_exists('iconv')) {
$this->markTestSkipped('iconv extension missing');
}
if (PHP_INT_SIZE === 8) {
$GLOBALS['cfg']['IconvExtraParams'] = '//TRANSLIT';
Encoding::setEngine(Encoding::ENGINE_ICONV);
$this->assertEquals(
"This is the Euro symbol 'EUR'.",
Encoding::convertString(
'UTF-8',
'ISO-8859-1',
"This is the Euro symbol '€'."
)
);
} elseif (PHP_INT_SIZE === 4) {
// NOTE: this does not work on 32bit systems and requires "//IGNORE"
// NOTE: or it will throw "iconv(): Detected an illegal character in input string"
$GLOBALS['cfg']['IconvExtraParams'] = '//TRANSLIT//IGNORE';
Encoding::setEngine(Encoding::ENGINE_ICONV);
$this->assertEquals(
"This is the Euro symbol ''.",
Encoding::convertString(
'UTF-8',
'ISO-8859-1',
"This is the Euro symbol '€'."
)
);
}
}
/**
* @return void
*/
public function testMbstring()
{
Encoding::setEngine(Encoding::ENGINE_MB);
$this->assertEquals(
"This is the Euro symbol '?'.",
Encoding::convertString(
'UTF-8',
'ISO-8859-1',
"This is the Euro symbol '€'."
)
);
}
/**
* Test for kanjiChangeOrder
*
* @return void
* @test
*/
public function testChangeOrder()
{
$this->assertEquals('ASCII,SJIS,EUC-JP,JIS', Encoding::getKanjiEncodings());
Encoding::kanjiChangeOrder();
$this->assertEquals('ASCII,EUC-JP,SJIS,JIS', Encoding::getKanjiEncodings());
Encoding::kanjiChangeOrder();
$this->assertEquals('ASCII,SJIS,EUC-JP,JIS', Encoding::getKanjiEncodings());
}
/**
* Test for Encoding::kanjiStrConv
*
* @return void
* @test
*/
public function testKanjiStrConv()
{
$this->assertEquals(
'test',
Encoding::kanjiStrConv('test', '', '')
);
$GLOBALS['kanji_encoding_list'] = 'ASCII,SJIS,EUC-JP,JIS';
$this->assertEquals(
'test è',
Encoding::kanjiStrConv('test è', '', '')
);
$this->assertEquals(
mb_convert_encoding('test è', 'ASCII', 'SJIS'),
Encoding::kanjiStrConv('test è', 'ASCII', '')
);
$this->assertEquals(
mb_convert_kana('全角', 'KV', 'SJIS'),
Encoding::kanjiStrConv('全角', '', 'kana')
);
}
/**
* Test for Encoding::kanjiFileConv
*
* @return void
* @test
*/
public function testFileConv()
{
$file_str = "教育漢字常用漢字";
$filename = 'test.kanji';
$file = fopen($filename, 'w');
fwrite($file, $file_str);
fclose($file);
$GLOBALS['kanji_encoding_list'] = 'ASCII,EUC-JP,SJIS,JIS';
$result = Encoding::kanjiFileConv($filename, 'JIS', 'kana');
$string = file_get_contents($result);
Encoding::kanjiChangeOrder();
$expected = Encoding::kanjiStrConv($file_str, 'JIS', 'kana');
Encoding::kanjiChangeOrder();
$this->assertEquals($string, $expected);
unlink($result);
}
/**
* Test for Encoding::kanjiEncodingForm
*
* @return void
* @test
*/
public function testEncodingForm()
{
$actual = Encoding::kanjiEncodingForm();
$this->assertStringContainsString(
'<input type="radio" name="knjenc"',
$actual
);
$this->assertStringContainsString(
'type="radio" name="knjenc"',
$actual
);
$this->assertStringContainsString(
'<input type="radio" name="knjenc" value="EUC-JP" id="kj-euc">',
$actual
);
$this->assertStringContainsString(
'<input type="radio" name="knjenc" value="SJIS" id="kj-sjis">',
$actual
);
$this->assertStringContainsString(
'<input type="checkbox" name="xkana" value="kana" id="kj-kana">',
$actual
);
}
/**
* @return void
*/
public function testListEncodings()
{
$GLOBALS['cfg']['AvailableCharsets'] = ['utf-8'];
$result = Encoding::listEncodings();
$this->assertContains('utf-8', $result);
}
}
|